Skip to content

Commit

Permalink
Add factory with django settings test
Browse files Browse the repository at this point in the history
  • Loading branch information
maldoinc committed Apr 16, 2024
1 parent 1e236a2 commit 121e605
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
15 changes: 15 additions & 0 deletions docs/pages/integrations/django.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ class S3Manager:
def upload(self, file: File) -> None: ...
```

It is also possible to use django settings in factories.

```python title="app/services/github_client.py"
@dataclass
class GithubClient:
api_key: str
```


```python title="app/services/factories.py"
@container.register
def github_client_factory() -> GithubClient:
return GithubClient(settings.GH_API_KEY)
```

### Use in views
```python title="app/views.py"
# s3_manager is automatically injected by wireup based on the annotated type.
Expand Down
4 changes: 3 additions & 1 deletion test/integration/django/factory/factories.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.conf import settings

from test.integration.django.service.random_service import RandomService

from wireup import container


@container.register
def _make_random_service() -> RandomService:
return RandomService()
return RandomService(settings.START_NUM)
5 changes: 4 additions & 1 deletion test/integration/django/service/random_service.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
class RandomService:
def __init__(self, num: int) -> None:
self.num = num

def get_random(self) -> int:
return 4
return self.num
9 changes: 6 additions & 3 deletions test/integration/django/test_django_integration.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import sys
import unittest
from test.integration.django.service.greeter_interface import GreeterService
from test.integration.django.service.random_service import RandomService

from django.conf import settings
from django.http import HttpRequest, HttpResponse
from django.test import Client
from django.urls import path
from typing_extensions import Annotated

from test.integration.django.service.random_service import RandomService
from wireup import Wire

settings.configure(
Expand All @@ -17,11 +16,15 @@
MIDDLEWARE=["wireup.integration.django_integration.WireupMiddleware"],
WIREUP={"SERVICE_MODULES": ["test.integration.django.service", "test.integration.django.factory"]},
SECRET_KEY="secret",
START_NUM=4,
)


def index(
request: HttpRequest, greeter: GreeterService, is_debug: Annotated[bool, Wire(param="DEBUG")], random_service: RandomService
request: HttpRequest,
greeter: GreeterService,
is_debug: Annotated[bool, Wire(param="DEBUG")],
random_service: RandomService,
) -> HttpResponse:
name = request.GET.get("name")
greeting = greeter.greet(name)
Expand Down

0 comments on commit 121e605

Please sign in to comment.