gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-merchant] 145/277: refactored the existing product and order test


From: gnunet
Subject: [taler-merchant] 145/277: refactored the existing product and order tests
Date: Sun, 05 Jul 2020 20:50:58 +0200

This is an automated email from the git hooks/post-receive script.

grothoff pushed a commit to branch master
in repository merchant.

commit 35a94ff2407b5bfd0be8bed56b284f1727e0ef67
Author: Jonathan Buchanan <jonathan.russ.buchanan@gmail.com>
AuthorDate: Sun May 24 19:50:56 2020 -0400

    refactored the existing product and order tests
---
 src/backenddb/test_merchantdb.c | 878 +++++++++++++++++++++++-----------------
 1 file changed, 507 insertions(+), 371 deletions(-)

diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index 9940c5e..d45089a 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -363,8 +363,8 @@ run_test_instances (struct TestInstances_Closure *cls)
 
   /* Test update instance */
   cls->is.name = "Test - updated";
-  if (0 > plugin->update_instance (plugin->cls,
-                                   &cls->is))
+  if (1 != plugin->update_instance (plugin->cls,
+                                    &cls->is))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Update instance failed\n");
@@ -436,40 +436,62 @@ test_instances (void *cls)
 }
 
 
-/**
- * Number of products detected.
- */
-static int product_count;
+static void
+free_product (struct TALER_MERCHANTDB_ProductDetails *pd)
+{
+  if (NULL != pd->description)
+    GNUNET_free (pd->description);
+  if (NULL != pd->description_i18n)
+    json_decref (pd->description_i18n);
+  if (NULL != pd->unit)
+    GNUNET_free (pd->unit);
+  if (NULL != pd->image)
+    json_decref (pd->image);
+  if (NULL != pd->address)
+    json_decref (pd->address);
+}
 
 
 /**
- * An array of product ids found from the lookup method.
+ * Closure for testing product lookup
  */
-static char *products_found[8];
+struct TestLookupProducts_Closure
+{
+  /**
+   * Number of product ids to compare to
+   */
+  unsigned int products_to_cmp_length;
 
+  /**
+   * Pointer to array of product ids
+   */
+  const char **product_ids_to_cmp;
 
-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);
-}
+  /**
+   * Pointer to array of number of matches for each product
+   */
+  unsigned int *results_matching;
+
+  /**
+   * Total number of results returned
+   */
+  unsigned int results_length;
+};
 
 
 static void
 lookup_products_cb (void *cls,
                     const char *product_id)
 {
-  product_count += 1;
-  if (8 >= product_count)
+  if (cls == NULL)
+    return;
+  struct TestLookupProducts_Closure *cmp = cls;
+  cmp->results_length += 1;
+  for (unsigned int i = 0; cmp->products_to_cmp_length > i; ++i)
   {
-    products_found[product_count - 1] =
-      GNUNET_malloc (sizeof(char) * (strlen (product_id) + 1));
-    strcpy (products_found[product_count - 1],
-            product_id);
+    if (0 == strcmp (cmp->product_ids_to_cmp[i],
+                     product_id))
+      cmp->results_matching[i] += 1;
   }
 }
 
@@ -508,14 +530,13 @@ 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))
+  if (1 != 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;
@@ -525,18 +546,69 @@ test_insert_product (const char *is,
 static int
 test_lookup_product (const char *is,
                      const char *pd_id,
-                     struct TALER_MERCHANTDB_ProductDetails *pd)
+                     const struct TALER_MERCHANTDB_ProductDetails *to_cmp)
 {
+  struct TALER_MERCHANTDB_ProductDetails lookup_result;
   if (0 > plugin->lookup_product (plugin->cls,
                                   is,
                                   pd_id,
-                                  pd))
+                                  &lookup_result))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup product failed\n");
-    plugin->drop_tables (plugin->cls);
+    free_product (&lookup_result);
+    return 1;
+  }
+  if (0 != check_products_equal (&lookup_result,
+                                 to_cmp))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup product failed: incorrect product returned\n");
+    free_product (&lookup_result);
+    return 1;
+  }
+  free_product (&lookup_result);
+  return 0;
+}
+
+
+static int
+test_lookup_products (const char *is,
+                      unsigned int products_length,
+                      const char **product_ids)
+{
+  struct TestLookupProducts_Closure cls;
+  cls.products_to_cmp_length = products_length;
+  cls.product_ids_to_cmp = product_ids;
+  unsigned int results_matching[products_length];
+  for (unsigned int i = 0; products_length > i; ++i)
+    results_matching[i] = 0;
+  cls.results_matching = results_matching;
+  cls.results_length = 0;
+  if (0 > plugin->lookup_products (plugin->cls,
+                                   is,
+                                   &lookup_products_cb,
+                                   &cls))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup products failed\n");
+    return 1;
+  }
+  if (products_length != cls.results_length)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup products failed: incorrect number of results\n");
     return 1;
   }
