-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathtest_conditions.py
More file actions
846 lines (731 loc) · 34.3 KB
/
test_conditions.py
File metadata and controls
846 lines (731 loc) · 34.3 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
"""Tests for GroupConditions and GroupOrders nested filter/order helpers."""
from __future__ import annotations
import uuid
from collections.abc import AsyncGenerator
from typing import Any
import pytest
import sqlalchemy as sa
from ai.backend.common.data.filter_specs import StringMatchSpec, UUIDEqualMatchSpec, UUIDInMatchSpec
from ai.backend.common.data.user.types import UserRole
from ai.backend.common.types import ResourceSlot, VFolderHostPermissionMap
from ai.backend.manager.data.auth.hash import PasswordHashAlgorithm
from ai.backend.manager.data.group.types import ProjectType
from ai.backend.manager.data.user.types import UserStatus
from ai.backend.manager.models.agent import AgentRow
from ai.backend.manager.models.container_registry import ContainerRegistryRow
from ai.backend.manager.models.deployment_auto_scaling_policy import (
DeploymentAutoScalingPolicyRow,
)
from ai.backend.manager.models.deployment_policy import DeploymentPolicyRow
from ai.backend.manager.models.deployment_revision import DeploymentRevisionRow
from ai.backend.manager.models.deployment_revision_preset import DeploymentRevisionPresetRow
from ai.backend.manager.models.domain import DomainRow
from ai.backend.manager.models.endpoint import EndpointRow
from ai.backend.manager.models.group import AssocGroupUserRow, GroupRow
from ai.backend.manager.models.group.conditions import GroupConditions
from ai.backend.manager.models.group.orders import GroupOrders
from ai.backend.manager.models.hasher.types import PasswordInfo
from ai.backend.manager.models.image import ImageRow
from ai.backend.manager.models.kernel import KernelRow
from ai.backend.manager.models.keypair import KeyPairRow
from ai.backend.manager.models.rbac_models import RoleRow, UserRoleRow
from ai.backend.manager.models.resource_policy import (
KeyPairResourcePolicyRow,
ProjectResourcePolicyRow,
UserResourcePolicyRow,
)
from ai.backend.manager.models.resource_preset import ResourcePresetRow
from ai.backend.manager.models.routing import RoutingRow
from ai.backend.manager.models.runtime_variant import RuntimeVariantRow
from ai.backend.manager.models.scaling_group import ScalingGroupRow
from ai.backend.manager.models.session import SessionRow
from ai.backend.manager.models.user import UserRow
from ai.backend.manager.models.utils import ExtendedAsyncSAEngine
from ai.backend.manager.models.vfolder import VFolderRow
from ai.backend.manager.repositories.base import BatchQuerier, OffsetPagination, QueryCondition
from ai.backend.manager.repositories.group.db_source import GroupDBSource
from ai.backend.testutils.db import with_tables
# Row imports above ensure mapper initialization (FK dependency order).
_WITH_TABLES = [
DomainRow,
ScalingGroupRow,
UserResourcePolicyRow,
ProjectResourcePolicyRow,
KeyPairResourcePolicyRow,
RoleRow,
UserRoleRow,
UserRow,
KeyPairRow,
GroupRow,
AssocGroupUserRow,
ContainerRegistryRow,
ImageRow,
VFolderRow,
EndpointRow,
DeploymentPolicyRow,
DeploymentAutoScalingPolicyRow,
RuntimeVariantRow,
DeploymentRevisionPresetRow,
DeploymentRevisionRow,
SessionRow,
AgentRow,
KernelRow,
RoutingRow,
ResourcePresetRow,
]
class TestGroupConditionsDomainNestedFilters:
"""Tests for Domain nested filter conditions in GroupConditions."""
def test_exists_domain_returns_callable(self) -> None:
spec = StringMatchSpec(value="test", case_insensitive=False, negated=False)
condition = GroupConditions.by_domain_description_contains(spec)
assert callable(condition)
def test_by_domain_description_contains_generates_exists(self) -> None:
spec = StringMatchSpec(value="research", case_insensitive=False, negated=False)
condition = GroupConditions.by_domain_description_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "domains" in sql
def test_by_domain_description_contains_case_insensitive(self) -> None:
spec = StringMatchSpec(value="research", case_insensitive=True, negated=False)
condition = GroupConditions.by_domain_description_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
# Default dialect compiles ilike as lower(...) LIKE lower(...)
assert "lower" in sql
def test_by_domain_description_contains_negated(self) -> None:
spec = StringMatchSpec(value="research", case_insensitive=False, negated=True)
condition = GroupConditions.by_domain_description_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "NOT LIKE" in sql.upper()
def test_by_domain_description_equals_generates_exists(self) -> None:
spec = StringMatchSpec(value="exact", case_insensitive=False, negated=False)
condition = GroupConditions.by_domain_description_equals(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "domains" in sql
def test_by_domain_description_equals_case_insensitive(self) -> None:
spec = StringMatchSpec(value="Exact", case_insensitive=True, negated=False)
condition = GroupConditions.by_domain_description_equals(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "lower" in sql
def test_by_domain_description_equals_negated(self) -> None:
spec = StringMatchSpec(value="exact", case_insensitive=False, negated=True)
condition = GroupConditions.by_domain_description_equals(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
# sa.not_(col == val) compiles as col != val
assert "!=" in sql or "NOT" in sql.upper()
def test_by_domain_is_active_true(self) -> None:
condition = GroupConditions.by_domain_is_active(True)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "is_active" in sql
def test_by_domain_is_active_false(self) -> None:
condition = GroupConditions.by_domain_is_active(False)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "is_active" in sql
def test_closure_independence(self) -> None:
spec_a = StringMatchSpec(value="alpha", case_insensitive=False, negated=False)
spec_b = StringMatchSpec(value="beta", case_insensitive=False, negated=False)
cond_a = GroupConditions.by_domain_description_contains(spec_a)
cond_b = GroupConditions.by_domain_description_contains(spec_b)
sql_a = str(cond_a().compile(compile_kwargs={"literal_binds": True}))
sql_b = str(cond_b().compile(compile_kwargs={"literal_binds": True}))
assert sql_a != sql_b
assert "alpha" in sql_a
assert "beta" in sql_b
def test_exists_domain_combined_single_exists(self) -> None:
"""Combined helper wraps raw column conditions into single EXISTS."""
def cond_is_active() -> sa.sql.expression.ColumnElement[bool]:
return DomainRow.is_active == True # noqa: E712
def cond_name_like() -> sa.sql.expression.ColumnElement[bool]:
return DomainRow.description.like("%test%")
conditions: list[QueryCondition] = [cond_is_active, cond_name_like]
combined = GroupConditions.exists_domain_combined(conditions)
sql = str(combined().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert sql.count("EXISTS") == 1
def test_exists_domain_combined_returns_column_element(self) -> None:
conditions: list[QueryCondition] = []
combined = GroupConditions.exists_domain_combined(conditions)
result = combined()
assert isinstance(result, sa.sql.expression.ColumnElement)
class TestGroupOrdersDomainNested:
"""Tests for Domain nested orders in GroupOrders."""
def test_by_domain_name_ascending(self) -> None:
order = GroupOrders.by_domain_name(ascending=True)
order_str = str(order)
assert "ASC" in order_str or "asc" in order_str.lower()
def test_by_domain_name_descending(self) -> None:
order = GroupOrders.by_domain_name(ascending=False)
order_str = str(order)
assert "DESC" in order_str or "desc" in order_str.lower()
def test_by_domain_name_contains_scalar_subquery(self) -> None:
order = GroupOrders.by_domain_name(ascending=True)
order_str = str(order.compile(compile_kwargs={"literal_binds": True}))
assert "domains" in order_str
assert "SELECT" in order_str.upper()
def test_by_domain_is_active_ascending(self) -> None:
order = GroupOrders.by_domain_is_active(ascending=True)
order_str = str(order.compile(compile_kwargs={"literal_binds": True}))
assert "domains" in order_str
assert "is_active" in order_str
def test_by_domain_created_at_ascending(self) -> None:
order = GroupOrders.by_domain_created_at(ascending=True)
order_str = str(order.compile(compile_kwargs={"literal_binds": True}))
assert "domains" in order_str
assert "created_at" in order_str
def test_by_domain_created_at_descending(self) -> None:
order = GroupOrders.by_domain_created_at(ascending=False)
order_str = str(order)
assert "DESC" in order_str or "desc" in order_str.lower()
def test_scalar_subquery_returns_clause_element(self) -> None:
order = GroupOrders.by_domain_name(ascending=True)
assert isinstance(order, sa.sql.ClauseElement)
class TestGroupNestedSearchIntegration:
"""DB integration tests: nested filter/order applied via GroupDBSource.search_projects."""
@pytest.fixture
async def db_with_cleanup(
self,
database_connection: ExtendedAsyncSAEngine,
) -> AsyncGenerator[ExtendedAsyncSAEngine, None]:
async with with_tables(database_connection, _WITH_TABLES):
yield database_connection
@pytest.fixture
async def group_db_source(
self,
db_with_cleanup: ExtendedAsyncSAEngine,
) -> GroupDBSource:
return GroupDBSource(db=db_with_cleanup)
@pytest.fixture
async def two_domains_with_projects(
self,
db_with_cleanup: ExtendedAsyncSAEngine,
) -> dict[str, list[uuid.UUID]]:
"""Create two domains (active/inactive) each with one project.
Returns mapping of domain_name -> [project_id].
"""
active_domain = f"active-dom-{uuid.uuid4().hex[:8]}"
inactive_domain = f"inactive-dom-{uuid.uuid4().hex[:8]}"
result: dict[str, list[uuid.UUID]] = {}
async with db_with_cleanup.begin_session() as session:
for domain_name, is_active, desc in [
(active_domain, True, "Research lab"),
(inactive_domain, False, "Archived department"),
]:
domain = DomainRow(
name=domain_name,
description=desc,
is_active=is_active,
total_resource_slots=ResourceSlot(),
allowed_vfolder_hosts=VFolderHostPermissionMap(),
allowed_docker_registries=[],
dotfiles=b"",
integration_id=None,
)
session.add(domain)
await session.flush()
policy = ProjectResourcePolicyRow(
name=f"pol-{uuid.uuid4().hex[:8]}",
max_vfolder_count=0,
max_quota_scope_size=-1,
max_network_count=3,
)
session.add(policy)
await session.flush()
for domain_name in [active_domain, inactive_domain]:
gid = uuid.uuid4()
group = GroupRow(
id=gid,
name=f"proj-{gid.hex[:8]}",
description="test project",
is_active=True,
domain_name=domain_name,
total_resource_slots=ResourceSlot(),
allowed_vfolder_hosts=VFolderHostPermissionMap(),
integration_id=None,
resource_policy=policy.name,
type=ProjectType.GENERAL,
)
session.add(group)
result[domain_name] = [gid]
await session.commit()
return result
async def test_search_projects_with_domain_is_active_filter(
self,
group_db_source: GroupDBSource,
two_domains_with_projects: dict[str, list[uuid.UUID]],
) -> None:
"""search_projects with by_domain_is_active(True) returns only projects in active domains."""
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_domain_is_active(True)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
active_domain = [d for d, _ in two_domains_with_projects.items() if "active-dom" in d][0]
assert result.items[0].id == two_domains_with_projects[active_domain][0]
async def test_search_projects_with_domain_description_contains_filter(
self,
group_db_source: GroupDBSource,
two_domains_with_projects: dict[str, list[uuid.UUID]],
) -> None:
"""search_projects with by_domain_description_contains filters by domain description."""
spec = StringMatchSpec(value="Research", case_insensitive=True, negated=False)
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_domain_description_contains(spec)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
async def test_search_projects_with_domain_description_negated_filter(
self,
group_db_source: GroupDBSource,
two_domains_with_projects: dict[str, list[uuid.UUID]],
) -> None:
"""Negated domain description filter excludes matching projects."""
spec = StringMatchSpec(value="Research", case_insensitive=True, negated=True)
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_domain_description_contains(spec)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
async def test_search_projects_ordered_by_domain_name(
self,
group_db_source: GroupDBSource,
two_domains_with_projects: dict[str, list[uuid.UUID]],
) -> None:
"""search_projects with by_domain_name order sorts by correlated domain name."""
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[],
orders=[GroupOrders.by_domain_name(ascending=True)],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 2
domain_names = sorted(two_domains_with_projects.keys())
assert result.items[0].id == two_domains_with_projects[domain_names[0]][0]
assert result.items[1].id == two_domains_with_projects[domain_names[1]][0]
async def test_search_projects_combined_domain_filter_and_order(
self,
group_db_source: GroupDBSource,
two_domains_with_projects: dict[str, list[uuid.UUID]],
) -> None:
"""Combining nested filter + nested order in single search call."""
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_domain_is_active(True)],
orders=[GroupOrders.by_domain_name(ascending=True)],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
class TestGroupConditionsUserIdFilters:
"""Tests for User ID (UUID) nested filter conditions in GroupConditions."""
def test_by_user_id_equals_generates_exists(self) -> None:
user_uuid = uuid.uuid4()
spec = UUIDEqualMatchSpec(value=user_uuid, negated=False)
condition = GroupConditions.by_user_id_equals(spec)
sql = str(condition().compile())
assert "EXISTS" in sql
assert "users" in sql
assert "association_groups_users" in sql
def test_by_user_id_equals_negated(self) -> None:
user_uuid = uuid.uuid4()
spec = UUIDEqualMatchSpec(value=user_uuid, negated=True)
condition = GroupConditions.by_user_id_equals(spec)
sql = str(condition().compile())
assert "EXISTS" in sql
assert "!=" in sql or "NOT" in sql.upper()
def test_by_user_id_in_generates_exists(self) -> None:
user_uuids = [uuid.uuid4(), uuid.uuid4()]
spec = UUIDInMatchSpec(values=user_uuids, negated=False)
condition = GroupConditions.by_user_id_in(spec)
sql = str(condition().compile())
assert "EXISTS" in sql
assert "users" in sql
assert "association_groups_users" in sql
assert "IN" in sql.upper()
def test_by_user_id_in_negated(self) -> None:
user_uuids = [uuid.uuid4(), uuid.uuid4()]
spec = UUIDInMatchSpec(values=user_uuids, negated=True)
condition = GroupConditions.by_user_id_in(spec)
sql = str(condition().compile())
assert "EXISTS" in sql
assert "NOT IN" in sql.upper()
class TestGroupConditionsUserNestedFilters:
"""Tests for User nested filter conditions in GroupConditions (M:N)."""
def test_by_user_username_contains_generates_exists(self) -> None:
spec = StringMatchSpec(value="alice", case_insensitive=False, negated=False)
condition = GroupConditions.by_user_username_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "users" in sql
assert "association_groups_users" in sql
def test_by_user_username_contains_case_insensitive(self) -> None:
spec = StringMatchSpec(value="alice", case_insensitive=True, negated=False)
condition = GroupConditions.by_user_username_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "lower" in sql
def test_by_user_username_contains_negated(self) -> None:
spec = StringMatchSpec(value="alice", case_insensitive=False, negated=True)
condition = GroupConditions.by_user_username_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "NOT LIKE" in sql.upper()
def test_by_user_username_equals_generates_exists(self) -> None:
spec = StringMatchSpec(value="alice", case_insensitive=False, negated=False)
condition = GroupConditions.by_user_username_equals(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "users" in sql
def test_by_user_username_equals_case_insensitive(self) -> None:
spec = StringMatchSpec(value="Alice", case_insensitive=True, negated=False)
condition = GroupConditions.by_user_username_equals(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "lower" in sql
def test_by_user_email_contains_generates_exists(self) -> None:
spec = StringMatchSpec(value="@example", case_insensitive=False, negated=False)
condition = GroupConditions.by_user_email_contains(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "users" in sql
def test_by_user_email_equals_generates_exists(self) -> None:
spec = StringMatchSpec(value="alice@example.com", case_insensitive=False, negated=False)
condition = GroupConditions.by_user_email_equals(spec)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "users" in sql
def test_by_user_is_active_true(self) -> None:
condition = GroupConditions.by_user_is_active(True)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "status" in sql
def test_by_user_is_active_false(self) -> None:
condition = GroupConditions.by_user_is_active(False)
sql = str(condition().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert "status" in sql
def test_exists_user_combined_single_exists(self) -> None:
"""Combined helper wraps raw column conditions into single EXISTS."""
def cond_status() -> sa.sql.expression.ColumnElement[bool]:
return UserRow.status == UserStatus.ACTIVE
def cond_username_like() -> sa.sql.expression.ColumnElement[bool]:
return UserRow.username.like("%test%")
conditions: list[QueryCondition] = [cond_status, cond_username_like]
combined = GroupConditions.exists_user_combined(conditions)
sql = str(combined().compile(compile_kwargs={"literal_binds": True}))
assert "EXISTS" in sql
assert sql.count("EXISTS") == 1
assert "association_groups_users" in sql
def test_exists_user_combined_returns_column_element(self) -> None:
conditions: list[QueryCondition] = []
combined = GroupConditions.exists_user_combined(conditions)
result = combined()
assert isinstance(result, sa.sql.expression.ColumnElement)
def test_closure_independence(self) -> None:
spec_a = StringMatchSpec(value="alice", case_insensitive=False, negated=False)
spec_b = StringMatchSpec(value="bob", case_insensitive=False, negated=False)
cond_a = GroupConditions.by_user_username_contains(spec_a)
cond_b = GroupConditions.by_user_username_contains(spec_b)
sql_a = str(cond_a().compile(compile_kwargs={"literal_binds": True}))
sql_b = str(cond_b().compile(compile_kwargs={"literal_binds": True}))
assert sql_a != sql_b
assert "alice" in sql_a
assert "bob" in sql_b
class TestGroupOrdersUserNested:
"""Tests for User nested orders in GroupOrders (M:N with MIN)."""
def test_by_user_username_ascending(self) -> None:
order = GroupOrders.by_user_username(ascending=True)
order_str = str(order)
assert "ASC" in order_str or "asc" in order_str.lower()
def test_by_user_username_descending(self) -> None:
order = GroupOrders.by_user_username(ascending=False)
order_str = str(order)
assert "DESC" in order_str or "desc" in order_str.lower()
def test_by_user_username_contains_min_subquery(self) -> None:
order = GroupOrders.by_user_username(ascending=True)
order_str = str(order.compile(compile_kwargs={"literal_binds": True}))
assert "users" in order_str
assert "min" in order_str.lower()
assert "association_groups_users" in order_str
def test_by_user_email_ascending(self) -> None:
order = GroupOrders.by_user_email(ascending=True)
order_str = str(order.compile(compile_kwargs={"literal_binds": True}))
assert "users" in order_str
assert "min" in order_str.lower()
def test_by_user_email_descending(self) -> None:
order = GroupOrders.by_user_email(ascending=False)
order_str = str(order)
assert "DESC" in order_str or "desc" in order_str.lower()
def test_returns_clause_element(self) -> None:
order = GroupOrders.by_user_username(ascending=True)
assert isinstance(order, sa.sql.ClauseElement)
class TestGroupUserNestedSearchIntegration:
"""DB integration tests: User nested filter/order via GroupDBSource.search_projects."""
@pytest.fixture
async def db_with_cleanup(
self,
database_connection: ExtendedAsyncSAEngine,
) -> AsyncGenerator[ExtendedAsyncSAEngine, None]:
async with with_tables(database_connection, _WITH_TABLES):
yield database_connection
@pytest.fixture
async def group_db_source(
self,
db_with_cleanup: ExtendedAsyncSAEngine,
) -> GroupDBSource:
return GroupDBSource(db=db_with_cleanup)
@pytest.fixture
async def projects_with_users(
self,
db_with_cleanup: ExtendedAsyncSAEngine,
) -> dict[str, dict[str, Any]]:
"""Create a domain with two projects, each associated with one user.
Returns mapping with project info including user details.
"""
domain_name = f"test-dom-{uuid.uuid4().hex[:8]}"
result: dict[str, dict[str, Any]] = {}
async with db_with_cleanup.begin_session() as session:
domain = DomainRow(
name=domain_name,
description="Test domain",
is_active=True,
total_resource_slots=ResourceSlot(),
allowed_vfolder_hosts=VFolderHostPermissionMap(),
allowed_docker_registries=[],
dotfiles=b"",
integration_id=None,
)
session.add(domain)
await session.flush()
user_policy = UserResourcePolicyRow(
name=f"upol-{uuid.uuid4().hex[:8]}",
max_vfolder_count=0,
max_quota_scope_size=-1,
max_session_count_per_model_session=0,
max_customized_image_count=0,
)
session.add(user_policy)
project_policy = ProjectResourcePolicyRow(
name=f"ppol-{uuid.uuid4().hex[:8]}",
max_vfolder_count=0,
max_quota_scope_size=-1,
max_network_count=3,
)
session.add(project_policy)
await session.flush()
active_user_id = uuid.uuid4()
active_user = UserRow(
uuid=active_user_id,
username="alice-active",
email="alice@example.com",
password=PasswordInfo(
password="test_password",
algorithm=PasswordHashAlgorithm.PBKDF2_SHA256,
rounds=100_000,
salt_size=32,
),
need_password_change=False,
full_name="Alice Active",
description="Active user",
status=UserStatus.ACTIVE,
status_info="admin-requested",
domain_name=domain_name,
role=UserRole.USER,
resource_policy=user_policy.name,
)
session.add(active_user)
inactive_user_id = uuid.uuid4()
inactive_user = UserRow(
uuid=inactive_user_id,
username="bob-inactive",
email="bob@example.com",
password=PasswordInfo(
password="test_password",
algorithm=PasswordHashAlgorithm.PBKDF2_SHA256,
rounds=100_000,
salt_size=32,
),
need_password_change=False,
full_name="Bob Inactive",
description="Inactive user",
status=UserStatus.INACTIVE,
status_info="admin-requested",
domain_name=domain_name,
role=UserRole.USER,
resource_policy=user_policy.name,
)
session.add(inactive_user)
await session.flush()
proj_a_id = uuid.uuid4()
proj_a = GroupRow(
id=proj_a_id,
name="proj-alpha",
description="Alpha project",
is_active=True,
domain_name=domain_name,
total_resource_slots=ResourceSlot(),
allowed_vfolder_hosts=VFolderHostPermissionMap(),
integration_id=None,
resource_policy=project_policy.name,
type=ProjectType.GENERAL,
)
session.add(proj_a)
proj_b_id = uuid.uuid4()
proj_b = GroupRow(
id=proj_b_id,
name="proj-beta",
description="Beta project",
is_active=True,
domain_name=domain_name,
total_resource_slots=ResourceSlot(),
allowed_vfolder_hosts=VFolderHostPermissionMap(),
integration_id=None,
resource_policy=project_policy.name,
type=ProjectType.GENERAL,
)
session.add(proj_b)
await session.flush()
assoc_a = AssocGroupUserRow(
group_id=proj_a_id,
user_id=active_user_id,
)
session.add(assoc_a)
assoc_b = AssocGroupUserRow(
group_id=proj_b_id,
user_id=inactive_user_id,
)
session.add(assoc_b)
await session.commit()
result["proj_alpha"] = {
"project_id": proj_a_id,
"user_id": active_user_id,
"username": "alice-active",
"email": "alice@example.com",
"is_active": True,
}
result["proj_beta"] = {
"project_id": proj_b_id,
"user_id": inactive_user_id,
"username": "bob-inactive",
"email": "bob@example.com",
"is_active": False,
}
return result
async def test_search_with_user_id_equals_filter(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Filter projects by user UUID (equals)."""
alpha_info = projects_with_users["proj_alpha"]
spec = UUIDEqualMatchSpec(value=alpha_info["user_id"], negated=False)
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_user_id_equals(spec)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
assert result.items[0].id == alpha_info["project_id"]
async def test_search_with_user_id_in_filter(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Filter projects by user UUID (in list)."""
alpha_info = projects_with_users["proj_alpha"]
beta_info = projects_with_users["proj_beta"]
spec = UUIDInMatchSpec(values=[alpha_info["user_id"], beta_info["user_id"]], negated=False)
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_user_id_in(spec)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 2
async def test_search_with_user_username_filter(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Filter projects by user username (contains)."""
spec = StringMatchSpec(value="alice", case_insensitive=True, negated=False)
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_user_username_contains(spec)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
assert result.items[0].id == projects_with_users["proj_alpha"]["project_id"]
async def test_search_with_user_email_filter(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Filter projects by user email (contains)."""
spec = StringMatchSpec(value="bob@", case_insensitive=False, negated=False)
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_user_email_contains(spec)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
assert result.items[0].id == projects_with_users["proj_beta"]["project_id"]
async def test_search_with_user_is_active_filter(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Filter projects by user active status."""
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[GroupConditions.by_user_is_active(True)],
orders=[],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 1
assert result.items[0].id == projects_with_users["proj_alpha"]["project_id"]
async def test_search_ordered_by_user_username(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Order projects by user username (MIN aggregation)."""
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[],
orders=[GroupOrders.by_user_username(ascending=True)],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 2
# alice < bob alphabetically
assert result.items[0].id == projects_with_users["proj_alpha"]["project_id"]
assert result.items[1].id == projects_with_users["proj_beta"]["project_id"]
async def test_search_ordered_by_user_email(
self,
group_db_source: GroupDBSource,
projects_with_users: dict[str, dict[str, Any]],
) -> None:
"""Order projects by user email (MIN aggregation)."""
querier = BatchQuerier(
pagination=OffsetPagination(limit=50, offset=0),
conditions=[],
orders=[GroupOrders.by_user_email(ascending=True)],
)
result = await group_db_source.search_projects(querier)
assert result.total_count == 2
# alice@example.com < bob@example.com
assert result.items[0].id == projects_with_users["proj_alpha"]["project_id"]
assert result.items[1].id == projects_with_users["proj_beta"]["project_id"]