-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.sh
executable file
·92 lines (75 loc) · 1.66 KB
/
test.sh
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
#!/bin/sh -eu
# SPDX-License-Identifier: WTFPL
# shellcheck enable=
cd "$(dirname "$0")"
TRUNC=
got=$(mktemp json2sqlite.XXXXXX)
csv=$(mktemp json2sqlite.XXXXXX)
trap 'rm "$got" "$csv"' EXIT
trunc () {
: > "$got"
}
run () {
[ -n "${TRUNC-}" ] && trunc
./json2sqlite -d "$got" "$@"
}
check () {
sqlite3 -csv "$got" -header "$@" > "$csv"
diff -u - "$csv"
}
# basic test input as list[dict]
TRUNC=y run -f - -t test --create <<- EOF
[{"col1": "1.1", "col2": "1.2"}, {"col1": "2.1", "col2": "2.2"}]
EOF
check "select * from test" <<- EOF
col1,col2
1.1,1.2
2.1,2.2
EOF
# basic test input as list[dict] as arguments
TRUNC=y run -t test --create -j '[{"col1": "1.1", "col2": "1.2"}, {"col1": "2.1", "col2": "2.2"}]'
check "select * from test" <<- EOF
col1,col2
1.1,1.2
2.1,2.2
EOF
# test input as dict (single entry)
TRUNC=y run -f - -t test --create <<- EOF
{"col1": "1.1", "col2": "1.2"}
EOF
check "select * from test" <<- EOF
col1,col2
1.1,1.2
EOF
# test input as dict[list]
TRUNC=y run -f - -t test --create <<- EOF
{"col1": ["1.1", "2.1"], "col2": ["1.2", "2.2"]}
EOF
check "select * from test" <<- EOF
col1,col2
1.1,1.2
2.1,2.2
EOF
# test input as list[list]
trunc
sqlite3 "$got" "create table test(foo, bar)"
run -f - -t test --create <<- EOF
[["1.1", "1.2"], ["2.1", "2.2"]]
EOF
check "select * from test" <<- EOF
foo,bar
1.1,1.2
2.1,2.2
EOF
# test --update
TRUNC=y run -f - -t test --create <<- EOF
[{"col1": "1.1", "col2": "1.2"}, {"col1": "2.1", "col2": "2.2"}]
EOF
run --file - --table test --update col1 <<- EOF
[{"col1": "1.1", "col2": "new1"}, {"col1": "2.1", "col2": "new2"}]
EOF
check "select * from test" <<- EOF
col1,col2
1.1,new1
2.1,new2
EOF