kfet.tests -- Deterministic responses for cancel_operation

This commit is contained in:
Aurélien Delobelle 2019-01-06 10:26:00 +01:00
parent 7c1d1df1a9
commit 7ca0144004

View file

@ -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"]}