From ed8f77e99ed1a6698a0affad9b7aa9a4885537b1 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 15 Jul 2019 16:57:14 +0100 Subject: [PATCH 01/30] handlers now have all and only explicit dependencies [handler_with_explicit_dependency] --- src/allocation/service_layer/handlers.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/allocation/service_layer/handlers.py b/src/allocation/service_layer/handlers.py index fa2698a4..bcaf1f44 100644 --- a/src/allocation/service_layer/handlers.py +++ b/src/allocation/service_layer/handlers.py @@ -1,8 +1,7 @@ # pylint: disable=unused-argument from __future__ import annotations from dataclasses import asdict -from typing import TYPE_CHECKING -from allocation.adapters import email, redis_eventpublisher +from typing import Callable, TYPE_CHECKING from allocation.domain import commands, events, model from allocation.domain.model import OrderLine @@ -65,9 +64,9 @@ def change_batch_quantity( def send_out_of_stock_notification( event: events.OutOfStock, - uow: unit_of_work.AbstractUnitOfWork, + send_mail: Callable, ): - email.send( + send_mail( "stock@made.com", f"Out of stock for {event.sku}", ) @@ -75,9 +74,9 @@ def send_out_of_stock_notification( def publish_allocated_event( event: events.Allocated, - uow: unit_of_work.AbstractUnitOfWork, + publish: Callable, ): - redis_eventpublisher.publish("line_allocated", event) + publish("line_allocated", event) def add_allocation_to_read_model( From 61c9877a36068ca8c3a86dab773ae29e1c72338c Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 6 Jan 2020 07:47:34 +0000 Subject: [PATCH 02/30] change reallocate handler to avoid cmd/event clash --- src/allocation/service_layer/handlers.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/allocation/service_layer/handlers.py b/src/allocation/service_layer/handlers.py index bcaf1f44..f56e6f77 100644 --- a/src/allocation/service_layer/handlers.py +++ b/src/allocation/service_layer/handlers.py @@ -43,10 +43,7 @@ def reallocate( event: events.Deallocated, uow: unit_of_work.AbstractUnitOfWork, ): - with uow: - product = uow.products.get(sku=event.sku) - product.events.append(commands.Allocate(**asdict(event))) - uow.commit() + allocate(commands.Allocate(**asdict(event)), uow=uow) def change_batch_quantity( From 4f2892841d11b46d196bd7a8056f500fbdae511a Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 6 Jan 2020 07:47:56 +0000 Subject: [PATCH 03/30] bring static handlers dicts across from messagebus --- src/allocation/service_layer/handlers.py | 15 ++++++++++++++- src/allocation/service_layer/messagebus.py | 21 +-------------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/allocation/service_layer/handlers.py b/src/allocation/service_layer/handlers.py index f56e6f77..6ab2bbe9 100644 --- a/src/allocation/service_layer/handlers.py +++ b/src/allocation/service_layer/handlers.py @@ -1,7 +1,7 @@ # pylint: disable=unused-argument from __future__ import annotations from dataclasses import asdict -from typing import Callable, TYPE_CHECKING +from typing import List, Dict, Callable, Type, TYPE_CHECKING from allocation.domain import commands, events, model from allocation.domain.model import OrderLine @@ -104,3 +104,16 @@ def remove_allocation_from_read_model( dict(orderid=event.orderid, sku=event.sku), ) uow.commit() + + +EVENT_HANDLERS = { + events.Allocated: [publish_allocated_event, add_allocation_to_read_model], + events.Deallocated: [remove_allocation_from_read_model, reallocate], + events.OutOfStock: [send_out_of_stock_notification], +} # type: Dict[Type[events.Event], List[Callable]] + +COMMAND_HANDLERS = { + commands.Allocate: allocate, + commands.CreateBatch: add_batch, + commands.ChangeBatchQuantity: change_batch_quantity, +} # type: Dict[Type[commands.Command], Callable] diff --git a/src/allocation/service_layer/messagebus.py b/src/allocation/service_layer/messagebus.py index 104111fa..12b2965b 100644 --- a/src/allocation/service_layer/messagebus.py +++ b/src/allocation/service_layer/messagebus.py @@ -1,7 +1,7 @@ # pylint: disable=broad-except from __future__ import annotations import logging -from typing import List, Dict, Callable, Type, Union, TYPE_CHECKING +from typing import Union, TYPE_CHECKING from allocation.domain import commands, events from . import handlers @@ -53,22 +53,3 @@ def handle_command( except Exception: logger.exception("Exception handling command %s", command) raise - - -EVENT_HANDLERS = { - events.Allocated: [ - handlers.publish_allocated_event, - handlers.add_allocation_to_read_model, - ], - events.Deallocated: [ - handlers.remove_allocation_from_read_model, - handlers.reallocate, - ], - events.OutOfStock: [handlers.send_out_of_stock_notification], -} # type: Dict[Type[events.Event], List[Callable]] - -COMMAND_HANDLERS = { - commands.Allocate: handlers.allocate, - commands.CreateBatch: handlers.add_batch, - commands.ChangeBatchQuantity: handlers.change_batch_quantity, -} # type: Dict[Type[commands.Command], Callable] From 36781d7f1b4b70475fef3b05a8e87751a62fe66e Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 5 Jan 2020 17:18:11 +0000 Subject: [PATCH 04/30] messagebus becomes a class, requires handlers [messagebus_as_class] --- src/allocation/service_layer/messagebus.py | 34 ++++++++++++++-------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/allocation/service_layer/messagebus.py b/src/allocation/service_layer/messagebus.py index 12b2965b..d210d66f 100644 --- a/src/allocation/service_layer/messagebus.py +++ b/src/allocation/service_layer/messagebus.py @@ -1,9 +1,8 @@ # pylint: disable=broad-except from __future__ import annotations import logging -from typing import Union, TYPE_CHECKING +from typing import Callable, Dict, List, Union, Type, TYPE_CHECKING from allocation.domain import commands, events -from . import handlers if TYPE_CHECKING: from . import unit_of_work @@ -13,16 +12,27 @@ Message = Union[commands.Command, events.Event] -def handle(message: Message, uow: unit_of_work.AbstractUnitOfWork): - queue = [message] - while queue: - message = queue.pop(0) - if isinstance(message, events.Event): - handle_event(message, queue, uow) - elif isinstance(message, commands.Command): - handle_command(message, queue, uow) - else: - raise Exception(f"{message} was not an Event or Command") +class MessageBus: + def __init__( + self, + uow: unit_of_work.AbstractUnitOfWork, + event_handlers: Dict[Type[events.Event], List[Callable]], + command_handlers: Dict[Type[commands.Command], Callable], + ): + self.uow = uow + self.event_handlers = event_handlers + self.command_handlers = command_handlers + + def handle(self, message: Message): + self.queue = [message] + while self.queue: + message = self.queue.pop(0) + if isinstance(message, events.Event): + self.handle_event(message) + elif isinstance(message, commands.Command): + self.handle_command(message) + else: + raise Exception(f"{message} was not an Event or Command") def handle_event( From 7e21991ff28b3802b5c4031edab410785f89fbf0 Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 7 Jan 2020 14:48:53 +0000 Subject: [PATCH 05/30] use self.handlers for handle_event and handle_command [messagebus_handlers_change] --- src/allocation/service_layer/messagebus.py | 46 +++++++++------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/src/allocation/service_layer/messagebus.py b/src/allocation/service_layer/messagebus.py index d210d66f..45679341 100644 --- a/src/allocation/service_layer/messagebus.py +++ b/src/allocation/service_layer/messagebus.py @@ -1,4 +1,4 @@ -# pylint: disable=broad-except +# pylint: disable=broad-except, attribute-defined-outside-init from __future__ import annotations import logging from typing import Callable, Dict, List, Union, Type, TYPE_CHECKING @@ -34,32 +34,22 @@ def handle(self, message: Message): else: raise Exception(f"{message} was not an Event or Command") - -def handle_event( - event: events.Event, - queue: List[Message], - uow: unit_of_work.AbstractUnitOfWork, -): - for handler in EVENT_HANDLERS[type(event)]: + def handle_event(self, event: events.Event): + for handler in self.event_handlers[type(event)]: + try: + logger.debug("handling event %s with handler %s", event, handler) + handler(event) + self.queue.extend(self.uow.collect_new_events()) + except Exception: + logger.exception("Exception handling event %s", event) + continue + + def handle_command(self, command: commands.Command): + logger.debug("handling command %s", command) try: - logger.debug("handling event %s with handler %s", event, handler) - handler(event, uow=uow) - queue.extend(uow.collect_new_events()) + handler = self.command_handlers[type(command)] + handler(command) + self.queue.extend(self.uow.collect_new_events()) except Exception: - logger.exception("Exception handling event %s", event) - continue - - -def handle_command( - command: commands.Command, - queue: List[Message], - uow: unit_of_work.AbstractUnitOfWork, -): - logger.debug("handling command %s", command) - try: - handler = COMMAND_HANDLERS[type(command)] - handler(command, uow=uow) - queue.extend(uow.collect_new_events()) - except Exception: - logger.exception("Exception handling command %s", command) - raise + logger.exception("Exception handling command %s", command) + raise From ff35294d1dc2bc480b0eb93460ec9fde17842544 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 15 Jul 2019 17:08:54 +0100 Subject: [PATCH 06/30] conftest change to backport, session_factory -> sqlite_session_factory --- tests/conftest.py | 11 +++-------- tests/integration/test_repository.py | 5 +++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 3b2a8066..8c1efcf7 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -18,24 +18,19 @@ @pytest.fixture -def in_memory_db(): +def in_memory_sqlite_db(): engine = create_engine("sqlite:///:memory:") metadata.create_all(engine) return engine @pytest.fixture -def sqlite_session_factory(in_memory_db): +def sqlite_session_factory(in_memory_sqlite_db): start_mappers() - yield sessionmaker(bind=in_memory_db) + yield sessionmaker(bind=in_memory_sqlite_db) clear_mappers() -@pytest.fixture -def sqlite_session(sqlite_session_factory): - return sqlite_session_factory() - - @retry(stop=stop_after_delay(10)) def wait_for_postgres_to_come_up(engine): return engine.connect() diff --git a/tests/integration/test_repository.py b/tests/integration/test_repository.py index 9b637f1a..245b26f2 100644 --- a/tests/integration/test_repository.py +++ b/tests/integration/test_repository.py @@ -2,8 +2,9 @@ from allocation.domain import model -def test_get_by_batchref(sqlite_session): - repo = repository.SqlAlchemyRepository(sqlite_session) +def test_get_by_batchref(sqlite_session_factory): + session = sqlite_session_factory() + repo = repository.SqlAlchemyRepository(session) b1 = model.Batch(ref="b1", sku="sku1", qty=100, eta=None) b2 = model.Batch(ref="b2", sku="sku1", qty=100, eta=None) b3 = model.Batch(ref="b3", sku="sku2", qty=100, eta=None) From 79c6b45bcbcc06cc195c60739eeefee0a2a65ccd Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 15 Jul 2019 17:09:18 +0100 Subject: [PATCH 07/30] conftest change to backport to ch 5, isolation serializable in all pg tests --- tests/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/conftest.py b/tests/conftest.py index 8c1efcf7..b0ceb0f2 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -49,7 +49,7 @@ def wait_for_redis_to_come_up(): @pytest.fixture(scope="session") def postgres_db(): - engine = create_engine(config.get_postgres_uri()) + engine = create_engine(config.get_postgres_uri(), isolation_level="SERIALIZABLE") wait_for_postgres_to_come_up(engine) metadata.create_all(engine) return engine From fe6f4996e9402851a702fbb8c4edc34694f128a3 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 15 Jul 2019 17:09:59 +0100 Subject: [PATCH 08/30] uow tests maybe backport, pass explicit uow to threads --- tests/integration/test_uow.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/tests/integration/test_uow.py b/tests/integration/test_uow.py index 55c4f24d..db705849 100644 --- a/tests/integration/test_uow.py +++ b/tests/integration/test_uow.py @@ -1,4 +1,4 @@ -# pylint: disable=broad-except +# pylint: disable=broad-except, too-many-arguments import threading import time import traceback @@ -75,15 +75,15 @@ class MyException(Exception): assert rows == [] -def try_to_allocate(orderid, sku, exceptions): +def try_to_allocate(orderid, sku, exceptions, session_factory): line = model.OrderLine(orderid, sku, 10) try: - with unit_of_work.SqlAlchemyUnitOfWork() as uow: + with unit_of_work.SqlAlchemyUnitOfWork(session_factory) as uow: product = uow.products.get(sku=sku) product.allocate(line) time.sleep(0.2) uow.commit() - except Exception as e: + except Exception as e: # pylint: disable=broad-except print(traceback.format_exc()) exceptions.append(e) @@ -96,8 +96,12 @@ def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory) order1, order2 = random_orderid(1), random_orderid(2) exceptions = [] # type: List[Exception] - try_to_allocate_order1 = lambda: try_to_allocate(order1, sku, exceptions) - try_to_allocate_order2 = lambda: try_to_allocate(order2, sku, exceptions) + try_to_allocate_order1 = lambda: try_to_allocate( + order1, sku, exceptions, postgres_session_factory + ) + try_to_allocate_order2 = lambda: try_to_allocate( + order2, sku, exceptions, postgres_session_factory + ) thread1 = threading.Thread(target=try_to_allocate_order1) thread2 = threading.Thread(target=try_to_allocate_order2) thread1.start() @@ -121,5 +125,5 @@ def test_concurrent_updates_to_version_are_not_allowed(postgres_session_factory) dict(sku=sku), ) assert orders.rowcount == 1 - with unit_of_work.SqlAlchemyUnitOfWork() as uow: + with unit_of_work.SqlAlchemyUnitOfWork(postgres_session_factory) as uow: uow.session.execute("select 1") From eb869900133711ae6c47471b0cc510850207ce63 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 7 Oct 2019 14:29:59 +0100 Subject: [PATCH 09/30] bootstrap script preps DI'd handlers and start orm [bootstrap_script] --- src/allocation/bootstrap.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/allocation/bootstrap.py diff --git a/src/allocation/bootstrap.py b/src/allocation/bootstrap.py new file mode 100644 index 00000000..805b13f5 --- /dev/null +++ b/src/allocation/bootstrap.py @@ -0,0 +1,44 @@ +import inspect +from typing import Callable +from allocation.adapters import email, orm, redis_eventpublisher +from allocation.service_layer import handlers, messagebus, unit_of_work + + +def bootstrap( + start_orm: bool = True, + uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), + send_mail: Callable = email.send, + publish: Callable = redis_eventpublisher.publish, +) -> messagebus.MessageBus: + + if start_orm: + orm.start_mappers() + + dependencies = {"uow": uow, "send_mail": send_mail, "publish": publish} + injected_event_handlers = { + event_type: [ + inject_dependencies(handler, dependencies) + for handler in event_handlers + ] + for event_type, event_handlers in handlers.EVENT_HANDLERS.items() + } + injected_command_handlers = { + command_type: inject_dependencies(handler, dependencies) + for command_type, handler in handlers.COMMAND_HANDLERS.items() + } + + return messagebus.MessageBus( + uow=uow, + event_handlers=injected_event_handlers, + command_handlers=injected_command_handlers, + ) + + +def inject_dependencies(handler, dependencies): + params = inspect.signature(handler).parameters + deps = { + name: dependency + for name, dependency in dependencies.items() + if name in params + } + return lambda message: handler(message, **deps) From 97687278f8be44150bf5f168ea9f2a86b5702032 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 15 Jul 2019 17:03:43 +0100 Subject: [PATCH 10/30] use bootstrap in service layer tests [bootstrap_tests] --- tests/unit/test_handlers.py | 110 +++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 53 deletions(-) diff --git a/tests/unit/test_handlers.py b/tests/unit/test_handlers.py index 04fb9630..cb21461e 100644 --- a/tests/unit/test_handlers.py +++ b/tests/unit/test_handlers.py @@ -1,10 +1,12 @@ # pylint: disable=no-self-use +from __future__ import annotations from datetime import date from unittest import mock import pytest +from allocation import bootstrap from allocation.adapters import repository -from allocation.domain import commands, events -from allocation.service_layer import handlers, messagebus, unit_of_work +from allocation.domain import commands +from allocation.service_layer import handlers, unit_of_work class FakeRepository(repository.AbstractRepository): @@ -37,81 +39,83 @@ def rollback(self): pass +def bootstrap_test_app(): + return bootstrap.bootstrap( + start_orm=False, + uow=FakeUnitOfWork(), + send_mail=lambda *args: None, + publish=lambda *args: None, + ) + + class TestAddBatch: def test_for_new_product(self): - uow = FakeUnitOfWork() - messagebus.handle( - commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None), uow - ) - assert uow.products.get("CRUNCHY-ARMCHAIR") is not None - assert uow.committed + bus = bootstrap_test_app() + bus.handle(commands.CreateBatch("b1", "CRUNCHY-ARMCHAIR", 100, None)) + assert bus.uow.products.get("CRUNCHY-ARMCHAIR") is not None + assert bus.uow.committed def test_for_existing_product(self): - uow = FakeUnitOfWork() - messagebus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None), uow) - messagebus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None), uow) - assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches] - - -@pytest.fixture(autouse=True) -def fake_redis_publish(): - with mock.patch("allocation.adapters.redis_eventpublisher.publish"): - yield + bus = bootstrap_test_app() + bus.handle(commands.CreateBatch("b1", "GARISH-RUG", 100, None)) + bus.handle(commands.CreateBatch("b2", "GARISH-RUG", 99, None)) + assert "b2" in [ + b.reference for b in bus.uow.products.get("GARISH-RUG").batches + ] class TestAllocate: def test_allocates(self): - uow = FakeUnitOfWork() - messagebus.handle( - commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None), uow - ) - messagebus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10), uow) - [batch] = uow.products.get("COMPLICATED-LAMP").batches + bus = bootstrap_test_app() + bus.handle(commands.CreateBatch("batch1", "COMPLICATED-LAMP", 100, None)) + bus.handle(commands.Allocate("o1", "COMPLICATED-LAMP", 10)) + [batch] = bus.uow.products.get("COMPLICATED-LAMP").batches assert batch.available_quantity == 90 def test_errors_for_invalid_sku(self): - uow = FakeUnitOfWork() - messagebus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None), uow) + bus = bootstrap_test_app() + bus.handle(commands.CreateBatch("b1", "AREALSKU", 100, None)) with pytest.raises(handlers.InvalidSku, match="Invalid sku NONEXISTENTSKU"): - messagebus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10), uow) + bus.handle(commands.Allocate("o1", "NONEXISTENTSKU", 10)) def test_commits(self): - uow = FakeUnitOfWork() - messagebus.handle( - commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None), uow - ) - messagebus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10), uow) - assert uow.committed + bus = bootstrap_test_app() + bus.handle(commands.CreateBatch("b1", "OMINOUS-MIRROR", 100, None)) + bus.handle(commands.Allocate("o1", "OMINOUS-MIRROR", 10)) + assert bus.uow.committed def test_sends_email_on_out_of_stock_error(self): - uow = FakeUnitOfWork() - messagebus.handle( - commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None), uow - ) + emails = [] - with mock.patch("allocation.adapters.email.send") as mock_send_mail: - messagebus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10), uow) - assert mock_send_mail.call_args == mock.call( - "stock@made.com", f"Out of stock for POPULAR-CURTAINS" - ) + def fake_send_mail(*args): + emails.append(args) + + bus = bootstrap.bootstrap( + start_orm=False, + uow=FakeUnitOfWork(), + send_mail=fake_send_mail, + publish=lambda *args: None, + ) + bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None)) + bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10)) + assert emails == [ + ("stock@made.com", f"Out of stock for POPULAR-CURTAINS"), + ] class TestChangeBatchQuantity: def test_changes_available_quantity(self): - uow = FakeUnitOfWork() - messagebus.handle( - commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None), uow - ) - [batch] = uow.products.get(sku="ADORABLE-SETTEE").batches + bus = bootstrap_test_app() + bus.handle(commands.CreateBatch("batch1", "ADORABLE-SETTEE", 100, None)) + [batch] = bus.uow.products.get(sku="ADORABLE-SETTEE").batches assert batch.available_quantity == 100 - messagebus.handle(commands.ChangeBatchQuantity("batch1", 50), uow) - + bus.handle(commands.ChangeBatchQuantity("batch1", 50)) assert batch.available_quantity == 50 def test_reallocates_if_necessary(self): - uow = FakeUnitOfWork() + bus = bootstrap_test_app() history = [ commands.CreateBatch("batch1", "INDIFFERENT-TABLE", 50, None), commands.CreateBatch("batch2", "INDIFFERENT-TABLE", 50, date.today()), @@ -119,12 +123,12 @@ def test_reallocates_if_necessary(self): commands.Allocate("order2", "INDIFFERENT-TABLE", 20), ] for msg in history: - messagebus.handle(msg, uow) - [batch1, batch2] = uow.products.get(sku="INDIFFERENT-TABLE").batches + bus.handle(msg) + [batch1, batch2] = bus.uow.products.get(sku="INDIFFERENT-TABLE").batches assert batch1.available_quantity == 10 assert batch2.available_quantity == 50 - messagebus.handle(commands.ChangeBatchQuantity("batch1", 25), uow) + bus.handle(commands.ChangeBatchQuantity("batch1", 25)) # order1 or order2 will be deallocated, so we'll have 25 - 20 assert batch1.available_quantity == 5 From 206081019ea47a63de8a3c772b3ec4961ca629a9 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 6 Jan 2020 07:32:55 +0000 Subject: [PATCH 11/30] fixture to start mappers explicitly, use in repo and uow tests --- tests/conftest.py | 9 ++++++--- tests/integration/test_repository.py | 3 +++ tests/integration/test_uow.py | 3 +++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b0ceb0f2..f91f93a0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,8 +26,13 @@ def in_memory_sqlite_db(): @pytest.fixture def sqlite_session_factory(in_memory_sqlite_db): - start_mappers() yield sessionmaker(bind=in_memory_sqlite_db) + + +@pytest.fixture +def mappers(): + start_mappers() + yield clear_mappers() @@ -57,9 +62,7 @@ def postgres_db(): @pytest.fixture def postgres_session_factory(postgres_db): - start_mappers() yield sessionmaker(bind=postgres_db) - clear_mappers() @pytest.fixture diff --git a/tests/integration/test_repository.py b/tests/integration/test_repository.py index 245b26f2..7961be2a 100644 --- a/tests/integration/test_repository.py +++ b/tests/integration/test_repository.py @@ -1,6 +1,9 @@ +import pytest from allocation.adapters import repository from allocation.domain import model +pytestmark = pytest.mark.usefixtures("mappers") + def test_get_by_batchref(sqlite_session_factory): session = sqlite_session_factory() diff --git a/tests/integration/test_uow.py b/tests/integration/test_uow.py index db705849..61f3aae5 100644 --- a/tests/integration/test_uow.py +++ b/tests/integration/test_uow.py @@ -3,11 +3,14 @@ import time import traceback from typing import List +from unittest.mock import Mock import pytest from allocation.domain import model from allocation.service_layer import unit_of_work from ..random_refs import random_sku, random_batchref, random_orderid +pytestmark = pytest.mark.usefixtures("mappers") + def insert_batch(session, ref, sku, qty, eta, product_version=1): session.execute( From 2105f0c03c5a330c0b994e3989aaa53924bccd63 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 6 Jan 2020 05:07:03 +0000 Subject: [PATCH 12/30] fix view tests to use bootstrap. [bootstrap_view_tests] --- tests/integration/test_views.py | 51 +++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/tests/integration/test_views.py b/tests/integration/test_views.py index 4dda3e54..1eea394e 100644 --- a/tests/integration/test_views.py +++ b/tests/integration/test_views.py @@ -1,35 +1,48 @@ +# pylint: disable=redefined-outer-name from datetime import date -from allocation import views +from sqlalchemy.orm import clear_mappers +import pytest +from allocation import bootstrap, views from allocation.domain import commands -from allocation.service_layer import messagebus, unit_of_work +from allocation.service_layer import unit_of_work today = date.today() -def test_allocations_view(sqlite_session_factory): - uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) - messagebus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None), uow) - messagebus.handle(commands.CreateBatch("sku2batch", "sku2", 50, today), uow) - messagebus.handle(commands.Allocate("order1", "sku1", 20), uow) - messagebus.handle(commands.Allocate("order1", "sku2", 20), uow) +@pytest.fixture +def sqlite_bus(sqlite_session_factory): + bus = bootstrap.bootstrap( + start_orm=True, + uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory), + send_mail=lambda *args: None, + publish=lambda *args: None, + ) + yield bus + clear_mappers() + + +def test_allocations_view(sqlite_bus): + sqlite_bus.handle(commands.CreateBatch("sku1batch", "sku1", 50, None)) + sqlite_bus.handle(commands.CreateBatch("sku2batch", "sku2", 50, today)) + sqlite_bus.handle(commands.Allocate("order1", "sku1", 20)) + sqlite_bus.handle(commands.Allocate("order1", "sku2", 20)) # add a spurious batch and order to make sure we're getting the right ones - messagebus.handle(commands.CreateBatch("sku1batch-later", "sku1", 50, today), uow) - messagebus.handle(commands.Allocate("otherorder", "sku1", 30), uow) - messagebus.handle(commands.Allocate("otherorder", "sku2", 10), uow) + sqlite_bus.handle(commands.CreateBatch("sku1batch-later", "sku1", 50, today)) + sqlite_bus.handle(commands.Allocate("otherorder", "sku1", 30)) + sqlite_bus.handle(commands.Allocate("otherorder", "sku2", 10)) - assert views.allocations("order1", uow) == [ + assert views.allocations("order1", sqlite_bus.uow) == [ {"sku": "sku1", "batchref": "sku1batch"}, {"sku": "sku2", "batchref": "sku2batch"}, ] -def test_deallocation(sqlite_session_factory): - uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory) - messagebus.handle(commands.CreateBatch("b1", "sku1", 50, None), uow) - messagebus.handle(commands.CreateBatch("b2", "sku1", 50, today), uow) - messagebus.handle(commands.Allocate("o1", "sku1", 40), uow) - messagebus.handle(commands.ChangeBatchQuantity("b1", 10), uow) +def test_deallocation(sqlite_bus): + sqlite_bus.handle(commands.CreateBatch("b1", "sku1", 50, None)) + sqlite_bus.handle(commands.CreateBatch("b2", "sku1", 50, today)) + sqlite_bus.handle(commands.Allocate("o1", "sku1", 40)) + sqlite_bus.handle(commands.ChangeBatchQuantity("b1", 10)) - assert views.allocations("o1", uow) == [ + assert views.allocations("o1", sqlite_bus.uow) == [ {"sku": "sku1", "batchref": "b2"}, ] From 44adc609d3ee261577fae5d2803b9ac9a9b901f7 Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 7 Oct 2019 14:44:15 +0100 Subject: [PATCH 13/30] use bootstrap in flask [flask_calls_bootstrap] --- src/allocation/entrypoints/flask_app.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/allocation/entrypoints/flask_app.py b/src/allocation/entrypoints/flask_app.py index ed482390..f50f3edd 100644 --- a/src/allocation/entrypoints/flask_app.py +++ b/src/allocation/entrypoints/flask_app.py @@ -1,14 +1,11 @@ from datetime import datetime from flask import Flask, jsonify, request - from allocation.domain import commands -from allocation.adapters import orm -from allocation.service_layer import messagebus, unit_of_work from allocation.service_layer.handlers import InvalidSku -from allocation import views +from allocation import bootstrap, views app = Flask(__name__) -orm.start_mappers() +bus = bootstrap.bootstrap() @app.route("/add_batch", methods=["POST"]) @@ -19,8 +16,7 @@ def add_batch(): cmd = commands.CreateBatch( request.json["ref"], request.json["sku"], request.json["qty"], eta ) - uow = unit_of_work.SqlAlchemyUnitOfWork() - messagebus.handle(cmd, uow) + bus.handle(cmd) return "OK", 201 @@ -30,8 +26,7 @@ def allocate_endpoint(): cmd = commands.Allocate( request.json["orderid"], request.json["sku"], request.json["qty"] ) - uow = unit_of_work.SqlAlchemyUnitOfWork() - messagebus.handle(cmd, uow) + bus.handle(cmd) except InvalidSku as e: return {"message": str(e)}, 400 @@ -40,8 +35,7 @@ def allocate_endpoint(): @app.route("/allocations/", methods=["GET"]) def allocations_view_endpoint(orderid): - uow = unit_of_work.SqlAlchemyUnitOfWork() - result = views.allocations(orderid, uow) + result = views.allocations(orderid, bus.uow) if not result: return "not found", 404 return jsonify(result), 200 From a82e57fd57dee284ad529a1efa3cc020d92d070a Mon Sep 17 00:00:00 2001 From: Harry Date: Mon, 6 Jan 2020 06:59:59 +0000 Subject: [PATCH 14/30] use bootstrap for redis --- src/allocation/entrypoints/redis_eventconsumer.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/allocation/entrypoints/redis_eventconsumer.py b/src/allocation/entrypoints/redis_eventconsumer.py index e04a8142..9b5b93d0 100644 --- a/src/allocation/entrypoints/redis_eventconsumer.py +++ b/src/allocation/entrypoints/redis_eventconsumer.py @@ -2,10 +2,8 @@ import logging import redis -from allocation import config +from allocation import bootstrap, config from allocation.domain import commands -from allocation.adapters import orm -from allocation.service_layer import messagebus, unit_of_work logger = logging.getLogger(__name__) @@ -13,19 +11,19 @@ def main(): - orm.start_mappers() + bus = bootstrap.bootstrap() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe("change_batch_quantity") for m in pubsub.listen(): - handle_change_batch_quantity(m) + handle_change_batch_quantity(m, bus) -def handle_change_batch_quantity(m): +def handle_change_batch_quantity(m, bus): logging.debug("handling %s", m) data = json.loads(m["data"]) cmd = commands.ChangeBatchQuantity(ref=data["batchref"], qty=data["qty"]) - messagebus.handle(cmd, uow=unit_of_work.SqlAlchemyUnitOfWork()) + bus.handle(cmd) if __name__ == "__main__": From 9ee84345231e55b4caff515a0739450ac34289be Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 7 Jan 2020 22:32:52 +0000 Subject: [PATCH 15/30] experiment with nonmagic DI zzzz [nomagic_di] --- src/allocation/bootstrap.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/allocation/bootstrap.py b/src/allocation/bootstrap.py index 805b13f5..94f26dd1 100644 --- a/src/allocation/bootstrap.py +++ b/src/allocation/bootstrap.py @@ -1,5 +1,6 @@ import inspect from typing import Callable +from allocation.domain import commands, events from allocation.adapters import email, orm, redis_eventpublisher from allocation.service_layer import handlers, messagebus, unit_of_work @@ -14,17 +15,24 @@ def bootstrap( if start_orm: orm.start_mappers() - dependencies = {"uow": uow, "send_mail": send_mail, "publish": publish} injected_event_handlers = { - event_type: [ - inject_dependencies(handler, dependencies) - for handler in event_handlers - ] - for event_type, event_handlers in handlers.EVENT_HANDLERS.items() + events.Allocated: [ + lambda e: handlers.publish_allocated_event(e, publish), + lambda e: handlers.add_allocation_to_read_model(e, uow), + ], + events.Deallocated: [ + lambda e: handlers.remove_allocation_from_read_model(e, uow), + lambda e: handlers.reallocate(e, uow), + ], + events.OutOfStock: [ + lambda e: handlers.send_out_of_stock_notification(e, send_mail) + ], } injected_command_handlers = { - command_type: inject_dependencies(handler, dependencies) - for command_type, handler in handlers.COMMAND_HANDLERS.items() + commands.Allocate: lambda c: handlers.allocate(c, uow), + commands.CreateBatch: lambda c: handlers.add_batch(c, uow), + commands.ChangeBatchQuantity: \ + lambda c: handlers.change_batch_quantity(c, uow), } return messagebus.MessageBus( From f09d269b8c4091de05d4dea7e8910d5a6dfb3b63 Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 7 Jan 2020 22:36:23 +0000 Subject: [PATCH 16/30] Revert "experiment with nonmagic DI zzzz" This reverts commit f42b193857eec8560443767de9fd7ada9f2acb96. --- src/allocation/bootstrap.py | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/src/allocation/bootstrap.py b/src/allocation/bootstrap.py index 94f26dd1..805b13f5 100644 --- a/src/allocation/bootstrap.py +++ b/src/allocation/bootstrap.py @@ -1,6 +1,5 @@ import inspect from typing import Callable -from allocation.domain import commands, events from allocation.adapters import email, orm, redis_eventpublisher from allocation.service_layer import handlers, messagebus, unit_of_work @@ -15,24 +14,17 @@ def bootstrap( if start_orm: orm.start_mappers() + dependencies = {"uow": uow, "send_mail": send_mail, "publish": publish} injected_event_handlers = { - events.Allocated: [ - lambda e: handlers.publish_allocated_event(e, publish), - lambda e: handlers.add_allocation_to_read_model(e, uow), - ], - events.Deallocated: [ - lambda e: handlers.remove_allocation_from_read_model(e, uow), - lambda e: handlers.reallocate(e, uow), - ], - events.OutOfStock: [ - lambda e: handlers.send_out_of_stock_notification(e, send_mail) - ], + event_type: [ + inject_dependencies(handler, dependencies) + for handler in event_handlers + ] + for event_type, event_handlers in handlers.EVENT_HANDLERS.items() } injected_command_handlers = { - commands.Allocate: lambda c: handlers.allocate(c, uow), - commands.CreateBatch: lambda c: handlers.add_batch(c, uow), - commands.ChangeBatchQuantity: \ - lambda c: handlers.change_batch_quantity(c, uow), + command_type: inject_dependencies(handler, dependencies) + for command_type, handler in handlers.COMMAND_HANDLERS.items() } return messagebus.MessageBus( From d6141c7f00f33f7be53d69d788783f7fb9a3d030 Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 14 Jul 2019 02:55:46 +0100 Subject: [PATCH 17/30] switch to a notifications class [notifications_class] --- src/allocation/adapters/email.py | 2 -- src/allocation/adapters/notifications.py | 28 ++++++++++++++++++++++++ src/allocation/bootstrap.py | 13 ++++++++--- src/allocation/config.py | 7 ++++++ src/allocation/service_layer/handlers.py | 5 +++-- 5 files changed, 48 insertions(+), 7 deletions(-) delete mode 100644 src/allocation/adapters/email.py create mode 100644 src/allocation/adapters/notifications.py diff --git a/src/allocation/adapters/email.py b/src/allocation/adapters/email.py deleted file mode 100644 index 1c37d427..00000000 --- a/src/allocation/adapters/email.py +++ /dev/null @@ -1,2 +0,0 @@ -def send(*args): - print("SENDING EMAIL:", *args) diff --git a/src/allocation/adapters/notifications.py b/src/allocation/adapters/notifications.py new file mode 100644 index 00000000..db29f7c8 --- /dev/null +++ b/src/allocation/adapters/notifications.py @@ -0,0 +1,28 @@ +# pylint: disable=too-few-public-methods +import abc +import smtplib +from allocation import config + + +class AbstractNotifications(abc.ABC): + @abc.abstractmethod + def send(self, destination, message): + raise NotImplementedError + + +DEFAULT_HOST = config.get_email_host_and_port()["host"] +DEFAULT_PORT = config.get_email_host_and_port()["port"] + + +class EmailNotifications(AbstractNotifications): + def __init__(self, smtp_host=DEFAULT_HOST, port=DEFAULT_PORT): + self.server = smtplib.SMTP(smtp_host, port=port) + self.server.noop() + + def send(self, destination, message): + msg = f"Subject: allocation service notification\n{message}" + self.server.sendmail( + from_addr="allocations@example.com", + to_addrs=[destination], + msg=msg, + ) diff --git a/src/allocation/bootstrap.py b/src/allocation/bootstrap.py index 805b13f5..22112a06 100644 --- a/src/allocation/bootstrap.py +++ b/src/allocation/bootstrap.py @@ -1,20 +1,27 @@ import inspect from typing import Callable -from allocation.adapters import email, orm, redis_eventpublisher +from allocation.adapters import orm, redis_eventpublisher +from allocation.adapters.notifications import ( + AbstractNotifications, + EmailNotifications, +) from allocation.service_layer import handlers, messagebus, unit_of_work def bootstrap( start_orm: bool = True, uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(), - send_mail: Callable = email.send, + notifications: AbstractNotifications = None, publish: Callable = redis_eventpublisher.publish, ) -> messagebus.MessageBus: + if notifications is None: + notifications = EmailNotifications() + if start_orm: orm.start_mappers() - dependencies = {"uow": uow, "send_mail": send_mail, "publish": publish} + dependencies = {"uow": uow, "notifications": notifications, "publish": publish} injected_event_handlers = { event_type: [ inject_dependencies(handler, dependencies) diff --git a/src/allocation/config.py b/src/allocation/config.py index 30a8eb07..bda1bbf2 100644 --- a/src/allocation/config.py +++ b/src/allocation/config.py @@ -19,3 +19,10 @@ def get_redis_host_and_port(): host = os.environ.get("REDIS_HOST", "localhost") port = 63791 if host == "localhost" else 6379 return dict(host=host, port=port) + + +def get_email_host_and_port(): + host = os.environ.get("EMAIL_HOST", "localhost") + port = 11025 if host == "localhost" else 1025 + http_port = 18025 if host == "localhost" else 8025 + return dict(host=host, port=port, http_port=http_port) diff --git a/src/allocation/service_layer/handlers.py b/src/allocation/service_layer/handlers.py index 6ab2bbe9..2d7aa8d4 100644 --- a/src/allocation/service_layer/handlers.py +++ b/src/allocation/service_layer/handlers.py @@ -6,6 +6,7 @@ from allocation.domain.model import OrderLine if TYPE_CHECKING: + from allocation.adapters import notifications from . import unit_of_work @@ -61,9 +62,9 @@ def change_batch_quantity( def send_out_of_stock_notification( event: events.OutOfStock, - send_mail: Callable, + notifications: notifications.AbstractNotifications, ): - send_mail( + notifications.send( "stock@made.com", f"Out of stock for {event.sku}", ) From 514b6a404b940c3e1bba24554ba48bb5bd0a4ea3 Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 14 Jul 2019 02:56:00 +0100 Subject: [PATCH 18/30] tests for notifcations [notifications_unit_tests] --- tests/integration/test_views.py | 3 ++- tests/unit/test_handlers.py | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/integration/test_views.py b/tests/integration/test_views.py index 1eea394e..ccd5d542 100644 --- a/tests/integration/test_views.py +++ b/tests/integration/test_views.py @@ -1,6 +1,7 @@ # pylint: disable=redefined-outer-name from datetime import date from sqlalchemy.orm import clear_mappers +from unittest import mock import pytest from allocation import bootstrap, views from allocation.domain import commands @@ -14,7 +15,7 @@ def sqlite_bus(sqlite_session_factory): bus = bootstrap.bootstrap( start_orm=True, uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory), - send_mail=lambda *args: None, + notifications=mock.Mock(), publish=lambda *args: None, ) yield bus diff --git a/tests/unit/test_handlers.py b/tests/unit/test_handlers.py index cb21461e..f1218540 100644 --- a/tests/unit/test_handlers.py +++ b/tests/unit/test_handlers.py @@ -1,12 +1,14 @@ # pylint: disable=no-self-use from __future__ import annotations +from collections import defaultdict from datetime import date -from unittest import mock +from typing import Dict, List import pytest from allocation import bootstrap -from allocation.adapters import repository from allocation.domain import commands -from allocation.service_layer import handlers, unit_of_work +from allocation.service_layer import handlers +from allocation.adapters import notifications, repository +from allocation.service_layer import unit_of_work class FakeRepository(repository.AbstractRepository): @@ -39,11 +41,19 @@ def rollback(self): pass +class FakeNotifications(notifications.AbstractNotifications): + def __init__(self): + self.sent = defaultdict(list) # type: Dict[str, List[str]] + + def send(self, destination, message): + self.sent[destination].append(message) + + def bootstrap_test_app(): return bootstrap.bootstrap( start_orm=False, uow=FakeUnitOfWork(), - send_mail=lambda *args: None, + notifications=FakeNotifications(), publish=lambda *args: None, ) @@ -86,21 +96,17 @@ def test_commits(self): assert bus.uow.committed def test_sends_email_on_out_of_stock_error(self): - emails = [] - - def fake_send_mail(*args): - emails.append(args) - + fake_notifs = FakeNotifications() bus = bootstrap.bootstrap( start_orm=False, uow=FakeUnitOfWork(), - send_mail=fake_send_mail, + notifications=fake_notifs, publish=lambda *args: None, ) bus.handle(commands.CreateBatch("b1", "POPULAR-CURTAINS", 9, None)) bus.handle(commands.Allocate("o1", "POPULAR-CURTAINS", 10)) - assert emails == [ - ("stock@made.com", f"Out of stock for POPULAR-CURTAINS"), + assert fake_notifs.sent["stock@made.com"] == [ + f"Out of stock for POPULAR-CURTAINS", ] From 51a00dfaa78345ece4e5f75bc999080051111bf4 Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 14 Jul 2019 03:23:07 +0100 Subject: [PATCH 19/30] add a mailhog fake email server to docker-compose --- docker-compose.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docker-compose.yml b/docker-compose.yml index dc2cc369..f964ab74 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,10 +10,12 @@ services: depends_on: - postgres - redis + - mailhog environment: - DB_HOST=postgres - DB_PASSWORD=abc123 - REDIS_HOST=redis + - EMAIL_HOST=mailhog - PYTHONDONTWRITEBYTECODE=1 volumes: - ./src:/src @@ -26,11 +28,13 @@ services: image: allocation-image depends_on: - redis_pubsub + - mailhog environment: - DB_HOST=postgres - DB_PASSWORD=abc123 - API_HOST=api - REDIS_HOST=redis + - EMAIL_HOST=mailhog - PYTHONDONTWRITEBYTECODE=1 - FLASK_APP=allocation/entrypoints/flask_app.py - FLASK_DEBUG=1 @@ -59,3 +63,8 @@ services: ports: - "63791:6379" + mailhog: + image: mailhog/mailhog + ports: + - "11025:1025" + - "18025:8025" From bb51578bdd3860de31702011b2a858b16f60520b Mon Sep 17 00:00:00 2001 From: Harry Date: Tue, 8 Oct 2019 12:56:58 +0100 Subject: [PATCH 20/30] logging.info needs backport --- src/allocation/adapters/orm.py | 3 +++ src/allocation/adapters/redis_eventpublisher.py | 2 +- src/allocation/entrypoints/redis_eventconsumer.py | 3 ++- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/allocation/adapters/orm.py b/src/allocation/adapters/orm.py index a87068a5..81d704ca 100644 --- a/src/allocation/adapters/orm.py +++ b/src/allocation/adapters/orm.py @@ -1,3 +1,4 @@ +import logging from sqlalchemy import ( Table, MetaData, @@ -12,6 +13,7 @@ from allocation.domain import model +logger = logging.getLogger(__name__) metadata = MetaData() @@ -59,6 +61,7 @@ def start_mappers(): + logger.info("Starting mappers") lines_mapper = mapper(model.OrderLine, order_lines) batches_mapper = mapper( model.Batch, diff --git a/src/allocation/adapters/redis_eventpublisher.py b/src/allocation/adapters/redis_eventpublisher.py index 6100956f..d607d6ac 100644 --- a/src/allocation/adapters/redis_eventpublisher.py +++ b/src/allocation/adapters/redis_eventpublisher.py @@ -12,5 +12,5 @@ def publish(channel, event: events.Event): - logging.debug("publishing: channel=%s, event=%s", channel, event) + logging.info("publishing: channel=%s, event=%s", channel, event) r.publish(channel, json.dumps(asdict(event))) diff --git a/src/allocation/entrypoints/redis_eventconsumer.py b/src/allocation/entrypoints/redis_eventconsumer.py index 9b5b93d0..6d0d49a7 100644 --- a/src/allocation/entrypoints/redis_eventconsumer.py +++ b/src/allocation/entrypoints/redis_eventconsumer.py @@ -11,6 +11,7 @@ def main(): + logger.info("Redis pubsub starting") bus = bootstrap.bootstrap() pubsub = r.pubsub(ignore_subscribe_messages=True) pubsub.subscribe("change_batch_quantity") @@ -20,7 +21,7 @@ def main(): def handle_change_batch_quantity(m, bus): - logging.debug("handling %s", m) + logger.info("handling %s", m) data = json.loads(m["data"]) cmd = commands.ChangeBatchQuantity(ref=data["batchref"], qty=data["qty"]) bus.handle(cmd) From f4fca95b571b34b723c452388bac8341058f9545 Mon Sep 17 00:00:00 2001 From: Harry Date: Sun, 14 Jul 2019 03:23:44 +0100 Subject: [PATCH 21/30] integration test for emails [chapter_13_dependency_injection_ends] --- tests/integration/test_email.py | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/integration/test_email.py diff --git a/tests/integration/test_email.py b/tests/integration/test_email.py new file mode 100644 index 00000000..4aade37b --- /dev/null +++ b/tests/integration/test_email.py @@ -0,0 +1,37 @@ +# pylint: disable=redefined-outer-name +import pytest +import requests +from sqlalchemy.orm import clear_mappers +from allocation import bootstrap, config +from allocation.domain import commands +from allocation.adapters import notifications +from allocation.service_layer import unit_of_work +from ..random_refs import random_sku + + +@pytest.fixture +def bus(sqlite_session_factory): + bus = bootstrap.bootstrap( + start_orm=True, + uow=unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory), + notifications=notifications.EmailNotifications(), + publish=lambda *args: None, + ) + yield bus + clear_mappers() + + +def get_email_from_mailhog(sku): + host, port = map(config.get_email_host_and_port().get, ["host", "http_port"]) + all_emails = requests.get(f"http://{host}:{port}/api/v2/messages").json() + return next(m for m in all_emails["items"] if sku in str(m)) + + +def test_out_of_stock_email(bus): + sku = random_sku() + bus.handle(commands.CreateBatch("batch1", sku, 9, None)) + bus.handle(commands.Allocate("order1", sku, 10)) + email = get_email_from_mailhog(sku) + assert email["Raw"]["From"] == "allocations@example.com" + assert email["Raw"]["To"] == ["stock@made.com"] + assert f"Out of stock for {sku}" in email["Raw"]["Data"] From c95fc4f8cd8cfed54381542c618da738e4106988 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 25 Feb 2021 01:02:38 +0000 Subject: [PATCH 22/30] update travis config --- .travis.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index fcd3ceea..bf0b9de2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,10 +1,11 @@ dist: xenial language: python python: 3.8 - script: - make all - branches: except: - - /.*_exercise$/ + - "/.*_exercise$/" +env: + global: + secure: cNctlzVCjUj1oOrRW0aryxhJHB/u0b6vmn532jcgyCRS1aQjMJtT61O7tW6yMk4wOaH4Lr0kAI0J6+1lnRYf5g11H1M+IpEHGMWgJgImsysDpLRWUGmDJTez/ii8psk0SfOP/0ZwQp+QxOB92CHdPeOPOmu2HFa00V82/H7gousXR7ywQRNthLHwso36O8+UoHc4qw8nIbjcHzbfD6ysJNynmaUMlB3mRTU1hkjGKKpA2Xyl4tmkIhp3NCPJc0WR4SgB3y0u3dVOC+RtbRzl/XpEbjsZHHNloBirK+8ERn9ISBBh/mvfo6qTix743e+xvhtBlLJjk3o4H0VMH+wQ3zIpIh4TKbhPCMqWY3gvtKDVRHD+Sywk2TE6zSz0sDPWk248MC2QsL7sgeFwcnFHOWy2iKf4YyuZtoaJuX+2tw23cDCdMS6wbARlT8Kb5QwMlsxuKYN/04kQB+9nXTVsWKJGIwLKdYRzshnlzqB/UEe2vrjZcbBixCp4pbZ2jSzw2881he4KSbVGIJdZYSFetMuaN0P9obtdaJU4V+IhwzFyyapjZhEGCTl+l/m8uGdJ5DOhFlZ7OczHja7DKUuQvB3AbnMGvN518C+fJkJpWxAn5UeIp3d0ZZm32XVKt3k8PJaP7LBYdxnr3JCRit7+kNnlP7Ho0NjvX6GTHQ+r+JM= From c455e35ad613cee2a96c40033f217241a8410f14 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 25 Feb 2021 01:32:55 +0000 Subject: [PATCH 23/30] readme fixes --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index df5c823c..636ed7c3 100644 --- a/README.md +++ b/README.md @@ -3,23 +3,23 @@ ## Chapters Each chapter has its own branch which contains all the commits for that chapter, -so it has the state that corresponds to the _end_ of that chapter. If you want -to try and code along with a chapter, you'll want to check out the branch for the -previous chapter. +so it has the state that corresponds to the _end_ of that chapter. +If you want to try and code along with a chapter, +you'll want to check out the branch for the previous chapter. -https://github.com/python-leap/code/branches/all +https://github.com/cosmicpython/code/branches/all ## Exercises -Branches for the exercises follow the convention `{chatper_name}_exercise`, eg -https://github.com/python-leap/code/tree/chapter_04_service_layer_exercise +Branches for the exercises follow the convention `{chapter_name}_exercise`, +eg https://github.com/cosmicpython/code/tree/chapter_04_service_layer_exercise ## Requirements * docker with docker-compose -* for chapters 1 and 2, and optionally for the rest: a local python3.7 virtualenv +* for chapters 1 and 2, and optionally for the rest: a local python3.8 virtualenv ## Building the containers From 2f8feba3974c6c0506b402b7b429ebbf5c120b75 Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 25 Feb 2021 01:45:17 +0000 Subject: [PATCH 24/30] attempt docker login --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index bf0b9de2..d69e8e14 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,8 @@ dist: xenial language: python python: 3.8 +before_install: + - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin script: - make all branches: From 539e35c689f9f52348b39b897bdc468ed255131e Mon Sep 17 00:00:00 2001 From: Harry Date: Thu, 25 Feb 2021 01:48:56 +0000 Subject: [PATCH 25/30] upgrade travis to focal + 3.9 why the heck not --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index d69e8e14..8b4550c1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ -dist: xenial +dist: focal language: python -python: 3.8 +python: 3.9 before_install: - echo "$DOCKER_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin script: From 15922acc755aa272dff0945f17d59d354116f64a Mon Sep 17 00:00:00 2001 From: Sergey Fursov Date: Fri, 2 Apr 2021 18:00:45 +0300 Subject: [PATCH 26/30] fix requirements installation command in README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 636ed7c3..63e8510c 100644 --- a/README.md +++ b/README.md @@ -45,10 +45,10 @@ pip install pytest pip install pytest sqlalchemy # for chapter 4+5 -pip install requirements.txt +pip install -r requirements.txt # for chapter 6+ -pip install requirements.txt +pip install -r requirements.txt pip install -e src/ ``` From 280b9d596a2fdc981b1e3282d3343df883e9d2b8 Mon Sep 17 00:00:00 2001 From: adamculp Date: Sat, 22 May 2021 16:23:39 -0400 Subject: [PATCH 27/30] Corrected commands of the individual tests make commands --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 63e8510c..561cbb70 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,9 @@ pip install -e src/ ```sh make test # or, to run individual test types -make unit -make integration -make e2e +make unit-tests +make integration-tests +make e2e-tests # or, if you have a local virtualenv make up pytest tests/unit From 9aa5b82dc593adebd564b089a439a6f18c8e022b Mon Sep 17 00:00:00 2001 From: Harry Percival Date: Fri, 17 Mar 2023 12:53:12 +0000 Subject: [PATCH 28/30] pin sqlalchmy to <2 --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 882cb352..789a24fe 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ # app -sqlalchemy +sqlalchemy<2 flask psycopg2-binary redis From af5550869895fbd255d98c6809f6fed961e2bf6b Mon Sep 17 00:00:00 2001 From: Daniel Faber <59205635+DanielSchiessl@users.noreply.github.com> Date: Fri, 17 Nov 2023 18:37:50 +0100 Subject: [PATCH 29/30] run tests on pull request --- .github/workflows/run_tests_on_pull_request.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/workflows/run_tests_on_pull_request.yml diff --git a/.github/workflows/run_tests_on_pull_request.yml b/.github/workflows/run_tests_on_pull_request.yml new file mode 100644 index 00000000..940e6afa --- /dev/null +++ b/.github/workflows/run_tests_on_pull_request.yml @@ -0,0 +1,11 @@ +on: + pull_request: + types: [opened, synchronize, reopened] +jobs: + run_tests: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + - name: run tests + run: make all \ No newline at end of file From 4038abbe52e4197b139c300a532cb1330271b9c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alfonso=20Montero=20L=C3=B3pez?= Date: Fri, 24 Jan 2025 14:33:19 +0100 Subject: [PATCH 30/30] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 561cbb70..8b35464c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Example application code for the python architecture book +# Example application code for the "Architecture Patterns with Python" book ## Chapters