Skip to content

Commit

Permalink
tests: update tests; refactor async client fixture; exclude path fiel…
Browse files Browse the repository at this point in the history
…d from fake predefined group; avoid using upsert
  • Loading branch information
dantetemplar authored and ArtemSBulgakov committed Aug 31, 2023
1 parent f51e528 commit ff93d54
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 32 deletions.
12 changes: 6 additions & 6 deletions tests/app/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,6 @@ async def _init_models(session: AsyncSession):
await conn.run_sync(Base.metadata.create_all)


@pytest_asyncio.fixture()
async def client():
async with httpx.AsyncClient(app=app, base_url="http://127.0.0.1:8000/") as client:
yield client


async def _restore_session(session: AsyncSession):
from src.storages.sql.models.base import Base

Expand Down Expand Up @@ -93,3 +87,9 @@ def tag_repository(storage) -> "AbstractTagRepository":
@pytest.fixture(scope="package")
def event_repository(storage) -> "AbstractEventRepository":
return SqlEventRepository(storage)


@pytest_asyncio.fixture()
async def async_client():
async with httpx.AsyncClient(app=app, base_url="http://testserver") as client:
yield client
9 changes: 6 additions & 3 deletions tests/app/test_app_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
from fastapi import FastAPI
from fastapi.testclient import TestClient

from src.main import app
from src.repositories.predefined.repository import (
JsonGroupStorage,
JsonUserStorage,
JsonTagStorage,
)

from src.main import app

fake = Faker()


Expand Down Expand Up @@ -59,7 +58,9 @@ def fake_user() -> JsonUserStorage.InJsonUser:
predefined_groups = []
for group in fake_groups:
predefined_group = JsonGroupStorage.PredefinedGroup(
alias=group.alias, description=group.description, name=group.name, path=group.path
alias=group.alias,
description=group.description,
name=group.name, # path=group.path
)
predefined_groups.append(predefined_group)

Expand All @@ -79,6 +80,8 @@ def fake_user() -> JsonUserStorage.InJsonUser:
groups_file.write(group_storage.json())
tags_file.write(tag_storage.json())

# TODO: Generate iCal files


@pytest.mark.asyncio
async def test_startup():
Expand Down
18 changes: 9 additions & 9 deletions tests/app/test_event_groups_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
async def create_event_group(event_group_repository):
await setup_repositories()
event_group_schema = get_fake_create_event_group()
event_group = await event_group_repository.create_or_read(event_group_schema)
event_group = await event_group_repository.create(event_group_schema)
return event_group


