async def test_find_user_details(mock_user_model_find_user_by_id: mock.Mock): mock_user_model_find_user_by_id.return_value = fixtures.user_fixture() result = await domain.find_user_by_id(user_id=dto.UserID("000000000000000000000001")) assert result == fixtures.user_fixture() mock_user_model_find_user_by_id.assert_called_once_with( user_id=dto.UserID("000000000000000000000001") )
def test_list_users_pagination(mock_domain_list_users: mock.Mock): mock_domain_list_users.return_value = dto.UserSummaryPaginatedResponse( results=[ dto.UserSummary( id=dto.UserID("00000000000000000000000a"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000b"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000c"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), ], next_cursor=dto.UserID("00000000000000000000000c"), ) response = client.get("/users?limit=3") expected_response = ( 200, { "results": [ { "id": "00000000000000000000000a", "email": "*****@*****.**", "ctime": "2020-01-01T00:00:00+00:00", "mtime": "2020-01-02T00:00:00+00:00", }, { "id": "00000000000000000000000b", "email": "*****@*****.**", "ctime": "2020-01-01T00:00:00+00:00", "mtime": "2020-01-02T00:00:00+00:00", }, { "id": "00000000000000000000000c", "email": "*****@*****.**", "ctime": "2020-01-01T00:00:00+00:00", "mtime": "2020-01-02T00:00:00+00:00", }, ], "next_cursor": "00000000000000000000000c", }, ) assert (response.status_code, response.json()) == expected_response assert mock_domain_list_users.called_once_with()
async def test_find_user_by_id_not_found(): collection = await user_model._get_collection() await collection.insert_one(fixtures.user_bson_fixture()) actual = await user_model.find_user_by_id( dto.UserID("00000000000000000000000f")) assert actual is None
async def test_list_user_filter_cursor_invalid_user_id(): collection = await user_model._get_collection() await collection.insert_one(fixtures.user_bson_fixture()) actual = await user_model.list_users( dto.UserSummaryFilter(limit=20, cursor=dto.UserID("1"))) expected: List[dto.UserSummary] = [] assert list(actual) == expected
def test_user_detail_not_found(mock_domain_find_user_by_id: mock.Mock): mock_domain_find_user_by_id.return_value = None response = client.get("/users/00000000000000000000000f") expected_response = (404, {"detail": "User not found."}) assert (response.status_code, response.json()) == expected_response mock_domain_find_user_by_id.assert_called_once_with( user_id=dto.UserID("00000000000000000000000f") )
def test_user_detail_success(mock_domain_find_user_by_id: mock.Mock): mock_domain_find_user_by_id.return_value = fixtures.user_fixture() response = client.get("/users/00000000000000000000000a") expected_response = (200, fixtures.user_json_fixture()) assert (response.status_code, response.json()) == expected_response mock_domain_find_user_by_id.assert_called_once_with( user_id=dto.UserID("00000000000000000000000a") )
async def test_list_users_pagination(mock_user_model_list_users: mock.Mock): mock_user_model_list_users.return_value = [ dto.UserSummary( id=dto.UserID("00000000000000000000000a"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000b"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000c"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), ] result = await domain.list_users(dto.UserSummaryFilter(limit=3)) assert result == dto.UserSummaryPaginatedResponse( results=[ dto.UserSummary( id=dto.UserID("00000000000000000000000a"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000b"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000c"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), ], next_cursor=dto.UserID("00000000000000000000000c"), )
def bson_to_user_id(obj_id: ObjectId) -> dto.UserID: return dto.UserID(str(obj_id))
actual = await user_model.list_users(dto.UserSummaryFilter(limit=20)) expected = [fixtures.user_summary_fixture()] assert list(actual) == expected @pytest.mark.parametrize( "user_summary_filter_kwargs, expected", [ ( { "limit": 3 }, [ dto.UserSummary( id=dto.UserID("00000000000000000000000f"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000e"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC), ), dto.UserSummary( id=dto.UserID("00000000000000000000000d"), email="*****@*****.**", ctime=datetime(2020, 1, 1, 0, 0, tzinfo=UTC), mtime=datetime(2020, 1, 2, 0, 0, tzinfo=UTC),