github.com/vulppine/fotoDen@v0.3.0/generator/generator_test.go (about)

     1  package generator
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path"
     8  	"testing"
     9  )
    10  
    11  func TestJSONRW(t *testing.T) {
    12  	dir := t.TempDir()
    13  
    14  	type JSON struct {
    15  		Test string
    16  	}
    17  
    18  	err := WriteJSON(path.Join(dir, "tmp_json.json"), "single", JSON{"test"})
    19  	if err != nil {
    20  		t.Errorf("Error - WriteJSON: " + fmt.Sprint(err))
    21  	}
    22  
    23  	json := new(JSON)
    24  
    25  	err = ReadJSON(path.Join(dir, "tmp_json.json"), json)
    26  	if err != nil {
    27  		t.Errorf("Error - ReadJSON: " + fmt.Sprint(err))
    28  	}
    29  
    30  	if json.Test != "test" {
    31  		t.Errorf("Error - ReadJSON: test string does not match: " + json.Test)
    32  	}
    33  
    34  	t.Log(fmt.Sprint(json))
    35  }
    36  
    37  func TestFotoDenConfigRW(t *testing.T) {
    38  	dir := t.TempDir()
    39  
    40  	err := WriteConfig(DefaultConfig, path.Join(dir, "tmp_config.json"))
    41  	if err != nil {
    42  		t.Errorf("Error - WritefotoDenConfig: " + fmt.Sprint(err))
    43  	}
    44  
    45  	f, _ := ioutil.ReadFile(path.Join(dir, "tmp_config.json"))
    46  	t.Log(string(f))
    47  
    48  	err = OpenConfig(path.Join(dir, "tmp_config.json"))
    49  	if err != nil {
    50  		t.Errorf("Error - OpenfotoDenConfig: " + fmt.Sprint(err))
    51  	}
    52  	t.Log(fmt.Sprint(CurrentConfig))
    53  }
    54  
    55  func TestFolderInfoCRW(t *testing.T) {
    56  	dir := t.TempDir()
    57  
    58  	folder, err := GenerateFolderInfo(dir, dir)
    59  	if err != nil {
    60  		t.Errorf("Error - GenerateFolderInfo: " + fmt.Sprint(err))
    61  	}
    62  
    63  	err = folder.WriteFolderInfo((path.Join(dir, "folderInfo.json")))
    64  	if err != nil {
    65  		t.Errorf("Error - WriteFolderInfo: " + fmt.Sprint(err))
    66  	}
    67  
    68  	err = folder.ReadFolderInfo((path.Join(dir, "folderInfo.json")))
    69  	if err != nil {
    70  		t.Errorf("Error - ReadFolderInfo: " + fmt.Sprint(err))
    71  	}
    72  	t.Log(fmt.Sprint(folder))
    73  }
    74  
    75  func TestItemsInfoCRW(t *testing.T) {
    76  	dir := t.TempDir()
    77  
    78  	items, err := GenerateItemInfo("../test_images")
    79  	if err != nil {
    80  		t.Errorf("Error - GenerateItemInfo: " + fmt.Sprint(err))
    81  	}
    82  
    83  	err = items.WriteItemsInfo(path.Join(dir, "itemsInfo.json"))
    84  	if err != nil {
    85  		t.Errorf("Error - WriteItemsInfo: " + fmt.Sprint(err))
    86  	}
    87  
    88  	err = items.ReadItemsInfo(path.Join(dir, "itemsInfo.json"))
    89  	if err != nil {
    90  		t.Errorf("Error: ReadItemsInfo: " + fmt.Sprint(err))
    91  	}
    92  	t.Log(fmt.Sprint(items))
    93  }
    94  
    95  func TestBatchCopyConvert(t *testing.T) {
    96  	dir := t.TempDir()
    97  	dir2 := path.Join(dir, t.TempDir())
    98  
    99  	src, err := ioutil.ReadDir("../test_images")
   100  	if err != nil {
   101  		t.Errorf("Error: Opening test images folder: " + fmt.Sprint(err))
   102  	}
   103  	srcfiles := GetArrayOfFiles(src)
   104  
   105  	defer os.Chdir(WorkingDirectory)
   106  	os.Chdir("../test_images")
   107  
   108  	err = BatchCopyFile(srcfiles, dir)
   109  	if err != nil {
   110  		t.Errorf("Error: BatchCopyFile: " + fmt.Sprint(err))
   111  	}
   112  
   113  	os.Chdir(dir)
   114  
   115  	err = BatchImageConversion(srcfiles, "test", dir2, ImageScale{ScalePercent: 0.99})
   116  	if err != nil {
   117  		t.Errorf("Error: BatchImageConversion: " + fmt.Sprint(err))
   118  	}
   119  }
   120  
   121  func TestWebConfigCRW(t *testing.T) {
   122  	dir := t.TempDir()
   123  
   124  	webconfig := GenerateWebConfig("https://localhost/")
   125  
   126  	err := webconfig.WriteWebConfig(path.Join(dir, "config.json"))
   127  	if err != nil {
   128  		t.Errorf("Error - WriteWebConfig: " + fmt.Sprint(err))
   129  	}
   130  
   131  	err = webconfig.ReadWebConfig(path.Join(dir, "config.json"))
   132  	if err != nil {
   133  		t.Errorf("Error - ReadWebConfig: " + fmt.Sprint(err))
   134  	}
   135  
   136  	t.Log(fmt.Sprint(webconfig))
   137  }