+  for (unsigned int i = 0; products_length > i; ++i)
+  {
+    if (1 != cls.results_matching[i])
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Lookup products failed: mismatched data\n");
+      return 1;
+    }
+  }
   return 0;
 }
 
@@ -559,239 +631,227 @@ test_delete_product (const char *is,
 
 
 /**
- * Function that tests products.
- *
- * @param cls closure with config
+ * Closure for product tests.
  */
-static int
-test_products (void *cls)
+struct TestProducts_Closure
 {
-  /* Test making an instance */
+  /**
+   * The instance settings
+   */
+  struct TALER_MERCHANTDB_InstanceSettings is;
+
+  /**
+   * The instance public key
+   */
   struct TALER_MerchantPublicKeyP merchant_pub;
+
+  /**
+   * The instance private key
+   */
   struct TALER_MerchantPrivateKeyP merchant_priv;
-  GNUNET_CRYPTO_eddsa_key_create (&merchant_priv.eddsa_priv);
-  GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
-                                      &merchant_pub.eddsa_pub);
-  struct TALER_MERCHANTDB_InstanceSettings is;
-  is.id = "test_instance_0";
-  is.name = "Test";
-  is.address = json_array ();
-  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 ("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_ ();
-  TEST_RET_ON_FAIL (test_insert_instance (&merchant_pub,
-                                          &merchant_priv,
-                                          &is));
-
-  /* Test creating a product */
-  struct TALER_MERCHANTDB_ProductDetails pd;
-  pd.description = "This is a test product";
-  pd.description_i18n = json_array ();
-  pd.unit = "boxes";
-  TALER_string_to_amount ("EUR:120.40",
-                          &pd.price);
-  pd.taxes = json_array ();
-  pd.total_stock = 55;
-  pd.total_sold = 0;
-  pd.total_lost = 0;
-  pd.image = json_array ();
-  pd.address = json_array ();
-  pd.next_restock = GNUNET_TIME_absolute_get_zero_ ();
-  TEST_RET_ON_FAIL (test_insert_product ("test_instance_0",
-                                         "is_0_pd_0",
-                                         &pd));
+
+  /**
+   * The array of products
+   */
+  struct TALER_MERCHANTDB_ProductDetails products[2];
+
+  /**
+   * The array of product ids
+   */
+  const char *product_ids[2];
+};
+
+
+/**
+ * Sets up the data structures used in the product tests
+ */
+static void
+pre_test_products (struct TestProducts_Closure *cls)
+{
+  /* Instance */
+  GNUNET_CRYPTO_eddsa_key_create (&cls->merchant_priv.eddsa_priv);
+  GNUNET_CRYPTO_eddsa_key_get_public (&cls->merchant_priv.eddsa_priv,
+                                      &cls->merchant_pub.eddsa_pub);
+  cls->is.id = "test_inst_products";
+  cls->is.name = "Test";
+  cls->is.address = json_array ();
+  GNUNET_assert (NULL != cls->is.address);
+  GNUNET_assert (0 == json_array_append (cls->is.address,
+                                         json_string ("123 Example St")));
+  cls->is.jurisdiction = json_array ();
+  GNUNET_assert (NULL != cls->is.jurisdiction);
+  GNUNET_assert (0 == json_array_append (cls->is.jurisdiction,
+                                         json_string ("Ohio")));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:1200.40",
+                                         &cls->is.default_max_deposit_fee));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:1200.40",
+                                         &cls->is.default_max_wire_fee));
+  cls->is.default_wire_fee_amortization = 1;
+  cls->is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
+  cls->is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
+
+  /* Products */
+  cls->product_ids[0] = "test_products_pd_0";
+  cls->products[0].description = "This is a test product";
+  cls->products[0].description_i18n = json_array ();
+  GNUNET_assert (NULL != cls->products[0].description_i18n);
+  cls->products[0].unit = "boxes";
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:120.40",
+                                         &cls->products[0].price));
+  cls->products[0].taxes = json_array ();
+  GNUNET_assert (NULL != cls->products[0].taxes);
+  cls->products[0].total_stock = 55;
+  cls->products[0].total_sold = 0;
+  cls->products[0].total_lost = 0;
+  cls->products[0].image = json_array ();
+  GNUNET_assert (NULL != cls->products[0].image);
+  cls->products[0].address = json_array ();
+  GNUNET_assert (NULL != cls->products[0].address);
+  cls->products[0].next_restock = GNUNET_TIME_absolute_get_zero_ ();
+
+  cls->product_ids[1] = "test_products_pd_1";
+  cls->products[1].description = "This is a another test product";
+  cls->products[1].description_i18n = json_array ();
+  GNUNET_assert (NULL != cls->products[1].description_i18n);
+  cls->products[1].unit = "cans";
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:4.95",
+                                         &cls->products[1].price));
+  cls->products[1].taxes = json_array ();
+  GNUNET_assert (NULL != cls->products[1].taxes);
+  cls->products[1].total_stock = 5001;
+  cls->products[1].total_sold = 0;
+  cls->products[1].total_lost = 0;
+  cls->products[1].image = json_array ();
+  GNUNET_assert (NULL != cls->products[1].image);
+  cls->products[1].address = json_array ();
+  GNUNET_assert (NULL != cls->products[1].address);
+  cls->products[1].next_restock = GNUNET_TIME_absolute_get_zero_ ();
+}
+
+
+/**
+ * Handles all teardown after testing
+ */
+static void
+post_test_products (struct TestProducts_Closure *cls)
+{
+  /* Cleanup instance */
+  json_decref (cls->is.address);
+  json_decref (cls->is.jurisdiction);
+
+  /* Cleanup products */
+  json_decref (cls->products[0].description_i18n);
+  json_decref (cls->products[0].taxes);
+  json_decref (cls->products[0].image);
+  json_decref (cls->products[0].address);
+
+  json_decref (cls->products[1].description_i18n);
+  json_decref (cls->products[1].taxes);
+  json_decref (cls->products[1].image);
+  json_decref (cls->products[1].address);
+}
+
+
+static int
+run_test_products (struct TestProducts_Closure *cls)
+{
+  /* Insert the instance */
+  TEST_RET_ON_FAIL (test_insert_instance (&cls->merchant_pub,
+                                          &cls->merchant_priv,
+                                          &cls->is));
+
+  /* Test inserting a product */
+  TEST_RET_ON_FAIL (test_insert_product (cls->is.id,
+                                         cls->product_ids[0],
+                                         &cls->products[0]));
 
   /* Test lookup of individual products */
-  struct TALER_MERCHANTDB_ProductDetails 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: incorrect product returned\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }
-  free_product (&lookup_pd);
+  TEST_RET_ON_FAIL (test_lookup_product (cls->is.id,
+                                         cls->product_ids[0],
+                                         &cls->products[0]));
 
   /* Make sure it fails correctly for products that don't exist */
