Coverage for src/lexigram/admin/data/filter_specs.py: 0%

32 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-24 23:18 +0800

1"""Filter specification types for admin data queries. 

2 

3Composable via ``&`` into :class:`CombinedSpec` trees consumed by data 

4sources that support structured where-clauses. 

5""" 

6 

7from __future__ import annotations 

8 

9from typing import Any, Protocol 

10 

11# ============================================================================ 

12# Filter SpecificationProtocol Types 

13# ============================================================================ 

14 

15 

16class EqualSpec: 

17 """Filter specification for exact equality: column == value.""" 

18 

19 def __init__(self, field: str, value: object) -> None: 

20 """Initialize EqualSpec with field name and value to match.""" 

21 self.field = field 

22 self.value = value 

23 

24 def __and__(self, other: FilterSpec) -> CombinedSpec: 

25 """Combine with another spec using AND.""" 

26 return CombinedSpec(specs=[self, other]) 

27 

28 

29class InSpec: 

30 """Filter specification for IN query: column IN values.""" 

31 

32 def __init__(self, field: str, values: list[object]) -> None: 

33 """Initialize InSpec with field name and list of values.""" 

34 self.field = field 

35 self.values = values 

36 

37 def __and__(self, other: FilterSpec) -> CombinedSpec: 

38 """Combine with another spec using AND.""" 

39 return CombinedSpec(specs=[self, other]) 

40 

41 

42class GreaterThanOrEqualSpec: 

43 """Filter specification for >= comparison: column >= value.""" 

44 

45 def __init__(self, field: str, value: object) -> None: 

46 """Initialize GreaterThanOrEqualSpec with field name and value.""" 

47 self.field = field 

48 self.value = value 

49 

50 def __and__(self, other: FilterSpec) -> CombinedSpec: 

51 """Combine with another spec using AND.""" 

52 return CombinedSpec(specs=[self, other]) 

53 

54 

55class LessThanOrEqualSpec: 

56 """Filter specification for <= comparison: column <= value.""" 

57 

58 def __init__(self, field: str, value: object) -> None: 

59 """Initialize LessThanOrEqualSpec with field name and value.""" 

60 self.field = field 

61 self.value = value 

62 

63 def __and__(self, other: FilterSpec) -> CombinedSpec: 

64 """Combine with another spec using AND.""" 

65 return CombinedSpec(specs=[self, other]) 

66 

67 

68FilterSpec = EqualSpec | InSpec | GreaterThanOrEqualSpec | LessThanOrEqualSpec 

69 

70 

71class CombinedSpec: 

72 """Combined filter specification (AND of multiple specs).""" 

73 

74 def __init__(self, specs: list[FilterSpec]) -> None: 

75 """Initialize CombinedSpec with list of filter specs.""" 

76 self.specs = specs 

77 

78 def __and__(self, other: FilterSpec) -> CombinedSpec: 

79 """Combine with another spec using AND.""" 

80 return CombinedSpec(specs=[*self.specs, other])