@pytest.mark.asyncio
async def test_get_event_group(client: httpx.AsyncClient, event_group_repository):
async def test_get_event_group(async_client: httpx.AsyncClient, event_group_repository):
event_group = await create_event_group(event_group_repository)
response = await client.get(f"event-groups/{event_group.id}")
response = await async_client.get(f"event-groups/{event_group.id}")
response_from_api = response.json()
assert response.status_code == 200
assert event_group.alias in response_from_api["alias"]
Expand All @@ -25,9 +25,9 @@ async def test_get_event_group(client: httpx.AsyncClient, event_group_repository


@pytest.mark.asyncio
async def test_find_event_group_by_path(client: httpx.AsyncClient, event_group_repository):
async def test_find_event_group_by_path(async_client: httpx.AsyncClient, event_group_repository):
event_group = await create_event_group(event_group_repository)
response = await client.get(f"event-groups/by-path?path={event_group.path}")
response = await async_client.get(f"event-groups/by-path?path={event_group.path}")
assert response.status_code == 200
response_from_api = response.json()
assert event_group.alias in response_from_api["alias"]
Expand All @@ -37,20 +37,20 @@ async def test_find_event_group_by_path(client: httpx.AsyncClient, event_group_r


@pytest.mark.asyncio
async def test_not_find_event_group_by_path(client: httpx.AsyncClient, event_group_repository):
async def test_not_find_event_group_by_path(async_client: httpx.AsyncClient, event_group_repository):
await setup_repositories()
response = await client.get("event-groups/by-path?path=nonexistingpath")
response = await async_client.get("event-groups/by-path?path=nonexistingpath")
assert response.status_code == 404


@pytest.mark.asyncio
async def test_list_event_groups(client: httpx.AsyncClient, event_group_repository):
async def test_list_event_groups(async_client: httpx.AsyncClient, event_group_repository):
event_groups = []
event_groups_number = 10
for i in range(event_groups_number):
event_group = await create_event_group(event_group_repository)
event_groups.append(event_group)
response = await client.get("event-groups/")
response = await async_client.get("event-groups/")
response_from_api = response.json()
assert response.status_code == 200

Expand Down
4 changes: 2 additions & 2 deletions tests/app/test_tags_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ async def create_tag(tag_repository):


@pytest.mark.asyncio
async def test_list_tags(client: httpx.AsyncClient, tag_repository):
async def test_list_tags(async_client: httpx.AsyncClient, tag_repository):
tags = []
tags_number = 10
for i in range(tags_number):
tag = await create_tag(tag_repository)
tags.append(tag)
response = await client.get("tags/")
response = await async_client.get("tags/")
response_from_api = response.json()
assert response.status_code == 200
for i in range(tags_number):
Expand Down
39 changes: 27 additions & 12 deletions tests/repositories/test_event_groups.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest
from faker import Faker
from sqlalchemy.exc import IntegrityError

from src.repositories.event_groups import AbstractEventGroupRepository
from src.schemas.event_groups import CreateEventGroup, ViewEventGroup, UpdateEventGroup

fake = Faker()
Expand All @@ -11,9 +13,9 @@ def get_fake_event_group() -> "CreateEventGroup":
return CreateEventGroup(alias=fake.slug(), name=fake.name(), path=fake_path, description=fake.slug())


async def _create_event_group(event_group_repository) -> "ViewEventGroup":
async def _create_event_group(event_group_repository: AbstractEventGroupRepository) -> "ViewEventGroup":
event_group_schema = get_fake_event_group()
event_group = await event_group_repository.create_or_read(event_group_schema)
event_group = await event_group_repository.create(event_group_schema)
assert event_group is not None
assert isinstance(event_group, ViewEventGroup)
assert event_group.id is not None
Expand All @@ -22,9 +24,9 @@ async def _create_event_group(event_group_repository) -> "ViewEventGroup":
return event_group


async def _batch_create_event_group(event_group_repository) -> list["ViewEventGroup"]:
async def _batch_create_event_group(event_group_repository: AbstractEventGroupRepository) -> list["ViewEventGroup"]:
event_group_schemas = [get_fake_event_group() for _ in range(10)]
event_groups = await event_group_repository.batch_create_or_read(event_group_schemas)
event_groups = await event_group_repository.batch_create(event_group_schemas)

assert event_groups is not None
assert isinstance(event_groups, list)
Expand All @@ -49,9 +51,11 @@ async def test_create_if_not_exists(event_group_repository):
@pytest.mark.asyncio
async def test_create_if_not_exists_HIT(event_group_repository):
event_group_schema = get_fake_event_group()
event_group = await event_group_repository.create_or_read(event_group_schema)
hit = await event_group_repository.create_or_read(event_group_schema)
assert event_group.id == hit.id
event_group = await event_group_repository.create(event_group_schema)
assert isinstance(event_group, ViewEventGroup)

with pytest.raises(IntegrityError):
await event_group_repository.create(event_group_schema)


@pytest.mark.asyncio
Expand Down Expand Up @@ -95,16 +99,27 @@ async def test_update(event_group_repository):
@pytest.mark.asyncio
async def test_path_duplicate(event_group_repository):
event_group = await _create_event_group(event_group_repository)

assert isinstance(event_group, ViewEventGroup)
new_scheme = CreateEventGroup(alias=fake.slug(), name=fake.name(), path=event_group.path)
new_event_group = await event_group_repository.create_or_read(new_scheme)
assert event_group.id == new_event_group.id
with pytest.raises(IntegrityError):
await event_group_repository.create(new_scheme)


@pytest.mark.asyncio
async def test_null_path(event_group_repository):
scheme = CreateEventGroup(alias=fake.slug(), name=fake.name())
new_scheme = CreateEventGroup(alias=fake.slug(), name=fake.name())
event_group = await event_group_repository.create_or_read(scheme)
new_event_group = await event_group_repository.create_or_read(new_scheme)
event_group = await event_group_repository.create(scheme)
new_event_group = await event_group_repository.create(new_scheme)
assert event_group.id != new_event_group.id


@pytest.mark.asyncio
async def test_batch_read_with_ordering(event_group_repository):
event_group1 = await _create_event_group(event_group_repository)
event_group2 = await _create_event_group(event_group_repository)
event_group3 = await _create_event_group(event_group_repository)
event_groups = await event_group_repository.batch_read([event_group3.id, event_group2.id, event_group1.id])
assert event_groups[0].id == event_group3.id
assert event_groups[1].id == event_group2.id
assert event_groups[2].id == event_group1.id

1 comment on commit ff93d54

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src
   exceptions.py39782%11, 28, 42, 56, 84, 98, 112
   main.py91298%131, 176
src/app/auth
   common.py281739%12–21, 25–32, 36–44, 49–50
   dependencies.py16662%33–38, 42
   jwt.py503334%26–28, 32–36, 40–44, 48–52, 56–69, 73–80
src/app/event_groups
   routes.py673548%35–41, 60–71, 111–115, 135, 180–206
src/app/ics
   routes.py594229%34–43, 49–75, 95–118
src/app/users
   routes.py251348%32–34, 54–59, 78–80, 102–107
src/app/workshops
   __init__.py550%1–9
   routes.py16160%1–63
src/repositories
   crud.py941089%89, 105, 108, 125, 142, 156–157, 174–180
src/repositories/event_groups
   repository.py58297%57–58
src/repositories/workshops
   __init__.py330%1–4
   abc.py440%1–10
   repository.py34340%1–68
src/schemas
   events.py71692%48, 56, 79, 98–100
   tags.py40198%36
   workshops.py58580%1–85
TOTAL150629480% 

Tests Skipped Failures Errors Time
53 0 💤 1 ❌ 0 🔥 12.521s ⏱️

Please sign in to comment.