[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[taler-merchant] 134/277: finished writing db tests for products
From: |
gnunet |
Subject: |
[taler-merchant] 134/277: finished writing db tests for products |
Date: |
Sun, 05 Jul 2020 20:50:47 +0200 |
This is an automated email from the git hooks/post-receive script.
grothoff pushed a commit to branch master
in repository merchant.
commit b4fc637130072ed0e49c614d12688240e91713b0
Author: Jonathan Buchanan <jonathan.russ.buchanan@gmail.com>
AuthorDate: Thu May 21 18:08:24 2020 -0400
finished writing db tests for products
---
src/backenddb/test_merchantdb.c | 238 ++++++++++++++++++++++++++++++++--------
1 file changed, 191 insertions(+), 47 deletions(-)
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index 4d2b66b..aa82bf5 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -36,6 +36,11 @@ static int result;
*/
static struct TALER_MERCHANTDB_Plugin *plugin;
+#define TEST_RET_ON_FAIL(test) if (0 != test) \
+ { \
+ return 1; \
+ }
+
/**
* Number of instances detected.
*/
@@ -83,7 +88,7 @@ lookup_instances_cb (void *cls,
const struct TALER_MERCHANTDB_AccountDetails accounts[])
{
instance_count += 1;
- if (8 > instance_count)
+ if (8 >= instance_count)
{
/* Duplicate the instance settings */
copy_instance (&instances_found[instance_count - 1], is);
@@ -116,6 +121,42 @@ check_instances_equal (const struct
TALER_MERCHANTDB_InstanceSettings *a,
}
+/**
+ * Number of instances detected.
+ */
+static int product_count;
+
+/**
+ * An array of instance settings found from the lookup method.
+ */
+static char *products_found[8];
+
+
+static void
+free_product (struct TALER_MERCHANTDB_ProductDetails *pd)
+{
+ GNUNET_free (pd->description);
+ json_decref (pd->description_i18n);
+ GNUNET_free (pd->unit);
+ json_decref (pd->image);
+ json_decref (pd->address);
+}
+
+
+static void
+lookup_products_cb (void *cls,
+ const char *product_id)
+{
+ product_count += 1;
+ if (8 >= product_count)
+ {
+ products_found[product_count - 1] =
+ GNUNET_malloc (sizeof(char) * (strlen (product_id) + 1));
+ strcpy (products_found[product_count - 1], product_id);
+ }
+}
+
+
static int
check_products_equal (const struct TALER_MERCHANTDB_ProductDetails *a,
const struct TALER_MERCHANTDB_ProductDetails *b)
@@ -123,7 +164,7 @@ check_products_equal (const struct
TALER_MERCHANTDB_ProductDetails *a,
if ((0 != strcmp (a->description, b->description)) ||
(1 != json_equal (a->description_i18n, b->description_i18n)) ||
(0 != strcmp (a->unit, b->unit)) ||
- (0 != TALER_amount_cmp_currency (&a->price, &b->price)) ||
+ (GNUNET_OK != TALER_amount_cmp_currency (&a->price, &b->price)) ||
(0 != TALER_amount_cmp (&a->price, &b->price)) ||
(1 != json_equal (a->taxes, b->taxes)) ||
(a->total_stock != b->total_stock) ||
@@ -145,24 +186,6 @@ check_products_equal (const struct
TALER_MERCHANTDB_ProductDetails *a,
static int
test_instances (void *cls)
{
- /* Test lookup instances- there should be nothing here */
- instance_count = 0;
- if (0 > plugin->lookup_instances (plugin->cls, false,
- &lookup_instances_cb, cls))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Lookup instances failed\n");
- plugin->drop_tables (plugin->cls); /* Try to drop tables if possible to
clean up for the next test */
- return 1;
- }
- if (instance_count != 0)
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Non-zero number of instances found after initialization\n");
- plugin->drop_tables (plugin->cls);
- return 1;
- }
-
/* Test making an instance */
struct TALER_MerchantPublicKeyP merchant_pub;
struct TALER_MerchantPrivateKeyP merchant_priv;
@@ -312,6 +335,55 @@ test_instances (void *cls)
}
+static int
+test_insert_product (const char *is,
+ const char *pd_id,
+ const struct TALER_MERCHANTDB_ProductDetails *pd)
+{
+ if (0 > plugin->insert_product (plugin->cls, is, pd_id,
+ pd))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Insert product failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ return 0;
+}
+
+
+static int
+test_lookup_product (const char *is,
+ const char *pd_id,
+ struct TALER_MERCHANTDB_ProductDetails *pd)
+{
+ if (0 > plugin->lookup_product (plugin->cls, is, pd_id,
+ pd))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup product failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ return 0;
+}
+
+
+static int
+test_delete_product (const char *is,
+ const char *pd)
+{
+ if (0 > plugin->delete_product (plugin->cls, is, pd))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Delete product failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ return 0;
+}
+
+
/**
* Function that tests products.
*
@@ -333,8 +405,8 @@ test_products (void *cls)
json_array_append (is.address, json_string ("123 Example St"));
is.jurisdiction = json_array ();
json_array_append (is.jurisdiction, json_string ("Ohio"));
- TALER_string_to_amount ("USD:1200.40", &is.default_max_deposit_fee);
- TALER_string_to_amount ("USD:1200.40", &is.default_max_wire_fee);
+ TALER_string_to_amount ("EUR:1200.40", &is.default_max_deposit_fee);
+ TALER_string_to_amount ("EUR:1200.40", &is.default_max_wire_fee);
is.default_wire_fee_amortization = 1;
is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
@@ -352,44 +424,31 @@ test_products (void *cls)
pd.description = "This is a test product";
pd.description_i18n = json_array ();
pd.unit = "boxes";
- TALER_string_to_amount ("USD:1200.40", &pd.price);
+ TALER_string_to_amount ("EUR:120.40", &pd.price);
pd.taxes = json_array ();
pd.total_stock = 55;
- pd.total_sold = 28;
- pd.total_lost = 2;
+ pd.total_sold = 0;
+ pd.total_lost = 0;
pd.image = json_array ();
pd.address = json_array ();
pd.next_restock = GNUNET_TIME_absolute_get_zero_ ();
- if (0 > plugin->insert_product (plugin->cls, "test_instance_1", "is_0_pd_0",
- &pd))
- {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Insert product failed\n");
- plugin->drop_tables (plugin->cls);
- return 1;
- }
+ TEST_RET_ON_FAIL (test_insert_product ("test_instance_0", "is_0_pd_0", &pd));
/* Test lookup of individual products */
struct TALER_MERCHANTDB_ProductDetails lookup_pd;
- if (0 > plugin->lookup_product (plugin->cls, "test_instance_1", "is_0_pd_0",
- &lookup_pd))
+ TEST_RET_ON_FAIL (test_lookup_product ("test_instance_0", "is_0_pd_0",
+ &lookup_pd));
+ if (0 != check_products_equal (&pd, &lookup_pd))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Lookup product failed\n");
+ "Lookup product failed: incorrect product returned\n");
plugin->drop_tables (plugin->cls);
return 1;
}
- /* This test currently FAILS */
- /*if (0 != check_products_equal(&pd, &lookup_pd)) {
- GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Lookup product failed: incorrect product returned\n");
- result = 1;
- plugin->drop_tables (plugin->cls);
- return;
- }*/
+ free_product (&lookup_pd);
/* Make sure it fails correctly for products that don't exist */
- if (0 != plugin->lookup_product (plugin->cls, "test_instance_1",
+ if (0 != plugin->lookup_product (plugin->cls, "test_instance_0",
"fictional_product", &lookup_pd))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -398,11 +457,95 @@ test_products (void *cls)
return 1;
}
+ /* Test product update */
+ pd.description = "This is a test product that has been updated!";
+ if (0 > plugin->update_product (plugin->cls, "test_instance_0", "is_0_pd_0",
+ &pd))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Update product failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ TEST_RET_ON_FAIL (test_lookup_product ("test_instance_0", "is_0_pd_0",
+ &lookup_pd));
+ if (0 != check_products_equal (&pd, &lookup_pd))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Update product failed: lookup returns old product\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ free_product (&lookup_pd);
+
+ /* Test collective product lookup */
+ struct TALER_MERCHANTDB_ProductDetails pd1 = pd;
+ pd1.description = "This is another product.";
+ pd1.unit = "cans";
+ TALER_string_to_amount ("EUR:4.95", &pd1.price);
+ TEST_RET_ON_FAIL (test_insert_product ("test_instance_0", "is_0_pd_1",
&pd1));
+ product_count = 0;
+ if (0 > plugin->lookup_products (plugin->cls, "test_instance_0",
+ &lookup_products_cb, cls))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup products failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ if (2 != product_count)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup products failed: incorrect number of products
found\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ if (! (((0 == strcmp ("is_0_pd_0", products_found[0])) &&
+ (0 == strcmp ("is_0_pd_1", products_found[1]))) ||
+ ((0 == strcmp ("is_0_pd_1", products_found[0])) &&
+ (0 == strcmp ("is_0_pd_0", products_found[1])))))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup products failed: incorrect product ids found\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ GNUNET_free (products_found[0]);
+ GNUNET_free (products_found[1]);
+
/* Test product deletion */
- if (0 > plugin->delete_product (plugin->cls, "test_instance_1", "is_0_pd_0"))
+ TEST_RET_ON_FAIL (test_delete_product ("test_instance_0", "is_0_pd_0"));
+ product_count = 0;
+ if (0 > plugin->lookup_products (plugin->cls, "test_instance_0",
+ &lookup_products_cb, cls))
{
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
- "Delete product failed\n");
+ "Lookup products failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ GNUNET_free (products_found[0]);
+ if (1 != product_count)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup products/delete product failed: incorrect number of
products after deletion\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ TEST_RET_ON_FAIL (test_delete_product ("test_instance_0", "is_0_pd_1"));
+ product_count = 0;
+ if (0 > plugin->lookup_products (plugin->cls, "test_instance_0",
+ &lookup_products_cb, cls))
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup products failed\n");
+ plugin->drop_tables (plugin->cls);
+ return 1;
+ }
+ if (0 != product_count)
+ {
+ GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+ "Lookup products/delete product failed: incorrect number of
products after deletion\n");
plugin->drop_tables (plugin->cls);
return 1;
}
@@ -451,6 +594,7 @@ run (void *cls)
/* Run the preflight */
plugin->preflight (plugin->cls);
+ /* TODO: break reused code into functions */
/* Test instances */
result = test_instances (cls);
if (0 != result)
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [taler-merchant] 139/277: wrote db tests for accounts, (continued)
- [taler-merchant] 139/277: wrote db tests for accounts, gnunet, 2020/07/05
- [taler-merchant] 140/277: DB prep work, gnunet, 2020/07/05
- [taler-merchant] 142/277: wip on DB plugin for tip pickup, gnunet, 2020/07/05
- [taler-merchant] 144/277: refactored instance test code, gnunet, 2020/07/05
- [taler-merchant] 146/277: wrote tests for deposits in the db api, gnunet, 2020/07/05
- [taler-merchant] 149/277: more db tests for transfers and tips, gnunet, 2020/07/05
- [taler-merchant] 151/277: backenddb tests for lookup_transfers, lookup_transfer_summary, lookup_transfer_details, gnunet, 2020/07/05
- [taler-merchant] 150/277: backenddb tests for refunds, gnunet, 2020/07/05
- [taler-merchant] 153/277: fixed test for increase_refund, gnunet, 2020/07/05
- [taler-merchant] 108/277: sketch for GET /transfers cmd, gnunet, 2020/07/05
- [taler-merchant] 134/277: finished writing db tests for products,
gnunet <=
- [taler-merchant] 137/277: implement POST /tips/ID/pickup, gnunet, 2020/07/05
- [taler-merchant] 141/277: Merge branch 'protocolV1' of git+ssh://git.taler.net/merchant into protocolV1, gnunet, 2020/07/05
- [taler-merchant] 143/277: more SQL for tips, gnunet, 2020/07/05
- [taler-merchant] 148/277: fixed inventory locking, test code for transfer lookups, gnunet, 2020/07/05
- [taler-merchant] 155/277: fix ftbfs, gnunet, 2020/07/05
- [taler-merchant] 154/277: return creation_time instead of leaving it uninitizlied, mark problematic missing lines, gnunet, 2020/07/05
- [taler-merchant] 158/277: work on tip APIs, gnunet, 2020/07/05
- [taler-merchant] 156/277: implement lookup tip details, gnunet, 2020/07/05
- [taler-merchant] 159/277: add POST /tips logic, gnunet, 2020/07/05
- [taler-merchant] 164/277: fixed db test building and added test for wire fee storage, gnunet, 2020/07/05