-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_raw_loader_exporter.py
More file actions
195 lines (123 loc) · 5.12 KB
/
test_raw_loader_exporter.py
File metadata and controls
195 lines (123 loc) · 5.12 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# =============================================================================
# UNIT TESTS FOR raw_loader_exporter.py
# =============================================================================
import pandas as pd
import pytest
from data_pipeline.shared.raw_loader_exporter import (
FILE_LOADERS,
load_logical_table,
export_file,
)
# ------------------------------------------------------------
# FIXTURES (SHARED TEST DATA)
# ------------------------------------------------------------
@pytest.fixture
def valid_customers_df():
return pd.DataFrame(
{
"customer_id": [1, 2],
"customer_zip_code_prefix": [
"zip1",
"zip2",
],
"customer_city": ["city1", "city2"],
"customer_state": ["state1", "state2"],
}
)
# ------------------------------------------------------------
# FILE LOADER
# ------------------------------------------------------------
def test_load_logical_table_success(tmp_path, valid_customers_df):
log = {"info": [], "error": []}
# logs info for successful file(s) reading
def info(msg):
log["info"].append(msg)
def error(msg):
log["error"].append(msg)
valid_customers_df.to_csv(tmp_path / "df_customers_2026_01.csv", index=False)
df = load_logical_table(tmp_path, "df_customers", log_info=info, log_error=error)
assert df is not None
assert len(df) == len(valid_customers_df)
assert len(log["info"]) == 1
assert len(log["error"]) == 0
def test_load_logical_table_concat(tmp_path, valid_customers_df):
valid_customers_df.to_csv(tmp_path / "df_customers_2026_01.csv", index=False)
valid_customers_df.to_csv(tmp_path / "df_customers_2026_02.csv", index=False)
df_concat = load_logical_table(tmp_path, "df_customers")
assert df_concat is not None
assert len(df_concat) == len(valid_customers_df) * 2
def test_load_logical_table_no_files_found(tmp_path):
log = {"info": [], "error": []}
# logs error for no file(s) found
def error(msg):
log["error"].append(msg)
def info(msg):
log["info"].append(msg)
df = load_logical_table(tmp_path, "df_customers", log_info=info, log_error=error)
assert df is None
assert len(log["info"]) == 0
assert len(log["error"]) == 1
def test_load_logical_table_fails_on_mixed_file_format(tmp_path, valid_customers_df):
valid_customers_df.to_csv(tmp_path / "df_customers_2026_01.csv", index=False)
valid_customers_df.to_parquet(
tmp_path / "df_customers_2026_02.parquet", index=False
)
with pytest.raises(RuntimeError):
load_logical_table(tmp_path, "df_customers")
def test_load_logical_table_all_matching_files_fails_to_load(tmp_path, monkeypatch):
def fake_loader(path):
raise ValueError("failed")
monkeypatch.setitem(FILE_LOADERS, ".csv", fake_loader)
(tmp_path / "df_customers_2026_01.csv").write_text("anything")
df = load_logical_table(tmp_path, "df_customers")
assert df is None
# ------------------------------------------------------------
# FILE EXPORTER
# ------------------------------------------------------------
def test_export_file_csv_success(tmp_path, valid_customers_df):
log = {"info": [], "error": []}
# logs info for successful file(s) export
def info(msg):
log["info"].append(msg)
def error(msg):
log["error"].append(msg)
output_path = tmp_path / "out" / "df_customers.csv"
ok = export_file(valid_customers_df, output_path, log_info=info, log_error=error)
assert ok is True
assert output_path.exists()
assert len(log["info"]) == 1
assert len(log["error"]) == 0
def test_export_file_parquet_success(tmp_path, valid_customers_df):
log = {"info": [], "error": []}
# logs info for successful file(s) export
def info(msg):
log["info"].append(msg)
def error(msg):
log["error"].append(msg)
output_path = tmp_path / "out" / "df_customers.csv"
ok = export_file(valid_customers_df, output_path, log_info=info, log_error=error)
assert ok is True
assert output_path.exists()
assert len(log["info"]) == 1
assert len(log["error"]) == 0
def test_export_file_fails_on_unsupported_extension(tmp_path, valid_customers_df):
log = {"info": [], "error": []}
# logs error for unsupported extension
def info(msg):
log["info"].append(msg)
def error(msg):
log["error"].append(msg)
output_path = tmp_path / "df_customers.txt"
ok = export_file(valid_customers_df, output_path, log_info=info, log_error=error)
assert ok is False
assert output_path.exists() is False
assert len(log["info"]) == 0
assert len(log["error"]) == 1
def test_export_file_creates_parent_directory(tmp_path, valid_customers_df):
nested_path = tmp_path / "nested" / "folder" / "file.csv"
ok = export_file(valid_customers_df, nested_path)
assert ok is True
assert nested_path.exists()
# =============================================================================
# UNIT TESTS END
# =============================================================================