diff --git a/test.sh b/test.sh index 0a4e60d..11fb0f4 100644 --- a/test.sh +++ b/test.sh @@ -19,8 +19,34 @@ Sql__test_open_failure(){ fi } +################################################# +# Tests the success case of Sql__open for PostgreSQL +################################################# +Sql__test_postgres_open(){ + # Open DB + Sql__open "$Sql__DRIVER_POSTGRES" + if [[ $? -ne 0 ]]; then + echo "'Sql__open' returned an error" + return 1 + fi + + # Close DB + Sql__close + if [[ $? -ne 0 ]]; then + echo "'Sql__close' returned an error" + return 1 + fi +} + ################################################# # Tests the success case of Sql__open for MySQL +# +# This tests differs from PostgreSQL because +# this actually tests an implementation detail +# that users don't particularly need to worry +# about, but is important to test from the +# testing end. The two interfaces for both +# drivers are still exactly the same. ################################################# Sql__test_mysql_open(){ # Open DB @@ -52,96 +78,59 @@ Sql__test_mysql_open(){ fi } +################################################# +# Tests a successful PostgreSQL query +################################################# +Sql__test_postgres_query(){ + Sql__generic_test_query "$Sql__DRIVER_POSTGRES" +} + ################################################# # Tests a successful MySQL query ################################################# Sql__test_mysql_query(){ - # Open DB - Sql__open "$Sql__DRIVER_MYSQL" - - # Query - local formatted_result="" - local result="" - result="$(Sql__execute "SELECT * FROM people;")" - - # Handle result - if [[ $? -eq 0 ]]; then - # Echo Result - while read -r record; do - while IFS=$'\t' read id name; do - if [[ $formatted_result = "" ]]; then - formatted_result="$id,$name" - else - formatted_result="$formatted_result | $id,$name" - fi - done <<< "$record" - done <<< "$result" - else - echo "Something wrong with query!" - echo "$result" - return 1 - fi - - # Verify result - local expected_result="1,Brandon | 2,Ryan | 3,Rigby | 4,Norbert" - if [[ "$formatted_result" != "$expected_result" ]]; then - echo "Something wrong with query result!" - echo "Expected: '$expected_result'" - echo "Actual: '$formatted_result'" - return 1 - fi + Sql__generic_test_query "$Sql__DRIVER_MYSQL" +} - # Close DB - Sql__close +################################################# +# Tests the a failed PostgreSQL query +################################################# +Sql__test_postgres_query_failure(){ + Sql__generic_test_query_failure "$Sql__DRIVER_POSTGRES" } ################################################# # Tests the a failed MySQL query ################################################# Sql__test_mysql_query_failure(){ - # Open DB - Sql__open "$Sql__DRIVER_MYSQL" - - # Query - result="$(Sql__execute "SELECT * FROM;")" - - # Handle result - if [[ $? -eq 0 ]]; then - echo "Query should have failed, but was successful!" - echo "Output:" - echo "$result" - return 1 - fi - - # Close DB - Sql__close + Sql__generic_test_query_failure "$Sql__DRIVER_MYSQL" } ################################################# -# Tests the success case of Sql__open for PostgreSQL +# Test the successful and unsuccessful case for +# the Sql__table_exists function for PostgreSQL ################################################# -Sql__test_postgres_open(){ - # Open DB - Sql__open "$Sql__DRIVER_POSTGRES" - if [[ $? -ne 0 ]]; then - echo "'Sql__open' returned an error" - return 1 - fi +Sql__test_postgres_table_exists(){ + Sql__generic_test_table_exists "$Sql__DRIVER_POSTGRES" +} - # Close DB - Sql__close - if [[ $? -ne 0 ]]; then - echo "'Sql__close' returned an error" - return 1 - fi +################################################# +# Test the successful and unsuccessful case for +# the Sql__table_exists function for MySQL +################################################# +Sql__test_mysql_table_exists(){ + Sql__generic_test_table_exists "$Sql__DRIVER_MYSQL" } ################################################# -# Tests a successful PostgreSQL query +# Tests the successful case for the Sql__execute +# method for a driver +# +# @param $1: The driver to test ################################################# -Sql__test_postgres_query(){ +Sql__generic_test_query(){ # Open DB - Sql__open "$Sql__DRIVER_POSTGRES" + Sql__open "$1" # Query local formatted_result="" @@ -180,11 +169,18 @@ Sql__test_postgres_query(){ } ################################################# -# Tests the a failed PostgreSQL query +# Tests the failure case for the Sql__execute +# method for a driver +# +# @param $1: The driver to test ################################################# -Sql__test_postgres_query_failure(){ +Sql__generic_test_query_failure() { # Open DB - Sql__open "$Sql__DRIVER_POSTGRES" + Sql__open "$1" + if [[ $? -ne 0 ]]; then + echo "Failed to open database connection." + return 1 + fi # Query result="$(Sql__execute "SELECT * FROM;")" @@ -203,38 +199,18 @@ Sql__test_postgres_query_failure(){ ################################################# # Test the successful and unsuccessful case for -# the Sql__table_exists function for PostgreSQL +# the Sql__table_exists function for a driver +# +# @param $1: The driver to test ################################################# -Sql__test_postgres_table_exists(){ +Sql__generic_test_table_exists() { # Open DB - Sql__open "$Sql__DRIVER_POSTGRES" - - # Test that "people" table exists - Sql__table_exists "people" - if [[ "$?" -ne 0 ]]; then - echo "Table 'people' should exist, but it does not!" - return 1 - fi - - # Test that "animals" table does not exist - Sql__table_exists "animals" - if [[ "$?" -eq 0 ]]; then - echo "Table 'animals' should not exist, but it does!" + Sql__open "$1" + if [[ $? -ne 0 ]]; then + echo "Failed to open database connection." return 1 fi - # Close DB - Sql__close -} - -################################################# -# Test the successful and unsuccessful case for -# the Sql__table_exists function for MySQL -################################################# -Sql__test_mysql_table_exists(){ - # Open DB - Sql__open "$Sql__DRIVER_MYSQL" - # Test that "people" table exists Sql__table_exists "people" if [[ "$?" -ne 0 ]]; then