From 7ca014400479d07855e668e8569bc1da986bee11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Delobelle?= Date: Sun, 6 Jan 2019 10:26:00 +0100 Subject: [PATCH] kfet.tests -- Deterministic responses for cancel_operation --- kfet/views.py | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/kfet/views.py b/kfet/views.py index a2a69930..51bfc1a4 100644 --- a/kfet/views.py +++ b/kfet/views.py @@ -1301,13 +1301,27 @@ def kpsul_cancel_operations(request): stock=F("stock") + to_articles_stocks[article] ) + # Need refresh from db cause we used update on querysets. + # Sort objects by pk to get deterministic responses. + opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts] + opegroups = ( + OperationGroup.objects.values("id", "amount", "is_cof") + .filter(pk__in=opegroups_pk) + .order_by("pk") + ) + opes = sorted(opes) + checkouts_pk = [checkout.pk for checkout in to_checkouts_balances] + checkouts = ( + Checkout.objects.values("id", "balance") + .filter(pk__in=checkouts_pk) + .order_by("pk") + ) + articles_pk = [article.pk for articles in to_articles_stocks] + articles = Article.objects.values("id", "stock").filter(pk__in=articles_pk) + # Websocket data websocket_data = {"opegroups": [], "opes": [], "checkouts": [], "articles": []} - # Need refresh from db cause we used update on querysets - opegroups_pk = [opegroup.pk for opegroup in to_groups_amounts] - opegroups = OperationGroup.objects.values("id", "amount", "is_cof").filter( - pk__in=opegroups_pk - ) + for opegroup in opegroups: websocket_data["opegroups"].append( { @@ -1327,16 +1341,10 @@ def kpsul_cancel_operations(request): "canceled_at": canceled_at, } ) - # Need refresh from db cause we used update on querysets - checkouts_pk = [checkout.pk for checkout in to_checkouts_balances] - checkouts = Checkout.objects.values("id", "balance").filter(pk__in=checkouts_pk) for checkout in checkouts: websocket_data["checkouts"].append( {"id": checkout["id"], "balance": checkout["balance"]} ) - # Need refresh from db cause we used update on querysets - articles_pk = [article.pk for articles in to_articles_stocks] - articles = Article.objects.values("id", "stock").filter(pk__in=articles_pk) for article in articles: websocket_data["articles"].append( {"id": article["id"], "stock": article["stock"]}