From 7153eaca364fb5c0999597fabb238eca52ac225d Mon Sep 17 00:00:00 2001 From: Carla Date: Wed, 15 Jul 2015 16:29:42 +0200 Subject: [PATCH 1/3] First tests for postal codes --- geocoder/postal-codes/test/data/test.sh | 47 +++++ geocoder/postal-codes/test/functions/test.sh | 36 ++++ geocoder/postal-codes/test/run.sh | 192 +++++++++++++++++++ 3 files changed, 275 insertions(+) create mode 100644 geocoder/postal-codes/test/data/test.sh create mode 100644 geocoder/postal-codes/test/functions/test.sh create mode 100644 geocoder/postal-codes/test/run.sh diff --git a/geocoder/postal-codes/test/data/test.sh b/geocoder/postal-codes/test/data/test.sh new file mode 100644 index 0000000..2ec3f7a --- /dev/null +++ b/geocoder/postal-codes/test/data/test.sh @@ -0,0 +1,47 @@ +#!/bin/sh + + +#################################################### TESTS GO HERE #################################################### + +function test_geocoding_quality_zipcodes_availability() { + #Checks count of available polygon zipcodes + sql "SELECT count(*) FROM available_services where postal_code_polygons is true" should 4 + #Checks count of available point zipcodes + sql "SELECT count(*) FROM available_services where postal_code_points is true" should 65 +} + +function test_geocoding_quality_zipcodes_usa() { + #Checks that zipcode polygons are available + sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'USA'" should 1 + #Checks that zipcode points are available + sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'USA'" should 1 + + +} + +function test_geocoding_quality_zipcodes_fra() { + #Checks that zipcode polygons are available + sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'FRA'" should 1 + #Checks that zipcode points are available + sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'FRA'" should 1 + +} + +function test_geocoding_quality_zipcodes_can() { + #Checks that zipcode polygons are available + sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'CAN'" should 1 + #Checks that zipcode points are available + sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'CAN'" should 1 + +} + +function test_geocoding_quality_zipcodes_aus() { + #Checks that zipcode polygons are available + sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'AUS'" should 1 + #Checks that zipcode points are available + sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'AUS'" should 1 + + +} + +#################################################### TESTS END HERE #################################################### diff --git a/geocoder/postal-codes/test/functions/test.sh b/geocoder/postal-codes/test/functions/test.sh new file mode 100644 index 0000000..591bce5 --- /dev/null +++ b/geocoder/postal-codes/test/functions/test.sh @@ -0,0 +1,36 @@ +#!/bin/sh + + +#################################################### TESTS GO HERE #################################################### + +function test_geocoding_functions_zipcodes_usa() { + #Checks that zipcode polygons are available + sql "SELECT (admin0_available_services(Array['USA'])).postal_code_points" should true + #Checks that zipcode points are available + sql "SELECT (admin0_available_services(Array['USA'])).postal_code_polygons" should true + +} + +function test_geocoding_functions_zipcodes_fra() { + #Checks that zipcode polygons are available + sql "SELECT (admin0_available_services(Array['FRA'])).postal_code_points" should true + #Checks that zipcode points are available + sql "SELECT (admin0_available_services(Array['FRA'])).postal_code_polygons" should true +} + +function test_geocoding_functions_zipcodes_can() { + #Checks that zipcode polygons are available + sql "SELECT (admin0_available_services(Array['CAN'])).postal_code_points" should true + #Checks that zipcode points are available + sql "SELECT (admin0_available_services(Array['CAN'])).postal_code_polygons" should true +} + +function test_geocoding_functions_zipcodes_aus() { + #Checks that zipcode polygons are available + sql "SELECT (admin0_available_services(Array['AUS'])).postal_code_points" should true + #Checks that zipcode points are available + sql "SELECT (admin0_available_services(Array['AUS'])).postal_code_polygons" should true + +} + +#################################################### TESTS END HERE #################################################### diff --git a/geocoder/postal-codes/test/run.sh b/geocoder/postal-codes/test/run.sh new file mode 100644 index 0000000..2fff134 --- /dev/null +++ b/geocoder/postal-codes/test/run.sh @@ -0,0 +1,192 @@ +#!/bin/sh +source data/test.sh +source functions/test.sh + +# +# The following script has two use modes: +# +# API +# ==================================== +# SQL_API_ROUTE and API_KEY need to be +# conveniently filled for the requests +# to work. +# Example: bash run.sh api sql_api_route api_key test_type +# +# SQL +# ==================================== +# It is expected that you run this script +# as a PostgreSQL superuser, for example: +# +# bash run.sh db database role test_type +# +# test_type defines the kind of tests to run: data or functions + +MODE='' +TEST_TYPE='' + +# DB settings +DATABASE='' +ROLE='' +CMD='echo psql' +CMD=psql + +# SQL API settings +SQL_API_ROUTE='' +API_KEY='' + +OK=0 +PARTIALOK=0 + +function set_failed() { + OK=1 + PARTIALOK=1 +} + +function clear_partial_result() { + PARTIALOK=0 +} + +function sql() { + local QUERY="$1" + if [[ $MODE = "api" ]] + then + # SQL API mode + echo $QUERY + RESULT=$(curl -s -X POST "https://$SQL_API_ROUTE/sql?api_key=$API_KEY&format=csv" --data-urlencode "q=$QUERY" |tail -n1 | tr -d '\r') + if [[ "$2" == "should" ]] + then + if [[ "${RESULT}" != "$3" ]] + then + log_error "QUERY '${QUERY}' expected result '${3}' but got '${RESULT}'" + set_failed + fi + fi + # Database mode + elif [[ $MODE = "db" ]] + then + if [ -n "${ROLE}" ]; then + log_debug "Executing query '${QUERY}' as ${ROLE}" + RESULT=`${CMD} -U "${ROLE}" ${DATABASE} -c "${QUERY}" -A -t` + else + log_debug "Executing query '${QUERY}'" + RESULT=`${CMD} ${DATABASE} -c "${QUERY}" -A -t` + fi + CODERESULT=$? + + echo ${RESULT} + echo + + if [[ ${CODERESULT} -ne 0 ]] + then + echo -n "FAILED TO EXECUTE QUERY: " + log_warning "${QUERY}" + if [[ "$2" != "fails" ]] + then + log_error "${QUERY}" + set_failed + fi + else + if [[ "$2" == "fails" ]] + then + log_error "QUERY: '${QUERY}' was expected to fail and it did not fail" + set_failed + fi + fi + + if [[ "$2" == "should" ]] + then + if [[ "${RESULT}" != "$3" ]] + then + log_error "QUERY '${QUERY}' expected result '${3}' but got '${RESULT}'" + set_failed + fi + fi + fi +} + +function log_info() +{ + echo + echo + echo + _log "1;34m" "$1" +} + +function log_error() { + _log "1;31m" "$1" +} + +function log_debug() { + _log "1;32m" "> $1" +} + +function log_warning() { + _log "0;33m" "$1" +} + +function _log() { + echo -e "\033[$1$2\033[0m" +} + +# '############################ HELPERS #############################' + + +function run_tests() { + local FAILED_TESTS=() + local TESTS + + MODE="$1" + if [[ $MODE = "api" ]] + then + SQL_API_ROUTE="$2" + API_KEY="$3" + TEST_TYPE="$4" + fi + if [[ $MODE = "db" ]] + then + DATABASE="$2" + ROLE="$3" + TEST_TYPE="$4" + fi + + if [[ $# -ge 4 ]] + then + if [[ $TEST_TYPE = "data" ]] + then + TESTS=`cat data/test.sh| perl -n -e'/function (test.*)\(\)/ && print "$1\n"'` + elif [[ $TEST_TYPE = "functions" ]] + then + TESTS=`cat functions/test.sh| perl -n -e'/function (test.*)\(\)/ && print "$1\n"'` + fi + else + TESTS="$@" + fi + for t in ${TESTS} + do + echo "####################################################################" + echo "#" + echo "# Running: ${t}" + echo "#" + echo "####################################################################" + clear_partial_result + eval ${t} + if [[ ${PARTIALOK} -ne 0 ]] + then + FAILED_TESTS+=(${t}) + fi + done + if [[ ${OK} -ne 0 ]] + then + echo + log_error "The following tests are failing:" + printf -- '\t%s\n' "${FAILED_TESTS[@]}" + else + echo + log_info "Test finished" + fi +} + + +run_tests $@ + +exit ${OK} From fcc7f79b33b8d9fcf1942d2d55250f1321233511 Mon Sep 17 00:00:00 2001 From: Carla Date: Wed, 15 Jul 2015 17:13:30 +0200 Subject: [PATCH 2/3] Adds function tests for polygons --- geocoder/postal-codes/test/functions/test.sh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/geocoder/postal-codes/test/functions/test.sh b/geocoder/postal-codes/test/functions/test.sh index 591bce5..2fc3454 100644 --- a/geocoder/postal-codes/test/functions/test.sh +++ b/geocoder/postal-codes/test/functions/test.sh @@ -8,7 +8,7 @@ function test_geocoding_functions_zipcodes_usa() { sql "SELECT (admin0_available_services(Array['USA'])).postal_code_points" should true #Checks that zipcode points are available sql "SELECT (admin0_available_services(Array['USA'])).postal_code_polygons" should true - + sql "SELECT ST_GeometryType((geocode_postalcode_polygons(Array['11211'], 'USA')).geom)" should ST_MultiPolygon } function test_geocoding_functions_zipcodes_fra() { @@ -16,6 +16,8 @@ function test_geocoding_functions_zipcodes_fra() { sql "SELECT (admin0_available_services(Array['FRA'])).postal_code_points" should true #Checks that zipcode points are available sql "SELECT (admin0_available_services(Array['FRA'])).postal_code_polygons" should true + sql "SELECT ST_GeometryType(((geocode_postalcode_polygons(Array['23270'], Array['FRA'])).geom))" should ST_MultiPolygon + } function test_geocoding_functions_zipcodes_can() { @@ -23,6 +25,8 @@ function test_geocoding_functions_zipcodes_can() { sql "SELECT (admin0_available_services(Array['CAN'])).postal_code_points" should true #Checks that zipcode points are available sql "SELECT (admin0_available_services(Array['CAN'])).postal_code_polygons" should true + sql "SELECT ST_GeometryType((geocode_postalcode_polygons(Array['A0J'], 'CAN')).geom)" should ST_MultiPolygon + } function test_geocoding_functions_zipcodes_aus() { @@ -30,6 +34,8 @@ function test_geocoding_functions_zipcodes_aus() { sql "SELECT (admin0_available_services(Array['AUS'])).postal_code_points" should true #Checks that zipcode points are available sql "SELECT (admin0_available_services(Array['AUS'])).postal_code_polygons" should true + sql "SELECT ST_GeometryType((geocode_postalcode_polygons(Array['3012'], 'AUS')).geom)" should ST_MultiPolygon + } From a93b9d719ed2cfb292ab06f711926a6809236b00 Mon Sep 17 00:00:00 2001 From: Carla Date: Wed, 15 Jul 2015 17:16:03 +0200 Subject: [PATCH 3/3] Adds data tests --- geocoder/postal-codes/test/data/test.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/geocoder/postal-codes/test/data/test.sh b/geocoder/postal-codes/test/data/test.sh index 2ec3f7a..25e93f8 100644 --- a/geocoder/postal-codes/test/data/test.sh +++ b/geocoder/postal-codes/test/data/test.sh @@ -15,6 +15,7 @@ function test_geocoding_quality_zipcodes_usa() { sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'USA'" should 1 #Checks that zipcode points are available sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'USA'" should 1 + sql "SELECT ST_GeometryType(the_geom) from postal_code_polygons where postal_code = '11211' and adm0_a3 = 'USA'" should ST_MultiPolygon } @@ -24,7 +25,7 @@ function test_geocoding_quality_zipcodes_fra() { sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'FRA'" should 1 #Checks that zipcode points are available sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'FRA'" should 1 - + sql "SELECT ST_GeometryType(the_geom) from postal_code_polygons where postal_code = '23270' and adm0_a3 = 'FRA'" should ST_MultiPolygon } function test_geocoding_quality_zipcodes_can() { @@ -32,6 +33,7 @@ function test_geocoding_quality_zipcodes_can() { sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'CAN'" should 1 #Checks that zipcode points are available sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'CAN'" should 1 + sql "SELECT ST_GeometryType(the_geom) from postal_code_polygons where postal_code = 'A0J' and adm0_a3 = 'CAN'" should ST_MultiPolygon } @@ -40,7 +42,7 @@ function test_geocoding_quality_zipcodes_aus() { sql "SELECT count(*) FROM available_services where postal_code_polygons is true and adm0_a3 = 'AUS'" should 1 #Checks that zipcode points are available sql "SELECT count(*) FROM available_services where postal_code_points is true and adm0_a3 = 'AUS'" should 1 - + sql "SELECT ST_GeometryType(the_geom) from postal_code_polygons where postal_code = '3012' and adm0_a3 = 'AUS'" should ST_MultiPolygon }