OILS / web / table / csv2html-test.sh View on Github | oils.pub

222 lines, 80 significant
1#!/usr/bin/env bash
2#
3# Usage:
4# ./csv2html-test.sh <function name>
5
6set -o nounset
7set -o pipefail
8set -o errexit
9
10readonly REPO_ROOT=$(readlink -f $(dirname $0))/../..
11
12source $REPO_ROOT/test/common.sh
13source $REPO_ROOT/web/table/html.sh
14
15
16readonly BASE_DIR=_tmp/www
17
18link-static() {
19 mkdir -p $BASE_DIR
20
21 ln -s -f -v \
22 $PWD/../ajax.js \
23 $PWD/table-sort.js \
24 $PWD/table-sort.css \
25 $BASE_DIR
26}
27
28html-head() {
29 PYTHONPATH=$REPO_ROOT $REPO_ROOT/doctools/html_head.py "$@"
30}
31
32header() {
33 html-head --title 'csv2html-test' \
34 ajax.js table-sort.js table-sort.css
35
36 table-sort-begin
37}
38
39write-html() {
40 local name=$1
41 shift
42
43 local out=$BASE_DIR/$name.html
44
45 { header
46 ./csv2html.py "$@" _tmp/$name.csv
47 table-sort-end $name
48 } > $out
49 echo "Wrote $out"
50}
51
52test-no-schema() {
53 cat >_tmp/foo.csv <<EOF
54a_number,b
551,2
563,4
57NA,4
58EOF
59
60 write-html foo
61}
62
63test-schema() {
64 cat >_tmp/bar.csv <<EOF
65name,name_HREF,num
66spam,#spam,11
67eggs,#eggs,22
68ham,#ham,99
69xxx,#xxx,123456
70zzz,#zzz,NA
71EOF
72
73 # NOTE: Columns are out of order, which is OK.
74
75 # type: could be html-anchor:shell-id, html-href:shell-id
76
77 cat >_tmp/bar.schema.csv <<EOF
78column_name,type
79num,integer
80name,string
81name_HREF,string
82EOF
83
84 write-html bar
85
86 cp _tmp/bar.csv _tmp/bar2.csv
87 cp _tmp/bar.schema.csv _tmp/bar2.schema.csv
88 write-html bar2 --thead-offset 1
89}
90
91test-precision() {
92 cat >_tmp/prec.csv <<EOF
93name,age
94andy,1.2345
95bob,2.3456789
96EOF
97
98 # NOTE: Columns are out of order, which is OK.
99
100 # type: could be html-anchor:shell-id, html-href:shell-id
101
102 cat >_tmp/prec.schema.csv <<EOF
103column_name,type,precision
104name,string,1
105age,double,2
106EOF
107
108 write-html prec
109}
110
111# NOTE: This combination doesn't work, but the default precision of 1 is OK?
112test-precision-href() {
113 cat >_tmp/prec-href.csv <<EOF
114name,age,age_HREF
115andy,1.2345,http://example.com/foo
116bob,2.3456789,http://example.com/bar
117EOF
118
119 # NOTE: Columns are out of order, which is OK.
120
121 # type: could be html-anchor:shell-id, html-href:shell-id
122
123 cat >_tmp/prec.schema.csv <<EOF
124column_name,type,precision
125name,string,1
126age,double,2
127age_HREF,string,0
128EOF
129
130 write-html prec-href
131}
132
133test-timestamp() {
134 cat >_tmp/timestamp.csv <<EOF
135name,start_time,end_time
136python3,0.1,1000000000.2
137bash,1100000000.3,1200000000.4
138EOF
139
140 # NOTE: Columns are out of order, which is OK.
141
142 # type: could be html-anchor:shell-id, html-href:shell-id
143
144 cat >_tmp/timestamp.schema.csv <<EOF
145column_name,type,strftime
146name,string,-
147start_time,float,iso
148end_time,float,iso
149EOF
150
151 write-html timestamp
152
153 cp _tmp/timestamp.csv _tmp/timestamp2.csv
154
155 cat >_tmp/timestamp2.schema.csv <<EOF
156column_name,type,strftime
157name,string,-
158start_time,float,iso
159end_time,float,%H:%M:%S
160EOF
161
162 write-html timestamp2
163}
164
165test-row-css-class() {
166 cat >_tmp/css.csv <<EOF
167name,age
168andy,1.2345
169bob,2.3456789
170EOF
171
172 # NOTE: Columns are out of order, which is OK.
173
174 # type: could be html-anchor:shell-id, html-href:shell-id
175
176 cat >_tmp/css.schema.csv <<EOF
177column_name,type,precision
178name,string,1
179age,double,3
180EOF
181
182 write-html css --css-class-pattern 'myclass ^a'
183
184 cat >_tmp/css2.csv <<EOF
185ROW_CSS_CLASS,name,age
186pass,andy,1.2345
187fail,bob,2.3456789
188EOF
189
190 # NOTE: Columns are out of order, which is OK.
191
192 # type: could be html-anchor:shell-id, html-href:shell-id
193
194 cat >_tmp/css2.schema.csv <<EOF
195column_name,type,precision
196ROW_CSS_CLASS,string,0
197name,string,1
198age,double,3
199EOF
200
201 write-html css2
202}
203
204test-schema2sqlite() {
205 sed 's/,/\t/g' >_tmp/foo.schema.tsv <<EOF
206column_name,type,precision
207ROW_CSS_CLASS,string,0
208name,string,1
209age,double,3
210EOF
211
212 ./schema2sqlite.py _tmp/foo.schema.tsv
213}
214
215all() {
216 link-static
217 echo
218 run-test-funcs
219}
220
221"$@"
222