forked from DGNum/gestioCOF
Fix kpsul tests
This commit is contained in:
parent
3aa928e8f0
commit
693e4252d5
1 changed files with 93 additions and 52 deletions
|
@ -3,6 +3,8 @@ from datetime import datetime, timedelta
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from unittest import mock
|
from unittest import mock
|
||||||
|
|
||||||
|
from asgiref.sync import async_to_sync
|
||||||
|
from channels.layers import get_channel_layer
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
from django.test import Client, TestCase
|
from django.test import Client, TestCase
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
@ -1808,10 +1810,13 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.account.balance = Decimal("50.00")
|
self.account.balance = Decimal("50.00")
|
||||||
self.account.save()
|
self.account.save()
|
||||||
|
|
||||||
# Mock consumer of K-Psul websocket to catch what we're sending
|
# Create a channel to listen to KPsul's messages
|
||||||
kpsul_consumer_patcher = mock.patch("kfet.consumers.KPsul")
|
channel_layer = get_channel_layer()
|
||||||
self.kpsul_consumer_mock = kpsul_consumer_patcher.start()
|
self.channel = async_to_sync(channel_layer.new_channel)()
|
||||||
self.addCleanup(kpsul_consumer_patcher.stop)
|
|
||||||
|
async_to_sync(channel_layer.group_add)("kfet.kpsul", self.channel)
|
||||||
|
|
||||||
|
self.receive_msg = lambda: async_to_sync(channel_layer.receive)(self.channel)
|
||||||
|
|
||||||
# Reset cache of kfet config
|
# Reset cache of kfet config
|
||||||
kfet_config._conf_init = False
|
kfet_config._conf_init = False
|
||||||
|
@ -2043,9 +2048,12 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.assertEqual(self.article.stock, 18)
|
self.assertEqual(self.article.stock, 18)
|
||||||
|
|
||||||
# Check websocket data
|
# Check websocket data
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_once_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"groups": [
|
"groups": [
|
||||||
{
|
{
|
||||||
"add": True,
|
"add": True,
|
||||||
|
@ -2307,9 +2315,12 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("110.75"))
|
self.assertEqual(self.checkout.balance, Decimal("110.75"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_once_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"groups": [
|
"groups": [
|
||||||
{
|
{
|
||||||
"add": True,
|
"add": True,
|
||||||
|
@ -2478,9 +2489,12 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("89.25"))
|
self.assertEqual(self.checkout.balance, Decimal("89.25"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_once_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"groups": [
|
"groups": [
|
||||||
{
|
{
|
||||||
"add": True,
|
"add": True,
|
||||||
|
@ -2635,9 +2649,12 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_once_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"groups": [
|
"groups": [
|
||||||
{
|
{
|
||||||
"add": True,
|
"add": True,
|
||||||
|
@ -2750,9 +2767,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
||||||
|
|
||||||
ws_data_ope = self.kpsul_consumer_mock.group_send.call_args[0][1]["groups"][0][
|
ws_data = self.receive_msg()
|
||||||
"entries"
|
ws_data_ope = ws_data["groups"][0]["entries"][0]
|
||||||
][0]
|
|
||||||
self.assertEqual(ws_data_ope["addcost_amount"], Decimal("1.00"))
|
self.assertEqual(ws_data_ope["addcost_amount"], Decimal("1.00"))
|
||||||
self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD")
|
self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD")
|
||||||
|
|
||||||
|
@ -2790,9 +2807,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
||||||
|
|
||||||
ws_data_ope = self.kpsul_consumer_mock.group_send.call_args[0][1]["groups"][0][
|
ws_data = self.receive_msg()
|
||||||
"entries"
|
ws_data_ope = ws_data["groups"][0]["entries"][0]
|
||||||
][0]
|
|
||||||
self.assertEqual(ws_data_ope["addcost_amount"], Decimal("0.80"))
|
self.assertEqual(ws_data_ope["addcost_amount"], Decimal("0.80"))
|
||||||
self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD")
|
self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD")
|
||||||
|
|
||||||
|
@ -2828,9 +2845,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("106.00"))
|
self.assertEqual(self.checkout.balance, Decimal("106.00"))
|
||||||
|
|
||||||
ws_data_ope = self.kpsul_consumer_mock.group_send.call_args[0][1]["groups"][0][
|
ws_data = self.receive_msg()
|
||||||
"entries"
|
ws_data_ope = ws_data["groups"][0]["entries"][0]
|
||||||
][0]
|
|
||||||
self.assertEqual(ws_data_ope["addcost_amount"], Decimal("1.00"))
|
self.assertEqual(ws_data_ope["addcost_amount"], Decimal("1.00"))
|
||||||
self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD")
|
self.assertEqual(ws_data_ope["addcost_for__trigramme"], "ADD")
|
||||||
|
|
||||||
|
@ -2864,9 +2881,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.accounts["addcost"].refresh_from_db()
|
self.accounts["addcost"].refresh_from_db()
|
||||||
self.assertEqual(self.accounts["addcost"].balance, Decimal("15.00"))
|
self.assertEqual(self.accounts["addcost"].balance, Decimal("15.00"))
|
||||||
|
|
||||||
ws_data_ope = self.kpsul_consumer_mock.group_send.call_args[0][1]["groups"][0][
|
ws_data = self.receive_msg()
|
||||||
"entries"
|
ws_data_ope = ws_data["groups"][0]["entries"][0]
|
||||||
][0]
|
|
||||||
self.assertEqual(ws_data_ope["addcost_amount"], None)
|
self.assertEqual(ws_data_ope["addcost_amount"], None)
|
||||||
self.assertEqual(ws_data_ope["addcost_for__trigramme"], None)
|
self.assertEqual(ws_data_ope["addcost_for__trigramme"], None)
|
||||||
|
|
||||||
|
@ -2899,9 +2916,9 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.accounts["addcost"].refresh_from_db()
|
self.accounts["addcost"].refresh_from_db()
|
||||||
self.assertEqual(self.accounts["addcost"].balance, Decimal("0.00"))
|
self.assertEqual(self.accounts["addcost"].balance, Decimal("0.00"))
|
||||||
|
|
||||||
ws_data_ope = self.kpsul_consumer_mock.group_send.call_args[0][1]["groups"][0][
|
ws_data = self.receive_msg()
|
||||||
"entries"
|
ws_data_ope = ws_data["groups"][0]["entries"][0]
|
||||||
][0]
|
|
||||||
self.assertEqual(ws_data_ope["addcost_amount"], None)
|
self.assertEqual(ws_data_ope["addcost_amount"], None)
|
||||||
self.assertEqual(ws_data_ope["addcost_for__trigramme"], None)
|
self.assertEqual(ws_data_ope["addcost_for__trigramme"], None)
|
||||||
|
|
||||||
|
@ -3123,9 +3140,12 @@ class KPsulPerformOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.assertEqual(article2.stock, -6)
|
self.assertEqual(article2.stock, -6)
|
||||||
|
|
||||||
# Check websocket data
|
# Check websocket data
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_once_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"groups": [
|
"groups": [
|
||||||
{
|
{
|
||||||
"add": True,
|
"add": True,
|
||||||
|
@ -3218,10 +3238,13 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.account.balance = Decimal("50.00")
|
self.account.balance = Decimal("50.00")
|
||||||
self.account.save()
|
self.account.save()
|
||||||
|
|
||||||
# Mock consumer of K-Psul websocket to catch what we're sending
|
# Create a channel to listen to KPsul's messages
|
||||||
kpsul_consumer_patcher = mock.patch("kfet.consumers.KPsul")
|
channel_layer = get_channel_layer()
|
||||||
self.kpsul_consumer_mock = kpsul_consumer_patcher.start()
|
self.channel = async_to_sync(channel_layer.new_channel)()
|
||||||
self.addCleanup(kpsul_consumer_patcher.stop)
|
|
||||||
|
async_to_sync(channel_layer.group_add)("kfet.kpsul", self.channel)
|
||||||
|
|
||||||
|
self.receive_msg = lambda: async_to_sync(channel_layer.receive)(self.channel)
|
||||||
|
|
||||||
def _assertResponseOk(self, response):
|
def _assertResponseOk(self, response):
|
||||||
"""
|
"""
|
||||||
|
@ -3271,7 +3294,11 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
on_acc=self.account,
|
on_acc=self.account,
|
||||||
checkout=self.checkout,
|
checkout=self.checkout,
|
||||||
content=[
|
content=[
|
||||||
{"type": Operation.PURCHASE, "article": self.article, "article_nb": 2}
|
{
|
||||||
|
"type": Operation.PURCHASE,
|
||||||
|
"article": self.article,
|
||||||
|
"article_nb": 2,
|
||||||
|
}
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
operation = group.opes.get()
|
operation = group.opes.get()
|
||||||
|
@ -3345,9 +3372,15 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
{"checkouts": [], "articles": [{"id": self.article.pk, "stock": 22}]},
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
|
{
|
||||||
|
"type": "kpsul",
|
||||||
|
"checkouts": [],
|
||||||
|
"articles": [{"id": self.article.pk, "stock": 22}],
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_purchase_with_addcost(self):
|
def test_purchase_with_addcost(self):
|
||||||
|
@ -3407,11 +3440,11 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("95.00"))
|
self.assertEqual(self.checkout.balance, Decimal("95.00"))
|
||||||
|
|
||||||
ws_data_checkouts = self.kpsul_consumer_mock.group_send.call_args[0][1][
|
ws_data = self.receive_msg()
|
||||||
"checkouts"
|
|
||||||
]
|
|
||||||
self.assertListEqual(
|
self.assertListEqual(
|
||||||
ws_data_checkouts, [{"id": self.checkout.pk, "balance": Decimal("95.00")}]
|
ws_data["checkouts"],
|
||||||
|
[{"id": self.checkout.pk, "balance": Decimal("95.00")}],
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_purchase_cash_with_addcost(self):
|
def test_purchase_cash_with_addcost(self):
|
||||||
|
@ -3447,11 +3480,11 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
addcost_account.refresh_from_db()
|
addcost_account.refresh_from_db()
|
||||||
self.assertEqual(addcost_account.balance, Decimal("9.00"))
|
self.assertEqual(addcost_account.balance, Decimal("9.00"))
|
||||||
|
|
||||||
ws_data_checkouts = self.kpsul_consumer_mock.group_send.call_args[0][1][
|
ws_data = self.receive_msg()
|
||||||
"checkouts"
|
|
||||||
]
|
|
||||||
self.assertListEqual(
|
self.assertListEqual(
|
||||||
ws_data_checkouts, [{"id": self.checkout.pk, "balance": Decimal("94.00")}]
|
ws_data["checkouts"],
|
||||||
|
[{"id": self.checkout.pk, "balance": Decimal("94.00")}],
|
||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch("django.utils.timezone.now")
|
@mock.patch("django.utils.timezone.now")
|
||||||
|
@ -3533,9 +3566,12 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("89.25"))
|
self.assertEqual(self.checkout.balance, Decimal("89.25"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"checkouts": [{"id": self.checkout.pk, "balance": Decimal("89.25")}],
|
"checkouts": [{"id": self.checkout.pk, "balance": Decimal("89.25")}],
|
||||||
"articles": [],
|
"articles": [],
|
||||||
},
|
},
|
||||||
|
@ -3620,9 +3656,12 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("110.75"))
|
self.assertEqual(self.checkout.balance, Decimal("110.75"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
{
|
{
|
||||||
|
"type": "kpsul",
|
||||||
"checkouts": [{"id": self.checkout.pk, "balance": Decimal("110.75")}],
|
"checkouts": [{"id": self.checkout.pk, "balance": Decimal("110.75")}],
|
||||||
"articles": [],
|
"articles": [],
|
||||||
},
|
},
|
||||||
|
@ -3707,9 +3746,11 @@ class KPsulCancelOperationsViewTests(ViewTestCaseMixin, TestCase):
|
||||||
self.checkout.refresh_from_db()
|
self.checkout.refresh_from_db()
|
||||||
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
self.assertEqual(self.checkout.balance, Decimal("100.00"))
|
||||||
|
|
||||||
self.kpsul_consumer_mock.group_send.assert_called_with(
|
ws_data = self.receive_msg()
|
||||||
"kfet.kpsul",
|
|
||||||
{"checkouts": [], "articles": []},
|
self.assertDictEqual(
|
||||||
|
ws_data,
|
||||||
|
{"type": "kpsul", "checkouts": [], "articles": []},
|
||||||
)
|
)
|
||||||
|
|
||||||
@mock.patch("django.utils.timezone.now")
|
@mock.patch("django.utils.timezone.now")
|
||||||
|
|
Loading…
Reference in a new issue