-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_demos.py
More file actions
360 lines (295 loc) · 12.9 KB
/
test_demos.py
File metadata and controls
360 lines (295 loc) · 12.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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
"""Unit tests for feast.demos — demo notebook generation."""
import json
import pathlib
import textwrap
import pytest
from feast.demos import (
_extract_store_info,
_find_feature_store_yamls,
_is_operator_client,
copy_demo_notebooks,
)
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
_LOCAL_YAML = textwrap.dedent("""\
project: local_proj
provider: local
registry:
path: data/registry.db
registry_type: file
offline_store:
type: file
online_store:
type: sqlite
path: data/online_store.db
entity_key_serialization_version: 3
""")
_OPERATOR_YAML = textwrap.dedent("""\
project: remote_proj
provider: local
offline_store:
host: feast-offline.svc.cluster.local
port: 80
type: remote
online_store:
path: http://feast-online.svc.cluster.local:80
type: remote
registry:
path: feast-registry.svc.cluster.local:80
registry_type: remote
auth:
type: oidc
entity_key_serialization_version: 3
""")
_VECTOR_YAML = textwrap.dedent("""\
project: vec_proj
provider: local
registry:
path: data/registry.db
registry_type: file
offline_store:
type: file
online_store:
type: pgvector
vector_enabled: true
embedding_dim: 512
entity_key_serialization_version: 3
""")
def _write(tmp_path: pathlib.Path, rel: str, content: str) -> pathlib.Path:
p = tmp_path / rel
p.parent.mkdir(parents=True, exist_ok=True)
p.write_text(content)
return p
def _sections(nb: dict) -> list[str]:
"""Return the first line of every markdown cell that starts with #."""
return [
"".join(cell["source"]).splitlines()[0]
for cell in nb["cells"]
if cell["cell_type"] == "markdown" and "".join(cell["source"]).startswith("#")
]
# ---------------------------------------------------------------------------
# _extract_store_info
# ---------------------------------------------------------------------------
class TestExtractStoreInfo:
def test_local_defaults(self):
info = _extract_store_info({})
assert info["project"] == "my_feast_project"
assert info["provider"] == "local"
assert info["online_store_type"] == "sqlite"
assert info["offline_store_type"] == "file"
assert info["registry_type"] == "file"
assert info["auth_type"] == "no_auth"
assert info["vector_enabled"] is False
assert info["embedding_dim"] is None
def test_operator_client_yaml(self):
config = {
"project": "sample",
"provider": "local",
"offline_store": {"type": "remote", "host": "h", "port": 80},
"online_store": {"type": "remote", "path": "http://h:80"},
"registry": {"registry_type": "remote", "path": "h:80"},
"auth": {"type": "oidc"},
}
info = _extract_store_info(config)
assert info["registry_type"] == "remote"
assert info["online_store_type"] == "remote"
assert info["offline_store_type"] == "remote"
assert info["auth_type"] == "oidc"
def test_registry_type_key_takes_priority_over_type(self):
config = {"registry": {"registry_type": "remote", "type": "file"}}
info = _extract_store_info(config)
assert info["registry_type"] == "remote"
def test_registry_type_fallback_to_type(self):
config = {"registry": {"type": "snowflake"}}
info = _extract_store_info(config)
assert info["registry_type"] == "snowflake"
def test_string_registry_path_stays_file(self):
info = _extract_store_info({"registry": "data/registry.db"})
assert info["registry_type"] == "file"
def test_vector_enabled(self):
config = {
"online_store": {
"type": "pgvector",
"vector_enabled": True,
"embedding_dim": 512,
}
}
info = _extract_store_info(config)
assert info["vector_enabled"] is True
assert info["embedding_dim"] == 512
def test_online_store_as_string(self):
info = _extract_store_info({"online_store": "Redis"})
assert info["online_store_type"] == "redis"
def test_offline_store_as_string(self):
info = _extract_store_info({"offline_store": "BigQuery"})
assert info["offline_store_type"] == "bigquery"
# ---------------------------------------------------------------------------
# _is_operator_client
# ---------------------------------------------------------------------------
class TestIsOperatorClient:
def _info(self, registry="remote", online="remote", offline="remote"):
return {
"registry_type": registry,
"online_store_type": online,
"offline_store_type": offline,
}
def test_all_remote_is_operator(self):
assert _is_operator_client(self._info()) is True
def test_local_registry_not_operator(self):
assert _is_operator_client(self._info(registry="file")) is False
def test_local_online_not_operator(self):
assert _is_operator_client(self._info(online="sqlite")) is False
def test_local_offline_not_operator(self):
assert _is_operator_client(self._info(offline="file")) is False
# ---------------------------------------------------------------------------
# _find_feature_store_yamls
# ---------------------------------------------------------------------------
class TestFindFeatureStoreYamls:
def test_direct(self, tmp_path):
_write(tmp_path, "feature_store.yaml", "project: p")
found = _find_feature_store_yamls(tmp_path)
assert len(found) == 1
assert found[0].name == "feature_store.yaml"
def test_feast_config_root(self, tmp_path):
_write(tmp_path, "feast-config/feature_store.yaml", "project: p")
found = _find_feature_store_yamls(tmp_path)
assert len(found) == 1
def test_feast_config_multiple_files(self, tmp_path):
_write(tmp_path, "feast-config/rag.yaml", "project: rag")
_write(tmp_path, "feast-config/rec.yml", "project: rec")
found = _find_feature_store_yamls(tmp_path)
assert len(found) == 2
def test_feast_config_any_extension(self, tmp_path):
_write(tmp_path, "feast-config/project_a.yaml", "project: a")
_write(tmp_path, "feast-config/project_b", "project: b")
found = _find_feature_store_yamls(tmp_path)
assert len(found) == 2
def test_feast_config_ignores_directories(self, tmp_path):
_write(tmp_path, "feast-config/valid.yaml", "project: p")
(tmp_path / "feast-config" / "subdir").mkdir()
found = _find_feature_store_yamls(tmp_path)
assert len(found) == 1
def test_multiple_sources(self, tmp_path):
_write(tmp_path, "feature_store.yaml", "project: root")
_write(tmp_path, "feast-config/a.yaml", "project: a")
_write(tmp_path, "feast-config/b", "project: b")
found = _find_feature_store_yamls(tmp_path)
assert len(found) == 3
def test_no_yaml_returns_empty(self, tmp_path):
assert _find_feature_store_yamls(tmp_path) == []
# ---------------------------------------------------------------------------
# copy_demo_notebooks — file generation
# ---------------------------------------------------------------------------
class TestCopyDemoNotebooks:
def test_generates_notebooks(self, tmp_path):
_write(tmp_path, "feature_store.yaml", _LOCAL_YAML)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
assert (out / "local_proj" / "01_feature_store_overview.ipynb").exists()
assert (out / "local_proj" / "02_historical_features_training.ipynb").exists()
assert (out / "local_proj" / "03_online_features_serving.ipynb").exists()
def test_valid_notebook_json(self, tmp_path):
_write(tmp_path, "feature_store.yaml", _LOCAL_YAML)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
nb = json.loads(
(out / "local_proj" / "01_feature_store_overview.ipynb").read_text()
)
assert nb["nbformat"] == 4
assert isinstance(nb["cells"], list)
def test_raises_if_output_exists(self, tmp_path):
_write(tmp_path, "feature_store.yaml", _LOCAL_YAML)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
with pytest.raises(FileExistsError):
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
def test_overwrite_flag(self, tmp_path):
_write(tmp_path, "feature_store.yaml", _LOCAL_YAML)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
copy_demo_notebooks(
output_dir=str(out), repo_path=str(tmp_path), overwrite=True
)
def test_no_yaml_returns_without_creating_output(self, tmp_path):
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
assert not out.exists()
def test_multiple_projects(self, tmp_path):
_write(
tmp_path,
"feast-config/proj_a.yaml",
"project: proj_a\nprovider: local\n",
)
_write(
tmp_path,
"feast-config/proj_b.yaml",
"project: proj_b\nprovider: local\n",
)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
assert (out / "proj_a").is_dir()
assert (out / "proj_b").is_dir()
# ---------------------------------------------------------------------------
# Notebook content — section headings
# ---------------------------------------------------------------------------
class TestNotebookContent:
def _notebooks(self, tmp_path, yaml_content):
_write(tmp_path, "feature_store.yaml", yaml_content)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
project = _extract_store_info(__import__("yaml").safe_load(yaml_content))[
"project"
]
return {
name: json.loads((out / project / name).read_text())
for name in [
"01_feature_store_overview.ipynb",
"02_historical_features_training.ipynb",
"03_online_features_serving.ipynb",
]
}
def test_local_overview_has_apply_section(self, tmp_path):
nbs = self._notebooks(tmp_path, _LOCAL_YAML)
sections = _sections(nbs["01_feature_store_overview.ipynb"])
assert any("Apply Feature Definitions" in s for s in sections)
def test_remote_overview_has_registry_sync(self, tmp_path):
nbs = self._notebooks(tmp_path, _OPERATOR_YAML)
sections = _sections(nbs["01_feature_store_overview.ipynb"])
assert any("Registry Sync" in s for s in sections)
def test_historical_no_apply_section(self, tmp_path):
nbs = self._notebooks(tmp_path, _LOCAL_YAML)
sections = _sections(nbs["02_historical_features_training.ipynb"])
assert not any("Apply" in s for s in sections)
def test_online_no_apply_section(self, tmp_path):
nbs = self._notebooks(tmp_path, _LOCAL_YAML)
sections = _sections(nbs["03_online_features_serving.ipynb"])
assert not any("Apply" in s for s in sections)
def test_vector_notebook_has_vector_section(self, tmp_path):
nbs = self._notebooks(tmp_path, _VECTOR_YAML)
sections = _sections(nbs["03_online_features_serving.ipynb"])
assert any("Vector" in s for s in sections)
def test_non_vector_notebook_no_vector_section(self, tmp_path):
nbs = self._notebooks(tmp_path, _LOCAL_YAML)
sections = _sections(nbs["03_online_features_serving.ipynb"])
assert not any("Vector" in s for s in sections)
def test_auth_section_present_for_oidc(self, tmp_path):
nbs = self._notebooks(tmp_path, _OPERATOR_YAML)
sections = _sections(nbs["03_online_features_serving.ipynb"])
assert any("Authentication" in s for s in sections)
def test_auth_section_absent_for_no_auth(self, tmp_path):
nbs = self._notebooks(tmp_path, _LOCAL_YAML)
sections = _sections(nbs["03_online_features_serving.ipynb"])
assert not any("Authentication" in s for s in sections)
def test_path_setup_cell_contains_yaml_path(self, tmp_path):
_write(tmp_path, "feature_store.yaml", _LOCAL_YAML)
out = tmp_path / "out"
copy_demo_notebooks(output_dir=str(out), repo_path=str(tmp_path))
nb = json.loads(
(out / "local_proj" / "01_feature_store_overview.ipynb").read_text()
)
code_sources = [
"".join(c["source"]) for c in nb["cells"] if c["cell_type"] == "code"
]
yaml_path = str((tmp_path / "feature_store.yaml").resolve())
assert any(yaml_path in src for src in code_sources)