89 lines
3.0 KiB
Python
89 lines
3.0 KiB
Python
from datetime import datetime, timezone
|
|
|
|
from app.models import Employee
|
|
from app.services.dataset_versions import get_or_create_current_version, sync_employees_payload
|
|
|
|
|
|
def _employee(profile_key: str, checksum: str, *, status: str = "active") -> Employee:
|
|
return Employee(
|
|
profile_key=profile_key,
|
|
profile_type=profile_key.split(":", 1)[0],
|
|
profile_id=profile_key.split(":", 1)[1],
|
|
canonical_url=f"https://www.hse.ru/{profile_key}",
|
|
full_name=profile_key,
|
|
status=status,
|
|
first_seen_at=datetime.now(timezone.utc),
|
|
last_seen_at=datetime.now(timezone.utc),
|
|
current_data={"profile_key": profile_key},
|
|
current_checksum=checksum,
|
|
)
|
|
|
|
|
|
def test_dataset_version_hash_is_stable_for_same_employee_state(db_session):
|
|
db_session.add(_employee("staff:alpha", "a" * 64))
|
|
db_session.commit()
|
|
|
|
first = get_or_create_current_version(db_session)
|
|
db_session.commit()
|
|
second = get_or_create_current_version(db_session)
|
|
|
|
assert second.id == first.id
|
|
assert second.hash == first.hash
|
|
assert second.employee_count == 1
|
|
|
|
|
|
def test_dataset_version_hash_changes_when_employee_checksum_changes(db_session):
|
|
employee = _employee("staff:alpha", "a" * 64)
|
|
db_session.add(employee)
|
|
db_session.commit()
|
|
first = get_or_create_current_version(db_session)
|
|
db_session.commit()
|
|
|
|
employee.current_checksum = "b" * 64
|
|
db_session.commit()
|
|
second = get_or_create_current_version(db_session)
|
|
|
|
assert second.hash != first.hash
|
|
assert second.previous_hash == first.hash
|
|
|
|
|
|
def test_sync_employees_diff_spans_multiple_intermediate_versions(db_session):
|
|
alpha = _employee("staff:alpha", "a" * 64)
|
|
db_session.add(alpha)
|
|
db_session.commit()
|
|
first = get_or_create_current_version(db_session)
|
|
db_session.commit()
|
|
|
|
beta = _employee("staff:beta", "b" * 64)
|
|
db_session.add(beta)
|
|
db_session.commit()
|
|
get_or_create_current_version(db_session)
|
|
db_session.commit()
|
|
|
|
alpha.current_checksum = "c" * 64
|
|
alpha.current_data = {"profile_key": "staff:alpha", "changed": True}
|
|
db_session.commit()
|
|
|
|
payload = sync_employees_payload(db_session, client_hash=first.hash, include_data=False)
|
|
|
|
assert payload["mode"] == "delta"
|
|
assert [item["profile_key"] for item in payload["changes"]["added"]] == ["staff:beta"]
|
|
assert [item["profile_key"] for item in payload["changes"]["updated"]] == ["staff:alpha"]
|
|
assert payload["changes"]["dismissed"] == []
|
|
assert payload["changes"]["removed"] == []
|
|
|
|
|
|
def test_sync_employees_reports_dismissed_as_tombstone(db_session):
|
|
alpha = _employee("staff:alpha", "a" * 64)
|
|
db_session.add(alpha)
|
|
db_session.commit()
|
|
first = get_or_create_current_version(db_session)
|
|
db_session.commit()
|
|
|
|
alpha.status = "dismissed"
|
|
db_session.commit()
|
|
payload = sync_employees_payload(db_session, client_hash=first.hash, include_data=False)
|
|
|
|
assert payload["changes"]["dismissed"][0]["profile_key"] == "staff:alpha"
|
|
assert payload["changes"]["dismissed"][0]["status"] == "dismissed"
|