summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-04-24 20:14:41 +0200
committerChristian Grothoff <christian@grothoff.org>2021-04-24 20:14:41 +0200
commit50f2485ae6ce6e99dcf29712c2008eae17e3ddc3 (patch)
treea6a6456df9edc0d5519ddaa40776accefc5d3241 /src
parent77c92672cd0f83964e8a6992295595653b0f31db (diff)
downloadmerchant-50f2485ae6ce6e99dcf29712c2008eae17e3ddc3.tar.gz
merchant-50f2485ae6ce6e99dcf29712c2008eae17e3ddc3.tar.bz2
merchant-50f2485ae6ce6e99dcf29712c2008eae17e3ddc3.zip
fix product update logic
Diffstat (limited to 'src')
-rw-r--r--src/backenddb/plugin_merchantdb_postgres.c17
-rw-r--r--src/backenddb/test_merchantdb.c22
2 files changed, 24 insertions, 15 deletions
diff --git a/src/backenddb/plugin_merchantdb_postgres.c b/src/backenddb/plugin_merchantdb_postgres.c
index efdfff9a..84c3827e 100644
--- a/src/backenddb/plugin_merchantdb_postgres.c
+++ b/src/backenddb/plugin_merchantdb_postgres.c
@@ -1111,14 +1111,16 @@ postgres_update_product (void *cls,
GNUNET_PQ_query_param_string (pd->unit),
GNUNET_PQ_query_param_string (pd->image), /* $6 */
TALER_PQ_query_param_json (pd->taxes),
- TALER_PQ_query_param_amount (&pd->price),
- GNUNET_PQ_query_param_uint64 (&pd->total_stock),
- GNUNET_PQ_query_param_uint64 (&pd->total_lost), /* $10 */
+ TALER_PQ_query_param_amount (&pd->price), /* $8+$9 */
+ GNUNET_PQ_query_param_uint64 (&pd->total_stock), /* $10 */
+ GNUNET_PQ_query_param_uint64 (&pd->total_lost),
+ GNUNET_PQ_query_param_uint64 (&pd->total_sold),
TALER_PQ_query_param_json (pd->address),
GNUNET_PQ_query_param_absolute_time (&pd->next_restock),
GNUNET_PQ_query_param_end
};
+ GNUNET_assert (pd->total_stock >= pd->total_lost + pd->total_sold);
check_connection (pg);
return GNUNET_PQ_eval_prepared_non_select (pg->conn,
"update_product",
@@ -6317,17 +6319,18 @@ postgres_connect (void *cls)
",price_frac=$9"
",total_stock=$10"
",total_lost=$11"
- ",address=$12"
- ",next_restock=$13"
+ ",total_sold=$12"
+ ",address=$13"
+ ",next_restock=$14"
" WHERE merchant_serial="
" (SELECT merchant_serial"
" FROM merchant_instances"
" WHERE merchant_id=$1)"
" AND product_id=$2"
" AND total_stock <= $10"
- " AND $10 - total_sold >= $11"
+ " AND total_sold <= $12"
" AND total_lost <= $11",
- 13),
+ 14),
/* for postgres_lock_product() */
GNUNET_PQ_make_prepare ("lock_product",
diff --git a/src/backenddb/test_merchantdb.c b/src/backenddb/test_merchantdb.c
index dd32e652..bd357ae8 100644
--- a/src/backenddb/test_merchantdb.c
+++ b/src/backenddb/test_merchantdb.c
@@ -40,12 +40,14 @@ static struct TALER_MERCHANTDB_Plugin *plugin;
#define TEST_WITH_FAIL_CLAUSE(test, on_fail) \
if (0 != (test)) \
{ \
+ GNUNET_break (0); \
on_fail \
}
#define TEST_COND_RET_ON_FAIL(cond, msg) \
if (! (cond)) \
{ \
+ GNUNET_break (0); \
GNUNET_log (GNUNET_ERROR_TYPE_ERROR, \
msg); \
return 1; \
@@ -872,6 +874,7 @@ test_lookup_product (const struct InstanceData *instance,
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);
@@ -1120,7 +1123,7 @@ run_test_products (struct TestProducts_Closure *cls)
json_array_append_new (cls->products[0].product.taxes,
json_string ("2% sales tax")));
cls->products[0].product.total_stock = 100;
- cls->products[0].product.total_sold = 10;
+ cls->products[0].product.total_sold = 0; /* will be ignored! */
cls->products[0].product.total_lost = 7;
GNUNET_free (cls->products[0].product.image);
cls->products[0].product.image = GNUNET_strdup ("image");
@@ -1134,25 +1137,28 @@ run_test_products (struct TestProducts_Closure *cls)
{
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));
}
{
- struct ProductData sold_dec = cls->products[0];
- sold_dec.product.total_sold = 5;
- TEST_RET_ON_FAIL (test_update_product (&cls->instance,
- &sold_dec,
- GNUNET_DB_STATUS_SUCCESS_NO_RESULTS));
- }
- {
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));
}
+ cls->products[0].product.total_sold = 5;
+ {
+ struct ProductData sold_dec = cls->products[0];
+
+ TEST_RET_ON_FAIL (test_update_product (&cls->instance,
+ &sold_dec,
+ GNUNET_DB_STATUS_SUCCESS_ONE_RESULT));
+ }
TEST_RET_ON_FAIL (test_lookup_product (&cls->instance,
&cls->products[0]));
TEST_RET_ON_FAIL (test_update_product (&cls->instance,