Skip to content

Commit 41be4a5

Browse files
Refactor tests (#7)
1 parent 5412483 commit 41be4a5

File tree

1 file changed

+75
-99
lines changed

1 file changed

+75
-99
lines changed

test.sh

Lines changed: 75 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,34 @@ Sql__test_open_failure(){
1919
fi
2020
}
2121

22+
#################################################
23+
# Tests the success case of Sql__open for PostgreSQL
24+
#################################################
25+
Sql__test_postgres_open(){
26+
# Open DB
27+
Sql__open "$Sql__DRIVER_POSTGRES"
28+
if [[ $? -ne 0 ]]; then
29+
echo "'Sql__open' returned an error"
30+
return 1
31+
fi
32+
33+
# Close DB
34+
Sql__close
35+
if [[ $? -ne 0 ]]; then
36+
echo "'Sql__close' returned an error"
37+
return 1
38+
fi
39+
}
40+
2241
#################################################
2342
# Tests the success case of Sql__open for MySQL
43+
#
44+
# This tests differs from PostgreSQL because
45+
# this actually tests an implementation detail
46+
# that users don't particularly need to worry
47+
# about, but is important to test from the
48+
# testing end. The two interfaces for both
49+
# drivers are still exactly the same.
2450
#################################################
2551
Sql__test_mysql_open(){
2652
# Open DB
@@ -52,96 +78,59 @@ Sql__test_mysql_open(){
5278
fi
5379
}
5480

81+
#################################################
82+
# Tests a successful PostgreSQL query
83+
#################################################
84+
Sql__test_postgres_query(){
85+
Sql__generic_test_query "$Sql__DRIVER_POSTGRES"
86+
}
87+
5588
#################################################
5689
# Tests a successful MySQL query
5790
#################################################
5891
Sql__test_mysql_query(){
59-
# Open DB
60-
Sql__open "$Sql__DRIVER_MYSQL"
61-
62-
# Query
63-
local formatted_result=""
64-
local result=""
65-
result="$(Sql__execute "SELECT * FROM people;")"
66-
67-
# Handle result
68-
if [[ $? -eq 0 ]]; then
69-
# Echo Result
70-
while read -r record; do
71-
while IFS=$'\t' read id name; do
72-
if [[ $formatted_result = "" ]]; then
73-
formatted_result="$id,$name"
74-
else
75-
formatted_result="$formatted_result | $id,$name"
76-
fi
77-
done <<< "$record"
78-
done <<< "$result"
79-
else
80-
echo "Something wrong with query!"
81-
echo "$result"
82-
return 1
83-
fi
84-
85-
# Verify result
86-
local expected_result="1,Brandon | 2,Ryan | 3,Rigby | 4,Norbert"
87-
if [[ "$formatted_result" != "$expected_result" ]]; then
88-
echo "Something wrong with query result!"
89-
echo "Expected: '$expected_result'"
90-
echo "Actual: '$formatted_result'"
91-
return 1
92-
fi
92+
Sql__generic_test_query "$Sql__DRIVER_MYSQL"
93+
}
9394

94-
# Close DB
95-
Sql__close
95+
#################################################
96+
# Tests the a failed PostgreSQL query
97+
#################################################
98+
Sql__test_postgres_query_failure(){
99+
Sql__generic_test_query_failure "$Sql__DRIVER_POSTGRES"
96100
}
97101

98102
#################################################
99103
# Tests the a failed MySQL query
100104
#################################################
101105
Sql__test_mysql_query_failure(){
102-
# Open DB
103-
Sql__open "$Sql__DRIVER_MYSQL"
104-
105-
# Query
106-
result="$(Sql__execute "SELECT * FROM;")"
107-
108-
# Handle result
109-
if [[ $? -eq 0 ]]; then
110-
echo "Query should have failed, but was successful!"
111-
echo "Output:"
112-
echo "$result"
113-
return 1
114-
fi
115-
116-
# Close DB
117-
Sql__close
106+
Sql__generic_test_query_failure "$Sql__DRIVER_MYSQL"
118107
}
119108

120109
#################################################
121-
# Tests the success case of Sql__open for PostgreSQL
110+
# Test the successful and unsuccessful case for
111+
# the Sql__table_exists function for PostgreSQL
122112
#################################################
123-
Sql__test_postgres_open(){
124-
# Open DB
125-
Sql__open "$Sql__DRIVER_POSTGRES"
126-
if [[ $? -ne 0 ]]; then
127-
echo "'Sql__open' returned an error"
128-
return 1
129-
fi
113+
Sql__test_postgres_table_exists(){
114+
Sql__generic_test_table_exists "$Sql__DRIVER_POSTGRES"
115+
}
130116

131-
# Close DB
132-
Sql__close
133-
if [[ $? -ne 0 ]]; then
134-
echo "'Sql__close' returned an error"
135-
return 1
136-
fi
117+
#################################################
118+
# Test the successful and unsuccessful case for
119+
# the Sql__table_exists function for MySQL
120+
#################################################
121+
Sql__test_mysql_table_exists(){
122+
Sql__generic_test_table_exists "$Sql__DRIVER_MYSQL"
137123
}
138124

139125
#################################################
140-
# Tests a successful PostgreSQL query
126+
# Tests the successful case for the Sql__execute
127+
# method for a driver
128+
#
129+
# @param $1: The driver to test
141130
#################################################
142-
Sql__test_postgres_query(){
131+
Sql__generic_test_query(){
143132
# Open DB
144-
Sql__open "$Sql__DRIVER_POSTGRES"
133+
Sql__open "$1"
145134

146135
# Query
147136
local formatted_result=""
@@ -180,11 +169,18 @@ Sql__test_postgres_query(){
180169
}
181170

182171
#################################################
183-
# Tests the a failed PostgreSQL query
172+
# Tests the failure case for the Sql__execute
173+
# method for a driver
174+
#
175+
# @param $1: The driver to test
184176
#################################################
185-
Sql__test_postgres_query_failure(){
177+
Sql__generic_test_query_failure() {
186178
# Open DB
187-
Sql__open "$Sql__DRIVER_POSTGRES"
179+
Sql__open "$1"
180+
if [[ $? -ne 0 ]]; then
181+
echo "Failed to open database connection."
182+
return 1
183+
fi
188184

189185
# Query
190186
result="$(Sql__execute "SELECT * FROM;")"
@@ -203,38 +199,18 @@ Sql__test_postgres_query_failure(){
203199

204200
#################################################
205201
# Test the successful and unsuccessful case for
206-
# the Sql__table_exists function for PostgreSQL
202+
# the Sql__table_exists function for a driver
203+
#
204+
# @param $1: The driver to test
207205
#################################################
208-
Sql__test_postgres_table_exists(){
206+
Sql__generic_test_table_exists() {
209207
# Open DB
210-
Sql__open "$Sql__DRIVER_POSTGRES"
211-
212-
# Test that "people" table exists
213-
Sql__table_exists "people"
214-
if [[ "$?" -ne 0 ]]; then
215-
echo "Table 'people' should exist, but it does not!"
216-
return 1
217-
fi
218-
219-
# Test that "animals" table does not exist
220-
Sql__table_exists "animals"
221-
if [[ "$?" -eq 0 ]]; then
222-
echo "Table 'animals' should not exist, but it does!"
208+
Sql__open "$1"
209+
if [[ $? -ne 0 ]]; then
210+
echo "Failed to open database connection."
223211
return 1
224212
fi
225213

226-
# Close DB
227-
Sql__close
228-
}
229-
230-
#################################################
231-
# Test the successful and unsuccessful case for
232-
# the Sql__table_exists function for MySQL
233-
#################################################
234-
Sql__test_mysql_table_exists(){
235-
# Open DB
236-
Sql__open "$Sql__DRIVER_MYSQL"
237-
238214
# Test that "people" table exists
239215
Sql__table_exists "people"
240216
if [[ "$?" -ne 0 ]]; then

0 commit comments

Comments
 (0)