File: test_reference.py

package info (click to toggle)
python-odmantic 1.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 1,640 kB
  • sloc: python: 8,547; sh: 37; makefile: 34; xml: 13; javascript: 3
file content (41 lines) | stat: -rw-r--r-- 932 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pytest

from odmantic.field import Field
from odmantic.model import Model
from odmantic.reference import Reference


def test_build_query_filter_across_reference():
    class Referenced(Model):
        a: int

    class M(Model):
        ref: Referenced = Reference()

    with pytest.raises(
        NotImplementedError, match="filtering across references is not supported"
    ):
        M.ref.a == 12


def test_build_query_filter_across_reference_no_attribute():
    class Referenced(Model):
        a: int

    class M(Model):
        ref: Referenced = Reference()

    with pytest.raises(AttributeError):
        M.ref.does_not_exist  # type: ignore


def test_reference_with_custom_primary_field():
    class Referenced(Model):
        key: int = Field(primary_field=True)

    class M(Model):
        ref: Referenced = Reference()

    r = Referenced(key=1)
    m = M(ref=r)
    assert m.model_dump_doc()["ref"] == 1