89 lines
3.4 KiB
Python
89 lines
3.4 KiB
Python
|
|
import asyncio
|
||
|
|
import importlib
|
||
|
|
from datetime import datetime
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
timetracking_router = importlib.import_module("app.timetracking.backend.router")
|
||
|
|
|
||
|
|
|
||
|
|
def test_manual_time_keeps_local_wall_clock_and_creates_one_entry_per_employee(monkeypatch):
|
||
|
|
captured = {}
|
||
|
|
|
||
|
|
monkeypatch.setattr(timetracking_router, "_resolve_case_customer_id", lambda *_args: 77)
|
||
|
|
|
||
|
|
def fake_execute_query(query, params):
|
||
|
|
captured["query"] = query
|
||
|
|
captured["params"] = params
|
||
|
|
employee_ids = params[-1]
|
||
|
|
return [{"id": index + 1, "medarbejder_id": employee_id} for index, employee_id in enumerate(employee_ids)]
|
||
|
|
|
||
|
|
monkeypatch.setattr(timetracking_router, "execute_query", fake_execute_query)
|
||
|
|
|
||
|
|
result = asyncio.run(
|
||
|
|
timetracking_router.create_manual_time_v1(
|
||
|
|
{
|
||
|
|
"sag_id": 88,
|
||
|
|
"medarbejder_ids": [12, 34],
|
||
|
|
"worked_date": "2026-08-18",
|
||
|
|
"start_tid": "2026-08-18T10:00:00",
|
||
|
|
"slut_tid": "2026-08-18T11:00:00",
|
||
|
|
"faktisk_tid_min": 60,
|
||
|
|
},
|
||
|
|
current_user={"id": 9, "username": "tester"},
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
assert result["count"] == 2
|
||
|
|
assert [row["medarbejder_id"] for row in result["created"]] == [12, 34]
|
||
|
|
assert captured["params"][-1] == [12, 34]
|
||
|
|
assert captured["params"][10] == datetime(2026, 8, 18, 10, 0)
|
||
|
|
assert captured["params"][11] == datetime(2026, 8, 18, 11, 0)
|
||
|
|
assert "UNNEST(%s::bigint[])" in captured["query"]
|
||
|
|
|
||
|
|
|
||
|
|
def test_manual_time_bulk_resolves_current_user_and_removes_duplicates(monkeypatch):
|
||
|
|
captured = {}
|
||
|
|
monkeypatch.setattr(timetracking_router, "_resolve_case_customer_id", lambda *_args: 77)
|
||
|
|
|
||
|
|
def fake_execute_query(_query, params):
|
||
|
|
captured["employee_ids"] = params[-1]
|
||
|
|
return [{"id": 1, "medarbejder_id": employee_id} for employee_id in params[-1]]
|
||
|
|
|
||
|
|
monkeypatch.setattr(timetracking_router, "execute_query", fake_execute_query)
|
||
|
|
|
||
|
|
result = asyncio.run(
|
||
|
|
timetracking_router.create_manual_time_v1(
|
||
|
|
{
|
||
|
|
"sag_id": 88,
|
||
|
|
"medarbejder_ids": [None, 9, 12, 12],
|
||
|
|
"faktisk_tid_min": 30,
|
||
|
|
},
|
||
|
|
current_user={"id": 9, "username": "tester"},
|
||
|
|
)
|
||
|
|
)
|
||
|
|
|
||
|
|
assert captured["employee_ids"] == [9, 12]
|
||
|
|
assert result["count"] == 2
|
||
|
|
|
||
|
|
|
||
|
|
def test_case_time_form_sends_local_time_and_multiple_employees():
|
||
|
|
template = Path("app/modules/sag/templates/detail_v3.html").read_text()
|
||
|
|
manual_form_script = template.split("async function createManualTimeV1(event)", 1)[1].split(
|
||
|
|
"document.addEventListener('DOMContentLoaded'", 1
|
||
|
|
)[0]
|
||
|
|
|
||
|
|
assert 'class="form-check-input mt-0 time-v1-employee-option"' in template
|
||
|
|
assert "{% for user in assignment_users %}" in template
|
||
|
|
assert "loadTimeV1EmployeeOptions" not in template
|
||
|
|
assert "medarbejder_ids: employeeIds" in manual_form_script
|
||
|
|
assert "startObj = `${dateVal}T${tStart}:00`;" in manual_form_script
|
||
|
|
assert "endObj = `${endDate}T${tEnd}:00`;" in manual_form_script
|
||
|
|
assert ".toISOString()" not in manual_form_script
|
||
|
|
|
||
|
|
quick_time_script = template.split("async function registerQuickTimeFromComment(content)", 1)[1].split(
|
||
|
|
"function resolveCommentAwaitCustomerStatus", 1
|
||
|
|
)[0]
|
||
|
|
assert "startIso = `${dateValue}T${startValue}:00`;" in quick_time_script
|
||
|
|
assert "endIso = addMinutesToTimeV1LocalIso(startIso, minutes);" in quick_time_script
|
||
|
|
assert ".toISOString()" not in quick_time_script
|