Files
toiminnanohjaus-django/apps/jobs/tests/fakes.py
T
petriandClaude Opus 5.5 1ca03137fd Vaihe 1/4: asiakastyöt (numerointi, tagit, linkit, jäsenet, muutoshistoria)
- Uusi jobs-moduuli: asiakastyön kentät ilman rahakenttiä, juokseva
  vuosinumero VVVV-NNNN (laskuririvi lukitaan), tagit, linkit dialogissa,
  jäsenet ja muutoshistoria nimiarvoineen.
- Tilojen oikeudet: lasku luotu / laskutettu johto tai työn laskuttaja,
  arkistointi vain johto; alihankkija näkee vain jäsenyystyönsä eikä muokkaa.
- Lista ja haku (numero, nimi, asiakas), suodattimet, omat työt oletuksena,
  arkistoidut piilossa, myöhässä tekstinä ja kuvakkeena.
- Asiakasvalitsin hakuineen ja pikalisäyksineen (myös pikavastaanottoa varten).
- Asiakkaan sivulle asiakastyöt-osio; valikkoon Asiakastyöt.
- Lukurajapinnat muihin moduuleihin: UserQueries.choices,
  CustomerQueries.summaries, CatalogQueries.service_units; jobs.*-oikeudet.
- Import-linter-sopimukset, käännökset, testit (domain, application,
  integraatio, selain) sekä määrittelyn ja CLAUDE.md:n päivitys.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 14:38:42 +02:00

165 lines
5.4 KiB
Python

"""In-memory test doubles for the jobs application layer (no database)."""
from collections.abc import Iterable, Sequence
from dataclasses import dataclass, field
from datetime import date
from apps.catalog.application.dto import ServiceUnitSummary
from apps.customers.application.dto import CustomerSummary
from apps.customers.domain.enums import CustomerType
from apps.jobs.domain.entities import ChangeRecord, CustomerJob, JobLink
from apps.jobs.domain.value_objects import JobNumber
from apps.users.application.dto import UserChoice
from apps.users.domain.enums import Role
class InMemoryJobRepository:
def __init__(self) -> None:
self.jobs: dict[int, CustomerJob] = {}
self.counters: dict[int, int] = {}
self.changes: list[tuple[int, int, ChangeRecord]] = []
self._next = 1
def get(self, job_id: int) -> CustomerJob | None:
return self.jobs.get(job_id)
def save(self, job: CustomerJob) -> CustomerJob:
if job.id is None:
job = job.with_id(self._next)
self._next += 1
assert job.id is not None
self.jobs[job.id] = job
return job
def next_number(self, year: int) -> JobNumber:
self.counters[year] = self.counters.get(year, 0) + 1
return JobNumber(year, self.counters[year])
def record_changes(self, job_id: int, user_id: int, changes: Sequence[ChangeRecord]) -> None:
self.changes.extend((job_id, user_id, c) for c in changes)
def change_rows(self) -> list[tuple[str, str, str]]:
return [(c.field, c.old_value, c.new_value) for _, _, c in self.changes]
class InMemoryLinkRepository:
def __init__(self) -> None:
self.links: dict[int, JobLink] = {}
self._next = 1
def get(self, link_id: int) -> JobLink | None:
return self.links.get(link_id)
def save(self, link: JobLink) -> JobLink:
if link.id is None:
link = link.with_id(self._next)
self._next += 1
assert link.id is not None
self.links[link.id] = link
return link
def delete(self, link_id: int) -> None:
del self.links[link_id]
@dataclass(frozen=True)
class _Row:
id: int
name: str
@dataclass
class FakeCustomerQueries:
"""Customers with their contacts and billing units, as the customers module shows them."""
customers: dict[int, str] = field(default_factory=dict)
contacts_by_customer: dict[int, list[_Row]] = field(default_factory=dict)
units_by_customer: dict[int, list[_Row]] = field(default_factory=dict)
def add(
self,
customer_id: int,
name: str,
*,
contacts: Iterable[tuple[int, str]] = (),
units: Iterable[tuple[int, str]] = (),
) -> None:
self.customers[customer_id] = name
self.contacts_by_customer[customer_id] = [_Row(i, n) for i, n in contacts]
self.units_by_customer[customer_id] = [_Row(i, n) for i, n in units]
def _summary(self, customer_id: int) -> CustomerSummary:
return CustomerSummary(
id=customer_id,
name=self.customers[customer_id],
type=CustomerType.COMPANY,
business_id=None,
is_internal=False,
)
def get_summary(self, customer_id: int) -> CustomerSummary | None:
return self._summary(customer_id) if customer_id in self.customers else None
def summaries(self, customer_ids: Iterable[int]) -> dict[int, CustomerSummary]:
return {i: self._summary(i) for i in customer_ids if i in self.customers}
def contacts(self, customer_id: int) -> list[_Row]:
return self.contacts_by_customer.get(customer_id, [])
def billing_units(self, customer_id: int) -> list[_Row]:
return self.units_by_customer.get(customer_id, [])
@dataclass
class FakeUserQueries:
users: dict[int, UserChoice] = field(default_factory=dict)
def add(self, user_id: int, name: str, role: Role = Role.EMPLOYEE, active: bool = True) -> None:
self.users[user_id] = UserChoice(id=user_id, name=name, role=role, is_active=active)
def choices(
self,
ids: Iterable[int] | None = None,
*,
active_only: bool = False,
exclude_roles: Iterable[Role] = (),
) -> list[UserChoice]:
wanted = set(ids) if ids is not None else None
excluded = set(exclude_roles)
return sorted(
(
u
for u in self.users.values()
if (wanted is None or u.id in wanted)
and (u.is_active or not active_only)
and u.role not in excluded
),
key=lambda u: u.name,
)
@dataclass
class FakeCatalogQueries:
units: list[tuple[ServiceUnitSummary, bool]] = field(default_factory=list) # (unit, active)
def add(self, unit_id: int, name: str, *, shared: bool = False, active: bool = True) -> None:
self.units.append((ServiceUnitSummary(id=unit_id, name=name, is_shared=shared), active))
def active_service_units(self) -> list[ServiceUnitSummary]:
return [u for u, active in self.units if active]
def service_units(self, ids: Iterable[int]) -> list[ServiceUnitSummary]:
wanted = set(ids)
return [u for u, _ in self.units if u.id in wanted]
class FixedClock:
def __init__(self, today: date) -> None:
self._today = today
def today(self) -> date:
return self._today
def set(self, today: date) -> None:
self._today = today