Skip to content

Commit ec31989

Browse files
committed
Added tests
1 parent 91f1ce1 commit ec31989

25 files changed

+433
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ Running migrations:
178178
### Start Server
179179
Now, that we have everything setup, we can start the server
180180
```shell
181-
ellar runserver
181+
ellar runserver --reload
182182
```
183183
In the above example, we added `/-django-example` as path prefix. This is important because we need to group all django admin views
184184
with a prefix and also to avoid unnecessary `Page Not Found` errors.

tests/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import pytest
2+
from django.contrib.auth import get_user_model
3+
from ellar.testing import Test
4+
from example_app.root_module import ApplicationModule
5+
6+
test_module = Test.create_test_module(modules=[ApplicationModule])
7+
test_module.create_application()
8+
# test_module.create_application() is very important if there are things that requires django settings.
9+
# In my case, `user_model = get_user_model()`
10+
11+
user_model = get_user_model()
12+
13+
14+
@pytest.fixture()
15+
def admin_user():
16+
return user_model.object.create_superuser(
17+
username="ellar", email="[email protected]", password="password"
18+
)
19+
20+
21+
@pytest.fixture()
22+
def client():
23+
with test_module.get_test_client() as _client:
24+
yield _client

tests/example_app/__init__.py

Whitespace-only changes.

tests/example_app/apps/__init__.py

Whitespace-only changes.

tests/example_app/apps/event/__init__.py

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Define endpoints routes in python class-based fashion
3+
example:
4+
5+
@Controller("/dogs", tag="Dogs", description="Dogs Resources")
6+
class MyController(ControllerBase):
7+
@get('/')
8+
def index(self):
9+
return {'detail': "Welcome Dog's Resources"}
10+
"""
11+
import typing as t
12+
13+
from ellar.common import Controller, ControllerBase, get, post
14+
15+
from ...interfaces.events_repository import IEventRepository
16+
from .schemas import EventSchema, EventSchemaOut
17+
18+
19+
@Controller("/event")
20+
class EventController(ControllerBase):
21+
def __init__(self, event_repo: IEventRepository):
22+
self.event_repo = event_repo
23+
24+
@post("/", response={201: EventSchemaOut})
25+
def create_event(self, event: EventSchema):
26+
event = self.event_repo.create_event(**event.dict())
27+
return 201, event
28+
29+
@get("/", response=t.List[EventSchema], name="event-list")
30+
def list_events(self):
31+
return list(self.event_repo.list_events())
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""
2+
@Module(
3+
controllers=[MyController],
4+
providers=[
5+
YourService,
6+
ProviderConfig(IService, use_class=AService),
7+
ProviderConfig(IFoo, use_value=FooService()),
8+
],
9+
routers=(routerA, routerB)
10+
statics='statics',
11+
template='template_folder',
12+
# base_directory -> default is the `event` folder
13+
)
14+
class MyModule(ModuleBase):
15+
def register_providers(self, container: Container) -> None:
16+
# for more complicated provider registrations
17+
pass
18+
19+
"""
20+
from ellar.common import Module
21+
from ellar.core import ModuleBase
22+
from ellar.di import Container
23+
24+
from .controllers import EventController
25+
26+
27+
@Module(
28+
controllers=[EventController],
29+
providers=[],
30+
routers=[],
31+
)
32+
class EventModule(ModuleBase):
33+
"""
34+
Event Module
35+
"""
36+
37+
def register_providers(self, container: Container) -> None:
38+
"""for more complicated provider registrations, use container.register_instance(...)"""
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Define endpoints routes in python function fashion
3+
example:
4+
5+
my_router = ModuleRouter("/cats", tag="Cats", description="Cats Resource description")
6+
7+
@my_router.get('/')
8+
def index(request: Request):
9+
return {'detail': 'Welcome to Cats Resource'}
10+
"""
11+
12+
from ellar.common import ModuleRouter # noqa
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Define Serializers/DTOs
3+
Example:
4+
5+
class ASampleDTO(Serializer):
6+
name: str
7+
age: t.Optional[int] = None
8+
9+
for dataclasses, Inherit from DataclassSerializer
10+
11+
@dataclass
12+
class ASampleDTO(DataclassSerializer):
13+
name: str
14+
age: t.Optional[int] = None
15+
"""
16+
from datetime import date
17+
18+
from ellar.common import Serializer
19+
20+
21+
class EventSchema(Serializer):
22+
title: str
23+
start_date: date
24+
end_date: date
25+
26+
class Config:
27+
orm_mode = True
28+
29+
30+
class EventSchemaOut(Serializer):
31+
id: int
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
Create a provider and declare its scope
3+
4+
@injectable
5+
class AProvider
6+
pass
7+
8+
@injectable(scope=transient_scope)
9+
class BProvider
10+
pass
11+
"""
12+
from ellar.di import injectable # noqa

0 commit comments

Comments
 (0)