summaryrefslogtreecommitdiff
path: root/jstests/tool/csvimport1.js
blob: 0bcdcc8e789809668f914c2c267164ff229e7ed5 (plain)
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
// csvimport1.js

t = new ToolTest("csvimport1");

c = t.startDB("foo");

base = [];
base.push({
    a: 1,
    b: "this is some text.\nThis text spans multiple lines, and just for fun\ncontains a comma",
    "c": "This has leading and trailing whitespace!"
});
base.push({
    a: 2,
    b: "When someone says something you \"put it in quotes\"",
    "c": "I like embedded quotes/slashes\\backslashes"
});
base.push({
    a: 3,
    b: "  This line contains the empty string and has leading and trailing whitespace inside the quotes!  ",
    "c": ""
});
base.push({a: 4, b: "", "c": "How are empty entries handled?"});
base.push({a: 5, b: "\"\"", c: "\"This string is in quotes and contains empty quotes (\"\")\""});
base.push({a: "a", b: "b", c: "c"});

assert.eq(0, c.count(), "setup");

t.runTool("import",
          "--file",
          "jstests/tool/data/csvimport1.csv",
          "-d",
          t.baseName,
          "-c",
          "foo",
          "--type",
          "csv",
          "-f",
          "a,b,c");
assert.soon(base.length + " == c.count()", "after import 1 ");

a = c.find().sort({a: 1}).toArray();
for (i = 0; i < base.length; i++) {
    delete a[i]._id;
    assert.docEq(base[i], a[i], "csv parse " + i);
}

c.drop();
assert.eq(0, c.count(), "after drop");

t.runTool("import",
          "--file",
          "jstests/tool/data/csvimport1.csv",
          "-d",
          t.baseName,
          "-c",
          "foo",
          "--type",
          "csv",
          "--headerline");
assert.soon("c.findOne()", "no data after sleep");
assert.eq(base.length - 1, c.count(), "after import 2");

x = c.find().sort({a: 1}).toArray();
for (i = 0; i < base.length - 1; i++) {
    delete x[i]._id;
    assert.docEq(base[i], x[i], "csv parse with headerline " + i);
}

t.stop();