-
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathcsv.reader.test.ts
More file actions
196 lines (172 loc) · 6.11 KB
/
csv.reader.test.ts
File metadata and controls
196 lines (172 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import path from "path";
import { DataFrame, readCSV, Series, streamCSV, toCSV, toJSON } from "../../dist/danfojs-node/src";
import fs from 'fs';
import process from 'process';
// Add Jest types for TypeScript support
import { describe, expect, it } from '@jest/globals';
describe("readCSV", () => {
const testSamplesDir = path.join(process.cwd(), "test", "samples");
it("Read local csv file works", async () => {
const filePath = path.join(testSamplesDir, "titanic.csv");
let df = await readCSV(filePath, { header: true, preview: 5 });
expect(df.shape).toEqual([5, 8]);
expect(df.columns).toEqual([
'Survived',
'Pclass',
'Name',
'Sex',
'Age',
'Siblings/Spouses Aboard',
'Parents/Children Aboard',
'Fare'
]);
expect(df.dtypes).toEqual([
'int32', 'int32',
'string', 'string',
'int32', 'int32',
'int32', 'float32'
]);
});
it("Read local CSV file with config works", async () => {
const filePath = path.join(testSamplesDir, "titanic.csv");
const frameConfig = {
columns: [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H'
]
};
let df = await readCSV(filePath, { frameConfig, header: true, preview: 5 });
expect(df.shape).toEqual([5, 8]);
expect(df.columns).toEqual([
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H'
]);
expect(df.dtypes).toEqual([
'int32', 'int32',
'string', 'string',
'int32', 'int32',
'int32', 'float32'
]);
});
it("Read local csv with correct types and format works", async () => {
const filePath = path.join(testSamplesDir, "iris.csv");
let df = await readCSV(filePath, { header: true, preview: 5 });
const values = [
[5.1, 3.5, 1.4, 0.2, 0.0],
[4.9, 3.0, 1.4, 0.2, 0.0],
[4.7, 3.2, 1.3, 0.2, 0.0],
[4.6, 3.1, 1.5, 0.2, 0.0],
[5.0, 3.6, 1.4, 0.2, 0.0]
];
expect(df.values).toEqual(values);
});
it("Throws error if file not found", async () => {
const filePath = "notfound.csv";
await expect(readCSV(filePath)).rejects.toThrow("ENOENT: no such file or directory");
});
it("Throws error if file not found over http", async () => {
const filePath = "https://getdata.com/notfound.csv";
await expect(readCSV(filePath)).rejects.toThrow(/HTTP \d+:/);
});
it("Throws error when reading empty CSV file", async () => {
const filePath = path.join(testSamplesDir, "empty.csv");
// Create empty file
fs.writeFileSync(filePath, "");
await expect(readCSV(filePath)).rejects.toThrow("No data found in CSV file");
fs.unlinkSync(filePath); // Clean up
});
it("Throws error when reading malformed CSV", async () => {
const filePath = path.join(testSamplesDir, "malformed.csv");
// Create malformed CSV file
fs.writeFileSync(filePath, "a,b,c\n1,2\n3,4,5,6");
await expect(readCSV(filePath)).rejects.toThrow("CSV parsing errors");
fs.unlinkSync(filePath); // Clean up
});
it("Throws error when DataFrame creation fails", async () => {
const filePath = path.join(testSamplesDir, "invalid.csv");
await expect(readCSV(filePath)).rejects.toThrow("ENOENT: no such file or directory");
});
});
describe("streamCSV", () => {
const testSamplesDir = path.join(process.cwd(), "test", "samples");
it("Streaming local csv file with callback works", async () => {
const filePath = path.join(testSamplesDir, "titanic.csv");
await streamCSV(filePath, (df) => {
if (df) {
expect(df.shape).toEqual([1, 8]);
expect(df.columns).toEqual([
'Survived',
'Pclass',
'Name',
'Sex',
'Age',
'Siblings/Spouses Aboard',
'Parents/Children Aboard',
'Fare'
]);
} else {
expect(df).toBeNull();
}
}, { header: true });
});
it("Throws error when streaming non-existent file", async () => {
const filePath = "notfound.csv";
await expect(streamCSV(filePath, () => {})).rejects.toThrow("ENOENT: no such file or directory");
});
it("Throws error when streaming malformed CSV", async () => {
const filePath = path.join(testSamplesDir, "malformed_stream.csv");
// Create malformed CSV file
fs.writeFileSync(filePath, "a,b,c\n1,2\n3,4,5,6");
await expect(streamCSV(filePath, () => {})).rejects.toThrow("CSV parsing errors");
fs.unlinkSync(filePath); // Clean up
});
});
describe("toCSV", () => {
const testSamplesDir = path.join(process.cwd(), "test", "samples");
it("toCSV works", async () => {
const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
expect(toCSV(df, {})).toBe(`a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11,12\n`);
});
it("toCSV works for specified separator", async () => {
const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
expect(toCSV(df, { sep: "+" })).toBe(`a+b+c+d\n1+2+3+4\n5+6+7+8\n9+10+11+12\n`);
});
it("toCSV write to local file works", async () => {
const data = [[1, 2, 3, "4"], [5, 6, 7, "8"], [9, 10, 11, "12"]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
const filePath = path.join(testSamplesDir, "test_write.csv");
// Write file
toCSV(df, { sep: ",", filePath });
// Verify file was written
expect(fs.existsSync(filePath)).toBe(true);
// Read and verify contents
const fileContent = fs.readFileSync(filePath, 'utf-8');
expect(fileContent).toBe(`a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11,12\n`);
// Clean up
fs.unlinkSync(filePath);
});
it("toCSV works for series", async () => {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let df = new Series(data);
expect(toCSV(df, { sep: "+" })).toBe(`1+2+3+4+5+6+7+8+9+10+11+12`);
});
it("calling df.toCSV works", async () => {
const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
expect(df.toCSV()).toBe(`a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11,12\n`);
});
});