|
| 1 | +"""Unit tests for Milvus online store feature view versioning.""" |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | +from unittest.mock import MagicMock |
| 5 | + |
| 6 | +from feast import Entity, FeatureView |
| 7 | +from feast.field import Field |
| 8 | +from feast.types import Float32 |
| 9 | +from feast.value_type import ValueType |
| 10 | + |
| 11 | + |
| 12 | +def _make_feature_view(name="driver_stats", version_number=None, version_tag=None): |
| 13 | + entity = Entity( |
| 14 | + name="driver_id", |
| 15 | + join_keys=["driver_id"], |
| 16 | + value_type=ValueType.INT64, |
| 17 | + ) |
| 18 | + fv = FeatureView( |
| 19 | + name=name, |
| 20 | + entities=[entity], |
| 21 | + ttl=timedelta(days=1), |
| 22 | + schema=[Field(name="trips_today", dtype=Float32)], |
| 23 | + ) |
| 24 | + if version_number is not None: |
| 25 | + fv.current_version_number = version_number |
| 26 | + if version_tag is not None: |
| 27 | + fv.projection.version_tag = version_tag |
| 28 | + return fv |
| 29 | + |
| 30 | + |
| 31 | +def _make_config(project="test_project", versioning=False): |
| 32 | + config = MagicMock() |
| 33 | + config.project = project |
| 34 | + config.entity_key_serialization_version = 2 |
| 35 | + config.registry.enable_online_feature_view_versioning = versioning |
| 36 | + return config |
| 37 | + |
| 38 | + |
| 39 | +class TestTableId: |
| 40 | + """Test _table_id with versioning enabled/disabled.""" |
| 41 | + |
| 42 | + def test_no_versioning(self): |
| 43 | + from feast.infra.online_stores.milvus_online_store.milvus import _table_id |
| 44 | + |
| 45 | + fv = _make_feature_view() |
| 46 | + config = _make_config(versioning=False) |
| 47 | + assert _table_id(config.project, fv) == "test_project_driver_stats" |
| 48 | + |
| 49 | + def test_versioning_enabled_with_version(self): |
| 50 | + from feast.infra.online_stores.milvus_online_store.milvus import _table_id |
| 51 | + |
| 52 | + fv = _make_feature_view(version_number=2) |
| 53 | + config = _make_config(versioning=True) |
| 54 | + assert ( |
| 55 | + _table_id(config.project, fv, enable_versioning=True) |
| 56 | + == "test_project_driver_stats_v2" |
| 57 | + ) |
| 58 | + |
| 59 | + def test_projection_version_tag_takes_priority(self): |
| 60 | + from feast.infra.online_stores.milvus_online_store.milvus import _table_id |
| 61 | + |
| 62 | + fv = _make_feature_view(version_number=1, version_tag=3) |
| 63 | + config = _make_config(versioning=True) |
| 64 | + assert ( |
| 65 | + _table_id(config.project, fv, enable_versioning=True) |
| 66 | + == "test_project_driver_stats_v3" |
| 67 | + ) |
| 68 | + |
| 69 | + def test_version_zero_no_suffix(self): |
| 70 | + from feast.infra.online_stores.milvus_online_store.milvus import _table_id |
| 71 | + |
| 72 | + fv = _make_feature_view(version_number=0) |
| 73 | + config = _make_config(versioning=True) |
| 74 | + assert ( |
| 75 | + _table_id(config.project, fv, enable_versioning=True) |
| 76 | + == "test_project_driver_stats" |
| 77 | + ) |
| 78 | + |
| 79 | + def test_versioning_enabled_no_version_set(self): |
| 80 | + from feast.infra.online_stores.milvus_online_store.milvus import _table_id |
| 81 | + |
| 82 | + fv = _make_feature_view() |
| 83 | + config = _make_config(versioning=True) |
| 84 | + assert ( |
| 85 | + _table_id(config.project, fv, enable_versioning=True) |
| 86 | + == "test_project_driver_stats" |
| 87 | + ) |
| 88 | + |
| 89 | + def test_versioning_disabled_ignores_version(self): |
| 90 | + from feast.infra.online_stores.milvus_online_store.milvus import _table_id |
| 91 | + |
| 92 | + fv = _make_feature_view(version_number=5) |
| 93 | + config = _make_config(versioning=False) |
| 94 | + assert _table_id(config.project, fv) == "test_project_driver_stats" |
| 95 | + |
| 96 | + |
| 97 | +class TestMilvusVersionedReadSupport: |
| 98 | + """Test that MilvusOnlineStore passes _check_versioned_read_support.""" |
| 99 | + |
| 100 | + def test_allowed_with_version_tag(self): |
| 101 | + from feast.infra.online_stores.milvus_online_store.milvus import ( |
| 102 | + MilvusOnlineStore, |
| 103 | + ) |
| 104 | + |
| 105 | + store = MilvusOnlineStore() |
| 106 | + fv = _make_feature_view() |
| 107 | + fv.projection.version_tag = 2 |
| 108 | + store._check_versioned_read_support([(fv, ["trips_today"])]) |
| 109 | + |
| 110 | + def test_allowed_without_version_tag(self): |
| 111 | + from feast.infra.online_stores.milvus_online_store.milvus import ( |
| 112 | + MilvusOnlineStore, |
| 113 | + ) |
| 114 | + |
| 115 | + store = MilvusOnlineStore() |
| 116 | + fv = _make_feature_view() |
| 117 | + store._check_versioned_read_support([(fv, ["trips_today"])]) |
| 118 | + |
| 119 | + |
| 120 | +class TestTeardownDropsAllVersions: |
| 121 | + """Teardown should drop the base collection AND all versioned collections.""" |
| 122 | + |
| 123 | + def _build_store_with_collections(self, existing_collections): |
| 124 | + from feast.infra.online_stores.milvus_online_store.milvus import ( |
| 125 | + MilvusOnlineStore, |
| 126 | + ) |
| 127 | + |
| 128 | + store = MilvusOnlineStore() |
| 129 | + store.client = MagicMock() |
| 130 | + store.client.list_collections.return_value = existing_collections |
| 131 | + store._connect = MagicMock(return_value=store.client) |
| 132 | + store._collections = {name: MagicMock() for name in existing_collections} |
| 133 | + return store |
| 134 | + |
| 135 | + def test_teardown_drops_base_and_all_versioned_collections(self): |
| 136 | + fv = _make_feature_view() |
| 137 | + config = _make_config(versioning=True) |
| 138 | + existing = [ |
| 139 | + "test_project_driver_stats", |
| 140 | + "test_project_driver_stats_v1", |
| 141 | + "test_project_driver_stats_v2", |
| 142 | + "test_project_other_view", # unrelated, must not be dropped |
| 143 | + ] |
| 144 | + store = self._build_store_with_collections(existing) |
| 145 | + |
| 146 | + store.teardown(config, [fv], []) |
| 147 | + |
| 148 | + dropped = {call.args[0] for call in store.client.drop_collection.call_args_list} |
| 149 | + assert dropped == { |
| 150 | + "test_project_driver_stats", |
| 151 | + "test_project_driver_stats_v1", |
| 152 | + "test_project_driver_stats_v2", |
| 153 | + } |
| 154 | + assert "test_project_other_view" not in dropped |
| 155 | + |
| 156 | + def test_update_drops_all_versions_for_deleted_table(self): |
| 157 | + fv = _make_feature_view() |
| 158 | + config = _make_config(versioning=True) |
| 159 | + existing = [ |
| 160 | + "test_project_driver_stats", |
| 161 | + "test_project_driver_stats_v3", |
| 162 | + "test_project_driver_stats_v4", |
| 163 | + ] |
| 164 | + store = self._build_store_with_collections(existing) |
| 165 | + |
| 166 | + store.update( |
| 167 | + config=config, |
| 168 | + tables_to_delete=[fv], |
| 169 | + tables_to_keep=[], |
| 170 | + entities_to_delete=[], |
| 171 | + entities_to_keep=[], |
| 172 | + partial=False, |
| 173 | + ) |
| 174 | + |
| 175 | + dropped = {call.args[0] for call in store.client.drop_collection.call_args_list} |
| 176 | + assert dropped == { |
| 177 | + "test_project_driver_stats", |
| 178 | + "test_project_driver_stats_v3", |
| 179 | + "test_project_driver_stats_v4", |
| 180 | + } |
0 commit comments