-
-
Notifications
You must be signed in to change notification settings - Fork 224
Expand file tree
/
Copy pathcsv.reader.test.js
More file actions
181 lines (161 loc) · 5.9 KB
/
csv.reader.test.js
File metadata and controls
181 lines (161 loc) · 5.9 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
/* eslint-disable no-undef */
describe("readCSV", function () {
this.timeout(10000);
it("Read remote csv file works", async function () {
const remoteFile = "https://raw.githubusercontent.com/javascriptdata/danfojs/dev/src/danfojs-node/test/samples/titanic.csv";
let df = await dfd.readCSV(remoteFile, { 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 remote csv file with config works", async function () {
const remoteFile = "https://raw.githubusercontent.com/javascriptdata/danfojs/dev/src/danfojs-node/test/samples/titanic.csv";
const frameConfig = {
columns: [
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H'
]
};
let df = await dfd.readCSV(remoteFile, { header: true, preview: 5, frameConfig });
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 remote csv file works and returns correct data type", async function () {
const remoteFile = "https://raw.githubusercontent.com/javascriptdata/danfojs/dev/src/danfojs-node/test/samples/titanic.csv";
let df = await dfd.readCSV(remoteFile, { header: true, preview: 2 });
const values = [
[ 0, 3, 'Mr. Owen Harris Braund', 'male', 22, 1, 0, 7.25 ],
[ 1, 1, 'Mrs. John Bradley (Florence Briggs Thayer) Cumings', 'female', 38, 1, 0, 71.2833 ]
];
assert.deepEqual(df.values, values);
});
it("Should throw error when reading non-existent remote file", async function () {
const remoteFile = "https://raw.githubusercontent.com/javascriptdata/danfojs/dev/nonexistent.csv";
try {
await dfd.readCSV(remoteFile);
assert.fail("Should have thrown an error");
} catch (error) {
assert.ok(error instanceof Error);
}
});
it("Should throw error when reading malformed CSV", async function () {
const malformedCSV = new File([ "a,b,c\n1,2\n3,4,5,6" ], "malformed.csv", { type: "text/csv" });
try {
await dfd.readCSV(malformedCSV);
assert.fail("Should have thrown an error");
} catch (error) {
assert.ok(error instanceof Error);
}
});
it("Should throw error when reading invalid file type", async function () {
const invalidFile = new File([ "not a csv" ], "test.txt", { type: "text/plain" });
try {
await dfd.readCSV(invalidFile);
assert.fail("Should have thrown an error");
} catch (error) {
assert.ok(error instanceof Error);
}
});
it("Preserves leading zeros when dtype is string", async function () {
// Create a CSV file with leading zeros
const csvContent = "codes\n012345\n001234";
const file = new File([ csvContent ], "leading_zeros.csv", { type: "text/csv" });
const df = await dfd.readCSV(file, {
frameConfig: {
dtypes: [ "string" ]
}
});
assert.deepEqual(df.values, [ [ "012345" ], [ "001234" ] ]);
assert.deepEqual(df.dtypes, [ "string" ]);
// Verify the values are actually strings
const jsonData = dfd.toJSON(df);
assert.deepEqual(jsonData, [ { codes: "012345" }, { codes: "001234" } ]);
});
it("Converts to numbers when dtype is not string", async function () {
// Create a CSV file with leading zeros
const csvContent = "codes\n012345\n001234";
const file = new File([ csvContent ], "leading_zeros.csv", { type: "text/csv" });
const df = await dfd.readCSV(file); // 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 = dfd.toJSON(df);
assert.deepEqual(jsonData, [ { codes: 12345 }, { codes: 1234 } ]);
});
});
// describe("streamCSV", function () {
// this.timeout(100000);
// it("Streaming remote csv file with callback works", async function () {
// const remoteFile = "https://raw.githubusercontent.com/javascriptdata/danfojs/dev/src/danfojs-node/test/samples/titanic.csv";
// await dfd.streamCSV(remoteFile, (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, preview: 3 });
// });
// });
describe("toCSV", function () {
it("toCSV works", async function () {
const data = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ];
let df = new dfd.DataFrame(data, { columns: [ "a", "b", "c", "d" ] });
assert.deepEqual(dfd.toCSV(df, { download: false }), `a,b,c,d\n1,2,3,4\n5,6,7,8\n9,10,11,12\n`);
});
it("toCSV works for specified seperator", async function () {
const data = [ [ 1, 2, 3, 4 ], [ 5, 6, 7, 8 ], [ 9, 10, 11, 12 ] ];
let df = new dfd.DataFrame(data, { columns: [ "a", "b", "c", "d" ] });
assert.deepEqual(dfd.toCSV(df, { sep: "+", download: false }), `a+b+c+d\n1+2+3+4\n5+6+7+8\n9+10+11+12\n`);
});
it("toCSV works for series", async function () {
const data = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ];
let df = new dfd.Series(data);
assert.deepEqual(dfd.toCSV(df, { sep: "+", download: false }), `1+2+3+4+5+6+7+8+9+10+11+12`);
});
});