Skip to content

Commit

Permalink
add ci
Browse files Browse the repository at this point in the history
  • Loading branch information
AtticusZeller committed Jan 10, 2024
1 parent 1d42ded commit cf8644a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
10 changes: 6 additions & 4 deletions src/app/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
def __init__(self, model: type[ModelType]):
self.model = model

async def get(self, db: AsyncClient, *, table_name: str, id: str) -> ModelType | None:
async def get(
self, db: AsyncClient, *, table_name: str, id: str
) -> ModelType | None:
"""get by table_name"""

async def create(
self, db: AsyncClient, *, obj_in: CreateSchemaType
self, db: AsyncClient, *, obj_in: CreateSchemaType
) -> ModelType | None:
"""create by CreateSchemaType"""

async def update(
self, db: AsyncClient, *, obj_in: UpdateSchemaType
self, db: AsyncClient, *, obj_in: UpdateSchemaType
) -> ModelType | None:
"""update by UpdateSchemaType"""

async def delete(
self, db: AsyncClient, *, obj_in: UpdateSchemaType
self, db: AsyncClient, *, obj_in: UpdateSchemaType
) -> ModelType | None:
"""remove by UpdateSchemaType"""
8 changes: 4 additions & 4 deletions src/app/crud/crud_item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from supabase_py_async import AsyncClient

from app.crud.base import CRUDBase, ModelType
from app.crud.base import CRUDBase
from app.schemas import Item, ItemCreate, ItemUpdate


Expand All @@ -10,11 +10,11 @@ async def create(self, db: AsyncClient, *, obj_in: ItemCreate) -> Item:
await db.table(obj_in.table_name).insert(**obj_in.model_dump()).execute()
)
return self.model(**data[0])

async def get(self, db: AsyncClient, *, table_name: str, id: str) -> Item | None:
data, count = (
await db.table(table_name).select("*").eq("id", id).execute()
)
data, count = await db.table(table_name).select("*").eq("id", id).execute()
return self.model(**data[0])

async def get_multi_by_owner(
self, db: AsyncClient, *, table_name: str
) -> list[Item]:
Expand Down
2 changes: 1 addition & 1 deletion src/app/schemas/item.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from pydantic import BaseModel


## request


Expand All @@ -24,6 +23,7 @@ class ItemUpdate(ItemBase):
id: str
test_data: str


## response


Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
from collections.abc import Generator
from typing import AsyncGenerator
from collections.abc import AsyncGenerator, Generator

import pytest
from _pytest.config import Config
from dotenv import load_dotenv
from fastapi.testclient import TestClient
from supabase_py_async import AsyncClient, create_client

from app.main import app
from supabase_py_async import create_client, AsyncClient


def pytest_configure(config: Config) -> None:
Expand All @@ -20,6 +19,7 @@ def client() -> Generator:
with TestClient(app) as c:
yield c


# FIXME: AttributeError: 'async_generator' object has no attribute 'table'
@pytest.fixture(scope="module")
@pytest.mark.anyio
Expand Down
21 changes: 12 additions & 9 deletions tests/crud/test_item.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
import pytest
from faker import Faker
from supabase_py_async import AsyncClient

from app import crud
from app.schemas.item import ItemCreate, ItemUpdate, Item
from app.schemas.item import Item, ItemCreate, ItemUpdate

from supabase_py_async import AsyncClient

@pytest.mark.asyncio
async def test_create_item(db: AsyncClient) -> None:
test_data = Faker().text()
item_in = ItemCreate(table_name='test_table', test_data=test_data)
item_in = ItemCreate(table_name="test_table", test_data=test_data)
item: Item = await crud.item.create(db=db, obj_in=item_in)
assert item.test_data == test_data


@pytest.mark.asyncio
async def test_get_item(db: AsyncClient) -> None:
test_data = Faker().text()
item_in = ItemCreate(table_name='test_table', test_data=test_data)
item_in = ItemCreate(table_name="test_table", test_data=test_data)
item: Item = await crud.item.create(db=db, obj_in=item_in)
stored_item = await crud.item.get(db, table_name='test_table', id=item.id)
stored_item = await crud.item.get(db, table_name="test_table", id=item.id)
assert stored_item
assert item.id == stored_item.id
assert item.test_data == stored_item.test_data


@pytest.mark.asyncio
async def test_update_item(db: AsyncClient) -> None:
test_data = Faker().text()
item_in = ItemCreate(table_name='test_table', test_data=test_data)
item_in = ItemCreate(table_name="test_table", test_data=test_data)
item: Item = await crud.item.create(db=db, obj_in=item_in)
test_data2 = Faker().text()
item_update = ItemUpdate(table_name='test_table', id=item.id, test_data=test_data2)
item_update = ItemUpdate(table_name="test_table", id=item.id, test_data=test_data2)
item2 = await crud.item.update(db=db, obj_in=item_update)
assert item.id == item2.id
assert item.test_data == item2.test_data
assert item2.test_data == test_data2


@pytest.mark.asyncio
async def test_delete_item(db: AsyncClient) -> None:
test_data = Faker().text()
item_in = ItemCreate(table_name='test_table', test_data=test_data)
item_in = ItemCreate(table_name="test_table", test_data=test_data)
item: Item = await crud.item.create(db=db, obj_in=item_in)
item2 = await crud.item.delete(db=db, obj_in=item)
item3 = await crud.item.get(db, table_name='test_table', id=item.id)
item3 = await crud.item.get(db, table_name="test_table", id=item.id)
assert item3 is None
assert item.id == item2.id
assert item.test_data == item2.test_data
Expand Down

0 comments on commit cf8644a

Please sign in to comment.