-  if (0 != plugin->lookup_product (plugin->cls,
-                                   "test_instance_0",
-                                   "fictional_product",
-                                   &lookup_pd))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Lookup product failed: product returned where there was 
none\n");
-    plugin->drop_tables (plugin->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))
+  cls->products[0].description =
+    "This is a test product that has been updated!";
+  if (1 != plugin->update_product (plugin->cls,
+                                   cls->is.id,
+                                   cls->product_ids[0],
+                                   &cls->products[0]))
   {
     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_RET_ON_FAIL (test_lookup_product (cls->is.id,
+                                         cls->product_ids[0],
+                                         &cls->products[0]));
 
   /* 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_RET_ON_FAIL (test_insert_product (cls->is.id,
+                                         cls->product_ids[1],
+                                         &cls->products[1]));
+  TEST_RET_ON_FAIL (test_lookup_products (cls->is.id,
+                                          2,
+                                          cls->product_ids));
 
   /* Test product deletion */
-  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,
-                "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;
-  }
-
-  json_decref (pd.description_i18n);
-  json_decref (pd.taxes);
-  json_decref (pd.image);
-  json_decref (pd.address);
-
-  /* Clean up: delete the instance */
-  /* This test currently FAILS */
-  /*
-  if (0 > plugin->purge_instance(plugin->cls,
-                                 "test_instance_0")) {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Purge instance failed\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }*/json_decref (is.address);
-  json_decref (is.jurisdiction);
+  TEST_RET_ON_FAIL (test_delete_product (cls->is.id,
+                                         cls->product_ids[1]));
+  TEST_RET_ON_FAIL (test_lookup_products (cls->is.id,
+                                          1,
+                                          cls->product_ids));
+  TEST_RET_ON_FAIL (test_delete_product (cls->is.id,
+                                         cls->product_ids[0]));
+  TEST_RET_ON_FAIL (test_lookup_products (cls->is.id,
+                                          0,
+                                          NULL));
 
   return 0;
 }
 
 
