-
-
Notifications
You must be signed in to change notification settings - Fork 225
Expand file tree
/
Copy pathcsv.reader.test.ts
More file actions
245 lines (213 loc) · 7.66 KB
/
csv.reader.test.ts
File metadata and controls
245 lines (213 loc) · 7.66 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import path from "path";
import chai, { assert, expect } from "chai";
import { describe, it } from "mocha";
import chaiAsPromised from "chai-as-promised";
import { DataFrame, readCSV, Series, streamCSV, toCSV, toJSON } from "../../dist/danfojs-node/src";
import fs from 'fs';
import process from 'process';
chai.use(chaiAsPromised);
describe("readCSV", function () {
this.timeout(10000);
const testSamplesDir = path.join(process.cwd(), "test", "samples");
it("Read local csv file works", async function () {
const filePath = path.join(testSamplesDir, "titanic.csv");
let df = await readCSV(filePath, { header: true, preview: 5 });
assert.deepEqual(df.shape, [5, 8]);
assert.deepEqual(df.columns, [
'Survived',
'Pclass',
'Name',
'Sex',
'Age',
'Siblings/Spouses Aboard',
'Parents/Children Aboard',
'Fare'
]);
assert.deepEqual(df.dtypes, [
'int32', 'int32',
'string', 'string',
'int32', 'int32',
'int32', 'float32'
]);
});
it("Read local CSV file with config works", async function () {
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 });
assert.deepEqual(df.shape, [5, 8]);
assert.deepEqual(df.columns, [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H'
]);
assert.deepEqual(df.dtypes, [
'int32', 'int32',
'string', 'string',
'int32', 'int32',
'int32', 'float32'
]);
});
it("Read local csv with correct types and format works", async function () {
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]
];
assert.deepEqual(df.values, values);
});
it("Throws error if file not found", async function () {
const filePath = "notfound.csv";
await expect(readCSV(filePath)).to.be.rejectedWith("ENOENT: no such file or directory");
});
it("Throws error if file not found over http", async function () {
const filePath = "https://getdata.com/notfound.csv";
await expect(readCSV(filePath)).to.be.rejectedWith(/HTTP \d+:/);
});
it("Throws error when reading empty CSV file", async function () {
const filePath = path.join(testSamplesDir, "empty.csv");
// Create empty file
fs.writeFileSync(filePath, "");
await expect(readCSV(filePath)).to.be.rejectedWith("No data found in CSV file");
fs.unlinkSync(filePath); // Clean up
});
it("Throws error when reading malformed CSV", async function () {
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)).to.be.rejectedWith("CSV parsing errors");
fs.unlinkSync(filePath); // Clean up
});
it("Throws error when DataFrame creation fails", async function () {
const filePath = path.join(testSamplesDir, "invalid.csv");
await expect(readCSV(filePath)).to.be.rejectedWith("ENOENT: no such file or directory");
});
it("Preserves leading zeros when dtype is string", async function () {
const filePath = path.join(testSamplesDir, "leading_zeros.csv");
// Create test CSV file
fs.writeFileSync(filePath, "codes\n012345\n001234");
try {
const df = await readCSV(filePath, {
frameConfig: {
dtypes: ["string"]
}
});
assert.deepEqual(df.values, [["012345"], ["001234"]]);
assert.deepEqual(df.dtypes, ["string"]);
// Verify the values are actually strings
const jsonData = toJSON(df);
assert.deepEqual(jsonData, [{ codes: "012345" }, { codes: "001234" }]);
// Clean up
fs.unlinkSync(filePath);
} catch (error) {
// Clean up even if test fails
fs.unlinkSync(filePath);
throw error;
}
});
it("Converts to numbers when dtype is not string", async function () {
const filePath = path.join(testSamplesDir, "leading_zeros.csv");
// Create test CSV file
fs.writeFileSync(filePath, "codes\n012345\n001234");
try {
const df = await readCSV(filePath); // default behavior without string dtype
// Values should be converted to numbers
assert.deepEqual(df.values, [[12345], [1234]]);
assert.deepEqual(df.dtypes, ["int32"]);
// Verify JSON output
const jsonData = toJSON(df);
assert.deepEqual(jsonData, [{ codes: 12345 }, { codes: 1234 }]);
// Clean up
fs.unlinkSync(filePath);
} catch (error) {
// Clean up even if test fails
fs.unlinkSync(filePath);
throw error;
}
});
});
describe("streamCSV", function () {
this.timeout(100000);
const testSamplesDir = path.join(process.cwd(), "test", "samples");
it("Streaming local csv file with callback works", async function () {
const filePath = path.join(testSamplesDir, "titanic.csv");
await streamCSV(filePath, (df) => {
if (df) {
assert.deepEqual(df.shape, [1, 8]);
assert.deepEqual(df.columns, [
'Survived',
'Pclass',
'Name',
'Sex',
'Age',
'Siblings/Spouses Aboard',
'Parents/Children Aboard',
'Fare'
]);
} else {
assert.deepEqual(df, null);
}
}, { header: true });
});
it("Throws error when streaming non-existent file", async function () {
const filePath = "notfound.csv";
await expect(streamCSV(filePath, () => {})).to.be.rejectedWith("ENOENT: no such file or directory");
});
it("Throws error when streaming malformed CSV", async function () {
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, () => {})).to.be.rejectedWith("CSV parsing errors");
fs.unlinkSync(filePath); // Clean up
});
});
describe("toCSV", function () {
const testSamplesDir = path.join(process.cwd(), "test", "samples");
it("toCSV works", async function () {
const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
assert.deepEqual(toCSV(df, {}), `a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11,12\n`);
});
it("toCSV works for specified separator", async function () {
const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
assert.deepEqual(toCSV(df, { sep: "+" }), `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 function () {
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");
toCSV(df, { sep: ",", filePath });
// Clean up
fs.unlinkSync(filePath);
});
it("toCSV works for series", async function () {
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let df = new Series(data);
assert.deepEqual(toCSV(df, { sep: "+" }), `1+2+3+4+5+6+7+8+9+10+11+12`);
});
it("calling df.toCSV works", async function () {
const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]];
let df = new DataFrame(data, { columns: ["a", "b", "c", "d"] });
assert.deepEqual(df.toCSV(), `a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11,12\n`);
});
});