new test for uint and json APIs

This commit is contained in:
Christian Grothoff 2015-06-10 17:39:34 +02:00
parent aef3b7c350
commit 9a3dd7cb25
5 changed files with 49 additions and 9 deletions

View File

@ -230,7 +230,9 @@ TALER_PQ_query_param_uint64 (const uint64_t *x);
/** /**
* Generate query parameter for a JSON object (stored as a string * Generate query parameter for a JSON object (stored as a string
* in the DB). * in the DB). Note that @a x must really be a JSON object or array,
* passing just a value (string, integer) is not supported and will
* result in an abort.
* *
* @param x pointer to the json object to pass * @param x pointer to the json object to pass
*/ */
@ -471,7 +473,7 @@ TALER_PQ_result_spec_uint16 (const char *name,
*/ */
struct TALER_PQ_ResultSpec struct TALER_PQ_ResultSpec
TALER_PQ_result_spec_uint32 (const char *name, TALER_PQ_result_spec_uint32 (const char *name,
uint16_t *u32); uint32_t *u32);
/** /**

View File

@ -17,7 +17,7 @@ libtalerpq_la_SOURCES = \
libtalerpq_la_LIBADD = \ libtalerpq_la_LIBADD = \
$(top_builddir)/src/util/libtalerutil.la \ $(top_builddir)/src/util/libtalerutil.la \
-lgnunetutil \ -lgnunetutil -ljansson \
-lpq $(XLIB) -lpq $(XLIB)
libtalerpq_la_LDFLAGS = \ libtalerpq_la_LDFLAGS = \
@ -36,5 +36,5 @@ test_pq_SOURCES = \
test_pq_LDADD = \ test_pq_LDADD = \
libtalerpq.la \ libtalerpq.la \
$(top_builddir)/src/util/libtalerutil.la \ $(top_builddir)/src/util/libtalerutil.la \
-lgnunetutil \ -lgnunetutil -ljansson \
-lpq $(XLIB) -lpq $(XLIB)

View File

@ -237,6 +237,7 @@ TALER_PQ_exec_prepared (PGconn *db_conn,
char *str; char *str;
str = json_dumps (json, JSON_COMPACT); str = json_dumps (json, JSON_COMPACT);
GNUNET_assert (NULL != str);
scratch[soff++] = str; scratch[soff++] = str;
param_values[off] = (void *) str; param_values[off] = (void *) str;
param_lengths[off] = strlen (str); param_lengths[off] = strlen (str);

View File

@ -294,7 +294,7 @@ TALER_PQ_result_spec_uint16 (const char *name,
*/ */
struct TALER_PQ_ResultSpec struct TALER_PQ_ResultSpec
TALER_PQ_result_spec_uint32 (const char *name, TALER_PQ_result_spec_uint32 (const char *name,
uint16_t *u32) uint32_t *u32)
{ {
struct TALER_PQ_ResultSpec res = struct TALER_PQ_ResultSpec res =
{TALER_PQ_RF_UINT32, (void *) u32, sizeof (*u32), (name), NULL }; {TALER_PQ_RF_UINT32, (void *) u32, sizeof (*u32), (name), NULL };

View File

@ -13,7 +13,6 @@
You should have received a copy of the GNU General Public License along with You should have received a copy of the GNU General Public License along with
TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/>
*/ */
/** /**
* @file pq/test_pq.c * @file pq/test_pq.c
* @brief Tests for Postgres convenience API * @brief Tests for Postgres convenience API
@ -61,10 +60,14 @@ postgres_prepare (PGconn *db_conn)
",namount_frac" ",namount_frac"
",namount_curr" ",namount_curr"
",vsize" ",vsize"
",u16"
",u32"
",u64"
",json"
") VALUES " ") VALUES "
"($1, $2, $3, $4, $5, $6," "($1, $2, $3, $4, $5, $6,"
"$7, $8, $9, $10, $11, $12);", "$7, $8, $9, $10, $11, $12, $13, $14, $15, $16);",
11, NULL); 16, NULL);
PREPARE ("test_select", PREPARE ("test_select",
"SELECT" "SELECT"
" pub" " pub"
@ -79,6 +82,10 @@ postgres_prepare (PGconn *db_conn)
",namount_frac" ",namount_frac"
",namount_curr" ",namount_curr"
",vsize" ",vsize"
",u16"
",u32"
",u64"
",json"
" FROM test_pq" " FROM test_pq"
" ORDER BY abs_time DESC " " ORDER BY abs_time DESC "
" LIMIT 1;", " LIMIT 1;",
@ -116,6 +123,14 @@ run_queries (PGconn *conn)
char msg[] = "Hello"; char msg[] = "Hello";
void *msg2; void *msg2;
size_t msg2_len; size_t msg2_len;
uint16_t u16;
uint16_t u162;
uint32_t u32;
uint32_t u322;
uint64_t u64;
uint64_t u642;
json_t *json;
json_t *json2;
priv = GNUNET_CRYPTO_rsa_private_key_create (1024); priv = GNUNET_CRYPTO_rsa_private_key_create (1024);
pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
@ -130,6 +145,12 @@ run_queries (PGconn *conn)
GNUNET_assert (GNUNET_OK == GNUNET_assert (GNUNET_OK ==
TALER_string_to_amount ("EUR:4.4", TALER_string_to_amount ("EUR:4.4",
&hamount)); &hamount));
u16 = 16;
u32 = 32;
u64 = 64;
json = json_object ();
json_object_set (json, "foo", json_integer (42));
GNUNET_assert (NULL != json);
/* FIXME: test TALER_PQ_result_spec_variable_size */ /* FIXME: test TALER_PQ_result_spec_variable_size */
{ {
struct TALER_PQ_QueryParam params_insert[] = { struct TALER_PQ_QueryParam params_insert[] = {
@ -141,6 +162,10 @@ run_queries (PGconn *conn)
TALER_PQ_query_param_amount (&hamount), TALER_PQ_query_param_amount (&hamount),
TALER_PQ_query_param_amount_nbo (&namount), TALER_PQ_query_param_amount_nbo (&namount),
TALER_PQ_query_param_fixed_size (msg, strlen (msg)), TALER_PQ_query_param_fixed_size (msg, strlen (msg)),
TALER_PQ_query_param_uint16 (&u16),
TALER_PQ_query_param_uint32 (&u32),
TALER_PQ_query_param_uint64 (&u64),
TALER_PQ_query_param_json (json),
TALER_PQ_query_param_end TALER_PQ_query_param_end
}; };
struct TALER_PQ_QueryParam params_select[] = { struct TALER_PQ_QueryParam params_select[] = {
@ -155,6 +180,10 @@ run_queries (PGconn *conn)
TALER_PQ_result_spec_amount ("hamount", &hamount2), TALER_PQ_result_spec_amount ("hamount", &hamount2),
TALER_PQ_result_spec_amount_nbo ("namount", &namount2), TALER_PQ_result_spec_amount_nbo ("namount", &namount2),
TALER_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len), TALER_PQ_result_spec_variable_size ("vsize", &msg2, &msg2_len),
TALER_PQ_result_spec_uint16 ("u16", &u162),
TALER_PQ_result_spec_uint32 ("u32", &u322),
TALER_PQ_result_spec_uint64 ("u64", &u642),
TALER_PQ_result_spec_json ("json", &json2),
TALER_PQ_result_spec_end TALER_PQ_result_spec_end
}; };
@ -219,7 +248,11 @@ run_queries (PGconn *conn)
strncmp (msg, strncmp (msg,
msg2, msg2,
msg2_len)); msg2_len));
GNUNET_break (16 == u162);
GNUNET_break (32 == u322);
GNUNET_break (64 == u642);
GNUNET_break (42 == json_integer_value (json_object_get (json2, "foo")));
json_decref (json2);
TALER_PQ_cleanup_result (results_select); TALER_PQ_cleanup_result (results_select);
PQclear (result); PQclear (result);
} }
@ -269,6 +302,10 @@ main(int argc,
",namount_frac INT4 NOT NULL" ",namount_frac INT4 NOT NULL"
",namount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL" ",namount_curr VARCHAR("TALER_CURRENCY_LEN_STR") NOT NULL"
",vsize VARCHAR NOT NULL" ",vsize VARCHAR NOT NULL"
",u16 INT2 NOT NULL"
",u32 INT4 NOT NULL"
",u64 INT8 NOT NULL"
",json VARCHAR NOT NULL"
")"); ")");
if (PGRES_COMMAND_OK != PQresultStatus (result)) if (PGRES_COMMAND_OK != PQresultStatus (result))
{ {