-/**
- * Number of orders detected.
- */
-static int order_count;
+static int
+test_products (void *cls)
+{
+  struct TestProducts_Closure test_cls;
+  pre_test_products (&test_cls);
+  int test_result = run_test_products (&test_cls);
+  post_test_products (&test_cls);
+  return test_result;
+}
 
 
 /**
- * An array of order ids found from the lookup method.
+ * Closure for testing order lookup
  */
-static char *orders_found[8];
-
+struct TestLookupOrders_Closure
+{
+  /**
+   * Number of orders to compare to
+   */
+  unsigned int orders_to_cmp_length;
 
-/**
- * An array of order serials found from the lookup method.
- */
-static uint64_t order_serials[8];
+  /**
+   * Pointer to (ordered) array of order ids
+   */
+  const char **order_ids_to_cmp;
 
+  /**
+   * Pointer to array of bools indicating matches in the correct index
+   */
+  bool *results_match;
 
-/**
- * An array of order timestamps found from the lookup method.
- */
-static struct GNUNET_TIME_Absolute order_timestamps[8];
+  /**
+   * Total number of results returned
+   */
+  unsigned int results_length;
+};
 
 
 static void
@@ -800,15 +860,19 @@ lookup_orders_cb (void *cls,
                   uint64_t order_serial,
                   struct GNUNET_TIME_Absolute timestamp)
 {
-  order_count += 1;
-  if (8 >= order_count)
+  if (cls == NULL)
+    return;
+  struct TestLookupOrders_Closure *cmp = cls;
+  unsigned int i = cmp->results_length;
+  cmp->results_length += 1;
+  if (cmp->orders_to_cmp_length > i)
   {
-    orders_found[order_count - 1] =
-      GNUNET_malloc (sizeof(char) * (strlen (order_id) + 1));
-    strcpy (orders_found[order_count - 1],
-            order_id);
-    order_serials[order_count - 1] = order_serial;
-    order_timestamps[order_count - 1] = timestamp;
+    /* Compare the orders */
+    if (0 == strcmp (cmp->order_ids_to_cmp[i],
+                     order_id))
+      cmp->results_match[i] = true;
+    else
+      cmp->results_match[i] = false;
   }
 }
 
@@ -819,15 +883,14 @@ test_insert_order (const char *instance_id,
                    struct GNUNET_TIME_Absolute pay_deadline,
                    const json_t *contract_terms)
 {
-  if (0 > plugin->insert_order (plugin->cls,
-                                instance_id,
-                                order_id,
-                                pay_deadline,
-                                contract_terms))
+  if (1 != plugin->insert_order (plugin->cls,
+                                 instance_id,
+                                 order_id,
+                                 pay_deadline,
+                                 contract_terms))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Insert order failed\n");
-    plugin->drop_tables (plugin->cls);
     return 1;
   }
   return 0;
