|
1 | | -import { parseCsv, mapCsvToProperties } from "../src/utils/parser"; |
2 | | -import * as fs from "fs"; |
3 | | -import * as path from "path"; |
4 | | -import { fileURLToPath } from "url"; |
5 | | - |
6 | | -const __filename = fileURLToPath(import.meta.url); |
7 | | -const __dirname = path.dirname(__filename); |
8 | | - |
9 | | -console.log("=== Testing CSV Examples ===\n"); |
10 | | - |
11 | | -// Test valid.csv |
12 | | -console.log("1. Testing valid.csv:"); |
13 | | -const validCsv = fs.readFileSync( |
14 | | - path.join(__dirname, "../doc/examples/csv/valid.csv"), |
15 | | - "utf-8" |
16 | | -); |
17 | | -const validResult = parseCsv(validCsv, true); |
18 | | -console.log(` - Rows parsed: ${validResult.data.length}`); |
19 | | -console.log(` - Warnings: ${validResult.warnings.length}`); |
20 | | -if (validResult.warnings.length > 0) { |
21 | | - console.log(" FAIL: Expected no warnings but got:"); |
22 | | - validResult.warnings.forEach((w) => |
23 | | - console.log(` Row ${w.row}, ${w.column}: ${w.message}`) |
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | +import { parseCsv } from "../src/utils/parser"; |
| 3 | +import fs from "fs"; |
| 4 | +import path from "path"; |
| 5 | + |
| 6 | +describe("CSV example files", () => { |
| 7 | + const validCsv = fs.readFileSync( |
| 8 | + path.join(__dirname, "../doc/examples/csv/valid.csv"), |
| 9 | + "utf-8" |
24 | 10 | ); |
25 | | -} else { |
26 | | - console.log(" PASS: No warnings as expected"); |
27 | | -} |
28 | | - |
29 | | -// Try to map properties |
30 | | -try { |
31 | | - const properties = mapCsvToProperties(validResult.data); |
32 | | - console.log(` - Properties created: ${Object.keys(properties).length}`); |
33 | | - console.log(" PASS: Properties mapped successfully"); |
34 | | -} catch (err) { |
35 | | - console.log(` FAIL: ${(err as Error).message}`); |
36 | | -} |
37 | | - |
38 | | -console.log("\n2. Testing invalid.csv:"); |
39 | | -const invalidCsv = fs.readFileSync( |
40 | | - path.join(__dirname, "../doc/examples/csv/invalid.csv"), |
41 | | - "utf-8" |
42 | | -); |
43 | | -const invalidResult = parseCsv(invalidCsv, true); |
44 | | -console.log(` - Rows parsed: ${invalidResult.data.length}`); |
45 | | -console.log(` - Warnings: ${invalidResult.warnings.length}`); |
46 | 11 |
|
47 | | -if (invalidResult.warnings.length === 0) { |
48 | | - console.log(" FAIL: Expected warnings but got none"); |
49 | | -} else { |
50 | | - console.log(" PASS: Warnings detected as expected:"); |
51 | | - invalidResult.warnings.forEach((w) => |
52 | | - console.log(` Row ${w.row}, ${w.column}: ${w.message}`) |
| 12 | + const invalidCsv = fs.readFileSync( |
| 13 | + path.join(__dirname, "../doc/examples/csv/invalid.csv"), |
| 14 | + "utf-8" |
53 | 15 | ); |
54 | | -} |
55 | 16 |
|
56 | | -// Try to map properties - should still work despite warnings |
57 | | -try { |
58 | | - const properties = mapCsvToProperties(invalidResult.data); |
59 | | - console.log(` - Properties created: ${Object.keys(properties).length}`); |
60 | | - console.log(" PASS: Properties mapped successfully despite warnings"); |
61 | | -} catch (err) { |
62 | | - console.log(` FAIL: ${(err as Error).message}`); |
63 | | -} |
| 17 | + it("valid.csv should produce no warnings", () => { |
| 18 | + const result = parseCsv(validCsv, true); |
| 19 | + expect(result.warnings.length).toBe(0); |
| 20 | + }); |
64 | 21 |
|
65 | | -console.log("\n=== Summary ==="); |
66 | | -console.log("All CSV example files tested successfully!"); |
67 | | -console.log( |
68 | | - "The validation system correctly identifies issues while still allowing data import." |
69 | | -); |
| 22 | + it("invalid.csv should produce warnings", () => { |
| 23 | + const result = parseCsv(invalidCsv, true); |
| 24 | + expect(result.warnings.length).toBeGreaterThan(0); |
| 25 | + }); |
| 26 | +}); |
0 commit comments