Skip to content

Commit

Permalink
Allow docs_src source files in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
subhashb committed Aug 21, 2024
1 parent 46b57cc commit 777dd51
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 13 deletions.
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"--ignore=tests/support"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"python.analysis.extraPaths": [
"./docs_src"
]
}
2 changes: 1 addition & 1 deletion docs/guides/change-state/application-services.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 !}
```
2 changes: 1 addition & 1 deletion docs/guides/change-state/command-handlers.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/change-state/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/change-state/persist-aggregates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions docs/guides/change-state/retrieve-aggregates.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.
Expand All @@ -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`
Expand Down Expand Up @@ -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
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
15 changes: 12 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
31 changes: 31 additions & 0 deletions tests/test_docs_src.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]", 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"

0 comments on commit 777dd51

Please sign in to comment.