-
Notifications
You must be signed in to change notification settings - Fork 0
/
datex_test.sh
executable file
·174 lines (143 loc) · 3.56 KB
/
datex_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
#
# datex_test.sh - testing environment for DATEX
#
# Copyright (c) 2021 Flavio Augusto (@facmachado)
#
# This software may be modified and distributed under the terms
# of the MIT license. See the LICENSE file for details.
#
#
# Checks for shunit2
#
if [ ! "$(command -v shunit2)" ]; then
echo 'Error: shunit2 not installed' >&2
exit 1
fi
#
# Defines task name
#
function task() {
echo -n ". $1 "
}
#
# Marks task as successful
#
function check() {
sleep 0.1
echo -e '\r\033[1;32m✓\033[0m'
}
#
# Unit test common routines, like Selenium Webdriver
#
function oneTimeSetUp() {
src_dir=$(dirname "${BASH_SOURCE[0]}")
output_json="$src_dir/output_json.awk"
output_csv="$src_dir/output_csv.awk"
DBFILE="$src_dir/data.csv"
echo "Test start: $(date)"
test -f "$DBFILE" || : >"$DBFILE"
}
function setUp() {
echo
}
function tearDown() {
sleep 0.2
}
function oneTimeTearDown() {
rm -f "$DBFILE"
echo -e "\nTest finish: $(date)"
}
#
# Test scenarios
#
function testSource() {
task 'ls data.csv'
ls "$DBFILE" >/dev/null 2>&1
assertEquals 'ls data.csv' 0 $? && \
check
task 'source datex.sh'
# shellcheck source=/dev/null
source "$src_dir/datex.sh"
assertEquals 'source datex.sh' 0 $? && \
check
}
function testCrudSetup() {
local result
task 'create header'
create_header f_var f_type f_value
assertEquals 'create header' 0 $? && \
check
task 'check empty table'
result=$(list_records | wc -l)
assertEquals 'check empty table' 1 "$result" && \
check
}
function testCrudWrite() {
local result
task 'insert_record 1'
insert_record f_var=seq f_type=integer f_value=1
result=$(select_record 1 | head -1)
assertEquals 'insert_record 1' 'id = 1' "$result" && \
check
task 'insert_record 2'
insert_record f_var=name f_type=string f_value=bob
result=$(select_record 2 | head -1)
assertEquals 'insert_record 2' 'id = 2' "$result" && \
check
task 'insert_record 3'
insert_record f_var=admin f_type=boolean f_value=false
result=$(select_record 3 | head -1)
assertEquals 'insert_record 3' 'id = 3' "$result" && \
check
task 'insert_record 4'
insert_record f_var=status f_type=integer f_value=0
result=$(select_record 4 | head -1)
assertEquals 'insert_record 4' 'id = 4' "$result" && \
check
task 'update_record 2'
update_record 2 f_var=user f_value=robert
result=$(select_record 2 | grep robert)
assertEquals 'update_record 2' 'f_value = robert' "$result" && \
check
task 'delete_record 4'
delete_record 4
result=$(grep -E ',1$' "$DBFILE" | grep -cE '^4,')
assertEquals 'delete_record 4' 1 "$result" && \
check
}
function testCrudRead() {
local result
task 'list_records'
result=$(list_records | wc -l)
assertEquals 'list_records' 23 "$result" && \
check
task 'list_records 2 2'
result=$(list_records 2 2 | wc -l)
assertEquals 'list_records 2 2' 15 "$result" && \
check
task 'search_records robert'
result=$(search_records robert)
assertContains 'search_records robert' 'f_value = robert' "$result" && \
check
task 'select_record 3'
result=$(select_record 3 | head -1)
assertEquals 'select_record 3' 'id = 3' "$result" && \
check
}
function testOutput() {
local result
task 'output_csv'
result=$(select_record 1 | awk -f "$output_csv")
assertContains 'output_csv' "$result" '"seq","integer",1' && \
check
task 'output_json'
result=$(select_record 1 | awk -f "$output_json")
assertContains 'output_json' "$result" '"f_var":"seq","f_type":"integer","f_value":1' && \
check
}
#
# Calls shunit2
#
# shellcheck disable=SC1091
source shunit2