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