109 lines
3.7 KiB
Python
109 lines
3.7 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
|
|
from sqlalchemy import create_engine, event
|
|
from sqlalchemy.orm import Session
|
|
|
|
from govoplan_access.backend.db.models import Account, Group, User
|
|
from govoplan_core.core.change_sequence import ChangeSequenceEntry
|
|
from govoplan_core.db.base import Base
|
|
from govoplan_files.backend.db.models import (
|
|
FileAsset,
|
|
FileConnectorCredential,
|
|
FileConnectorPolicy,
|
|
FileConnectorProfile,
|
|
FileConnectorSpace,
|
|
)
|
|
from govoplan_files.backend.manifest import _tenant_summary_batch
|
|
|
|
|
|
class FilesTenantSummaryBatchTests(unittest.TestCase):
|
|
def test_batch_summary_uses_one_grouped_query_per_owned_table(self) -> None:
|
|
engine = create_engine("sqlite+pysqlite:///:memory:")
|
|
Base.metadata.create_all(
|
|
engine,
|
|
tables=[
|
|
Account.__table__,
|
|
User.__table__,
|
|
Group.__table__,
|
|
FileAsset.__table__,
|
|
FileConnectorCredential.__table__,
|
|
FileConnectorPolicy.__table__,
|
|
FileConnectorProfile.__table__,
|
|
FileConnectorSpace.__table__,
|
|
ChangeSequenceEntry.__table__,
|
|
],
|
|
)
|
|
try:
|
|
with Session(engine) as session:
|
|
session.add_all(
|
|
[
|
|
FileAsset(
|
|
id="file-1",
|
|
tenant_id="tenant-1",
|
|
owner_type="tenant",
|
|
display_path="/one.txt",
|
|
filename="one.txt",
|
|
),
|
|
FileConnectorCredential(
|
|
id="credential-1",
|
|
tenant_id="tenant-1",
|
|
label="Credential",
|
|
),
|
|
FileConnectorPolicy(
|
|
id="policy-1",
|
|
tenant_id="tenant-1",
|
|
scope_type="tenant",
|
|
),
|
|
FileConnectorProfile(
|
|
id="profile-1",
|
|
tenant_id="tenant-1",
|
|
label="Profile",
|
|
provider="webdav",
|
|
),
|
|
FileConnectorSpace(
|
|
id="space-1",
|
|
tenant_id="tenant-1",
|
|
owner_type="tenant",
|
|
label="Space",
|
|
connector_profile_id="profile-1",
|
|
provider="webdav",
|
|
),
|
|
]
|
|
)
|
|
session.commit()
|
|
query_count = 0
|
|
|
|
def count_query(*_args: object) -> None:
|
|
nonlocal query_count
|
|
query_count += 1
|
|
|
|
event.listen(engine, "before_cursor_execute", count_query)
|
|
try:
|
|
counts = _tenant_summary_batch(
|
|
session,
|
|
["tenant-1", "tenant-empty"],
|
|
)
|
|
finally:
|
|
event.remove(engine, "before_cursor_execute", count_query)
|
|
|
|
self.assertEqual(5, query_count)
|
|
self.assertEqual(
|
|
{
|
|
"files": 1,
|
|
"connector_credentials": 1,
|
|
"connector_policies": 1,
|
|
"connector_profiles": 1,
|
|
"connector_spaces": 1,
|
|
},
|
|
counts["tenant-1"],
|
|
)
|
|
self.assertEqual(0, counts["tenant-empty"]["files"])
|
|
finally:
|
|
engine.dispose()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|