summaryrefslogtreecommitdiff
path: root/src/backenddb
diff options
context:
space:
mode:
authorJonathan Buchanan <jonathan.russ.buchanan@gmail.com>2020-05-21 18:08:24 -0400
committerJonathan Buchanan <jonathan.russ.buchanan@gmail.com>2020-05-21 18:08:24 -0400
commitb4fc637130072ed0e49c614d12688240e91713b0 (patch)
treeefe3d830e4be4b2910ba41bef4b078ba21805e1f /src/backenddb
parentb21e20f96e8906bed35b93c0bb3f6192778629f7 (diff)
downloadmerchant-b4fc637130072ed0e49c614d12688240e91713b0.tar.gz
merchant-b4fc637130072ed0e49c614d12688240e91713b0.tar.bz2
merchant-b4fc637130072ed0e49c614d12688240e91713b0.zip
finished writing db tests for products
Diffstat (limited to 'src/backenddb')
-rw-r--r--src/backenddb/test_merchantdb.c238
1 files changed, 191 insertions, 47 deletions
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index 4d2b66b1..aa82bf5b 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)