|
| 1 | +"""Tests for apply_chart_filters — timestamp day-range filter handling""" |
| 2 | + |
| 3 | +import os |
| 4 | +import django |
| 5 | + |
| 6 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ddpui.settings") |
| 7 | +os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" |
| 8 | +django.setup() |
| 9 | + |
| 10 | +import pytest |
| 11 | +from ddpui.core.charts.charts_service import apply_chart_filters |
| 12 | +from ddpui.core.datainsights.query_builder import AggQueryBuilder |
| 13 | + |
| 14 | +pytestmark = pytest.mark.django_db |
| 15 | + |
| 16 | + |
| 17 | +def make_filter(col, operator, value, data_type="varchar"): |
| 18 | + return {"column": col, "operator": operator, "value": value, "data_type": data_type} |
| 19 | + |
| 20 | + |
| 21 | +def get_where_sql(filters): |
| 22 | + """Apply filters and return compiled WHERE clauses as strings.""" |
| 23 | + qb = AggQueryBuilder() |
| 24 | + apply_chart_filters(qb, filters) |
| 25 | + return [ |
| 26 | + str(clause.compile(compile_kwargs={"literal_binds": True})) for clause in qb.where_clauses |
| 27 | + ] |
| 28 | + |
| 29 | + |
| 30 | +class TestApplyChartFilters: |
| 31 | + def test_equals_timestamp_generates_day_range(self): |
| 32 | + """timestamp equals must match full day using >= start AND < next day""" |
| 33 | + sql = get_where_sql([make_filter("created_at", "equals", "2026-06-15", "timestamp")]) |
| 34 | + assert len(sql) == 1 |
| 35 | + assert "2026-06-15" in sql[0] |
| 36 | + assert "2026-06-16" in sql[0] |
| 37 | + |
| 38 | + def test_not_equals_timestamp_excludes_full_day(self): |
| 39 | + """timestamp not_equals must exclude entire day using OR range""" |
| 40 | + sql = get_where_sql([make_filter("created_at", "not_equals", "2026-06-15", "timestamp")]) |
| 41 | + assert len(sql) == 1 |
| 42 | + assert "2026-06-15" in sql[0] |
| 43 | + assert "2026-06-16" in sql[0] |
| 44 | + |
| 45 | + def test_greater_than_timestamp_starts_from_next_day(self): |
| 46 | + """timestamp greater_than must start from next day to exclude the selected day""" |
| 47 | + sql = get_where_sql([make_filter("created_at", "greater_than", "2026-06-15", "timestamp")]) |
| 48 | + assert len(sql) == 1 |
| 49 | + assert "2026-06-16" in sql[0] |
| 50 | + |
| 51 | + def test_less_than_timestamp_no_shift_needed(self): |
| 52 | + """timestamp less_than works correctly — midnight is already the right boundary""" |
| 53 | + sql = get_where_sql([make_filter("created_at", "less_than", "2026-06-15", "timestamp")]) |
| 54 | + assert len(sql) == 1 |
| 55 | + assert "2026-06-15" in sql[0] |
| 56 | + assert "2026-06-16" not in sql[0] |
| 57 | + |
| 58 | + def test_greater_than_equal_timestamp_no_shift_needed(self): |
| 59 | + """timestamp greater_than_equal works correctly from start of selected day""" |
| 60 | + sql = get_where_sql( |
| 61 | + [make_filter("created_at", "greater_than_equal", "2026-06-15", "timestamp")] |
| 62 | + ) |
| 63 | + assert len(sql) == 1 |
| 64 | + assert "2026-06-15" in sql[0] |
| 65 | + assert "2026-06-16" not in sql[0] |
| 66 | + |
| 67 | + def test_less_than_equal_timestamp_includes_full_day(self): |
| 68 | + """timestamp less_than_equal must shift to next day to include entire selected day""" |
| 69 | + sql = get_where_sql( |
| 70 | + [make_filter("created_at", "less_than_equal", "2026-06-15", "timestamp")] |
| 71 | + ) |
| 72 | + assert len(sql) == 1 |
| 73 | + assert "2026-06-16" in sql[0] |
| 74 | + |
| 75 | + def test_non_timestamp_column_unaffected(self): |
| 76 | + """date-only column uses simple equality — no range logic applied""" |
| 77 | + sql = get_where_sql([make_filter("birth_date", "equals", "2026-06-15", "date")]) |
| 78 | + assert len(sql) == 1 |
| 79 | + assert "2026-06-16" not in sql[0] |
| 80 | + |
| 81 | + def test_multiple_equals_same_column_grouped(self): |
| 82 | + """multiple equals on same non-timestamp column merged into one OR clause""" |
| 83 | + filters = [ |
| 84 | + make_filter("status", "equals", "active"), |
| 85 | + make_filter("status", "equals", "pending"), |
| 86 | + ] |
| 87 | + sql = get_where_sql(filters) |
| 88 | + assert len(sql) == 1 |
| 89 | + assert "active" in sql[0] |
| 90 | + assert "pending" in sql[0] |
| 91 | + |
| 92 | + def test_timestamp_equals_not_grouped(self): |
| 93 | + """timestamp equals filters are never grouped — each gets its own range clause""" |
| 94 | + filters = [ |
| 95 | + make_filter("created_at", "equals", "2026-06-15", "timestamp"), |
| 96 | + make_filter("created_at", "equals", "2026-06-16", "timestamp"), |
| 97 | + ] |
| 98 | + sql = get_where_sql(filters) |
| 99 | + assert len(sql) == 2 |
| 100 | + |
| 101 | + def test_timestamptz_also_uses_range(self): |
| 102 | + """timestamptz and datetime columns also use day-range logic""" |
| 103 | + for dtype in ["timestamptz", "datetime", "timestamp with time zone"]: |
| 104 | + sql = get_where_sql([make_filter("created_at", "equals", "2026-06-15", dtype)]) |
| 105 | + assert len(sql) == 1 |
| 106 | + assert "2026-06-16" in sql[0], f"Failed for data_type={dtype}" |
| 107 | + |
| 108 | + def test_null_operators_unaffected(self): |
| 109 | + """is_null and is_not_null work the same for all column types""" |
| 110 | + for operator in ["is_null", "is_not_null"]: |
| 111 | + sql = get_where_sql([make_filter("created_at", operator, "", "timestamp")]) |
| 112 | + assert len(sql) == 1 |
0 commit comments