Skip to content

Commit 3afbe38

Browse files
committed
fixed some pytest deprecated warning
1 parent 3e6323b commit 3afbe38

39 files changed

+435
-185
lines changed

docs/basics/testing.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,19 @@ During testing, there two ways to apply or modify configuration.
246246
test_module = Test.create_test_module(
247247
controllers=[CarController,],
248248
providers=[ProviderConfig(CarRepository, use_class=CarRepository)],
249-
config_module='project_name.config.TestingConfiguration'
249+
config_module='project_name.config:TestingConfiguration'
250250
)
251251
self.controller: CarController = test_module.get(CarController)
252252
```
253+
Also, we can expose the testing config to environment for more global scope, for example:
254+
255+
```python
256+
# project_name/tests/conftest.py
257+
import os
258+
from ellar.constants import ELLAR_CONFIG_MODULE
259+
os.environ.setdefault(ELLAR_CONFIG_MODULE, 'project_name.config:TestingConfiguration')
260+
```
261+
253262
=== "Inline"
254263
This method doesn't require configuration file, we simply go ahead and define the configuration variables in a dictionary type set to `config_module`.
255264

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
## CATAPP Application
1+
## Car Application
22

33
This is just a quick demonstration of ellar web framework. Modifications will be made on this project as the Ellar project
44
evolves to become better.
55

6-
### Start Development Server
7-
Currently, to start development server, you need to tell uvicorn where the app
8-
instance is.
9-
So, run the command below:
6+
## Installation
7+
having setup up your virtual environment,
8+
```shell
9+
$(venv) pip install ellar-cli
10+
```
11+
12+
## Start Development Server
13+
Start the dev server by running the code below:
1014

1115
```bash
12-
uvicorn catapp.server.app --reload
16+
ellar runserver --reload
1317
```
1418

1519
Once the server is up, you can visit [SwaggerDoc](http://localhost:8000/docs#) or [ReDocs](http://localhost:8000/redoc#)
File renamed without changes.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_extensions as types
12+
from ellar.common import Body, Controller, Query, get, post, render, Version
13+
from ellar.core import ControllerBase
14+
15+
from .schemas import CarListFilter, CreateCarSerializer
16+
from .services import CarRepository
17+
18+
19+
@Controller("/car", description='Example of Car Resource with <strong>Controller</strong>', tag='Controller')
20+
class CarController(ControllerBase):
21+
def __init__(self, repo: CarRepository):
22+
self.repo = repo
23+
24+
@get("/list-html")
25+
@render()
26+
async def list(self):
27+
return self.repo.get_all()
28+
29+
@post()
30+
async def create(self, payload: types.Annotated[CreateCarSerializer, Body()]):
31+
result = self.repo.create_car(payload)
32+
result.update(message="This action adds a new car")
33+
return result
34+
35+
@get("/{car_id:str}")
36+
async def get_one(self, car_id: str):
37+
return f"This action returns a #{car_id} car"
38+
39+
@get()
40+
async def get_all(self, query: CarListFilter = Query()):
41+
res = dict(
42+
cars=self.repo.get_all(),
43+
message=f"This action returns all cars at limit={query.limit}, offset={query.offset}",
44+
)
45+
return res
46+
47+
@post("/", name='v2_create')
48+
@Version('v2')
49+
async def create_v2(self, payload: CreateCarSerializer):
50+
result = self.repo.create_car(payload)
51+
result.update(message="This action adds a new car - version 2")
52+
return result
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 `car` 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+
from .services import CarRepository
24+
from .controllers import CarController
25+
from .routers import router
26+
27+
@Module(
28+
controllers=[CarController],
29+
providers=[CarRepository],
30+
routers=[router],
31+
template_folder='views',
32+
static_folder='statics'
33+
)
34+
class CarModule(ModuleBase):
35+
"""
36+
Car Module
37+
"""
38+
39+
def register_providers(self, container: Container) -> None:
40+
"""for more complicated provider registrations, use container.register_instance(...)"""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 dataclasses import dataclass
13+
from typing import List
14+
15+
from ellar.common import Provide, render, Req
16+
from ellar.core.routing import ModuleRouter
17+
from ellar.core.templating import render_template
18+
from ellar.serializer import DataclassSerializer
19+
20+
from .services import CarRepository
21+
22+
router = ModuleRouter("/car-as-router", tag='Router', description='Example of car Resource from a <strong>ModuleRouter</strong>')
23+
24+
25+
@dataclass
26+
class CarObject(DataclassSerializer):
27+
name: str
28+
model: str
29+
30+
31+
@router.get(response={200: List[CarObject]})
32+
async def get_cars(repo: CarRepository = Provide()):
33+
return repo.get_all()
34+
35+
36+
@router.http_route('/html', methods=['get', 'post'])
37+
@render("index.html")
38+
async def get_car_html(repo: CarRepository = Provide()):
39+
return repo.get_all()
40+
41+
42+
@router.get('/html/outside')
43+
async def get_car_html_with_render(repo: CarRepository = Provide(), request=Req()):
44+
return render_template("car/list.html", request=request, model=repo.get_all())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 ellar.serializer import DataclassSerializer, Serializer
17+
from pydantic import Field
18+
19+
20+
class CreateCarSerializer(Serializer):
21+
name: str
22+
year: int = Field(..., gt=0)
23+
model: str
24+
25+
26+
class CarListFilter(Serializer):
27+
offset: int = 1
28+
limit: int = 10
29+
30+
31+
class CarSerializer(Serializer):
32+
id: str
33+
name: str
34+
year: int
35+
model: str
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
import typing as t
13+
14+
from ellar.di import injectable, singleton_scope
15+
16+
from .schemas import CarSerializer, CreateCarSerializer
17+
18+
19+
@injectable(scope=singleton_scope)
20+
class CarRepository:
21+
def __init__(self):
22+
self._cars: t.List[CarSerializer] = []
23+
24+
def create_car(self, data: CreateCarSerializer) -> dict:
25+
data = CarSerializer(id=len(self._cars) + 1, **data.dict())
26+
self._cars.append(data)
27+
return data.dict()
28+
29+
def get_all(self) -> t.List[CarSerializer]:
30+
return self._cars

0 commit comments

Comments
 (0)