def test_repo_can_create_user(): fakeDB = FakeDB() repo = UserRepository(fakeDB) user = repo.create_user( UserData(**{ "fullname": "Hisma Mulya Sudradjat", "email": "*****@*****.**" })) assert user is not None
def test_repo_can_get_user_by_email(): fakeDB = FakeDB() repo = UserRepository(fakeDB) user = repo.create_user( UserData(**{ "fullname": "Hisma Mulya Sudradjat", "email": "*****@*****.**" })) fetched_user = repo.get_user_by_email(user.email) assert fetched_user.dict() == user.dict()
def test_it_should_be_able_to_create_user(): fakedb = FakeDB() user_repo = UserRepository(fakedb) service = UserService(user_repo) new_user = service.create_user(user_data=UserData( fullname="Hisma Mulya", email="*****@*****.**")) assert new_user is not None
def test_it_should_be_able_to_get_user(): fakedb = FakeDB() user_repo = UserRepository(fakedb) service = UserService(user_repo) new_user = service.create_user(user_data=UserData( fullname="Hisma Mulya", email="*****@*****.**")) fetched_user = service.get_user(new_user.id) assert new_user.dict() == fetched_user.dict()
def test_it_should_be_able_to_raise_exception_if_theres_existing_user_on_create( ): fakedb = FakeDB() user_repo = UserRepository(fakedb) service = UserService(user_repo) new_user = service.create_user(user_data=UserData( fullname="Hisma Mulya", email="*****@*****.**")) with pytest.raises(Exception) as e: service.create_user(user_data=UserData(fullname="Mamazo", email="*****@*****.**"))
def test_repo_can_delete_user_by_id(): fakeDB = FakeDB() repo = UserRepository(fakeDB) user = repo.create_user( UserData(**{ "fullname": "Hisma Mulya Sudradjat", "email": "*****@*****.**" })) repo.delete_user(user.id) fetched_user = repo.get_user(user.id) assert fetched_user is None
from example.user.user import UserData from example.user.user_service import UserService from example.user.user_repository import UserRepository from example.database.db import FakeDB from fastapi import FastAPI, HTTPException app = FastAPI() db = FakeDB() user_repo = UserRepository(db) user_service = UserService(user_repo) @app.post("/user", status_code=201) def add_user(user_data: UserData): try: created_user = user_service.create_user(user_data) return { "status": "ok", "data": created_user } except Exception as e: raise HTTPException(status_code=400, detail=str(e)) @app.get("/user/{user_id}", status_code=200) def get_user(user_id: str): try: user = user_service.get_user(user_id) return {