From cf8644aa4a80f2ab48ecb49e4e9f00a64098cee9 Mon Sep 17 00:00:00 2001 From: Atticuszz <1831768457@qq.com> Date: Wed, 10 Jan 2024 21:48:00 +0800 Subject: [PATCH] add ci --- src/app/crud/base.py | 10 ++++++---- src/app/crud/crud_item.py | 8 ++++---- src/app/schemas/item.py | 2 +- tests/conftest.py | 6 +++--- tests/crud/test_item.py | 21 ++++++++++++--------- 5 files changed, 26 insertions(+), 21 deletions(-) diff --git a/src/app/crud/base.py b/src/app/crud/base.py index eafcfaa..b2452b9 100644 --- a/src/app/crud/base.py +++ b/src/app/crud/base.py @@ -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""" diff --git a/src/app/crud/crud_item.py b/src/app/crud/crud_item.py index 55f18a6..941ed68 100644 --- a/src/app/crud/crud_item.py +++ b/src/app/crud/crud_item.py @@ -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 @@ -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]: diff --git a/src/app/schemas/item.py b/src/app/schemas/item.py index 42bc257..2bfeffd 100644 --- a/src/app/schemas/item.py +++ b/src/app/schemas/item.py @@ -1,6 +1,5 @@ from pydantic import BaseModel - ## request @@ -24,6 +23,7 @@ class ItemUpdate(ItemBase): id: str test_data: str + ## response diff --git a/tests/conftest.py b/tests/conftest.py index 26c4ae5..f871042 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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: @@ -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 diff --git a/tests/crud/test_item.py b/tests/crud/test_item.py index ac0740b..229148e 100644 --- a/tests/crud/test_item.py +++ b/tests/crud/test_item.py @@ -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