github.com/mithrandie/csvq@v1.18.1/lib/query/file_info_test.go (about) 1 package query 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 "testing" 8 9 "github.com/mithrandie/csvq/lib/option" 10 "github.com/mithrandie/csvq/lib/parser" 11 12 "github.com/mithrandie/go-text" 13 ) 14 15 var fileInfoTests = []struct { 16 Name string 17 FilePath parser.Identifier 18 Repository string 19 Format option.Format 20 Delimiter rune 21 Encoding text.Encoding 22 Result *FileInfo 23 Error string 24 }{ 25 { 26 Name: "CSV", 27 FilePath: parser.Identifier{Literal: "table1"}, 28 Repository: TestDir, 29 Format: option.CSV, 30 Delimiter: ',', 31 Encoding: text.UTF8, 32 Result: &FileInfo{ 33 Path: "table1.csv", 34 Delimiter: ',', 35 Format: option.CSV, 36 Encoding: text.UTF8, 37 }, 38 }, 39 { 40 Name: "CSV with AutoSelect", 41 FilePath: parser.Identifier{Literal: "table1"}, 42 Repository: TestDir, 43 Format: option.AutoSelect, 44 Delimiter: ',', 45 Encoding: text.UTF8, 46 Result: &FileInfo{ 47 Path: "table1.csv", 48 Delimiter: ',', 49 Format: option.CSV, 50 Encoding: text.UTF8, 51 }, 52 }, 53 { 54 Name: "TSV", 55 FilePath: parser.Identifier{Literal: "table3"}, 56 Repository: TestDir, 57 Format: option.TSV, 58 Delimiter: ',', 59 Encoding: text.UTF8, 60 Result: &FileInfo{ 61 Path: "table3.tsv", 62 Delimiter: '\t', 63 Format: option.TSV, 64 Encoding: text.UTF8, 65 }, 66 }, 67 { 68 Name: "TSV with AutoSelect", 69 FilePath: parser.Identifier{Literal: "table3"}, 70 Repository: TestDir, 71 Format: option.AutoSelect, 72 Delimiter: ',', 73 Encoding: text.UTF8, 74 Result: &FileInfo{ 75 Path: "table3.tsv", 76 Delimiter: '\t', 77 Format: option.TSV, 78 Encoding: text.UTF8, 79 }, 80 }, 81 { 82 Name: "JSON", 83 FilePath: parser.Identifier{Literal: "table"}, 84 Repository: TestDir, 85 Format: option.JSON, 86 Delimiter: ',', 87 Encoding: text.UTF16, 88 Result: &FileInfo{ 89 Path: "table.json", 90 Delimiter: ',', 91 Format: option.JSON, 92 Encoding: text.UTF8, 93 }, 94 }, 95 { 96 Name: "JSON with AutoSelect", 97 FilePath: parser.Identifier{Literal: "table"}, 98 Repository: TestDir, 99 Format: option.AutoSelect, 100 Delimiter: ',', 101 Encoding: text.UTF16, 102 Result: &FileInfo{ 103 Path: "table.json", 104 Delimiter: ',', 105 Format: option.JSON, 106 Encoding: text.UTF8, 107 }, 108 }, 109 { 110 Name: "JSONL", 111 FilePath: parser.Identifier{Literal: "table7"}, 112 Repository: TestDir, 113 Format: option.JSONL, 114 Delimiter: ',', 115 Encoding: text.UTF16, 116 Result: &FileInfo{ 117 Path: "table7.jsonl", 118 Delimiter: ',', 119 Format: option.JSONL, 120 Encoding: text.UTF8, 121 }, 122 }, 123 { 124 Name: "JSONL with AutoSelect", 125 FilePath: parser.Identifier{Literal: "table7"}, 126 Repository: TestDir, 127 Format: option.AutoSelect, 128 Delimiter: ',', 129 Encoding: text.UTF16, 130 Result: &FileInfo{ 131 Path: "table7.jsonl", 132 Delimiter: ',', 133 Format: option.JSONL, 134 Encoding: text.UTF8, 135 }, 136 }, 137 { 138 Name: "LTSV", 139 FilePath: parser.Identifier{Literal: "table6"}, 140 Repository: TestDir, 141 Format: option.LTSV, 142 Delimiter: ',', 143 Encoding: text.UTF8, 144 Result: &FileInfo{ 145 Path: "table6.ltsv", 146 Delimiter: ',', 147 Format: option.LTSV, 148 Encoding: text.UTF8, 149 }, 150 }, 151 { 152 Name: "LTSV with AutoSelect", 153 FilePath: parser.Identifier{Literal: "table6"}, 154 Repository: TestDir, 155 Format: option.AutoSelect, 156 Delimiter: ',', 157 Encoding: text.UTF8, 158 Result: &FileInfo{ 159 Path: "table6.ltsv", 160 Delimiter: ',', 161 Format: option.LTSV, 162 Encoding: text.UTF8, 163 }, 164 }, 165 { 166 Name: "Fixed-Length", 167 FilePath: parser.Identifier{Literal: "fixed_length.txt"}, 168 Repository: TestDir, 169 Format: option.FIXED, 170 Delimiter: ',', 171 Encoding: text.UTF8, 172 Result: &FileInfo{ 173 Path: "fixed_length.txt", 174 Delimiter: ',', 175 Format: option.FIXED, 176 Encoding: text.UTF8, 177 }, 178 }, 179 { 180 Name: "Import Format", 181 FilePath: parser.Identifier{Literal: "autoselect"}, 182 Repository: TestDir, 183 Format: option.AutoSelect, 184 Delimiter: ',', 185 Encoding: text.UTF8, 186 Result: &FileInfo{ 187 Path: "autoselect", 188 Delimiter: ',', 189 Format: option.CSV, 190 Encoding: text.UTF8, 191 }, 192 }, 193 { 194 Name: "Not Exist Error", 195 FilePath: parser.Identifier{Literal: "notexist"}, 196 Repository: TestDir, 197 Format: option.CSV, 198 Delimiter: ',', 199 Encoding: text.UTF8, 200 Error: "file notexist does not exist", 201 }, 202 { 203 Name: "File Read Error", 204 FilePath: parser.Identifier{Literal: TestDir}, 205 Repository: TestDir, 206 Format: option.CSV, 207 Delimiter: ',', 208 Encoding: text.UTF8, 209 Error: fmt.Sprintf("file %s is unable to be read", TestDir), 210 }, 211 { 212 Name: "Filenames Ambiguous", 213 FilePath: parser.Identifier{Literal: "dup_name"}, 214 Repository: TestDir, 215 Format: option.AutoSelect, 216 Delimiter: ',', 217 Encoding: text.UTF8, 218 Error: fmt.Sprintf("filename dup_name is ambiguous"), 219 }, 220 } 221 222 func TestNewFileInfo(t *testing.T) { 223 options := TestTx.Flags.ImportOptions.Copy() 224 225 for _, v := range fileInfoTests { 226 options.Format = v.Format 227 options.Delimiter = v.Delimiter 228 options.Encoding = v.Encoding 229 fileInfo, err := NewFileInfo(v.FilePath, v.Repository, options, TestTx.Flags.ImportOptions.Format) 230 if err != nil { 231 if len(v.Error) < 1 { 232 t.Errorf("%s: unexpected error %q", v.Name, err) 233 } else if err.Error() != v.Error { 234 t.Errorf("%s: error %q, want error %q", v.Name, err.Error(), v.Error) 235 } 236 continue 237 } 238 if 0 < len(v.Error) { 239 t.Errorf("%s: no error, want error %q", v.Name, v.Error) 240 continue 241 } 242 243 abs, _ := filepath.Abs(filepath.Join(v.Repository, v.Result.Path)) 244 if fileInfo.Path != abs { 245 t.Errorf("%s: FileInfo.Path = %s, want %s", v.Name, filepath.Base(fileInfo.Path), abs) 246 } 247 if fileInfo.Delimiter != v.Result.Delimiter { 248 t.Errorf("%s: FileInfo.Delimiter = %q, want %q", v.Name, fileInfo.Delimiter, v.Result.Delimiter) 249 } 250 if fileInfo.Format != v.Result.Format { 251 t.Errorf("%s: FileInfo.Format = %s, want %s", v.Name, fileInfo.Format, v.Result.Format) 252 } 253 } 254 } 255 256 var fileInfoForCreateTests = []struct { 257 Name string 258 FilePath parser.Identifier 259 Repository string 260 Delimiter rune 261 Encoding text.Encoding 262 Result *FileInfo 263 Error string 264 }{ 265 { 266 Name: "CSV", 267 FilePath: parser.Identifier{Literal: "table1.csv"}, 268 Delimiter: ',', 269 Encoding: text.UTF8, 270 Result: &FileInfo{ 271 Path: "table1.csv", 272 Delimiter: ',', 273 Format: option.CSV, 274 Encoding: text.UTF8, 275 }, 276 }, 277 { 278 Name: "TSV", 279 FilePath: parser.Identifier{Literal: "table1.tsv"}, 280 Delimiter: ',', 281 Encoding: text.UTF8, 282 Result: &FileInfo{ 283 Path: "table1.tsv", 284 Delimiter: '\t', 285 Format: option.TSV, 286 Encoding: text.UTF8, 287 }, 288 }, 289 { 290 Name: "JSON", 291 FilePath: parser.Identifier{Literal: "table1.json"}, 292 Delimiter: ',', 293 Encoding: text.SJIS, 294 Result: &FileInfo{ 295 Path: "table1.json", 296 Delimiter: ',', 297 Format: option.JSON, 298 Encoding: text.UTF8, 299 }, 300 }, 301 { 302 Name: "JSONL", 303 FilePath: parser.Identifier{Literal: "table1.jsonl"}, 304 Delimiter: ',', 305 Encoding: text.SJIS, 306 Result: &FileInfo{ 307 Path: "table1.jsonl", 308 Delimiter: ',', 309 Format: option.JSONL, 310 Encoding: text.UTF8, 311 }, 312 }, 313 { 314 Name: "LTSV", 315 FilePath: parser.Identifier{Literal: "table1.ltsv"}, 316 Delimiter: ',', 317 Encoding: text.UTF8, 318 Result: &FileInfo{ 319 Path: "table1.ltsv", 320 Delimiter: ',', 321 Format: option.LTSV, 322 Encoding: text.UTF8, 323 }, 324 }, 325 { 326 Name: "GFM", 327 FilePath: parser.Identifier{Literal: "table1.md"}, 328 Delimiter: ',', 329 Encoding: text.UTF8, 330 Result: &FileInfo{ 331 Path: "table1.md", 332 Delimiter: ',', 333 Format: option.GFM, 334 Encoding: text.UTF8, 335 }, 336 }, 337 { 338 Name: "ORG", 339 FilePath: parser.Identifier{Literal: "table1.org"}, 340 Delimiter: ',', 341 Encoding: text.UTF8, 342 Result: &FileInfo{ 343 Path: "table1.org", 344 Delimiter: ',', 345 Format: option.ORG, 346 Encoding: text.UTF8, 347 }, 348 }, 349 } 350 351 func TestNewFileInfoForCreate(t *testing.T) { 352 for _, v := range fileInfoForCreateTests { 353 repo := v.Repository 354 if 0 < len(repo) { 355 dir, _ := os.Getwd() 356 repo = filepath.Join(dir, repo) 357 } 358 359 fileInfo, err := NewFileInfoForCreate(v.FilePath, repo, v.Delimiter, v.Encoding) 360 if err != nil { 361 if len(v.Error) < 1 { 362 t.Errorf("%s: unexpected error %q", v.Name, err) 363 } else if err.Error() != v.Error { 364 t.Errorf("%s: error %q, want error %q", v.Name, err.Error(), v.Error) 365 } 366 continue 367 } 368 if 0 < len(v.Error) { 369 t.Errorf("%s: no error, want error %q", v.Name, v.Error) 370 continue 371 } 372 373 abs, _ := filepath.Abs(filepath.Join(repo, v.Result.Path)) 374 if fileInfo.Path != abs { 375 t.Errorf("%s: FileInfo.Path = %s, want %s", v.Name, fileInfo.Path, abs) 376 } 377 if fileInfo.Delimiter != v.Result.Delimiter { 378 t.Errorf("%s: FileInfo.Delimiter = %q, want %q", v.Name, fileInfo.Delimiter, v.Result.Delimiter) 379 } 380 if fileInfo.Format != v.Result.Format { 381 t.Errorf("%s: FileInfo.Format = %s, want %s", v.Name, fileInfo.Format, v.Result.Format) 382 } 383 } 384 }