OILS / web / table / csv2html-test.sh View on Github | oilshell.org

190 lines, 71 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
111test-timestamp() {
112 cat >_tmp/timestamp.csv <<EOF
113name,start_time,end_time
114python3,0.1,1000000000.2
115bash,1100000000.3,1200000000.4
116EOF
117
118 # NOTE: Columns are out of order, which is OK.
119
120 # type: could be html-anchor:shell-id, html-href:shell-id
121
122 cat >_tmp/timestamp.schema.csv <<EOF
123column_name,type,strftime
124name,string,-
125start_time,float,iso
126end_time,float,iso
127EOF
128
129 write-html timestamp
130
131 cp _tmp/timestamp.csv _tmp/timestamp2.csv
132
133 cat >_tmp/timestamp2.schema.csv <<EOF
134column_name,type,strftime
135name,string,-
136start_time,float,iso
137end_time,float,%H:%M:%S
138EOF
139
140 write-html timestamp2
141}
142
143test-row-css-class() {
144 cat >_tmp/css.csv <<EOF
145name,age
146andy,1.2345
147bob,2.3456789
148EOF
149
150 # NOTE: Columns are out of order, which is OK.
151
152 # type: could be html-anchor:shell-id, html-href:shell-id
153
154 cat >_tmp/css.schema.csv <<EOF
155column_name,type,precision
156name,string,1
157age,double,3
158EOF
159
160 write-html css --css-class-pattern 'myclass ^a'
161
162 cat >_tmp/css2.csv <<EOF
163ROW_CSS_CLASS,name,age
164pass,andy,1.2345
165fail,bob,2.3456789
166EOF
167
168 # NOTE: Columns are out of order, which is OK.
169
170 # type: could be html-anchor:shell-id, html-href:shell-id
171
172 cat >_tmp/css2.schema.csv <<EOF
173column_name,type,precision
174ROW_CSS_CLASS,string,0
175name,string,1
176age,double,3
177EOF
178
179 write-html css2
180
181}
182
183all() {
184 link-static
185 echo
186 run-test-funcs
187}
188
189"$@"
190