diff options
Diffstat (limited to 'src/pq/test_pq.c')
-rw-r--r-- | src/pq/test_pq.c | 215 |
1 files changed, 147 insertions, 68 deletions
diff --git a/src/pq/test_pq.c b/src/pq/test_pq.c index 52e92b561..0fd2bfddf 100644 --- a/src/pq/test_pq.c +++ b/src/pq/test_pq.c @@ -1,6 +1,6 @@ /* This file is part of TALER - (C) 2015, 2016 Taler Systems SA + (C) 2015, 2016, 2023 Taler Systems SA TALER is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -21,6 +21,7 @@ #include "platform.h" #include "taler_util.h" #include "taler_pq_lib.h" +#include <gnunet/gnunet_pq_lib.h> /** @@ -29,29 +30,29 @@ * @param db database handle to initialize * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure */ -static int +static enum GNUNET_GenericReturnValue postgres_prepare (struct GNUNET_PQ_Context *db) { struct GNUNET_PQ_PreparedStatement ps[] = { GNUNET_PQ_make_prepare ("test_insert", "INSERT INTO test_pq (" - " hamount_val" - ",hamount_frac" - ",namount_val" - ",namount_frac" + " tamount" ",json" + ",aamount" + ",tamountc" + ",hash" + ",hashes" ") VALUES " - "($1, $2, $3, $4, $5);", - 5), + "($1, $2, $3, $4, $5, $6);"), GNUNET_PQ_make_prepare ("test_select", "SELECT" - " hamount_val" - ",hamount_frac" - ",namount_val" - ",namount_frac" + " tamount" ",json" - " FROM test_pq;", - 0), + ",aamount" + ",tamountc" + ",hash" + ",hashes" + " FROM test_pq;"), GNUNET_PQ_PREPARED_STATEMENT_END }; @@ -68,33 +69,64 @@ postgres_prepare (struct GNUNET_PQ_Context *db) static int run_queries (struct GNUNET_PQ_Context *conn) { - struct TALER_Amount hamount; - struct TALER_Amount hamount2; - struct TALER_AmountNBO namount; - struct TALER_AmountNBO namount2; - PGresult *result; - int ret; + struct TALER_Amount tamount; + struct TALER_Amount aamount[3]; + struct TALER_Amount tamountc; + struct GNUNET_HashCode hc = + {{0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef, + 0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef, + 0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef, + 0xdeadbeef,0xdeadbeef,0xdeadbeef,0xdeadbeef, }}; + struct GNUNET_HashCode hcs[2] = + {{{0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f, + 0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f, + 0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f, + 0xc0feec0f,0xc0feec0f,0xc0feec0f,0xc0feec0f,}}, + {{0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf, + 0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf, + 0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf, + 0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,0xdeadbeaf,}}}; json_t *json; - json_t *json2; GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:5.5", - &hamount)); - TALER_amount_hton (&namount, - &hamount); + TALER_string_to_amount ("EUR:5.3", + &aamount[0])); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("EUR:6.4", + &aamount[1])); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("EUR:7.5", + &aamount[2])); GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:4.4", - &hamount)); + TALER_string_to_amount ("EUR:7.7", + &tamount)); + GNUNET_assert (GNUNET_OK == + TALER_string_to_amount ("FOO:8.7", + &tamountc)); json = json_object (); - json_object_set_new (json, "foo", json_integer (42)); GNUNET_assert (NULL != json); + GNUNET_assert (0 == + json_object_set_new (json, + "foo", + json_integer (42))); { struct GNUNET_PQ_QueryParam params_insert[] = { - TALER_PQ_query_param_amount (&hamount), - TALER_PQ_query_param_amount_nbo (&namount), + TALER_PQ_query_param_amount (conn, + &tamount), TALER_PQ_query_param_json (json), + TALER_PQ_query_param_array_amount (3, + aamount, + conn), + TALER_PQ_query_param_amount_with_currency (conn, + &tamountc), + GNUNET_PQ_query_param_fixed_size (&hc, + sizeof (hc)), + TALER_PQ_query_param_array_hash_code (2, + hcs, + conn), GNUNET_PQ_query_param_end }; + PGresult *result; result = GNUNET_PQ_exec_prepared (conn, "test_insert", @@ -108,55 +140,76 @@ run_queries (struct GNUNET_PQ_Context *conn) return 1; } PQclear (result); + json_decref (json); } { + struct TALER_Amount tamount2; + struct TALER_Amount tamountc2; + struct TALER_Amount *pamount; + struct GNUNET_HashCode hc2; + struct GNUNET_HashCode *hcs2; + size_t npamount; + size_t nhcs; + json_t *json2; struct GNUNET_PQ_QueryParam params_select[] = { GNUNET_PQ_query_param_end }; + struct GNUNET_PQ_ResultSpec results_select[] = { + TALER_PQ_result_spec_amount ("tamount", + "EUR", + &tamount2), + TALER_PQ_result_spec_json ("json", + &json2), + TALER_PQ_result_spec_array_amount (conn, + "aamount", + "EUR", + &npamount, + &pamount), + TALER_PQ_result_spec_amount_with_currency ("tamountc", + &tamountc2), + GNUNET_PQ_result_spec_auto_from_type ("hash", + &hc2), + TALER_PQ_result_spec_array_hash_code (conn, + "hashes", + &nhcs, + &hcs2), + GNUNET_PQ_result_spec_end + }; - result = GNUNET_PQ_exec_prepared (conn, - "test_select", - params_select); if (1 != - PQntuples (result)) + GNUNET_PQ_eval_prepared_singleton_select (conn, + "test_select", + params_select, + results_select)) { GNUNET_break (0); - PQclear (result); return 1; } - } - - { - struct GNUNET_PQ_ResultSpec results_select[] = { - TALER_PQ_result_spec_amount ("hamount", "EUR", &hamount2), - TALER_PQ_result_spec_amount_nbo ("namount", "EUR", &namount2), - TALER_PQ_result_spec_json ("json", &json2), - GNUNET_PQ_result_spec_end - }; - - ret = GNUNET_PQ_extract_result (result, - results_select, - 0); - GNUNET_break (0 == - TALER_amount_cmp (&hamount, - &hamount2)); - GNUNET_assert (GNUNET_OK == - TALER_string_to_amount ("EUR:5.5", - &hamount)); - TALER_amount_ntoh (&hamount2, - &namount2); GNUNET_break (0 == - TALER_amount_cmp (&hamount, - &hamount2)); + TALER_amount_cmp (&tamount, + &tamount2)); GNUNET_break (42 == - json_integer_value (json_object_get (json2, "foo"))); + json_integer_value (json_object_get (json2, + "foo"))); + GNUNET_break (3 == npamount); + for (size_t i = 0; i < 3; i++) + { + GNUNET_break (0 == + TALER_amount_cmp (&aamount[i], + &pamount[i])); + } + GNUNET_break (0 == + TALER_amount_cmp (&tamountc, + &tamountc2)); + GNUNET_break (0 == GNUNET_memcmp (&hc,&hc2)); + for (size_t i = 0; i < 2; i++) + { + GNUNET_break (0 == + GNUNET_memcmp (&hcs[i], + &hcs2[i])); + } GNUNET_PQ_cleanup_result (results_select); - PQclear (result); } - json_decref (json); - if (GNUNET_OK != ret) - return 1; - return 0; } @@ -166,12 +219,37 @@ main (int argc, const char *const argv[]) { struct GNUNET_PQ_ExecuteStatement es[] = { + GNUNET_PQ_make_execute ("DO $$ " + " BEGIN" + " CREATE DOMAIN gnunet_hashcode AS BYTEA" + " CHECK(length(VALUE)=64);" + " EXCEPTION" + " WHEN duplicate_object THEN null;" + " END " + "$$;"), + GNUNET_PQ_make_execute ("DO $$ " + " BEGIN" + " CREATE TYPE taler_amount AS" + " (val INT8, frac INT4);" + " EXCEPTION" + " WHEN duplicate_object THEN null;" + " END " + "$$;"), + GNUNET_PQ_make_execute ("DO $$ " + " BEGIN" + " CREATE TYPE taler_amount_currency AS" + " (val INT8, frac INT4, curr VARCHAR(12));" + " EXCEPTION" + " WHEN duplicate_object THEN null;" + " END " + "$$;"), GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS test_pq (" - " hamount_val INT8 NOT NULL" - ",hamount_frac INT4 NOT NULL" - ",namount_val INT8 NOT NULL" - ",namount_frac INT4 NOT NULL" + " tamount taler_amount NOT NULL" ",json VARCHAR NOT NULL" + ",aamount taler_amount[]" + ",tamountc taler_amount_currency" + ",hash gnunet_hashcode" + ",hashes gnunet_hashcode[]" ")"), GNUNET_PQ_EXECUTE_STATEMENT_END }; @@ -196,6 +274,7 @@ main (int argc, GNUNET_PQ_disconnect (conn); return 1; } + ret = run_queries (conn); { struct GNUNET_PQ_ExecuteStatement ds[] = { |