github.com/Kindred87/Obsidian@v0.0.0-20210809203756-86936424b848/datasource/helpers_test.go (about) 1 package datasource 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "strconv" 8 "testing" 9 10 "github.com/Kindred87/Obsidian/directory" 11 "github.com/Kindred87/Obsidian/filetypes" 12 "github.com/xuri/excelize/v2" 13 ) 14 15 // checkForDiscrepancies checks for discrepancies between a specification interface and a Specification struct. 16 func checkForDiscrepancies(specInter specification, specStruct Specification, t *testing.T) { 17 testTables := []struct { 18 // Identifies the name of the field being tested. 19 comparisonName string 20 expected string 21 actual string 22 }{ 23 {"Alias", specInter.Alias(), specStruct.alias}, 24 {"WorkDir", strconv.FormatBool(specInter.IncludeProgramDirectory()), strconv.FormatBool(specStruct.IncludeProgramDirectory())}, 25 {"Paths", specInter.Paths()[0], specStruct.Paths()[0]}, 26 {"FileExt", specInter.FileType(), specStruct.FileType()}, 27 {"All value specs required", strconv.FormatBool(specInter.RequireAllValueSpecs()), strconv.FormatBool(specStruct.RequireAllValueSpecs())}, 28 {"ValueID-Value", specInter.ValueSpecs()[0].Value, specStruct.ValueSpecs()[0].Value}, 29 {"ValueID-Substring", strconv.FormatBool(specInter.ValueSpecs()[0].IsSubstring), strconv.FormatBool(specStruct.ValueSpecs()[0].IsSubstring)}, 30 {"ValueID-Row", fmt.Sprint(specInter.ValueSpecs()[0].InRow), strconv.Itoa(specStruct.ValueSpecs()[0].InRow)}, 31 {"ValueID-Column", fmt.Sprint(specInter.ValueSpecs()[0].InColumn), strconv.Itoa(specStruct.ValueSpecs()[0].InColumn)}, 32 } 33 34 for _, tt := range testTables { 35 if tt.expected != tt.actual { 36 t.Errorf("Comparison failed for " + tt.comparisonName) 37 } 38 } 39 } 40 41 // newHTMLSpec returns a Specification for an HTML datasource. 42 func newHTMLSpec() Specification { 43 spec := Specification{ 44 alias: "HTML Test alias", 45 includeProgramDirectory: true, 46 paths: []string{"C:/Obsidian"}, 47 fileType: filetypes.HTML.String(), 48 filenameSpecs: []FilenameSpec{newFilenameSpec()}, 49 valueSpecs: []ValueSpec{newValueSpec()}, 50 } 51 52 return spec 53 } 54 55 // newXLSXSpec returns a Specification for an XLSX datasource. 56 func newXLSXSpec() Specification { 57 spec := Specification{ 58 alias: "XLSX Test alias", 59 includeProgramDirectory: true, 60 fileType: filetypes.XLSX.String(), 61 filenameSpecs: []FilenameSpec{newFilenameSpec()}, 62 requireAllValueSpecs: true, 63 valueSpecs: []ValueSpec{newValueSpec()}, 64 } 65 secondValSpec := ValueSpec{ 66 Value: "foo2", 67 IsSubstring: false, 68 InRow: 1, 69 InColumn: 2, 70 AnyPage: true, 71 } 72 spec.valueSpecs = append(spec.valueSpecs, secondValSpec) 73 return spec 74 } 75 76 // newFilenameSpec returns a FilenameSpec with preassigned values. 77 func newFilenameSpec() FilenameSpec { 78 fSpec := FilenameSpec{} 79 fSpec.Filename = "Foo" 80 fSpec.NameIsSubstring = true 81 return fSpec 82 } 83 84 // newValueSpec returns a ValueSpec with qualifying values. 85 func newValueSpec() ValueSpec { 86 vSpec := ValueSpec{} 87 vSpec.Value = "foo" 88 vSpec.IsSubstring = false 89 vSpec.InRow = 1 90 vSpec.InColumn = 1 91 vSpec.AnyPage = true 92 return vSpec 93 } 94 95 // createExcelWorkbook creates an XLSX workbook of the given name and returns its path. 96 // Values are placed within the first row in sequential columns, starting at A or 1. 97 func createExcelWorkbook(alias string, values []string) (path string, err error) { 98 execDir, err := directory.ExeWithSep() 99 if err != nil { 100 return "", err 101 } 102 103 err = os.Chdir(execDir) 104 if err != nil { 105 return "", err 106 } 107 108 path = alias + filetypes.XLSX.StringWithDot() 109 110 fo := excelize.NewFile() 111 for i, value := range values { 112 columnName, err := excelize.ColumnNumberToName(i + 1) 113 if err != nil { 114 return "", err 115 } 116 fo.SetCellValue("Sheet1", columnName+"1", value) 117 } 118 119 return path, fo.SaveAs(path) 120 } 121 122 // addSheetToExcelWorkbook appends a sheet to the given file. 123 func addSheetToExcelWorkbook(path string, sheetName string, values []string) error { 124 if filepath.Ext(path) != filetypes.XLSX.StringWithDot() { 125 return fmt.Errorf("given file type was %s instead of %s", filepath.Ext(path), filetypes.XLSX.StringWithDot()) 126 } 127 128 fo, err := excelize.OpenFile(path) 129 if err != nil { 130 return err 131 } 132 133 sheetIndex := fo.NewSheet(sheetName) 134 for i, value := range values { 135 colName, err := excelize.ColumnNumberToName(i + 1) 136 if err != nil { 137 return err 138 } 139 140 err = fo.SetCellValue(fo.GetSheetName(sheetIndex), colName+"1", value) 141 if err != nil { 142 return err 143 } 144 } 145 146 err = fo.Save() 147 if err != nil { 148 return err 149 } 150 return nil 151 }