-
Notifications
You must be signed in to change notification settings - Fork 6
/
csv-test.ts
60 lines (52 loc) · 1.51 KB
/
csv-test.ts
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
import { assertArrayIncludes } from "https://deno.land/std@0.92.0/testing/asserts.ts"
import { readCSV, writeCSV } from '../src/csv.ts'
const csvReadPath = './examples/csv/prices.csv'
const csvWritePath = './examples/csv/names.csv'
Deno.test("reads a csv file", async () => {
const csv = await readCSV(csvReadPath)
assertArrayIncludes(csv, [{ Name: "One", Amount: "500", Price: "$0.5" }]);
})
Deno.test("reads a csv file without header", async () => {
const csv = await readCSV(csvReadPath, {
skipFirstRow: true
})
assertArrayIncludes(csv, [{ Name: "Two", Amount: "13", Price: "$10" }]);
})
Deno.test("writes a csv file", async () => {
const data = [
{
age: 70,
name: 'Rick'
},
{
age: 14,
name: 'Smith'
}
]
await writeCSV(csvWritePath, data)
const csv = await readCSV(csvWritePath)
assertArrayIncludes(csv, [{ age: "70", name: "Rick" }]);
})
Deno.test("appends to a csv file", async () => {
const data = [
{
age: 70,
name: 'Rick'
},
{
age: 14,
name: 'Smith'
}
]
await writeCSV(csvWritePath, data)
const data2 = [
{
age: 42,
name: 'Jodi'
}
]
await writeCSV(csvWritePath, data2, { append: true })
const csv = await readCSV(csvWritePath)
assertArrayIncludes(csv, [{ age: "70", name: "Rick" }])
assertArrayIncludes(csv, [{ age: "42", name: "Jodi" }])
})