aboutsummaryrefslogtreecommitdiff
path: root/src/backenddb/test_merchantdb.c
diff options
context:
space:
mode:
authorChristian Blättler <blatc2@bfh.ch>2024-06-13 11:34:46 +0200
committerChristian Blättler <blatc2@bfh.ch>2024-06-13 11:34:46 +0200
commit599db2c13cc7b4eddab22f1e8ef55dde23086f68 (patch)
tree53f1060f6507cffdaf3a139428d7ab4614b13efa /src/backenddb/test_merchantdb.c
parentb9315cf4675bfb2ae94b71cf8931963b27873a5b (diff)
parent2e8cfbe844d4fbcf5fa6f086be0b184821e1bdc2 (diff)
Merge branch 'master' into tokens-payment
# Conflicts: # src/backend/taler-merchant-httpd_private-post-orders.c # src/backenddb/Makefile.am # src/backenddb/merchant-0006.sql # src/backenddb/pg_insert_token_family_key.c
Diffstat (limited to 'src/backenddb/test_merchantdb.c')
-rw-r--r--src/backenddb/test_merchantdb.c251
1 files changed, 204 insertions, 47 deletions
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index f113b01f..3591a133 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -797,20 +797,48 @@ check_products_equal (const struct TALER_MERCHANTDB_ProductDetails *a,
*
* @param instance the instance to insert the product for.
* @param product the product data to insert.
+ * @param num_cats length of the @a cats array
+ * @param cats array of categories for the product
* @param expected_result the result we expect the db to return.
+ * @param expect_conflict expected conflict status
+ * @param expect_no_instance expected instance missing status
+ * @param expected_no_cat expected category missing index
* @return 0 when successful, 1 otherwise.
*/
static int
test_insert_product (const struct InstanceData *instance,
const struct ProductData *product,
- enum GNUNET_DB_QueryStatus expected_result)
+ unsigned int num_cats,
+ const uint64_t *cats,
+ enum GNUNET_DB_QueryStatus expected_result,
+ bool expect_conflict,
+ bool expect_no_instance,
+ ssize_t expected_no_cat)
{
+ bool conflict;
+ bool no_instance;
+ ssize_t no_cat;
+
TEST_COND_RET_ON_FAIL (expected_result ==
plugin->insert_product (plugin->cls,
instance->instance.id,
product->id,
- &product->product),
+ &product->product,
+ num_cats,
+ cats,
+ &no_instance,
+ &conflict,
+ &no_cat),
"Insert product failed\n");
+ if (expected_result > 0)
+ {
+ TEST_COND_RET_ON_FAIL (no_instance == expect_no_instance,
+ "No instance wrong");
+ TEST_COND_RET_ON_FAIL (conflict == expect_conflict,
+ "Conflict wrong");
+ TEST_COND_RET_ON_FAIL (no_cat == expected_no_cat,
+ "Wrong category missing returned");
+ }
return 0;
}
@@ -826,14 +854,54 @@ test_insert_product (const struct InstanceData *instance,
static int
test_update_product (const struct InstanceData *instance,
const struct ProductData *product,
- enum GNUNET_DB_QueryStatus expected_result)
-{
- TEST_COND_RET_ON_FAIL (expected_result ==
- plugin->update_product (plugin->cls,
- instance->instance.id,
- product->id,
- &product->product),
- "Update product failed\n");
+ unsigned int num_cats,
+ const uint64_t *cats,
+ enum GNUNET_DB_QueryStatus expected_result,
+ bool expect_no_instance,
+ bool expect_no_product,
+ bool expect_lost_reduced,
+ bool expect_sold_reduced,
+ bool expect_stocked_reduced,
+ ssize_t expected_no_cat)
+
+{
+ bool no_instance;
+ ssize_t no_cat;
+ bool no_product;
+ bool lost_reduced;
+ bool sold_reduced;
+ bool stocked_reduced;
+
+ TEST_COND_RET_ON_FAIL (
+ expected_result ==
+ plugin->update_product (plugin->cls,
+ instance->instance.id,
+ product->id,
+ &product->product,
+ num_cats,
+ cats,
+ &no_instance,
+ &no_cat,
+ &no_product,
+ &lost_reduced,
+ &sold_reduced,
+ &stocked_reduced),
+ "Update product failed\n");
+ if (expected_result > 0)
+ {
+ TEST_COND_RET_ON_FAIL (no_instance == expect_no_instance,
+ "No instance wrong");
+ TEST_COND_RET_ON_FAIL (no_product == expect_no_product,
+ "No product wrong");
+ TEST_COND_RET_ON_FAIL (lost_reduced == expect_lost_reduced,
+ "No product wrong");
+ TEST_COND_RET_ON_FAIL (stocked_reduced == expect_stocked_reduced,
+ "Stocked reduced wrong");
+ TEST_COND_RET_ON_FAIL (sold_reduced == expect_sold_reduced,
+ "Sold reduced wrong");
+ TEST_COND_RET_ON_FAIL (no_cat == expected_no_cat,
+ "Wrong category missing returned");
+ }
return 0;
}
@@ -850,25 +918,34 @@ test_lookup_product (const struct InstanceData *instance,
const struct ProductData *product)
{
struct TALER_MERCHANTDB_ProductDetails lookup_result;
+ size_t num_categories = 0;
+ uint64_t *categories = NULL;
+
if (0 > plugin->lookup_product (plugin->cls,
instance->instance.id,
product->id,
- &lookup_result))
+ &lookup_result,
+ &num_categories,
+ &categories))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Lookup product failed\n");
TALER_MERCHANTDB_product_details_free (&lookup_result);
return 1;
}
- const struct TALER_MERCHANTDB_ProductDetails *to_cmp = &product->product;
- if (0 != check_products_equal (&lookup_result,
- to_cmp))
+ GNUNET_free (categories);
{
- GNUNET_break (0);
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Lookup product failed: incorrect product returned\n");
- TALER_MERCHANTDB_product_details_free (&lookup_result);
- return 1;
+ const struct TALER_MERCHANTDB_ProductDetails *to_cmp = &product->product;
+
+ if (0 != check_products_equal (&lookup_result,
+ to_cmp))
+ {
+ GNUNET_break (0);
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup product failed: incorrect product returned\n");
+ TALER_MERCHANTDB_product_details_free (&lookup_result);
+ return 1;
+ }
}
TALER_MERCHANTDB_product_details_free (&lookup_result);
return 0;
@@ -1076,31 +1153,67 @@ run_test_products (struct TestProducts_Closure *cls)
/* Test that insert without an instance fails */
TEST_RET_ON_FAIL (test_insert_product (&cls->instance,
&cls->products[0],
- GNUNET_DB_STATUS_SUCCESS_NO_RESULTS));
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ true,
+ -1));
/* Insert the instance */
TEST_RET_ON_FAIL (test_insert_instance (&cls->instance,
GNUNET_DB_STATUS_SUCCESS_ONE_RESULT));
/* Test inserting a product */
TEST_RET_ON_FAIL (test_insert_product (&cls->instance,
&cls->products[0],
- GNUNET_DB_STATUS_SUCCESS_ONE_RESULT));
- /* Test that double insert fails */
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ -1));
+ /* Test that double insert succeeds */
TEST_RET_ON_FAIL (test_insert_product (&cls->instance,
&cls->products[0],
- GNUNET_DB_STATUS_SUCCESS_NO_RESULTS));
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ -1));
+ /* Test that conflicting insert fails */
+ {
+ uint64_t cat = 42;
+
+ TEST_RET_ON_FAIL (test_insert_product (&cls->instance,
+ &cls->products[0],
+ 1,
+ &cat,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ true,
+ false,
+ -1));
+ }
/* Test lookup of individual products */
TEST_RET_ON_FAIL (test_lookup_product (&cls->instance,
&cls->products[0]));
/* Make sure it fails correctly for products that don't exist */
- if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
- plugin->lookup_product (plugin->cls,
- cls->instance.instance.id,
- "nonexistent_product",
- NULL))
{
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Lookup product failed\n");
- return 1;
+ size_t num_categories = 0;
+ uint64_t *categories = NULL;
+
+ if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS !=
+ plugin->lookup_product (plugin->cls,
+ cls->instance.instance.id,
+ "nonexistent_product",
+ NULL,
+ &num_categories,
+ &categories))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup product failed\n");
+ return 1;
+ }
+ GNUNET_free (categories);
}
/* Test product update */
cls->products[0].product.description =
@@ -1126,37 +1239,76 @@ run_test_products (struct TestProducts_Closure *cls)
json_array_append_new (cls->products[0].product.address,
json_string ("444 Some Street")));
cls->products[0].product.next_restock = GNUNET_TIME_timestamp_get ();
- TEST_RET_ON_FAIL (test_update_product (&cls->instance,
- &cls->products[0],
- GNUNET_DB_STATUS_SUCCESS_ONE_RESULT));
+ TEST_RET_ON_FAIL (test_update_product (
+ &cls->instance,
+ &cls->products[0],
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ false,
+ false,
+ false,
+ -1));
{
struct ProductData stock_dec = cls->products[0];
stock_dec.product.total_stock = 40;
- TEST_RET_ON_FAIL (test_update_product (&cls->instance,
- &stock_dec,
- GNUNET_DB_STATUS_SUCCESS_NO_RESULTS))
- ;
+ TEST_RET_ON_FAIL (test_update_product (
+ &cls->instance,
+ &stock_dec,
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ false,
+ false,
+ true,
+ -1));
}
{
struct ProductData lost_dec = cls->products[0];
lost_dec.product.total_lost = 1;
- TEST_RET_ON_FAIL (test_update_product (&cls->instance,
- &lost_dec,
- GNUNET_DB_STATUS_SUCCESS_NO_RESULTS))
- ;
+ TEST_RET_ON_FAIL (test_update_product (
+ &cls->instance,
+ &lost_dec,
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ true,
+ false,
+ false,
+ -1));
}
TEST_RET_ON_FAIL (test_lookup_product (&cls->instance,
&cls->products[0]));
- TEST_RET_ON_FAIL (test_update_product (&cls->instance,
- &cls->products[1],
- GNUNET_DB_STATUS_SUCCESS_NO_RESULTS));
+ TEST_RET_ON_FAIL (test_update_product (
+ &cls->instance,
+ &cls->products[1],
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ true,
+ false,
+ false,
+ false,
+ -1));
/* Test collective product lookup */
TEST_RET_ON_FAIL (test_insert_product (&cls->instance,
&cls->products[1],
- GNUNET_DB_STATUS_SUCCESS_ONE_RESULT));
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ -1));
TEST_RET_ON_FAIL (test_lookup_products (&cls->instance,
2,
cls->products));
@@ -2061,7 +2213,12 @@ run_test_orders (struct TestOrders_Closure *cls)
/* Test order lock */
TEST_RET_ON_FAIL (test_insert_product (&cls->instance,
&cls->product,
- GNUNET_DB_STATUS_SUCCESS_ONE_RESULT));
+ 0,
+ NULL,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT,
+ false,
+ false,
+ -1));
if (1 != plugin->insert_order_lock (plugin->cls,
cls->instance.instance.id,
cls->orders[0].id,