-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_optional_fields_complex_model.py
More file actions
139 lines (123 loc) · 4.93 KB
/
test_optional_fields_complex_model.py
File metadata and controls
139 lines (123 loc) · 4.93 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Test cases to ensure model type conversion works correctly."""
import pytest
from sqliter import SqliterDB
from tests.conftest import ComplexModel
@pytest.fixture
def db_mock_complex(db_mock: SqliterDB) -> SqliterDB:
"""Fixture for a mock database with a complex model."""
db_mock.create_table(ComplexModel)
db_mock.insert(
ComplexModel(
pk=1,
name="Alice",
age=30.5,
is_active=True,
score=85,
nullable_field="Not null",
)
)
db_mock.insert(
ComplexModel(
pk=2,
name="Bob",
age=25.0,
is_active=False,
score=90.5,
nullable_field=None,
)
)
return db_mock
class TestComplexModelPartialSelection:
"""Define test cases for selecting specific fields from a complex model."""
def test_select_all_fields(self, db_mock_complex: SqliterDB) -> None:
"""Select all fields and ensure their types are correct."""
results = db_mock_complex.select(ComplexModel).fetch_all()
assert len(results) == 2
for result in results:
assert isinstance(result.pk, int)
assert isinstance(result.name, str)
assert isinstance(result.age, float)
assert isinstance(result.is_active, bool)
assert isinstance(result.score, (int, float))
assert result.nullable_field is None or isinstance(
result.nullable_field, str
)
def test_select_subset_of_fields(self, db_mock_complex: SqliterDB) -> None:
"""Select a subset of fields and ensure their types are correct."""
fields = ["pk", "name", "age", "is_active", "score"]
results = db_mock_complex.select(
ComplexModel, fields=fields
).fetch_all()
assert len(results) == 2
for result in results:
assert isinstance(result.pk, int)
assert isinstance(result.name, str)
assert isinstance(result.age, float)
assert isinstance(result.is_active, bool)
assert isinstance(result.score, (int, float))
assert not hasattr(result, "birthday")
assert not hasattr(result, "nullable_field")
def test_select_with_type_conversion(
self, db_mock_complex: SqliterDB
) -> None:
"""Select a subset of fields and ensure their types are correct."""
fields = ["pk", "age", "is_active", "score"]
results = db_mock_complex.select(
ComplexModel, fields=fields
).fetch_all()
assert len(results) == 2
for result in results:
assert isinstance(result.pk, int)
assert isinstance(result.age, float)
assert isinstance(result.is_active, bool)
assert isinstance(result.score, (int, float))
def test_select_with_nullable_field(
self, db_mock_complex: SqliterDB
) -> None:
"""Select fields with a nullable field."""
fields = ["nullable_field"]
results = db_mock_complex.select(
ComplexModel, fields=fields
).fetch_all()
assert len(results) == 2
assert any(result.nullable_field is None for result in results)
assert any(isinstance(result.nullable_field, str) for result in results)
def test_select_with_union_field(self, db_mock_complex: SqliterDB) -> None:
"""Select fields with a Union type."""
fields = ["score"]
results = db_mock_complex.select(
ComplexModel, fields=fields
).fetch_all()
assert len(results) == 2
assert any(isinstance(result.score, int) for result in results)
assert any(isinstance(result.score, float) for result in results)
def test_select_with_filtering(self, db_mock_complex: SqliterDB) -> None:
"""Select fields with a filter."""
fields = ["pk", "name", "age"]
results = (
db_mock_complex.select(ComplexModel, fields=fields)
.filter(age__gt=28)
.fetch_all()
)
assert len(results) == 1
assert results[0].name == "Alice"
assert results[0].age > 28
def test_select_with_ordering(self, db_mock_complex: SqliterDB) -> None:
"""Select fields with ordering."""
fields = ["pk", "name", "age"]
results = (
db_mock_complex.select(ComplexModel, fields=fields)
.order("age", reverse=True)
.fetch_all()
)
assert len(results) == 2
assert results[0].name == "Alice"
assert results[1].name == "Bob"
def test_select_nonexistent_field(self, db_mock_complex: SqliterDB) -> None:
"""Select a nonexistent field and ensure an error is raised."""
with pytest.raises(
ValueError, match="Invalid fields specified: nonexistent_field"
):
db_mock_complex.select(
ComplexModel, fields=["nonexistent_field"]
).fetch_all()