@@ -837,37 +900,71 @@ test_insert_order (const char *instance_id,
 static int
 test_lookup_order (const char *is,
                    const char *od,
-                   json_t **contract_terms)
+                   const json_t *contract_to_cmp)
 {
+  json_t *lookup_terms = NULL;
   if (0 > plugin->lookup_order (plugin->cls,
                                 is,
                                 od,
-                                contract_terms))
+                                &lookup_terms))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup order failed\n");
-    plugin->drop_tables (plugin->cls);
+    if (NULL != lookup_terms)
+      json_decref (lookup_terms);
+    return 1;
+  }
+  if (1 != json_equal (contract_to_cmp,
+                       lookup_terms))
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup order failed: incorrect order returned\n");
+    if (NULL != lookup_terms)
+      json_decref (lookup_terms);
     return 1;
   }
+  json_decref (lookup_terms);
   return 0;
 }
 
 
 static int
 test_lookup_orders (const char *is,
-                    const struct TALER_MERCHANTDB_OrderFilter *of)
+                    const struct TALER_MERCHANTDB_OrderFilter *of,
+                    unsigned int orders_length,
+                    const char **order_ids_to_cmp)
 {
+  struct TestLookupOrders_Closure cls;
+  cls.orders_to_cmp_length = orders_length;
+  cls.order_ids_to_cmp = order_ids_to_cmp;
+  bool results_match[orders_length];
+  cls.results_match = results_match;
+  cls.results_length = 0;
   if (0 > plugin->lookup_orders (plugin->cls,
                                  is,
                                  of,
                                  &lookup_orders_cb,
-                                 NULL))
+                                 &cls))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Lookup orders failed\n");
-    plugin->drop_tables (plugin->cls);
     return 1;
   }
+  if (orders_length != cls.results_length)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                "Lookup orders failed: incorrect number of results\n");
+    return 1;
+  }
+  for (unsigned int i = 0; orders_length > i; ++i)
+  {
+    if (false == cls.results_match[i])
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                  "Lookup orders failed: mismatched data\n");
+      return 1;
+    }
+  }
   return 0;
 }
 
@@ -882,7 +979,6 @@ test_delete_order (const char *is,
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                 "Delete order failed\n");
-    plugin->drop_tables (plugin->cls);
     return 1;
   }
   return 0;
