Skip to content

Commit

Permalink
failing test reproducing issue art049#484
Browse files Browse the repository at this point in the history
  • Loading branch information
anentropic committed May 30, 2024
1 parent 6095d9d commit 9883552
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
default_language_version:
python: python3.8
python: python3.11
node: 15.4.0
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
91 changes: 88 additions & 3 deletions tests/integration/test_embedded_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import Dict, List, Tuple
from typing import Dict, List, Optional, Tuple

import pytest
from pymongo.database import Database

from odmantic.engine import AIOEngine, SyncEngine
from odmantic.field import Field
Expand Down Expand Up @@ -227,7 +228,9 @@ class User(Model):
assert sync_engine.find_one(User, User.id == Id(user=1, chat=1001)) is not None


async def test_embedded_model_custom_key_name_save_and_fetch(aio_engine: AIOEngine):
async def test_embedded_model_custom_key_name_save_and_fetch(
pymongo_database: Database, aio_engine: AIOEngine
):
class In(EmbeddedModel):
a: int = Field(key_name="in-a")

Expand All @@ -238,9 +241,19 @@ class Out(Model):
await aio_engine.save(instance)
fetched = await aio_engine.find_one(Out)
assert instance == fetched
document = pymongo_database[Out.__collection__].find_one(
{
+Out.id: instance.id # type: ignore
}
)
assert document
assert document["in"]
assert document["in"]["in-a"] == 3


def test_sync_embedded_model_custom_key_name_save_and_fetch(sync_engine: SyncEngine):
def test_sync_embedded_model_custom_key_name_save_and_fetch(
pymongo_database: Database, sync_engine: SyncEngine
):
class In(EmbeddedModel):
a: int = Field(key_name="in-a")

Expand All @@ -251,9 +264,18 @@ class Out(Model):
sync_engine.save(instance)
fetched = sync_engine.find_one(Out)
assert instance == fetched
document = pymongo_database[Out.__collection__].find_one(
{
+Out.id: instance.id # type: ignore
}
)
assert document
assert document["in"]
assert document["in"]["in-a"] == 3


async def test_embedded_model_list_custom_key_name_save_and_fetch(
pymongo_database: Database,
aio_engine: AIOEngine,
):
class In(EmbeddedModel):
Expand All @@ -266,9 +288,18 @@ class Out(Model):
await aio_engine.save(instance)
fetched = await aio_engine.find_one(Out)
assert instance == fetched
document = pymongo_database[Out.__collection__].find_one(
{
+Out.id: instance.id # type: ignore
}
)
assert document
assert document["in"]
assert document["in"][0]["in-a"] == 3


def test_sync_embedded_model_list_custom_key_name_save_and_fetch(
pymongo_database: Database,
sync_engine: SyncEngine,
):
class In(EmbeddedModel):
Expand All @@ -281,6 +312,60 @@ class Out(Model):
sync_engine.save(instance)
fetched = sync_engine.find_one(Out)
assert instance == fetched
document = pymongo_database[Out.__collection__].find_one(
{
+Out.id: instance.id # type: ignore
}
)
assert document
assert document["in"]
assert document["in"][0]["in-a"] == 3


async def test_embedded_model_optional_list_custom_key_name_save_and_fetch(
pymongo_database: Database, aio_engine: AIOEngine
):
class In(EmbeddedModel):
a: int = Field(key_name="in-a")

class Out(Model):
inner: Optional[List[In]] = Field(key_name="in", default_factory=list)

instance = Out(inner=[In(a=3)])
await aio_engine.save(instance)
fetched = await aio_engine.find_one(Out)
assert instance == fetched
document = pymongo_database[Out.__collection__].find_one(
{
+Out.id: instance.id, # type: ignore
}
)
assert document is not None
assert document["in"]
assert document["in"][0]["in-a"] == 3


def test_sync_embedded_model_optional_list_custom_key_name_save_and_fetch(
pymongo_database: Database, sync_engine: SyncEngine
):
class In(EmbeddedModel):
a: int = Field(key_name="in-a")

class Out(Model):
inner: Optional[List[In]] = Field(key_name="in", default_factory=list)

instance = Out(inner=[In(a=3)])
sync_engine.save(instance)
fetched = sync_engine.find_one(Out)
assert instance == fetched
document = pymongo_database[Out.__collection__].find_one(
{
+Out.id: instance.id, # type: ignore
}
)
assert document is not None
assert document["in"]
assert document["in"][0]["in-a"] == 3


async def test_embedded_model_dict_custom_key_name_save_and_fetch(
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ class Post(Model):
assert await aio_engine.find_one(Post, {"$text": {"$search": "python"}}) is not None


async def test_sync_custom_text_index(sync_engine: SyncEngine):
def test_sync_custom_text_index(sync_engine: SyncEngine):
class Post(Model):
title: str
content: str
Expand Down
12 changes: 11 additions & 1 deletion tests/integration/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@
import re
from datetime import datetime
from decimal import Decimal
from typing import Any, Dict, Generic, List, Pattern, Tuple, Type, TypeVar, Union
from typing import (
Any,
Dict,
Generic,
List,
Pattern,
Tuple,
Type,
TypeVar,
Union,
)

import pytest
from bson import Binary, Decimal128, Int64, ObjectId, Regex
Expand Down

0 comments on commit 9883552

Please sign in to comment.