-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathtest_loader.py
270 lines (210 loc) · 9.3 KB
/
test_loader.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import pytest
from sqlalchemy import Column, ForeignKey, Integer, String, Table
from sqlalchemy.orm import relationship
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyLoader
from strawberry_sqlalchemy_mapper.exc import InvalidLocalRemotePairs
pytest_plugins = ("pytest_asyncio",)
@pytest.fixture
def many_to_one_tables(base):
class Employee(base):
__tablename__ = "employee"
id = Column(Integer, autoincrement=True, primary_key=True)
name = Column(String, nullable=False)
department_id = Column(Integer, ForeignKey("department.id"))
department = relationship("Department", back_populates="employees")
__mapper_args__ = {"eager_defaults": True}
class Department(base):
__tablename__ = "department"
id = Column(Integer, autoincrement=True, primary_key=True)
name = Column(String, nullable=False)
employees = relationship("Employee", back_populates="department")
__mapper_args__ = {"eager_defaults": True}
return Employee, Department
def test_loader_init():
loader = StrawberrySQLAlchemyLoader(bind=None)
assert loader._bind is None
assert loader._loaders == {}
@pytest.mark.asyncio
async def test_loader_for(engine, base, sessionmaker, many_to_one_tables):
Employee, Department = many_to_one_tables
base.metadata.create_all(engine)
with sessionmaker() as session:
e1 = Employee(name="e1")
e2 = Employee(name="e2")
d1 = Department(name="d1")
d2 = Department(name="d2")
session.add(e1)
session.add(e2)
session.add(d1)
session.add(d2)
session.flush()
e1.department = d2
e2.department = d1
session.commit()
base_loader = StrawberrySQLAlchemyLoader(bind=session)
loader = base_loader.loader_for(Employee.department.property)
assert loader.max_batch_size is None
assert loader.cache is True
assert not loader.cache_map.cache_map
assert loader._loop is None
assert loader.load_fn is not None
key = tuple(
[
getattr(e1, local.key)
for local, _ in Employee.department.property.local_remote_pairs
]
)
department = await loader.load(key)
assert department.name == "d2"
loader = base_loader.loader_for(Department.employees.property)
employees = await loader.load((d2.id,))
assert {e.name for e in employees} == {"e1"}
@pytest.mark.asyncio
async def test_loader_with_async_session(
async_engine, base, async_sessionmaker, many_to_one_tables
):
Employee, Department = many_to_one_tables
async with async_engine.begin() as conn:
await conn.run_sync(base.metadata.create_all)
async with async_sessionmaker(expire_on_commit=False) as session:
e1 = Employee(name="e1")
e2 = Employee(name="e2")
d1 = Department(name="d1")
d2 = Department(name="d2")
session.add(e1)
session.add(e2)
session.add(d1)
session.add(d2)
await session.flush()
e1.department = d2
e2.department = d1
await session.commit()
d2_id = d2.id
department_loader_key = tuple(
[
getattr(e1, local.key)
for local, _ in Employee.department.property.local_remote_pairs
]
)
base_loader = StrawberrySQLAlchemyLoader(async_bind_factory=async_sessionmaker)
loader = base_loader.loader_for(Employee.department.property)
department = await loader.load(department_loader_key)
assert department.name == "d2"
loader = base_loader.loader_for(Department.employees.property)
employees = await loader.load((d2_id,))
assert {e.name for e in employees} == {"e1"}
def create_default_data_on_secondary_table_tests(session, Employee, Department):
e1 = Employee(name="e1", id=1)
e2 = Employee(name="e2", id=2)
d1 = Department(name="d1")
d2 = Department(name="d2")
d3 = Department(name="d3")
session.add_all([e1, e2, d1, d2, d3])
session.flush()
e1.department.append(d1)
e1.department.append(d2)
e2.department.append(d2)
return e1, e2, d1, d2, d3
@pytest.mark.asyncio
async def test_loader_for_secondary_table(engine, base, sessionmaker, secondary_tables):
Employee, Department = secondary_tables
base.metadata.create_all(engine)
with sessionmaker() as session:
e1, _, _, _, _ = create_default_data_on_secondary_table_tests(session=session, Employee=Employee, Department=Department)
session.commit()
base_loader = StrawberrySQLAlchemyLoader(bind=session)
loader = base_loader.loader_for(Employee.department.property)
key = tuple(
[
getattr(
e1, str(Employee.department.property.local_remote_pairs[0][0].key)),
]
)
departments = await loader.load(key)
assert {d.name for d in departments} == {"d1", "d2"}
@pytest.mark.asyncio
async def test_loader_for_secondary_tables_with_another_foreign_key(engine, base, sessionmaker, secondary_tables_with_another_foreign_key):
Employee, Department = secondary_tables_with_another_foreign_key
base.metadata.create_all(engine)
with sessionmaker() as session:
e1, _, _, _, _ = create_default_data_on_secondary_table_tests(session=session, Employee=Employee, Department=Department)
session.commit()
base_loader = StrawberrySQLAlchemyLoader(bind=session)
loader = base_loader.loader_for(Employee.department.property)
key = tuple(
[
getattr(
e1, str(Employee.department.property.local_remote_pairs[0][0].key)),
]
)
departments = await loader.load(key)
assert {d.name for d in departments} == {"d1", "d2"}
@pytest.mark.asyncio
async def test_loader_for_secondary_tables_with_more_secondary_tables(engine, base, sessionmaker, secondary_tables_with_more_secondary_tables):
Employee, Department, Building = secondary_tables_with_more_secondary_tables
base.metadata.create_all(engine)
with sessionmaker() as session:
e1, e2, _, _, _ = create_default_data_on_secondary_table_tests(session=session, Employee=Employee, Department=Department)
b1 = Building(id=2, name="Building 1")
b1.employees.append(e1)
b1.employees.append(e2)
session.add(b1)
session.commit()
base_loader = StrawberrySQLAlchemyLoader(bind=session)
loader = base_loader.loader_for(Employee.department.property)
key = tuple(
[
getattr(
e1, str(Employee.department.property.local_remote_pairs[0][0].key)),
]
)
departments = await loader.load(key)
assert {d.name for d in departments} == {"d1", "d2"}
@pytest.mark.asyncio
async def test_loader_for_secondary_tables_with_use_list_false(engine, base, sessionmaker, secondary_tables_with_use_list_false):
Employee, Department = secondary_tables_with_use_list_false
base.metadata.create_all(engine)
with sessionmaker() as session:
e1, _, _, _, _ = create_default_data_on_secondary_table_tests(session=session, Employee=Employee, Department=Department)
session.commit()
base_loader = StrawberrySQLAlchemyLoader(bind=session)
loader = base_loader.loader_for(Employee.department.property)
key = tuple(
[
getattr(
e1, str(Employee.department.property.local_remote_pairs[0][0].key)),
]
)
departments = await loader.load(key)
assert {d.name for d in departments} == {"d1"}
@pytest.mark.asyncio
async def test_loader_for_secondary_tables_with_normal_relationship(engine, base, sessionmaker, secondary_tables_with_normal_relationship):
Employee, Department, Building = secondary_tables_with_normal_relationship
base.metadata.create_all(engine)
with sessionmaker() as session:
e1, e2, _, _, _ = create_default_data_on_secondary_table_tests(session=session, Employee=Employee, Department=Department)
b1 = Building(id=2, name="Building 1")
b1.employees.append(e1)
b1.employees.append(e2)
session.add(b1)
session.commit()
base_loader = StrawberrySQLAlchemyLoader(bind=session)
loader = base_loader.loader_for(Employee.department.property)
key = tuple(
[
getattr(
e1, str(Employee.department.property.local_remote_pairs[0][0].key)),
]
)
departments = await loader.load(key)
assert {d.name for d in departments} == {"d1", "d2"}
@pytest.mark.asyncio
async def test_loader_for_secondary_tables_should_raise_exception_if_relationship_dont_has_local_remote_pairs(engine, base, sessionmaker, secondary_tables_with_normal_relationship):
Employee, Department, Building = secondary_tables_with_normal_relationship
base.metadata.create_all(engine)
with sessionmaker() as session:
base_loader = StrawberrySQLAlchemyLoader(bind=session)
Employee.department.property.local_remote_pairs = []
loader = base_loader.loader_for(Employee.department.property)
with pytest.raises(expected_exception=InvalidLocalRemotePairs):
await loader.load((1,))