Draft 0.11.0 (#594)
* fix for #584 * fix for #580 * fix typing * connect to db in test * refactor test * remove async mark * connect client * fix mypy * fix mypy * update deps * check py3.10? * remove py3.6, bump version
This commit is contained in:
149
tests/test_fastapi/test_recursion_error.py
Normal file
149
tests/test_fastapi/test_recursion_error.py
Normal file
@ -0,0 +1,149 @@
|
||||
import json
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
from typing import List
|
||||
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from fastapi import Depends, FastAPI
|
||||
from pydantic import BaseModel, Json
|
||||
from starlette.testclient import TestClient
|
||||
|
||||
import ormar
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
router = FastAPI()
|
||||
metadata = sqlalchemy.MetaData()
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
router.state.database = database
|
||||
|
||||
headers = {"content-type": "application/json"}
|
||||
|
||||
|
||||
@router.on_event("startup")
|
||||
async def startup() -> None:
|
||||
database_ = router.state.database
|
||||
if not database_.is_connected:
|
||||
await database_.connect()
|
||||
|
||||
|
||||
@router.on_event("shutdown")
|
||||
async def shutdown() -> None:
|
||||
database_ = router.state.database
|
||||
if database_.is_connected:
|
||||
await database_.disconnect()
|
||||
|
||||
|
||||
class User(ormar.Model):
|
||||
"""
|
||||
The user model
|
||||
"""
|
||||
|
||||
id: uuid.UUID = ormar.UUID(primary_key=True, default=uuid.uuid4)
|
||||
email: str = ormar.String(unique=True, max_length=100)
|
||||
username: str = ormar.String(unique=True, max_length=100)
|
||||
password: str = ormar.String(unique=True, max_length=100)
|
||||
verified: bool = ormar.Boolean(default=False)
|
||||
verify_key: str = ormar.String(unique=True, max_length=100, nullable=True)
|
||||
created_at: datetime = ormar.DateTime(default=datetime.now())
|
||||
|
||||
class Meta:
|
||||
tablename = "users"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class UserSession(ormar.Model):
|
||||
"""
|
||||
The user session model
|
||||
"""
|
||||
|
||||
id: uuid.UUID = ormar.UUID(primary_key=True, default=uuid.uuid4)
|
||||
user: User = ormar.ForeignKey(User)
|
||||
session_key: str = ormar.String(unique=True, max_length=64)
|
||||
created_at: datetime = ormar.DateTime(default=datetime.now())
|
||||
|
||||
class Meta:
|
||||
tablename = "user_sessions"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class QuizAnswer(BaseModel):
|
||||
right: bool
|
||||
answer: str
|
||||
|
||||
|
||||
class QuizQuestion(BaseModel):
|
||||
question: str
|
||||
answers: List[QuizAnswer]
|
||||
|
||||
|
||||
class QuizInput(BaseModel):
|
||||
title: str
|
||||
description: str
|
||||
questions: List[QuizQuestion]
|
||||
|
||||
|
||||
class Quiz(ormar.Model):
|
||||
id: uuid.UUID = ormar.UUID(primary_key=True, default=uuid.uuid4)
|
||||
title: str = ormar.String(max_length=100)
|
||||
description: str = ormar.String(max_length=300, nullable=True)
|
||||
created_at: datetime = ormar.DateTime(default=datetime.now())
|
||||
updated_at: datetime = ormar.DateTime(default=datetime.now())
|
||||
user_id: uuid.UUID = ormar.UUID(foreign_key=User.id)
|
||||
questions: Json = ormar.JSON(nullable=False)
|
||||
|
||||
class Meta:
|
||||
tablename = "quiz"
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
async def get_current_user():
|
||||
return await User(email="mail@example.com", username="aa", password="pass").save()
|
||||
|
||||
|
||||
@router.post("/create", response_model=Quiz)
|
||||
async def create_quiz_lol(
|
||||
quiz_input: QuizInput, user: User = Depends(get_current_user)
|
||||
):
|
||||
quiz = Quiz(**quiz_input.dict(), user_id=user.id)
|
||||
return await quiz.save()
|
||||
|
||||
|
||||
def test_quiz_creation():
|
||||
client = TestClient(app=router)
|
||||
with client as client:
|
||||
payload = {
|
||||
"title": "Some test question",
|
||||
"description": "A description",
|
||||
"questions": [
|
||||
{
|
||||
"question": "Is ClassQuiz cool?",
|
||||
"answers": [
|
||||
{"right": True, "answer": "Yes"},
|
||||
{"right": False, "answer": "No"},
|
||||
],
|
||||
},
|
||||
{
|
||||
"question": "Do you like open source?",
|
||||
"answers": [
|
||||
{"right": True, "answer": "Yes"},
|
||||
{"right": False, "answer": "No"},
|
||||
{"right": False, "answer": "Maybe"},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
response = client.post("/create", data=json.dumps(payload))
|
||||
assert response.status_code == 200
|
||||
@ -176,14 +176,15 @@ async def test_queryset_method():
|
||||
year=1930, title="Book 3"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_count_method():
|
||||
async with database:
|
||||
await sample_data()
|
||||
|
||||
count = await Author.objects.select_related("books").count()
|
||||
count = await Author.objects.select_related("books").count()
|
||||
assert count == 1
|
||||
|
||||
# The legacy functionality
|
||||
count = await Author.objects.select_related("books").count(distinct=False)
|
||||
count = await Author.objects.select_related("books").count(distinct=False)
|
||||
assert count == 3
|
||||
|
||||
@ -4,6 +4,7 @@ import databases
|
||||
import pydantic
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
from pydantic import Json
|
||||
|
||||
import ormar
|
||||
from ormar import QuerySet
|
||||
@ -68,7 +69,7 @@ class Note(ormar.Model):
|
||||
|
||||
|
||||
class ItemConfig(ormar.Model):
|
||||
class Meta:
|
||||
class Meta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
tablename = "item_config"
|
||||
@ -98,6 +99,16 @@ class Customer(ormar.Model):
|
||||
name: str = ormar.String(max_length=32)
|
||||
|
||||
|
||||
class JsonTestModel(ormar.Model):
|
||||
class Meta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
tablename = "test_model"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True)
|
||||
json_field: Json = ormar.JSON()
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
@ -268,6 +279,36 @@ async def test_bulk_create():
|
||||
assert len(completed) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_create_json_field():
|
||||
async with database:
|
||||
json_value = {"a": 1}
|
||||
test_model_1 = JsonTestModel(id=1, json_field=json_value)
|
||||
test_model_2 = JsonTestModel(id=2, json_field=json_value)
|
||||
|
||||
# store one with .save() and the other with .bulk_create()
|
||||
await test_model_1.save()
|
||||
await JsonTestModel.objects.bulk_create([test_model_2])
|
||||
|
||||
# refresh from the database
|
||||
await test_model_1.load()
|
||||
await test_model_2.load()
|
||||
|
||||
assert test_model_1.json_field == test_model_2.json_field # True
|
||||
|
||||
# try to query the json field
|
||||
table = JsonTestModel.Meta.table
|
||||
query = table.select().where(table.c.json_field["a"].as_integer() == 1)
|
||||
res = [
|
||||
JsonTestModel.from_row(record, source_model=JsonTestModel)
|
||||
for record in await database.fetch_all(query)
|
||||
]
|
||||
|
||||
assert test_model_1 in res
|
||||
assert test_model_2 in res
|
||||
assert len(res) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_bulk_create_with_relation():
|
||||
async with database:
|
||||
@ -408,6 +449,21 @@ async def test_bulk_operations_with_json():
|
||||
items = await ItemConfig.objects.all()
|
||||
assert all(x.pairs == ["1"] for x in items)
|
||||
|
||||
items = await ItemConfig.objects.filter(ItemConfig.id > 1).all()
|
||||
for item in items:
|
||||
item.pairs = {"b": 2}
|
||||
await ItemConfig.objects.bulk_update(items)
|
||||
items = await ItemConfig.objects.filter(ItemConfig.id > 1).all()
|
||||
assert all(x.pairs == {"b": 2} for x in items)
|
||||
|
||||
table = ItemConfig.Meta.table
|
||||
query = table.select().where(table.c.pairs["b"].as_integer() == 2)
|
||||
res = [
|
||||
ItemConfig.from_row(record, source_model=ItemConfig)
|
||||
for record in await database.fetch_all(query)
|
||||
]
|
||||
assert len(res) == 2
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_custom_queryset_cls():
|
||||
|
||||
Reference in New Issue
Block a user