@@ -890,80 +986,136 @@ test_delete_order (const char *is,
 
 
 /**
- * Function that tests orders.
- *
- * @param cls closure with config
+ * Container for order data
  */
-static int
-test_orders (void *cls)
+struct OrderData
 {
+  /**
+   * The id of the order
+   */
+  const char *id;
+
+  /**
+   * The pay deadline for the order
+   */
+  struct GNUNET_TIME_Absolute pay_deadline;
+
+  /**
+   * The contract of the order
+   */
+  json_t *contract;
+};
+
+
+/**
+ * Closure for order tests.
+ */
+struct TestOrders_Closure
+{
+  /**
+   * The instance settings
+   */
+  struct TALER_MERCHANTDB_InstanceSettings is;
+
+  /**
+   * The instance public key
+   */
   struct TALER_MerchantPublicKeyP merchant_pub;
+
+  /**
+   * The instance private key
+   */
   struct TALER_MerchantPrivateKeyP merchant_priv;
-  GNUNET_CRYPTO_eddsa_key_create (&merchant_priv.eddsa_priv);
-  GNUNET_CRYPTO_eddsa_key_get_public (&merchant_priv.eddsa_priv,
-                                      &merchant_pub.eddsa_pub);
-  struct TALER_MERCHANTDB_InstanceSettings is;
-  is.id = "test_instance_2";
-  is.name = "Test";
-  is.address = json_array ();
-  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 ("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_ ();
-  TEST_RET_ON_FAIL (test_insert_instance (&merchant_pub,
-                                          &merchant_priv,
-                                          &is));
-
-  /* Test insert order */
-  struct GNUNET_TIME_Absolute time_0 = GNUNET_TIME_absolute_get_zero_ ();
-  json_t *terms_0 = json_array ();
-  json_array_append (terms_0,
-                     json_string ("contract"));
-  TEST_RET_ON_FAIL (test_insert_order ("test_instance_2",
-                                       "is_2_or_0",
-                                       time_0,
-                                       terms_0));
+
+  /**
+   * The array of orders
+   */
+  struct OrderData orders[2];
+
+};
+
+
+static void
+pre_test_orders (struct TestOrders_Closure *cls)
+{
+  /* Instance */
+  GNUNET_CRYPTO_eddsa_key_create (&cls->merchant_priv.eddsa_priv);
+  GNUNET_CRYPTO_eddsa_key_get_public (&cls->merchant_priv.eddsa_priv,
+                                      &cls->merchant_pub.eddsa_pub);
+  cls->is.id = "test_inst_orders";
+  cls->is.name = "Test";
+  cls->is.address = json_array ();
+  GNUNET_assert (NULL != cls->is.address);
+  GNUNET_assert (0 == json_array_append (cls->is.address,
+                                         json_string ("123 Example St")));
+  cls->is.jurisdiction = json_array ();
+  GNUNET_assert (NULL != cls->is.jurisdiction);
+  GNUNET_assert (0 == json_array_append (cls->is.jurisdiction,
+                                         json_string ("Ohio")));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:1200.40",
+                                         &cls->is.default_max_deposit_fee));
+  GNUNET_assert (GNUNET_OK ==
+                 TALER_string_to_amount ("EUR:1200.40",
+                                         &cls->is.default_max_wire_fee));
+  cls->is.default_wire_fee_amortization = 1;
+  cls->is.default_wire_transfer_delay = GNUNET_TIME_relative_get_minute_ ();
+  cls->is.default_pay_delay = GNUNET_TIME_relative_get_second_ ();
+
+  /* Orders */
+  cls->orders[0].id = "test_orders_od_0";
+  cls->orders[0].pay_deadline = GNUNET_TIME_absolute_get_zero_ ();
+  cls->orders[0].contract = json_array ();
+  GNUNET_assert (NULL != cls->orders[0].contract);
+  GNUNET_assert (0 == json_array_append (cls->orders[0].contract,
+                                         json_string ("Dummy contract")));
+
+  cls->orders[1].id = "test_orders_od_1";
+  cls->orders[1].pay_deadline = GNUNET_TIME_absolute_get_zero_ ();
+  cls->orders[1].contract = json_array ();
+  GNUNET_assert (NULL != cls->orders[1].contract);
+  GNUNET_assert (0 == json_array_append (cls->orders[1].contract,
+                                         json_string ("Second contract")));
+}
+
+
+static void
+post_test_orders (struct TestOrders_Closure *cls)
+{
+  json_decref (cls->is.address);
+  json_decref (cls->is.jurisdiction);
+
+  json_decref (cls->orders[0].contract);
+  json_decref (cls->orders[1].contract);
+}
+
+
+static int
+run_test_orders (struct TestOrders_Closure *cls)
+{
+  /* Insert the instance */
+  TEST_RET_ON_FAIL (test_insert_instance (&cls->merchant_pub,
+                                          &cls->merchant_priv,
+                                          &cls->is));
+
+  /* Test inserting an order */
+  TEST_RET_ON_FAIL (test_insert_order (cls->is.id,
+                                       cls->orders[0].id,
+                                       cls->orders[0].pay_deadline,
+                                       cls->orders[0].contract));
 
   /* Test lookup order */
-  json_t *lookup_terms;
-  TEST_RET_ON_FAIL (test_lookup_order ("test_instance_2",
-                                       "is_2_or_0",
-                                       &lookup_terms));
-  if (1 != json_equal (terms_0,
-                       lookup_terms))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Lookup order failed: contract terms returned do not match\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }
-  json_decref (lookup_terms);
+  TEST_RET_ON_FAIL (test_lookup_order (cls->is.id,
+                                       cls->orders[0].id,
+                                       cls->orders[0].contract));
 
