fixes and refactor
This commit is contained in:
80
tests/test_customers_search_props.py
Normal file
80
tests/test_customers_search_props.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import pytest
|
||||
|
||||
try:
|
||||
from hypothesis import given, strategies as st, settings
|
||||
except Exception: # pragma: no cover
|
||||
pytest.skip("Hypothesis not installed; skipping property-based tests.", allow_module_level=True)
|
||||
|
||||
from app.services.customers_search import apply_customer_filters, apply_customer_sorting
|
||||
|
||||
|
||||
class FakeQuery:
|
||||
def __init__(self):
|
||||
self.filters = []
|
||||
self.orderings = []
|
||||
|
||||
def filter(self, *args):
|
||||
self.filters.extend(args)
|
||||
return self
|
||||
|
||||
def order_by(self, *args):
|
||||
self.orderings.extend(args)
|
||||
return self
|
||||
|
||||
|
||||
def _expected_filter_count(search, group, groups, state, states):
|
||||
s = (search or "").strip()
|
||||
search_filter = 1 if s else 0
|
||||
|
||||
eff_groups = [g for g in (groups or []) if g] or ([group] if group else [])
|
||||
groups_filter = 1 if eff_groups else 0
|
||||
|
||||
eff_states = [s for s in (states or []) if s] or ([state] if state else [])
|
||||
states_filter = 1 if eff_states else 0
|
||||
|
||||
return search_filter + groups_filter + states_filter
|
||||
|
||||
|
||||
@settings(deadline=None, max_examples=100)
|
||||
@given(
|
||||
search=st.text(min_size=0, max_size=200),
|
||||
group=st.one_of(st.none(), st.text(min_size=0, max_size=20)),
|
||||
state=st.one_of(st.none(), st.text(min_size=0, max_size=10)),
|
||||
groups=st.one_of(
|
||||
st.none(),
|
||||
st.lists(st.one_of(st.none(), st.text(min_size=0, max_size=10)), max_size=5),
|
||||
),
|
||||
states=st.one_of(
|
||||
st.none(),
|
||||
st.lists(st.one_of(st.none(), st.text(min_size=0, max_size=10)), max_size=5),
|
||||
),
|
||||
)
|
||||
def test_apply_customer_filters_property(search, group, groups, state, states):
|
||||
q = FakeQuery()
|
||||
q = apply_customer_filters(q, search=search, group=group, state=state, groups=groups, states=states)
|
||||
|
||||
assert len(q.filters) == _expected_filter_count(search, group, groups, state, states)
|
||||
|
||||
|
||||
@settings(deadline=None, max_examples=100)
|
||||
@given(
|
||||
sort_by=st.one_of(
|
||||
st.none(),
|
||||
st.sampled_from(["id", "name", "city", "email", "ID", "NAME", "CITY", "EMAIL"]),
|
||||
st.text(min_size=0, max_size=15),
|
||||
),
|
||||
sort_dir=st.one_of(
|
||||
st.none(),
|
||||
st.sampled_from(["asc", "ASC", "desc", "DESC", ""]),
|
||||
st.text(min_size=0, max_size=10),
|
||||
),
|
||||
)
|
||||
def test_apply_customer_sorting_property(sort_by, sort_dir):
|
||||
q = FakeQuery()
|
||||
q = apply_customer_sorting(q, sort_by=sort_by, sort_dir=sort_dir)
|
||||
|
||||
sb = (sort_by or "id").lower()
|
||||
expected_order_cols = 2 if sb == "name" else 1
|
||||
assert len(q.orderings) == expected_order_cols
|
||||
|
||||
|
||||
Reference in New Issue
Block a user