mirror of
https://github.com/ets-labs/python-dependency-injector.git
synced 2024-11-25 11:04:01 +03:00
Update quotes in fastapi-sqlalchemy example
This commit is contained in:
parent
274d1fe53b
commit
3c52756d3f
|
@ -8,7 +8,7 @@ from . import endpoints
|
||||||
|
|
||||||
def create_app() -> FastAPI:
|
def create_app() -> FastAPI:
|
||||||
container = Container()
|
container = Container()
|
||||||
container.config.from_yaml('config.yml')
|
container.config.from_yaml("config.yml")
|
||||||
container.wire(modules=[endpoints])
|
container.wire(modules=[endpoints])
|
||||||
|
|
||||||
db = container.db()
|
db = container.db()
|
||||||
|
|
|
@ -34,7 +34,7 @@ class Database:
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception('Session rollback because of exception')
|
logger.exception("Session rollback because of exception")
|
||||||
session.rollback()
|
session.rollback()
|
||||||
raise
|
raise
|
||||||
finally:
|
finally:
|
||||||
|
|
|
@ -10,7 +10,7 @@ from .repositories import NotFoundError
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get('/users')
|
@router.get("/users")
|
||||||
@inject
|
@inject
|
||||||
def get_list(
|
def get_list(
|
||||||
user_service: UserService = Depends(Provide[Container.user_service]),
|
user_service: UserService = Depends(Provide[Container.user_service]),
|
||||||
|
@ -18,7 +18,7 @@ def get_list(
|
||||||
return user_service.get_users()
|
return user_service.get_users()
|
||||||
|
|
||||||
|
|
||||||
@router.get('/users/{user_id}')
|
@router.get("/users/{user_id}")
|
||||||
@inject
|
@inject
|
||||||
def get_by_id(
|
def get_by_id(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
|
@ -30,7 +30,7 @@ def get_by_id(
|
||||||
return Response(status_code=status.HTTP_404_NOT_FOUND)
|
return Response(status_code=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
|
|
||||||
@router.post('/users', status_code=status.HTTP_201_CREATED)
|
@router.post("/users", status_code=status.HTTP_201_CREATED)
|
||||||
@inject
|
@inject
|
||||||
def add(
|
def add(
|
||||||
user_service: UserService = Depends(Provide[Container.user_service]),
|
user_service: UserService = Depends(Provide[Container.user_service]),
|
||||||
|
@ -38,7 +38,7 @@ def add(
|
||||||
return user_service.create_user()
|
return user_service.create_user()
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/users/{user_id}', status_code=status.HTTP_204_NO_CONTENT)
|
@router.delete("/users/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
@inject
|
@inject
|
||||||
def remove(
|
def remove(
|
||||||
user_id: int,
|
user_id: int,
|
||||||
|
@ -52,6 +52,6 @@ def remove(
|
||||||
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/status')
|
@router.get("/status")
|
||||||
def get_status():
|
def get_status():
|
||||||
return {'status': 'OK'}
|
return {"status": "OK"}
|
||||||
|
|
|
@ -7,7 +7,7 @@ from .database import Base
|
||||||
|
|
||||||
class User(Base):
|
class User(Base):
|
||||||
|
|
||||||
__tablename__ = 'users'
|
__tablename__ = "users"
|
||||||
|
|
||||||
id = Column(Integer, primary_key=True)
|
id = Column(Integer, primary_key=True)
|
||||||
email = Column(String, unique=True)
|
email = Column(String, unique=True)
|
||||||
|
@ -15,7 +15,7 @@ class User(Base):
|
||||||
is_active = Column(Boolean, default=True)
|
is_active = Column(Boolean, default=True)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f'<User(id="{self.id}", ' \
|
return f"<User(id={self.id}, " \
|
||||||
f'email="{self.email}", ' \
|
f"email=\"{self.email}\", " \
|
||||||
f'hashed_password="{self.hashed_password}", ' \
|
f"hashed_password=\"{self.hashed_password}\", " \
|
||||||
f'is_active="{self.is_active}")>'
|
f"is_active={self.is_active})>"
|
||||||
|
|
|
@ -46,9 +46,9 @@ class NotFoundError(Exception):
|
||||||
entity_name: str
|
entity_name: str
|
||||||
|
|
||||||
def __init__(self, entity_id):
|
def __init__(self, entity_id):
|
||||||
super().__init__(f'{self.entity_name} not found, id: {entity_id}')
|
super().__init__(f"{self.entity_name} not found, id: {entity_id}")
|
||||||
|
|
||||||
|
|
||||||
class UserNotFoundError(NotFoundError):
|
class UserNotFoundError(NotFoundError):
|
||||||
|
|
||||||
entity_name: str = 'User'
|
entity_name: str = "User"
|
||||||
|
|
|
@ -20,7 +20,7 @@ class UserService:
|
||||||
|
|
||||||
def create_user(self) -> User:
|
def create_user(self) -> User:
|
||||||
uid = uuid4()
|
uid = uuid4()
|
||||||
return self._repository.add(email=f'{uid}@email.com', password='pwd')
|
return self._repository.add(email=f"{uid}@email.com", password="pwd")
|
||||||
|
|
||||||
def delete_user_by_id(self, user_id: int) -> None:
|
def delete_user_by_id(self, user_id: int) -> None:
|
||||||
return self._repository.delete_by_id(user_id)
|
return self._repository.delete_by_id(user_id)
|
||||||
|
|
|
@ -18,18 +18,18 @@ def client():
|
||||||
def test_get_list(client):
|
def test_get_list(client):
|
||||||
repository_mock = mock.Mock(spec=UserRepository)
|
repository_mock = mock.Mock(spec=UserRepository)
|
||||||
repository_mock.get_all.return_value = [
|
repository_mock.get_all.return_value = [
|
||||||
User(id=1, email='test1@email.com', hashed_password='pwd', is_active=True),
|
User(id=1, email="test1@email.com", hashed_password="pwd", is_active=True),
|
||||||
User(id=2, email='test2@email.com', hashed_password='pwd', is_active=False),
|
User(id=2, email="test2@email.com", hashed_password="pwd", is_active=False),
|
||||||
]
|
]
|
||||||
|
|
||||||
with app.container.user_repository.override(repository_mock):
|
with app.container.user_repository.override(repository_mock):
|
||||||
response = client.get('/users')
|
response = client.get("/users")
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data == [
|
assert data == [
|
||||||
{'id': 1, 'email': 'test1@email.com', 'hashed_password': 'pwd', 'is_active': True},
|
{"id": 1, "email": "test1@email.com", "hashed_password": "pwd", "is_active": True},
|
||||||
{'id': 2, 'email': 'test2@email.com', 'hashed_password': 'pwd', 'is_active': False},
|
{"id": 2, "email": "test2@email.com", "hashed_password": "pwd", "is_active": False},
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@ -37,17 +37,17 @@ def test_get_by_id(client):
|
||||||
repository_mock = mock.Mock(spec=UserRepository)
|
repository_mock = mock.Mock(spec=UserRepository)
|
||||||
repository_mock.get_by_id.return_value = User(
|
repository_mock.get_by_id.return_value = User(
|
||||||
id=1,
|
id=1,
|
||||||
email='xyz@email.com',
|
email="xyz@email.com",
|
||||||
hashed_password='pwd',
|
hashed_password="pwd",
|
||||||
is_active=True,
|
is_active=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
with app.container.user_repository.override(repository_mock):
|
with app.container.user_repository.override(repository_mock):
|
||||||
response = client.get('/users/1')
|
response = client.get("/users/1")
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data == {'id': 1, 'email': 'xyz@email.com', 'hashed_password': 'pwd', 'is_active': True}
|
assert data == {"id": 1, "email": "xyz@email.com", "hashed_password": "pwd", "is_active": True}
|
||||||
repository_mock.get_by_id.assert_called_once_with(1)
|
repository_mock.get_by_id.assert_called_once_with(1)
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,35 +56,35 @@ def test_get_by_id_404(client):
|
||||||
repository_mock.get_by_id.side_effect = UserNotFoundError(1)
|
repository_mock.get_by_id.side_effect = UserNotFoundError(1)
|
||||||
|
|
||||||
with app.container.user_repository.override(repository_mock):
|
with app.container.user_repository.override(repository_mock):
|
||||||
response = client.get('/users/1')
|
response = client.get("/users/1")
|
||||||
|
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
@mock.patch('webapp.services.uuid4', return_value='xyz')
|
@mock.patch("webapp.services.uuid4", return_value="xyz")
|
||||||
def test_add(_, client):
|
def test_add(_, client):
|
||||||
repository_mock = mock.Mock(spec=UserRepository)
|
repository_mock = mock.Mock(spec=UserRepository)
|
||||||
repository_mock.add.return_value = User(
|
repository_mock.add.return_value = User(
|
||||||
id=1,
|
id=1,
|
||||||
email='xyz@email.com',
|
email="xyz@email.com",
|
||||||
hashed_password='pwd',
|
hashed_password="pwd",
|
||||||
is_active=True,
|
is_active=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
with app.container.user_repository.override(repository_mock):
|
with app.container.user_repository.override(repository_mock):
|
||||||
response = client.post('/users')
|
response = client.post("/users")
|
||||||
|
|
||||||
assert response.status_code == 201
|
assert response.status_code == 201
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data == {'id': 1, 'email': 'xyz@email.com', 'hashed_password': 'pwd', 'is_active': True}
|
assert data == {"id": 1, "email": "xyz@email.com", "hashed_password": "pwd", "is_active": True}
|
||||||
repository_mock.add.assert_called_once_with(email='xyz@email.com', password='pwd')
|
repository_mock.add.assert_called_once_with(email="xyz@email.com", password="pwd")
|
||||||
|
|
||||||
|
|
||||||
def test_remove(client):
|
def test_remove(client):
|
||||||
repository_mock = mock.Mock(spec=UserRepository)
|
repository_mock = mock.Mock(spec=UserRepository)
|
||||||
|
|
||||||
with app.container.user_repository.override(repository_mock):
|
with app.container.user_repository.override(repository_mock):
|
||||||
response = client.delete('/users/1')
|
response = client.delete("/users/1")
|
||||||
|
|
||||||
assert response.status_code == 204
|
assert response.status_code == 204
|
||||||
repository_mock.delete_by_id.assert_called_once_with(1)
|
repository_mock.delete_by_id.assert_called_once_with(1)
|
||||||
|
@ -95,13 +95,13 @@ def test_remove_404(client):
|
||||||
repository_mock.delete_by_id.side_effect = UserNotFoundError(1)
|
repository_mock.delete_by_id.side_effect = UserNotFoundError(1)
|
||||||
|
|
||||||
with app.container.user_repository.override(repository_mock):
|
with app.container.user_repository.override(repository_mock):
|
||||||
response = client.delete('/users/1')
|
response = client.delete("/users/1")
|
||||||
|
|
||||||
assert response.status_code == 404
|
assert response.status_code == 404
|
||||||
|
|
||||||
|
|
||||||
def test_status(client):
|
def test_status(client):
|
||||||
response = client.get('/status')
|
response = client.get("/status")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
data = response.json()
|
data = response.json()
|
||||||
assert data == {'status': 'OK'}
|
assert data == {"status": "OK"}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user