-  /* Make sure it fails correctly for products that don't exist */
-  if (0 != plugin->lookup_order (plugin->cls,
-                                 "test_instance_2",
-                                 "fictional_order",
-                                 &lookup_terms))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Lookup order failed: order returned where there was none\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }
+  /* Make sure it fails correctly for nonexistent orders */
 
-  /* Test lookups for multiple orders */
-  TEST_RET_ON_FAIL (test_insert_order ("test_instance_2",
-                                       "is_2_or_1",
-                                       time_0,
-                                       terms_0));
+  /* Test lookups on multiple orders */
+  TEST_RET_ON_FAIL (test_insert_order (cls->is.id,
+                                       cls->orders[1].id,
+                                       cls->orders[1].pay_deadline,
+                                       cls->orders[1].contract));
   struct TALER_MERCHANTDB_OrderFilter filter;
   filter.paid = TALER_MERCHANTDB_YNA_ALL;
   filter.refunded = TALER_MERCHANTDB_YNA_ALL;
@@ -971,69 +1123,53 @@ test_orders (void *cls)
   filter.date = GNUNET_TIME_absolute_get_zero_ ();
   filter.start_row = 0;
   filter.delta = 8;
-  order_count = 0;
-  TEST_RET_ON_FAIL (test_lookup_orders ("test_instance_2",
-                                        &filter));
-  if (2 != order_count)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Lookup orders failed: incorrect number of orders found\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }
-  if (! (((0 == strcmp ("is_2_or_0",
-                        orders_found[0])) &&
-          (0 == strcmp ("is_2_or_1",
-                        orders_found[1]))) ||
-         ((0 == strcmp ("is_2_or_1",
-                        orders_found[0])) &&
-          (0 == strcmp ("is_2_or_0",
-                        orders_found[1])))))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Lookup orders failed: incorrect order ids found\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }
-  GNUNET_free (orders_found[0]);
-  GNUNET_free (orders_found[1]);
+  const char *order_id_list[2] = {
+    cls->orders[0].id,
+    cls->orders[1].id
+  };
+  TEST_RET_ON_FAIL (test_lookup_orders (cls->is.id,
+                                        &filter,
+                                        2,
+                                        order_id_list));
 
   /* Test delete order */
-  TEST_RET_ON_FAIL (test_delete_order ("test_instance_2",
-                                       "is_2_or_0"));
-  order_count = 0;
-  TEST_RET_ON_FAIL (test_lookup_orders ("test_instance_2",
-                                        &filter));
-  if (1 != order_count)
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                "Delete order failed: order still present in database\n");
-    plugin->drop_tables (plugin->cls);
-    return 1;
-  }
-  GNUNET_free (orders_found[0]);
+  TEST_RET_ON_FAIL (test_delete_order (cls->is.id,
+                                       cls->orders[1].id));
+  TEST_RET_ON_FAIL (test_lookup_orders (cls->is.id,
+                                        &filter,
+                                        1,
+                                        order_id_list));
+  TEST_RET_ON_FAIL (test_delete_order (cls->is.id,
+                                       cls->orders[0].id));
+  TEST_RET_ON_FAIL (test_lookup_orders (cls->is.id,
+                                        &filter,
+                                        0,
+                                        NULL));
 
   return 0;
 }
 
 
+static int
+test_orders (void *cls)
+{
+  struct TestOrders_Closure test_cls;
+  pre_test_orders (&test_cls);
+  int test_result = run_test_orders (&test_cls);
+  post_test_orders (&test_cls);
+  return test_result;
+}
+
+
 /**
- * Function that runs all tests and returns upon error.
+ * Function that runs all tests and returns 1 upon error, 0 on success.
  */
 static int
 run_tests (void *cls)
 {
-  /* Test instances */
-  if (0 != test_instances (cls))
-    return 1;
-
-  /* Test products */
-  if (0 != test_products (cls))
-    return 1;
-
-  /* Test orders */
-  if (0 != test_orders (cls))
-    return 1;
+  TEST_RET_ON_FAIL (test_instances (cls));
+  TEST_RET_ON_FAIL (test_products (cls));
+  TEST_RET_ON_FAIL (test_orders (cls));
 
   return 0;
 }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]