-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_api_rest_registry.py
More file actions
2018 lines (1691 loc) · 71.4 KB
/
test_api_rest_registry.py
File metadata and controls
2018 lines (1691 loc) · 71.4 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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ast
import os
import tempfile
import pandas as pd
import pytest
from fastapi.testclient import TestClient
from feast import Entity, FeatureService, FeatureStore, FeatureView, Field, FileSource
from feast.api.registry.rest.rest_registry_server import RestRegistryServer
from feast.data_source import RequestSource
from feast.infra.offline_stores.file_source import SavedDatasetFileStorage
from feast.on_demand_feature_view import on_demand_feature_view
from feast.repo_config import RepoConfig
from feast.saved_dataset import SavedDataset
from feast.types import Float64, Int64
from feast.value_type import ValueType
@pytest.fixture
def fastapi_test_app():
# Create temp registry and data directory
tmp_dir = tempfile.TemporaryDirectory()
registry_path = os.path.join(tmp_dir.name, "registry.db")
# Create dummy parquet file (Feast requires valid sources)
parquet_file_path = os.path.join(tmp_dir.name, "data.parquet")
df = pd.DataFrame(
{
"user_id": [1, 2, 3],
"age": [25, 30, 22],
"income": [50000.0, 60000.0, 45000.0],
"event_timestamp": pd.to_datetime(
["2024-01-01", "2024-01-02", "2024-01-03"]
),
}
)
df.to_parquet(parquet_file_path)
# Setup minimal repo config
config = {
"registry": registry_path,
"project": "demo_project",
"provider": "local",
"offline_store": {"type": "file"},
"online_store": {"type": "sqlite", "path": ":memory:"},
}
user_profile_source = FileSource(
name="user_profile_source",
path=parquet_file_path,
event_timestamp_column="event_timestamp",
)
store = FeatureStore(config=RepoConfig.model_validate(config))
user_id_entity = Entity(
name="user_id", value_type=ValueType.INT64, description="User ID"
)
user_profile_feature_view = FeatureView(
name="user_profile",
entities=[user_id_entity],
ttl=None,
schema=[
Field(name="age", dtype=Int64),
Field(name="income", dtype=Float64),
],
source=user_profile_source,
tags={"environment": "production", "team": "ml", "version": "1.0"},
)
user_behavior_feature_view = FeatureView(
name="user_behavior",
entities=[user_id_entity],
ttl=None,
schema=[
Field(name="click_count", dtype=Int64),
Field(name="session_duration", dtype=Float64),
],
source=user_profile_source,
tags={"environment": "staging", "team": "analytics", "version": "2.0"},
)
user_preferences_feature_view = FeatureView(
name="user_preferences",
entities=[user_id_entity],
ttl=None,
schema=[
Field(name="preferred_category", dtype=Int64),
Field(name="engagement_score", dtype=Float64),
],
source=user_profile_source,
tags={"environment": "production", "team": "analytics", "version": "1.5"},
)
user_feature_service = FeatureService(
name="user_service",
features=[
user_profile_feature_view,
user_behavior_feature_view,
user_preferences_feature_view,
],
)
# Create a saved dataset for testing
saved_dataset_storage = SavedDatasetFileStorage(path=parquet_file_path)
test_saved_dataset = SavedDataset(
name="test_saved_dataset",
features=["user_profile:age", "user_profile:income"],
join_keys=["user_id"],
storage=saved_dataset_storage,
tags={"environment": "test", "version": "1.0"},
)
input_request = RequestSource(
name="input_request_source",
schema=[
Field(name="request_feature", dtype=Float64),
],
)
@on_demand_feature_view(
sources=[user_profile_feature_view, input_request],
schema=[
Field(name="combined_feature", dtype=Float64),
],
description="On-demand feature view with request source for testing",
)
def test_on_demand_feature_view(features_df: pd.DataFrame) -> pd.DataFrame:
df = pd.DataFrame()
df["combined_feature"] = features_df["age"] + features_df["request_feature"]
return df
# Apply objects
store.apply(
[
user_id_entity,
user_profile_feature_view,
user_behavior_feature_view,
user_preferences_feature_view,
user_feature_service,
test_on_demand_feature_view,
]
)
store.registry.apply_saved_dataset(test_saved_dataset, "demo_project")
# Build REST app with registered routes
rest_server = RestRegistryServer(store)
client = TestClient(rest_server.app)
yield client
tmp_dir.cleanup()
def test_entities_via_rest(fastapi_test_app):
response = fastapi_test_app.get("/entities?project=demo_project")
assert response.status_code == 200
assert "entities" in response.json()
response = fastapi_test_app.get("/entities/user_id?project=demo_project")
assert response.status_code == 200
data = response.json()
assert data["spec"]["name"] == "user_id"
# Check featureDefinition
assert "featureDefinition" in data
code = data["featureDefinition"]
assert code
assert "Entity" in code
assert "user_id" in code
try:
ast.parse(code)
except SyntaxError as e:
pytest.fail(f"featureDefinition is not valid Python: {e}")
def test_feature_views_via_rest(fastapi_test_app):
response = fastapi_test_app.get("/feature_views?project=demo_project")
assert response.status_code == 200
assert "featureViews" in response.json()
response = fastapi_test_app.get("/feature_views/user_profile?project=demo_project")
assert response.status_code == 200
data = response.json()
assert data["spec"]["name"] == "user_profile"
# Check featureDefinition
assert "featureDefinition" in data
code = data["featureDefinition"]
assert code
assert "FeatureView" in code
assert "user_profile" in code
try:
ast.parse(code)
except SyntaxError as e:
pytest.fail(f"featureDefinition is not valid Python: {e}")
def test_feature_views_type_field_via_rest(fastapi_test_app):
"""Test that the type field is correctly populated for feature views."""
# Test list endpoint
response = fastapi_test_app.get("/feature_views?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
# Verify all feature views have a type field
for fv in data["featureViews"]:
assert "type" in fv
assert fv["type"] is not None
assert fv["type"] in ["featureView", "onDemandFeatureView"]
# Test single endpoint
response = fastapi_test_app.get("/feature_views/user_profile?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "type" in data
assert data["type"] == "featureView"
assert data["spec"]["name"] == "user_profile"
def test_feature_views_entity_filtering_via_rest(fastapi_test_app):
"""Test that feature views can be filtered by entity."""
response = fastapi_test_app.get("/feature_views?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
all_feature_views = data["featureViews"]
response = fastapi_test_app.get("/feature_views?project=demo_project&entity=user")
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
filtered_feature_views = data["featureViews"]
assert len(filtered_feature_views) <= len(all_feature_views)
for fv in filtered_feature_views:
if "spec" in fv and "entities" in fv["spec"]:
assert "user" in fv["spec"]["entities"]
response = fastapi_test_app.get(
"/feature_views?project=demo_project&entity=nonexistent_entity"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
assert len(data["featureViews"]) == 0
def test_feature_views_comprehensive_filtering_via_rest(fastapi_test_app):
"""Test that feature views can be filtered by multiple criteria."""
response = fastapi_test_app.get("/feature_views?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
all_feature_views = data["featureViews"]
response = fastapi_test_app.get("/feature_views?project=demo_project&feature=age")
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
feature_filtered_views = data["featureViews"]
assert len(feature_filtered_views) <= len(all_feature_views)
response = fastapi_test_app.get(
"/feature_views?project=demo_project&data_source=user_profile_source"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
data_source_filtered_views = data["featureViews"]
assert len(data_source_filtered_views) <= len(all_feature_views)
# Test filtering on-demand feature views by request source data source
response = fastapi_test_app.get(
"/feature_views?project=demo_project&data_source=input_request_source"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
odfv_data_source_filtered_views = data["featureViews"]
# Should find the on-demand feature view that uses the request source
assert len(odfv_data_source_filtered_views) > 0
odfv_found = False
for fv in odfv_data_source_filtered_views:
if fv["type"] == "onDemandFeatureView":
odfv_found = True
break
assert odfv_found, (
"On-demand feature view should be found when filtering by request source data source"
)
response = fastapi_test_app.get(
"/feature_views?project=demo_project&feature_service=user_service"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
feature_service_filtered_views = data["featureViews"]
assert len(feature_service_filtered_views) <= len(all_feature_views)
response = fastapi_test_app.get(
"/feature_views?project=demo_project&entity=user&feature=age"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
combined_filtered_views = data["featureViews"]
assert len(combined_filtered_views) <= len(all_feature_views)
response = fastapi_test_app.get(
"/feature_views?project=demo_project&feature=nonexistent_feature"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
assert len(data["featureViews"]) == 0
response = fastapi_test_app.get(
"/feature_views?project=demo_project&data_source=nonexistent_source"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
assert len(data["featureViews"]) == 0
response = fastapi_test_app.get(
"/feature_views?project=demo_project&feature_service=nonexistent_service"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
assert len(data["featureViews"]) == 0
response = fastapi_test_app.get(
"/feature_views?project=demo_project&feature_service=restricted_feature_service"
)
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
assert len(data["featureViews"]) == 0
def test_feature_services_via_rest(fastapi_test_app):
response = fastapi_test_app.get("/feature_services?project=demo_project")
assert response.status_code == 200
assert "featureServices" in response.json()
response = fastapi_test_app.get(
"/feature_services/user_service?project=demo_project"
)
assert response.status_code == 200
data = response.json()
assert data["spec"]["name"] == "user_service"
# Check featureDefinition
assert "featureDefinition" in data
code = data["featureDefinition"]
assert code
assert "FeatureService" in code
assert "user_service" in code
try:
ast.parse(code)
except SyntaxError as e:
pytest.fail(f"featureDefinition is not valid Python: {e}")
def test_feature_services_feature_view_filtering_via_rest(fastapi_test_app):
"""Test that feature services can be filtered by feature view name."""
response = fastapi_test_app.get("/feature_services?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "featureServices" in data
all_feature_services = data["featureServices"]
response = fastapi_test_app.get(
"/feature_services?project=demo_project&feature_view=user_profile"
)
assert response.status_code == 200
data = response.json()
assert "featureServices" in data
filtered_feature_services = data["featureServices"]
assert len(filtered_feature_services) <= len(all_feature_services)
for fs in filtered_feature_services:
if "spec" in fs and "featureViewProjections" in fs["spec"]:
feature_view_names = [
fvp["name"] for fvp in fs["spec"]["featureViewProjections"]
]
assert "user_profile" in feature_view_names
response = fastapi_test_app.get(
"/feature_services?project=demo_project&feature_view=nonexistent_feature_view"
)
assert response.status_code == 200
data = response.json()
assert "featureServices" in data
assert len(data["featureServices"]) == 0
def test_data_sources_via_rest(fastapi_test_app):
response = fastapi_test_app.get("/data_sources?project=demo_project")
assert "dataSources" in response.json()
response = fastapi_test_app.get(
"/data_sources/user_profile_source?project=demo_project"
)
assert response.status_code == 200
data = response.json()
assert data["name"] == "user_profile_source"
# Check featureDefinition
assert "featureDefinition" in data
code = data["featureDefinition"]
assert code
assert "FileSource" in code
assert "user_profile_source" in code
try:
ast.parse(code)
except SyntaxError as e:
pytest.fail(f"featureDefinition is not valid Python: {e}")
def test_projects_via_rest(fastapi_test_app):
response = fastapi_test_app.get("/projects")
assert response.status_code == 200
assert isinstance(response.json()["projects"], list)
response = fastapi_test_app.get("/projects/demo_project")
assert response.status_code == 200
assert response.json()["spec"]["name"] == "demo_project"
def test_permissions_via_rest(fastapi_test_app):
response = fastapi_test_app.get("/permissions?project=demo_project")
assert response.status_code == 200
def test_lineage_registry_via_rest(fastapi_test_app):
"""Test the /lineage/registry endpoint."""
response = fastapi_test_app.get("/lineage/registry?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "relationships" in data
assert "indirect_relationships" in data
assert isinstance(data["relationships"], list)
assert isinstance(data["indirect_relationships"], list)
def test_lineage_registry_with_filters_via_rest(fastapi_test_app):
"""Test the /lineage/registry endpoint with filters."""
response = fastapi_test_app.get(
"/lineage/registry?project=demo_project&filter_object_type=featureView"
)
assert response.status_code == 200
response = fastapi_test_app.get(
"/lineage/registry?project=demo_project&filter_object_type=featureView&filter_object_name=user_profile"
)
assert response.status_code == 200
def test_object_relationships_via_rest(fastapi_test_app):
"""Test the /lineage/objects/{object_type}/{object_name} endpoint."""
response = fastapi_test_app.get(
"/lineage/objects/featureView/user_profile?project=demo_project"
)
assert response.status_code == 200
data = response.json()
assert "relationships" in data
assert isinstance(data["relationships"], list)
def test_object_relationships_with_indirect_via_rest(fastapi_test_app):
"""Test the object relationships endpoint with indirect relationships."""
response = fastapi_test_app.get(
"/lineage/objects/featureView/user_profile?project=demo_project&include_indirect=true"
)
assert response.status_code == 200
data = response.json()
assert "relationships" in data
assert isinstance(data["relationships"], list)
def test_object_relationships_invalid_type_via_rest(fastapi_test_app):
"""Test the object relationships endpoint with invalid object type."""
response = fastapi_test_app.get(
"/lineage/objects/invalidType/some_name?project=demo_project"
)
assert response.status_code == 422
data = response.json()
assert "status_code" in data
assert data["status_code"] == 422
assert "detail" in data
assert "Invalid object_type" in data["detail"]
assert "error_type" in data
assert data["error_type"] == "ValueError"
def test_complete_registry_data_via_rest(fastapi_test_app):
"""Test the /lineage/complete endpoint."""
response = fastapi_test_app.get("/lineage/complete?project=demo_project")
assert response.status_code == 200
data = response.json()
assert "project" in data
assert data["project"] == "demo_project"
assert "objects" in data
assert "relationships" in data
assert "indirectRelationships" in data
objects = data["objects"]
assert "entities" in objects
assert "dataSources" in objects
assert "featureViews" in objects
assert "featureServices" in objects
assert isinstance(objects["entities"], list)
assert isinstance(objects["dataSources"], list)
assert isinstance(objects["featureViews"], list)
assert isinstance(objects["featureServices"], list)
def test_complete_registry_data_cache_control_via_rest(fastapi_test_app):
"""Test the /lineage/complete endpoint with cache control."""
response = fastapi_test_app.get(
"/lineage/complete?project=demo_project&allow_cache=false"
)
assert response.status_code == 200
data = response.json()
assert "project" in data
response = fastapi_test_app.get(
"/lineage/complete?project=demo_project&allow_cache=true"
)
assert response.status_code == 200
def test_lineage_endpoint_error_handling(fastapi_test_app):
"""Test error handling in lineage endpoints."""
# Test missing project parameter
response = fastapi_test_app.get("/lineage/registry")
assert response.status_code == 422 # Validation error
data = response.json()
assert "status_code" in data
assert data["status_code"] == 422
assert "detail" in data
assert "error_type" in data
assert data["error_type"] == "RequestValidationError"
# Test invalid project
response = fastapi_test_app.get("/lineage/registry?project=nonexistent_project")
# Should still return 200 but with empty results
assert response.status_code == 200
# Test object relationships with missing parameters
response = fastapi_test_app.get("/lineage/objects/featureView/test_fv")
assert response.status_code == 422 # Missing required project parameter
data = response.json()
assert "status_code" in data
assert data["status_code"] == 422
assert "detail" in data
assert "error_type" in data
assert data["error_type"] == "RequestValidationError"
def test_saved_datasets_via_rest(fastapi_test_app):
# Test list saved datasets endpoint
response = fastapi_test_app.get("/saved_datasets?project=demo_project")
assert response.status_code == 200
response_data = response.json()
assert "savedDatasets" in response_data
assert isinstance(response_data["savedDatasets"], list)
assert len(response_data["savedDatasets"]) == 1
saved_dataset = response_data["savedDatasets"][0]
assert saved_dataset["spec"]["name"] == "test_saved_dataset"
assert "user_profile:age" in saved_dataset["spec"]["features"]
assert "user_profile:income" in saved_dataset["spec"]["features"]
assert "user_id" in saved_dataset["spec"]["joinKeys"]
assert saved_dataset["spec"]["tags"]["environment"] == "test"
assert saved_dataset["spec"]["tags"]["version"] == "1.0"
# Test get specific saved dataset endpoint
response = fastapi_test_app.get(
"/saved_datasets/test_saved_dataset?project=demo_project"
)
assert response.status_code == 200
response_data = response.json()
assert response_data["spec"]["name"] == "test_saved_dataset"
assert "user_profile:age" in response_data["spec"]["features"]
assert "user_profile:income" in response_data["spec"]["features"]
# Test with allow_cache parameter
response = fastapi_test_app.get(
"/saved_datasets/test_saved_dataset?project=demo_project&allow_cache=false"
)
assert response.status_code == 200
assert response.json()["spec"]["name"] == "test_saved_dataset"
# Test with tags filter
response = fastapi_test_app.get(
"/saved_datasets?project=demo_project&tags=environment:test"
)
assert response.status_code == 200
assert len(response.json()["savedDatasets"]) == 1
# Test with non-matching tags filter
response = fastapi_test_app.get(
"/saved_datasets?project=demo_project&tags=environment:production"
)
assert response.status_code == 200
assert len(response.json()["savedDatasets"]) == 0
# Test with multiple tags filter
response = fastapi_test_app.get(
"/saved_datasets?project=demo_project&tags=environment:test&tags=version:1.0"
)
assert response.status_code == 200
assert len(response.json()["savedDatasets"]) == 1
# Test non-existent saved dataset
response = fastapi_test_app.get("/saved_datasets/non_existent?project=demo_project")
assert response.status_code == 404
data = response.json()
assert "status_code" in data
assert data["status_code"] == 404
assert "detail" in data
assert "error_type" in data
assert data["error_type"] == "FeastObjectNotFoundException"
# Test missing project parameter
response = fastapi_test_app.get("/saved_datasets/test_saved_dataset")
assert (
response.status_code == 422
) # Unprocessable Entity for missing required query param
data = response.json()
assert "status_code" in data
assert data["status_code"] == 422
assert "detail" in data
assert "error_type" in data
assert data["error_type"] == "RequestValidationError"
@pytest.fixture
def fastapi_test_app_with_multiple_objects():
"""Test app with multiple objects for pagination and sorting tests."""
tmp_dir = tempfile.TemporaryDirectory()
registry_path = os.path.join(tmp_dir.name, "registry.db")
parquet_file_path = os.path.join(tmp_dir.name, "data.parquet")
df = pd.DataFrame(
{
"user_id": [1, 2, 3],
"age": [25, 30, 22],
"income": [50000.0, 60000.0, 45000.0],
"event_timestamp": pd.to_datetime(
["2024-01-01", "2024-01-02", "2024-01-03"]
),
}
)
df.to_parquet(parquet_file_path)
config = {
"registry": registry_path,
"project": "demo_project",
"provider": "local",
"offline_store": {"type": "file"},
"online_store": {"type": "sqlite", "path": ":memory:"},
}
store = FeatureStore(config=RepoConfig.model_validate(config))
# Create multiple entities for testing
entities = [
Entity(name="user_id", value_type=ValueType.INT64, description="User ID"),
Entity(
name="customer_id", value_type=ValueType.INT64, description="Customer ID"
),
Entity(name="product_id", value_type=ValueType.INT64, description="Product ID"),
Entity(name="order_id", value_type=ValueType.INT64, description="Order ID"),
Entity(name="session_id", value_type=ValueType.INT64, description="Session ID"),
]
data_sources = [
FileSource(
name="user_profile_source",
path=parquet_file_path,
event_timestamp_column="event_timestamp",
),
FileSource(
name="customer_data_source",
path=parquet_file_path,
event_timestamp_column="event_timestamp",
),
FileSource(
name="product_catalog_source",
path=parquet_file_path,
event_timestamp_column="event_timestamp",
),
]
feature_views = [
FeatureView(
name="user_profile",
entities=[entities[0]],
ttl=None,
schema=[
Field(name="age", dtype=Int64),
Field(name="income", dtype=Float64),
],
source=data_sources[0],
),
FeatureView(
name="customer_features",
entities=[entities[1]],
ttl=None,
schema=[
Field(name="age", dtype=Int64),
],
source=data_sources[1],
),
FeatureView(
name="product_features",
entities=[entities[2]],
ttl=None,
schema=[
Field(name="income", dtype=Float64),
],
source=data_sources[2],
),
]
feature_services = [
FeatureService(
name="user_service",
features=[feature_views[0]],
),
FeatureService(
name="customer_service",
features=[feature_views[1]],
),
FeatureService(
name="analytics_service",
features=[feature_views[0], feature_views[1]],
),
]
saved_datasets = [
SavedDataset(
name="dataset_alpha",
features=["user_profile:age"],
join_keys=["user_id"],
storage=SavedDatasetFileStorage(path=parquet_file_path),
tags={"environment": "test", "version": "1.0"},
),
SavedDataset(
name="dataset_beta",
features=["user_profile:income"],
join_keys=["user_id"],
storage=SavedDatasetFileStorage(path=parquet_file_path),
tags={"environment": "prod", "version": "2.0"},
),
SavedDataset(
name="dataset_gamma",
features=["customer_features:age"],
join_keys=["customer_id"],
storage=SavedDatasetFileStorage(path=parquet_file_path),
tags={"environment": "test", "version": "1.5"},
),
]
store.apply(entities + data_sources + feature_views + feature_services)
for dataset in saved_datasets:
store.registry.apply_saved_dataset(dataset, "demo_project")
rest_server = RestRegistryServer(store)
client = TestClient(rest_server.app)
yield client
tmp_dir.cleanup()
def test_entities_pagination_via_rest(fastapi_test_app_with_multiple_objects):
"""Test pagination for entities endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test basic pagination - first page
response = client.get("/entities?project=demo_project&page=1&limit=2")
assert response.status_code == 200
data = response.json()
assert "entities" in data
assert "pagination" in data
assert len(data["entities"]) == 2
assert data["pagination"]["page"] == 1
assert data["pagination"]["limit"] == 2
assert data["pagination"]["totalCount"] == 6
assert data["pagination"]["totalPages"] == 3
assert data["pagination"]["hasNext"] is True
# Test pagination - second page
response = client.get("/entities?project=demo_project&page=2&limit=2")
assert response.status_code == 200
data = response.json()
assert len(data["entities"]) == 2
assert data["pagination"]["page"] == 2
assert data["pagination"]["hasNext"] is True
# Test pagination - last page
response = client.get("/entities?project=demo_project&page=3&limit=2")
assert response.status_code == 200
data = response.json()
# Page 3 might be beyond available pages
assert data["pagination"]["page"] == 3
# Test pagination beyond available pages
response = client.get("/entities?project=demo_project&page=5&limit=2")
assert response.status_code == 200
data = response.json()
# Beyond available pages should not include entities key
assert "entities" not in data or len(data["entities"]) == 0
assert data["pagination"]["page"] == 5
def test_entities_sorting_via_rest(fastapi_test_app_with_multiple_objects):
"""Test sorting for entities endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test sorting by name ascending
response = client.get("/entities?project=demo_project&sort_by=name&sort_order=asc")
assert response.status_code == 200
data = response.json()
entity_names = [entity["spec"]["name"] for entity in data["entities"]]
assert entity_names == sorted(entity_names)
# Test sorting by name descending
response = client.get("/entities?project=demo_project&sort_by=name&sort_order=desc")
assert response.status_code == 200
data = response.json()
entity_names = [entity["spec"]["name"] for entity in data["entities"]]
assert entity_names == sorted(entity_names, reverse=True)
def test_entities_pagination_with_sorting_via_rest(
fastapi_test_app_with_multiple_objects,
):
"""Test combined pagination and sorting for entities endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test pagination with sorting
response = client.get(
"/entities?project=demo_project&page=1&limit=2&sort_by=name&sort_order=asc"
)
assert response.status_code == 200
data = response.json()
assert len(data["entities"]) == 2
entity_names = [entity["spec"]["name"] for entity in data["entities"]]
assert entity_names == sorted(entity_names)
assert data["pagination"]["page"] == 1
assert data["pagination"]["limit"] == 2
assert data["pagination"]["totalCount"] == 6
def test_feature_views_pagination_via_rest(fastapi_test_app_with_multiple_objects):
"""Test pagination for feature views endpoint."""
client = fastapi_test_app_with_multiple_objects
response = client.get("/feature_views?project=demo_project&page=1&limit=2")
assert response.status_code == 200
data = response.json()
assert "featureViews" in data
assert "pagination" in data
assert len(data["featureViews"]) == 2
assert data["pagination"]["page"] == 1
assert data["pagination"]["limit"] == 2
assert data["pagination"]["totalCount"] == 3
assert data["pagination"]["totalPages"] == 2
assert data["pagination"]["hasNext"] is True
def test_feature_views_sorting_via_rest(fastapi_test_app_with_multiple_objects):
"""Test sorting for feature views endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test sorting by name ascending
response = client.get(
"/feature_views?project=demo_project&sort_by=name&sort_order=asc"
)
assert response.status_code == 200
data = response.json()
fv_names = [fv["spec"]["name"] for fv in data["featureViews"]]
assert fv_names == sorted(fv_names)
# Test sorting by name descending
response = client.get(
"/feature_views?project=demo_project&sort_by=name&sort_order=desc"
)
assert response.status_code == 200
data = response.json()
fv_names = [fv["spec"]["name"] for fv in data["featureViews"]]
assert fv_names == sorted(fv_names, reverse=True)
def test_feature_services_pagination_via_rest(fastapi_test_app_with_multiple_objects):
"""Test pagination for feature services endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test basic pagination
response = client.get("/feature_services?project=demo_project&page=1&limit=2")
assert response.status_code == 200
data = response.json()
assert "featureServices" in data
assert "pagination" in data
assert len(data["featureServices"]) == 2
assert data["pagination"]["page"] == 1
assert data["pagination"]["limit"] == 2
assert data["pagination"]["totalCount"] == 3
assert data["pagination"]["totalPages"] == 2
def test_feature_services_sorting_via_rest(fastapi_test_app_with_multiple_objects):
"""Test sorting for feature services endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test sorting by name ascending
response = client.get(
"/feature_services?project=demo_project&sort_by=name&sort_order=asc"
)
assert response.status_code == 200
data = response.json()
fs_names = [fs["spec"]["name"] for fs in data["featureServices"]]
assert fs_names == sorted(fs_names)
def test_data_sources_pagination_via_rest(fastapi_test_app_with_multiple_objects):
"""Test pagination for data sources endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test basic pagination
response = client.get("/data_sources?project=demo_project&page=1&limit=2")
assert response.status_code == 200
data = response.json()
assert "dataSources" in data
assert "pagination" in data
assert len(data["dataSources"]) == 2
assert data["pagination"]["page"] == 1
assert data["pagination"]["limit"] == 2
assert data["pagination"]["totalCount"] == 3
assert data["pagination"]["totalPages"] == 2
def test_data_sources_sorting_via_rest(fastapi_test_app_with_multiple_objects):
"""Test sorting for data sources endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test sorting by name ascending
response = client.get(
"/data_sources?project=demo_project&sort_by=name&sort_order=asc"
)
assert response.status_code == 200
data = response.json()
ds_names = [ds["name"] for ds in data["dataSources"]]
assert ds_names == sorted(ds_names)
def test_saved_datasets_pagination_via_rest(fastapi_test_app_with_multiple_objects):
"""Test pagination for saved datasets endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test basic pagination
response = client.get("/saved_datasets?project=demo_project&page=1&limit=2")
assert response.status_code == 200
data = response.json()
assert "savedDatasets" in data
assert "pagination" in data
assert len(data["savedDatasets"]) == 2
assert data["pagination"]["page"] == 1
assert data["pagination"]["limit"] == 2
assert data["pagination"]["totalCount"] == 3
assert data["pagination"]["totalPages"] == 2
def test_saved_datasets_sorting_via_rest(fastapi_test_app_with_multiple_objects):
"""Test sorting for saved datasets endpoint."""
client = fastapi_test_app_with_multiple_objects
# Test sorting by name ascending
response = client.get(
"/saved_datasets?project=demo_project&sort_by=name&sort_order=asc"
)
assert response.status_code == 200
data = response.json()
sd_names = [sd["spec"]["name"] for sd in data["savedDatasets"]]
assert sd_names == sorted(sd_names)
# Test sorting by name descending
response = client.get(
"/saved_datasets?project=demo_project&sort_by=name&sort_order=desc"