File size: 4,647 Bytes
2cc63cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Guard against CREATE TABLE / INSERT column mismatches — catch them in-memory,
at validate_bundle() time, instead of ~10 minutes later as a cryptic Snowflake
'invalid identifier' error deep inside an executemany (see the Gap e2e failure:
FACT_CAMPAIGN_PERFORMANCE's row loader referenced a CHECKOUT_INITIATED column
CREATE TABLE never declared).
"""

from __future__ import annotations

from demoprep_app.dataset.contracts import DatasetBundle, DatasetColumn, DatasetTable
from demoprep_app.dataset.validator import ValidationReport, _check_schema_consistency
from demoprep_app.scenario.contract import ScenarioContract


def _scenario() -> ScenarioContract:
    return ScenarioContract(
        company_name="Acme",
        company_url="https://example.com",
        use_case="Marketing Funnel",
        scenario_type="campaign_performance",
        fact_grain="campaign-day",
    )


def test_schema_consistency_passes_for_a_clean_bundle():
    bundle = DatasetBundle(
        scenario=_scenario(),
        tables=[
            DatasetTable(
                name="FACT_CAMPAIGN_PERFORMANCE",
                grain="campaign-day",
                columns=[
                    DatasetColumn("FACT_CAMPAIGN_PERFORMANCE_KEY", "fact_key", "NUMBER", nullable=False),
                    DatasetColumn("IMPRESSIONS", "impressions", "NUMBER", nullable=False),
                    DatasetColumn("CLICKS", "clicks", "NUMBER", nullable=False),
                ],
                rows=[{"FACT_CAMPAIGN_PERFORMANCE_KEY": 1, "IMPRESSIONS": 100, "CLICKS": 5}],
                is_fact=True,
            )
        ],
    )
    report = ValidationReport()

    _check_schema_consistency(bundle, report)

    assert report.integrity_errors == []


def test_schema_consistency_catches_a_row_key_with_no_declared_column():
    # Mirrors the Gap failure: the row loader would INSERT a column
    # (CHECKOUT_INITIATED) that CREATE TABLE never declared for this table.
    bundle = DatasetBundle(
        scenario=_scenario(),
        tables=[
            DatasetTable(
                name="FACT_CAMPAIGN_PERFORMANCE",
                grain="campaign-day",
                columns=[
                    DatasetColumn("FACT_CAMPAIGN_PERFORMANCE_KEY", "fact_key", "NUMBER", nullable=False),
                    DatasetColumn("ADD_TO_CART", "add_to_cart", "NUMBER", nullable=False),
                    DatasetColumn("CONVERSIONS", "conversions", "NUMBER", nullable=False),
                ],
                rows=[
                    {
                        "FACT_CAMPAIGN_PERFORMANCE_KEY": 1,
                        "ADD_TO_CART": 10,
                        "CONVERSIONS": 2,
                        "CHECKOUT_INITIATED": 6,  # not a declared column
                    }
                ],
                is_fact=True,
            )
        ],
    )
    report = ValidationReport()

    _check_schema_consistency(bundle, report)

    assert len(report.integrity_errors) == 1
    assert "FACT_CAMPAIGN_PERFORMANCE" in report.integrity_errors[0]
    assert "CHECKOUT_INITIATED" in report.integrity_errors[0]


def test_schema_consistency_catches_column_names_that_collide_after_normalization():
    # Two distinct Python-level names that normalize to the same SQL identifier
    # would make CREATE TABLE emit a duplicate column.
    bundle = DatasetBundle(
        scenario=_scenario(),
        tables=[
            DatasetTable(
                name="FACT_CAMPAIGN_PERFORMANCE",
                grain="campaign-day",
                columns=[
                    DatasetColumn("Checkout Initiated", "checkout_initiated", "NUMBER", nullable=False),
                    DatasetColumn("Checkout-Initiated", "checkout_initiated_2", "NUMBER", nullable=False),
                ],
                rows=[{"Checkout Initiated": 1, "Checkout-Initiated": 2}],
                is_fact=True,
            )
        ],
    )
    report = ValidationReport()

    _check_schema_consistency(bundle, report)

    assert len(report.integrity_errors) == 1
    assert "CHECKOUT_INITIATED" in report.integrity_errors[0]
    assert "duplicate column" in report.integrity_errors[0]


def test_schema_consistency_ignores_empty_tables():
    bundle = DatasetBundle(
        scenario=_scenario(),
        tables=[
            DatasetTable(
                name="EMPTY_DIM",
                grain="empty",
                columns=[DatasetColumn("EMPTY_DIM_KEY", "dimension_key", "NUMBER", nullable=False)],
                rows=[],
            )
        ],
    )
    report = ValidationReport()

    _check_schema_consistency(bundle, report)

    assert report.integrity_errors == []