-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_models.py
More file actions
336 lines (302 loc) · 10.9 KB
/
test_models.py
File metadata and controls
336 lines (302 loc) · 10.9 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
"""Tests for MDL Pydantic models."""
import pytest
from datetime import datetime
from app.modules.mdl.models import (
MDLColumn,
MDLModel,
MDLRelationship,
MDLMetric,
MDLView,
MDLTimeGrain,
MDLManifest,
JoinType,
DatePart,
)
class TestMDLColumn:
"""Tests for MDLColumn model."""
def test_minimal_column(self):
"""Test creating a column with minimal required fields."""
col = MDLColumn(name="id", type="INTEGER")
assert col.name == "id"
assert col.type == "INTEGER"
assert col.not_null is False
assert col.is_calculated is False
assert col.is_hidden is False
def test_column_with_expression(self):
"""Test creating a calculated column with expression."""
col = MDLColumn(
name="full_name",
type="VARCHAR",
is_calculated=True,
expression="first_name || ' ' || last_name",
)
assert col.is_calculated is True
assert col.expression == "first_name || ' ' || last_name"
def test_column_with_relationship(self):
"""Test column referencing a relationship."""
col = MDLColumn(
name="customer_name",
type="VARCHAR",
relationship="orders_customers",
)
assert col.relationship == "orders_customers"
class TestMDLModel:
"""Tests for MDLModel model."""
def test_minimal_model(self):
"""Test creating a model with minimal fields."""
model = MDLModel(
name="users",
columns=[MDLColumn(name="id", type="INTEGER")],
)
assert model.name == "users"
assert len(model.columns) == 1
assert model.cached is False
def test_model_with_table_reference(self):
"""Test model with explicit table reference."""
model = MDLModel(
name="users",
table_reference={"catalog": "prod", "schema": "public", "table": "users"},
columns=[
MDLColumn(name="id", type="INTEGER"),
MDLColumn(name="email", type="VARCHAR"),
],
primary_key="id",
)
assert model.table_reference["table"] == "users"
assert model.primary_key == "id"
def test_model_with_ref_sql(self):
"""Test model defined by SQL query."""
model = MDLModel(
name="active_users",
ref_sql="SELECT * FROM users WHERE status = 'active'",
columns=[MDLColumn(name="id", type="INTEGER")],
)
assert model.ref_sql == "SELECT * FROM users WHERE status = 'active'"
class TestMDLRelationship:
"""Tests for MDLRelationship model."""
def test_one_to_many_relationship(self):
"""Test creating a one-to-many relationship."""
rel = MDLRelationship(
name="orders_customers",
models=["orders", "customers"],
join_type=JoinType.MANY_TO_ONE,
condition="orders.customer_id = customers.id",
)
assert rel.name == "orders_customers"
assert rel.join_type == JoinType.MANY_TO_ONE
assert len(rel.models) == 2
def test_join_type_enum(self):
"""Test all join type enum values."""
assert JoinType.ONE_TO_ONE.value == "ONE_TO_ONE"
assert JoinType.ONE_TO_MANY.value == "ONE_TO_MANY"
assert JoinType.MANY_TO_ONE.value == "MANY_TO_ONE"
assert JoinType.MANY_TO_MANY.value == "MANY_TO_MANY"
class TestMDLMetric:
"""Tests for MDLMetric model."""
def test_metric_with_measure(self):
"""Test creating a metric with measure."""
metric = MDLMetric(
name="total_revenue",
base_object="orders",
measure=[
MDLColumn(
name="revenue",
type="DECIMAL",
is_calculated=True,
expression="SUM(amount)",
)
],
)
assert metric.name == "total_revenue"
assert metric.base_object == "orders"
assert len(metric.measure) == 1
def test_metric_with_dimension_and_time_grain(self):
"""Test metric with dimensions and time grain."""
metric = MDLMetric(
name="monthly_sales",
base_object="orders",
dimension=[MDLColumn(name="category", type="VARCHAR")],
measure=[
MDLColumn(
name="total",
type="DECIMAL",
is_calculated=True,
expression="SUM(amount)",
)
],
time_grain=[
MDLTimeGrain(
name="order_date",
ref_column="created_at",
date_parts=[DatePart.YEAR, DatePart.MONTH],
)
],
)
assert len(metric.dimension) == 1
assert len(metric.time_grain) == 1
assert DatePart.MONTH in metric.time_grain[0].date_parts
class TestMDLTimeGrain:
"""Tests for MDLTimeGrain model."""
def test_time_grain_creation(self):
"""Test creating a time grain."""
tg = MDLTimeGrain(
name="created_date",
ref_column="created_at",
date_parts=[DatePart.YEAR, DatePart.QUARTER, DatePart.MONTH],
)
assert tg.name == "created_date"
assert tg.ref_column == "created_at"
assert len(tg.date_parts) == 3
def test_date_part_enum(self):
"""Test all date part enum values."""
assert DatePart.YEAR.value == "YEAR"
assert DatePart.QUARTER.value == "QUARTER"
assert DatePart.MONTH.value == "MONTH"
assert DatePart.WEEK.value == "WEEK"
assert DatePart.DAY.value == "DAY"
assert DatePart.HOUR.value == "HOUR"
assert DatePart.MINUTE.value == "MINUTE"
class TestMDLView:
"""Tests for MDLView model."""
def test_view_creation(self):
"""Test creating a view."""
view = MDLView(
name="active_customers",
statement="SELECT * FROM customers WHERE status = 'active'",
)
assert view.name == "active_customers"
assert "SELECT" in view.statement
def test_view_with_properties(self):
"""Test view with description."""
view = MDLView(
name="vip_customers",
statement="SELECT * FROM customers WHERE tier = 'vip'",
properties={"description": "VIP tier customers only"},
)
assert view.properties["description"] == "VIP tier customers only"
class TestMDLManifest:
"""Tests for MDLManifest model."""
def test_minimal_manifest(self):
"""Test creating a minimal manifest."""
manifest = MDLManifest(
catalog="analytics",
schema="public",
)
assert manifest.catalog == "analytics"
assert manifest.schema_name == "public"
assert manifest.models == []
assert manifest.relationships == []
def test_full_manifest(self):
"""Test creating a full manifest with all components."""
manifest = MDLManifest(
id="mdl_123",
db_connection_id="conn_456",
name="Sales Analytics",
catalog="analytics",
schema="public",
data_source="postgresql",
models=[
MDLModel(
name="orders",
columns=[
MDLColumn(name="id", type="INTEGER"),
MDLColumn(name="customer_id", type="INTEGER"),
MDLColumn(name="amount", type="DECIMAL"),
],
primary_key="id",
)
],
relationships=[
MDLRelationship(
name="orders_customers",
models=["orders", "customers"],
join_type=JoinType.MANY_TO_ONE,
condition="orders.customer_id = customers.id",
)
],
metrics=[
MDLMetric(
name="total_revenue",
base_object="orders",
measure=[
MDLColumn(
name="revenue",
type="DECIMAL",
is_calculated=True,
expression="SUM(amount)",
)
],
)
],
views=[
MDLView(
name="recent_orders",
statement="SELECT * FROM orders WHERE created_at > NOW() - INTERVAL '30 days'",
)
],
)
assert manifest.name == "Sales Analytics"
assert len(manifest.models) == 1
assert len(manifest.relationships) == 1
assert len(manifest.metrics) == 1
assert len(manifest.views) == 1
def test_manifest_to_dict(self):
"""Test converting manifest to dictionary."""
manifest = MDLManifest(
id="mdl_123",
db_connection_id="conn_456",
name="Test MDL",
catalog="test",
schema="public",
models=[
MDLModel(
name="users",
columns=[MDLColumn(name="id", type="INTEGER")],
)
],
)
d = manifest.to_dict()
assert d["id"] == "mdl_123"
assert d["catalog"] == "test"
assert len(d["models"]) == 1
assert d["models"][0]["name"] == "users"
def test_manifest_from_dict(self):
"""Test creating manifest from dictionary."""
data = {
"id": "mdl_789",
"db_connection_id": "conn_111",
"name": "Loaded MDL",
"catalog": "loaded",
"schema": "main",
"models": [
{
"name": "products",
"columns": [{"name": "id", "type": "INTEGER"}],
}
],
"relationships": [],
"created_at": "2024-01-01T12:00:00",
}
manifest = MDLManifest.from_dict(data)
assert manifest.id == "mdl_789"
assert manifest.name == "Loaded MDL"
assert len(manifest.models) == 1
assert manifest.models[0].name == "products"
def test_manifest_to_mdl_json(self):
"""Test exporting manifest to MDL JSON format (WrenAI compatible)."""
manifest = MDLManifest(
catalog="analytics",
schema="public",
models=[
MDLModel(
name="orders",
columns=[MDLColumn(name="id", type="INTEGER")],
primary_key="id",
)
],
)
mdl_json = manifest.to_mdl_json()
assert mdl_json["catalog"] == "analytics"
assert mdl_json["schema"] == "public"
assert len(mdl_json["models"]) == 1
assert mdl_json["models"][0]["primaryKey"] == "id"