diff --git a/.vscode/settings.json b/.vscode/settings.json index a02e82a2..570a1b5f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,5 +4,8 @@ "--ignore=tests/support" ], "python.testing.unittestEnabled": false, - "python.testing.pytestEnabled": true + "python.testing.pytestEnabled": true, + "python.analysis.extraPaths": [ + "./docs_src" + ] } \ No newline at end of file diff --git a/docs/guides/change-state/application-services.md b/docs/guides/change-state/application-services.md index be3fe25d..754a18cd 100644 --- a/docs/guides/change-state/application-services.md +++ b/docs/guides/change-state/application-services.md @@ -28,5 +28,5 @@ Application Services are defined with the `Domain.application_service` decorator: ```python hl_lines="32 34 41" -{! docs_src/guides/change-state/008.py !} +{! docs_src/guides/change_state_008.py !} ``` diff --git a/docs/guides/change-state/command-handlers.md b/docs/guides/change-state/command-handlers.md index 723cd39d..ccc8ceb5 100644 --- a/docs/guides/change-state/command-handlers.md +++ b/docs/guides/change-state/command-handlers.md @@ -20,7 +20,7 @@ domain events. Command Handlers are defined with the `Domain.command_handler` decorator: ```python hl_lines="20-23 47-53" -{! docs_src/guides/change-state/007.py !} +{! docs_src/guides/change_state_007.py !} ``` ## Workflow diff --git a/docs/guides/change-state/commands.md b/docs/guides/change-state/commands.md index d6844a9d..7c3acf39 100644 --- a/docs/guides/change-state/commands.md +++ b/docs/guides/change-state/commands.md @@ -24,7 +24,7 @@ to eventually make the rest of the system consistent. A command is defined with the `Domain.command` decorator: ```python hl_lines="13-16" -{! docs_src/guides/change-state/006.py !} +{! docs_src/guides/change_state_006.py !} ``` A command is always associated with an aggregate class with the `part_of` diff --git a/docs/guides/change-state/persist-aggregates.md b/docs/guides/change-state/persist-aggregates.md index 0c8edc0c..37ef9fdc 100644 --- a/docs/guides/change-state/persist-aggregates.md +++ b/docs/guides/change-state/persist-aggregates.md @@ -4,7 +4,7 @@ Aggregates are saved into the configured database using `add` method of the repository. ```python hl_lines="20" -{! docs_src/guides/change-state/001.py !} +{! docs_src/guides/change_state_001.py !} ``` 1. Identity, by default, is a string. @@ -44,7 +44,7 @@ This means changes across the aggregate cluster are committed as a single transaction (assuming the underlying database supports transactions, of course). ```python hl_lines="22-30 33" -{! docs_src/guides/change-state/002.py !} +{! docs_src/guides/change_state_002.py !} ``` !!!note @@ -57,7 +57,7 @@ The `add` method also publishes events to configured brokers upon successfully persisting to the database. ```python hl_lines="15" -{! docs_src/guides/change-state/003.py !} +{! docs_src/guides/change_state_003.py !} ``` ```shell hl_lines="12-16 21-22" diff --git a/docs/guides/change-state/retrieve-aggregates.md b/docs/guides/change-state/retrieve-aggregates.md index a6ea997c..38c5426e 100644 --- a/docs/guides/change-state/retrieve-aggregates.md +++ b/docs/guides/change-state/retrieve-aggregates.md @@ -4,7 +4,7 @@ An aggregate can be retreived with the repository's `get` method, if you know its identity: ```python hl_lines="16 20" -{! docs_src/guides/change-state/001.py !} +{! docs_src/guides/change_state_001.py !} ``` 1. Identity is explicitly set to **1**. @@ -26,7 +26,7 @@ expected to enclose methods that represent business queries. Defining a custom repository is straight-forward: ```python hl_lines="16" -{! docs_src/guides/change-state/004.py !} +{! docs_src/guides/change_state_004.py !} ``` 1. The repository is connected to `Person` aggregate through the `part_of` @@ -92,7 +92,7 @@ For the purposes of this guide, assume that the following `Person` aggregates exist in the database: ```python hl_lines="7-11" -{! docs_src/guides/change-state/005.py !} +{! docs_src/guides/change_state_005.py !} ``` ```shell diff --git a/docs_src/guides/change-state/001.py b/docs_src/guides/change_state_001.py similarity index 100% rename from docs_src/guides/change-state/001.py rename to docs_src/guides/change_state_001.py diff --git a/docs_src/guides/change-state/002.py b/docs_src/guides/change_state_002.py similarity index 100% rename from docs_src/guides/change-state/002.py rename to docs_src/guides/change_state_002.py diff --git a/docs_src/guides/change-state/003.py b/docs_src/guides/change_state_003.py similarity index 100% rename from docs_src/guides/change-state/003.py rename to docs_src/guides/change_state_003.py diff --git a/docs_src/guides/change-state/004.py b/docs_src/guides/change_state_004.py similarity index 100% rename from docs_src/guides/change-state/004.py rename to docs_src/guides/change_state_004.py diff --git a/docs_src/guides/change-state/005.py b/docs_src/guides/change_state_005.py similarity index 100% rename from docs_src/guides/change-state/005.py rename to docs_src/guides/change_state_005.py diff --git a/docs_src/guides/change-state/006.py b/docs_src/guides/change_state_006.py similarity index 100% rename from docs_src/guides/change-state/006.py rename to docs_src/guides/change_state_006.py diff --git a/docs_src/guides/change-state/007.py b/docs_src/guides/change_state_007.py similarity index 100% rename from docs_src/guides/change-state/007.py rename to docs_src/guides/change_state_007.py diff --git a/docs_src/guides/change-state/008.py b/docs_src/guides/change_state_008.py similarity index 89% rename from docs_src/guides/change-state/008.py rename to docs_src/guides/change_state_008.py index 59415ffd..7ddcdace 100644 --- a/docs_src/guides/change-state/008.py +++ b/docs_src/guides/change_state_008.py @@ -42,3 +42,9 @@ def activate_user(sefl, user_id: Identifier) -> None: user = current_domain.repository_for(User).get(user_id) user.activate() current_domain.repository_for(User).add(user) + + +auth.register(User) +auth.register(UserApplicationServices, part_of=User) +auth.register(Registered, part_of=User) +auth.init(traverse=False) diff --git a/tests/conftest.py b/tests/conftest.py index f4b0702f..52122bf1 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,20 @@ -"""Module to setup Factories and other required artifacts for tests +"""Module to setup Factories and other required artifacts for tests""" -isort:skip_file -""" +import os +import sys import pytest +def pytest_configure(config): + # Insert the docs_src path into sys.path so that we can import elements from there + # in our tests + docs_src_path = os.path.abspath( + os.path.join(os.path.dirname(__file__), "../docs_src") + ) + sys.path.insert(0, docs_src_path) + + def pytest_addoption(parser): """Additional options for running tests with pytest""" parser.addoption( diff --git a/tests/test_docs_src.py b/tests/test_docs_src.py new file mode 100644 index 00000000..f17c0e93 --- /dev/null +++ b/tests/test_docs_src.py @@ -0,0 +1,31 @@ +from guides.change_state_008 import User, UserApplicationServices + +from protean import current_domain + + +def test_import_my_module(): + try: + from guides.change_state_008 import ( + Registered, + User, + UserApplicationServices, + auth, + ) + + assert True # If the import succeeds, the test passes + except ImportError: + assert False, "Module in docs_src could not be imported" + + +def test_application_service_method_invocation(): + # Run a sample test from elements declared in docs_src + app_services_obj = UserApplicationServices() + + user_id = app_services_obj.register_user( + email="john.doe@gmail.com", name="John Doe" + ) + assert user_id is not None + + app_services_obj.activate_user(user_id) + user = current_domain.repository_for(User).get(user_id) + assert user.status == "ACTIVE"