-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_run_pipeline.py
More file actions
180 lines (138 loc) · 4.94 KB
/
test_run_pipeline.py
File metadata and controls
180 lines (138 loc) · 4.94 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# =============================================================================
# UNIT TESTS FOR run_pipeline.py
# =============================================================================
from data_pipeline.shared.run_context import RunContext
from data_pipeline.run_pipeline import snapshot_raw, main
import pytest
def test_snapshot_raw_raises_when_source_missing(tmp_path):
ctx = RunContext.create(base_path=tmp_path, run_id="x")
with pytest.raises(FileNotFoundError):
snapshot_raw(ctx)
def test_main_exits_on_validation_1_errors(monkeypatch, tmp_path):
fake_ctx = RunContext.create(base_path=tmp_path, run_id="x")
monkeypatch.setattr(
"data_pipeline.run_pipeline.RunContext.create",
lambda: fake_ctx,
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.apply_validation",
lambda *args, **kwargs: {
"errors": ["boom"], # force to fail on validation_1
"warnings": [],
},
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.snapshot_raw",
lambda *_: None,
)
with pytest.raises(SystemExit) as e:
main()
assert e.value.code == 1
assert (fake_ctx.logs_path / "validation_initial.json").exists()
def test_main_exits_on_validation_2_issues(monkeypatch, tmp_path):
fake_ctx = RunContext.create(base_path=tmp_path, run_id="x")
monkeypatch.setattr(
"data_pipeline.run_pipeline.RunContext.create",
lambda: fake_ctx,
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.TABLE_CONFIG",
{"dummy": {}},
)
# execution count
calls = {"count": 0}
def fake_validation(*args, **kwargs):
calls["count"] += 1
if calls["count"] == 1:
return {"errors": [], "warnings": []} # force to pass on validation_1
return {"errors": [], "warnings": ["warn"]} # fail on validation_2
monkeypatch.setattr(
"data_pipeline.run_pipeline.apply_validation",
fake_validation,
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.apply_contract",
lambda *a, **k: ({}, set()),
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.assemble_events",
lambda *a, **k: {"status": "success", "error": [], "info": []},
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.snapshot_raw",
lambda *_: None,
)
with pytest.raises(SystemExit) as e:
main()
assert e.value.code == 1
assert (fake_ctx.logs_path / "validation_post_contract.json").exists()
def test_main_success(monkeypatch, tmp_path):
fake_ctx = RunContext.create(base_path=tmp_path, run_id="x")
monkeypatch.setattr(
"data_pipeline.run_pipeline.RunContext.create",
lambda: fake_ctx,
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.TABLE_CONFIG",
{"dummy": {}},
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.apply_validation",
lambda *a, **k: {
"errors": [],
"warnings": [],
}, # Pass all validations
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.apply_contract",
lambda *a, **k: ({}, set()),
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.assemble_events",
lambda *a, **k: {
"status": "success",
"error": [],
"info": [],
}, # Pass, status success
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.build_semantic_layer",
lambda *a, **k: {
"status": "success",
"error": [],
"info": [],
}, # Pass, status success
)
monkeypatch.setattr(
"data_pipeline.run_pipeline.run_integrity_gate",
lambda *a, **k: {
"status": "success",
"errors": [],
"info": [],
}, # Pass, status success
)
# monkeypatch.setattr(
# "data_pipeline.run_pipeline.promote_semantic_version",
# lambda *a, **k: {
# "status": "success",
# "errors": [],
# "info": [],
# }, # Pass, status success
# )
monkeypatch.setattr(
"data_pipeline.run_pipeline.snapshot_raw",
lambda *_: None,
)
with pytest.raises(SystemExit) as e:
main()
assert e.value.code == 0
assert (fake_ctx.logs_path / "validation_initial.json").exists()
assert (fake_ctx.logs_path / "contract_report.json").exists()
assert (fake_ctx.logs_path / "validation_post_contract.json").exists()
assert (fake_ctx.logs_path / "assemble_report.json").exists()
assert (fake_ctx.logs_path / "semantic_report.json").exists()
assert (fake_ctx.logs_path / "publish_integrity_report.json").exists()
assert (fake_ctx.logs_path / "publish_promotion_report.json").exists()
# =============================================================================
# UNIT TESTS END
# =============================================================================