Skip to content

Commit c0a89dd

Browse files
committed
use classes in services/handlers test [tests_use_classes]
1 parent 94c0c0c commit c0a89dd

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

tests/unit/test_handlers.py

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# pylint: disable=no-self-use
12
from unittest import mock
23
import pytest
34
from allocation.adapters import repository
@@ -28,49 +29,47 @@ def rollback(self):
2829
pass
2930

3031

31-
def test_add_batch_for_new_product():
32-
uow = FakeUnitOfWork()
33-
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
34-
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
35-
assert uow.committed
36-
37-
38-
def test_add_batch_for_existing_product():
39-
uow = FakeUnitOfWork()
40-
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
41-
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
42-
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
43-
44-
45-
def test_allocate_returns_allocation():
46-
uow = FakeUnitOfWork()
47-
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
48-
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
49-
assert result == "batch1"
50-
51-
52-
def test_allocate_errors_for_invalid_sku():
53-
uow = FakeUnitOfWork()
54-
services.add_batch("b1", "AREALSKU", 100, None, uow)
55-
56-
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
57-
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
58-
59-
60-
def test_allocate_commits():
61-
uow = FakeUnitOfWork()
62-
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
63-
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
64-
assert uow.committed
65-
66-
67-
def test_sends_email_on_out_of_stock_error():
68-
uow = FakeUnitOfWork()
69-
services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow)
70-
71-
with mock.patch("allocation.adapters.email.send_mail") as mock_send_mail:
72-
services.allocate("o1", "POPULAR-CURTAINS", 10, uow)
73-
assert mock_send_mail.call_args == mock.call(
74-
"stock@made.com",
75-
f"Out of stock for POPULAR-CURTAINS",
76-
)
32+
class TestAddBatch:
33+
def test_for_new_product(self):
34+
uow = FakeUnitOfWork()
35+
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
36+
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
37+
assert uow.committed
38+
39+
def test_for_existing_product(self):
40+
uow = FakeUnitOfWork()
41+
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
42+
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
43+
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
44+
45+
46+
class TestAllocate:
47+
def test_returns_allocation(self):
48+
uow = FakeUnitOfWork()
49+
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
50+
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
51+
assert result == "batch1"
52+
53+
def test_errors_for_invalid_sku(self):
54+
uow = FakeUnitOfWork()
55+
services.add_batch("b1", "AREALSKU", 100, None, uow)
56+
57+
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
58+
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
59+
60+
def test_commits(self):
61+
uow = FakeUnitOfWork()
62+
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
63+
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
64+
assert uow.committed
65+
66+
def test_sends_email_on_out_of_stock_error(self):
67+
uow = FakeUnitOfWork()
68+
services.add_batch("b1", "POPULAR-CURTAINS", 9, None, uow)
69+
70+
with mock.patch("allocation.adapters.email.send_mail") as mock_send_mail:
71+
services.allocate("o1", "POPULAR-CURTAINS", 10, uow)
72+
assert mock_send_mail.call_args == mock.call(
73+
"stock@made.com",
74+
f"Out of stock for POPULAR-CURTAINS",
75+
)

0 commit comments

Comments
 (0)