fix quoting in order_by, add get_or_none
This commit is contained in:
@ -230,6 +230,8 @@ async def test_fernet_filters_nomatch():
|
||||
with pytest.raises(NoMatch):
|
||||
await Filter.objects.get(name="test1")
|
||||
|
||||
assert await Filter.objects.get_or_none(name="test1") is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_hash_filters_works():
|
||||
|
||||
@ -83,6 +83,18 @@ async def test_not_saved_raises_error(cleanup):
|
||||
await post.categories.add(news)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_not_existing_raises_error(cleanup):
|
||||
async with database:
|
||||
guido = await Author(first_name="Guido", last_name="Van Rossum").save()
|
||||
post = await Post.objects.create(title="Hello, M2M", author=guido)
|
||||
|
||||
with pytest.raises(NoMatch):
|
||||
await post.categories.get()
|
||||
|
||||
assert await post.categories.get_or_none() is None
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_assigning_related_objects(cleanup):
|
||||
async with database:
|
||||
@ -95,6 +107,9 @@ async def test_assigning_related_objects(cleanup):
|
||||
# or from the other end:
|
||||
await news.posts.add(post)
|
||||
|
||||
assert await post.categories.get_or_none(name="no exist") is None
|
||||
assert await post.categories.get_or_none(name="News") == news
|
||||
|
||||
# Creating columns object from instance:
|
||||
await post.categories.create(name="Tips")
|
||||
assert len(post.categories) == 2
|
||||
|
||||
@ -220,6 +220,8 @@ async def test_model_get():
|
||||
with pytest.raises(ormar.NoMatch):
|
||||
await User.objects.get()
|
||||
|
||||
assert await User.objects.get_or_none() is None
|
||||
|
||||
user = await User.objects.create(name="Tom")
|
||||
lookup = await User.objects.get()
|
||||
assert lookup == user
|
||||
|
||||
99
tests/test_reserved_sql_keywords_escaped.py
Normal file
99
tests/test_reserved_sql_keywords_escaped.py
Normal file
@ -0,0 +1,99 @@
|
||||
import databases
|
||||
import pytest
|
||||
import sqlalchemy
|
||||
|
||||
import ormar
|
||||
|
||||
from tests.settings import DATABASE_URL
|
||||
|
||||
database = databases.Database(DATABASE_URL, force_rollback=True)
|
||||
metadata = sqlalchemy.MetaData()
|
||||
|
||||
|
||||
class BaseMeta(ormar.ModelMeta):
|
||||
metadata = metadata
|
||||
database = database
|
||||
|
||||
|
||||
class User(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "user"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True, autoincrement=True, nullable=False)
|
||||
user: str = ormar.String(
|
||||
unique=True, index=True, nullable=False, max_length=255
|
||||
) # ID of the user on auth0
|
||||
first: str = ormar.String(nullable=False, max_length=255)
|
||||
last: str = ormar.String(nullable=False, max_length=255)
|
||||
email: str = ormar.String(unique=True, index=True, nullable=False, max_length=255)
|
||||
display_name: str = ormar.String(
|
||||
unique=True, index=True, nullable=False, max_length=255
|
||||
)
|
||||
pic_url: str = ormar.Text(nullable=True)
|
||||
|
||||
|
||||
class Task(ormar.Model):
|
||||
class Meta(BaseMeta):
|
||||
tablename = "task"
|
||||
|
||||
id: int = ormar.Integer(primary_key=True, autoincrement=True, nullable=False)
|
||||
from_: str = ormar.String(name="from", nullable=True, max_length=200)
|
||||
user = ormar.ForeignKey(User)
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True, scope="module")
|
||||
def create_test_database():
|
||||
engine = sqlalchemy.create_engine(DATABASE_URL)
|
||||
metadata.drop_all(engine)
|
||||
metadata.create_all(engine)
|
||||
yield
|
||||
metadata.drop_all(engine)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_single_model_quotes():
|
||||
async with database:
|
||||
await User.objects.create(
|
||||
user="test",
|
||||
first="first",
|
||||
last="last",
|
||||
email="email@com.com",
|
||||
display_name="first last",
|
||||
)
|
||||
|
||||
user = await User.objects.order_by("user").get(first="first")
|
||||
assert user.last == "last"
|
||||
assert user.email == "email@com.com"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_two_model_quotes():
|
||||
async with database:
|
||||
user = await User.objects.create(
|
||||
user="test",
|
||||
first="first",
|
||||
last="last",
|
||||
email="email@com.com",
|
||||
display_name="first last",
|
||||
)
|
||||
|
||||
await Task(user=user, from_="aa").save()
|
||||
await Task(user=user, from_="bb").save()
|
||||
|
||||
task = (
|
||||
await Task.objects.select_related("user")
|
||||
.order_by("user__user")
|
||||
.get(from_="aa")
|
||||
)
|
||||
assert task.user.last == "last"
|
||||
assert task.user.email == "email@com.com"
|
||||
|
||||
tasks = await Task.objects.select_related("user").order_by("-from").all()
|
||||
assert len(tasks) == 2
|
||||
assert tasks[0].user.last == "last"
|
||||
assert tasks[0].user.email == "email@com.com"
|
||||
assert tasks[0].from_ == "bb"
|
||||
|
||||
assert tasks[1].user.last == "last"
|
||||
assert tasks[1].user.email == "email@com.com"
|
||||
assert tasks[1].from_ == "aa"
|
||||
Reference in New Issue
Block a user