Skip to content

Commit 4606d7c

Browse files
authored
test: DynamodbOnlineStore public methods unit tests suite (#2571)
* test: DynamoDBOnlineStore online_write_batch unit test Signed-off-by: Miguel Trejo <[email protected]> * test: DynamoDBOnlineStore teardown unit test Signed-off-by: Miguel Trejo <[email protected]> * test: DynamoDBOnlineStore update unit test Signed-off-by: Miguel Trejo <[email protected]> * refactor: used fixture variable directly Signed-off-by: Miguel Trejo <[email protected]> * refactor: consistency in test names Signed-off-by: Miguel Trejo <[email protected]> * fix test errors Signed-off-by: Miguel Trejo <[email protected]> * mock all tests Signed-off-by: Miguel Trejo <[email protected]>
1 parent c439611 commit 4606d7c

File tree

1 file changed

+112
-23
lines changed

1 file changed

+112
-23
lines changed

sdk/python/tests/unit/infra/online_store/test_dynamodb_online_store.py

Lines changed: 112 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ def repo_config():
4343
)
4444

4545

46-
def test_online_store_config_default():
46+
@pytest.fixture
47+
def dynamodb_online_store():
48+
return DynamoDBOnlineStore()
49+
50+
51+
def test_dynamodb_online_store_config_default():
4752
"""Test DynamoDBOnlineStoreConfig default parameters."""
4853
aws_region = "us-west-2"
4954
dynamodb_store_config = DynamoDBOnlineStoreConfig(region=aws_region)
@@ -66,7 +71,7 @@ def test_dynamodb_table_default_params():
6671
assert dynamodb_table._dynamodb_resource is None
6772

6873

69-
def test_online_store_config_custom_params():
74+
def test_dynamodb_online_store_config_custom_params():
7075
"""Test DynamoDBOnlineStoreConfig custom parameters."""
7176
aws_region = "us-west-2"
7277
batch_size = 20
@@ -98,15 +103,14 @@ def test_dynamodb_table_custom_params():
98103
assert dynamodb_table._dynamodb_resource is None
99104

100105

101-
def test_online_store_config_dynamodb_client():
106+
def test_dynamodb_online_store_config_dynamodb_client(dynamodb_online_store):
102107
"""Test DynamoDBOnlineStoreConfig configure DynamoDB client with endpoint_url."""
103108
aws_region = "us-west-2"
104109
endpoint_url = "http://localhost:8000"
105-
dynamodb_store = DynamoDBOnlineStore()
106110
dynamodb_store_config = DynamoDBOnlineStoreConfig(
107111
region=aws_region, endpoint_url=endpoint_url
108112
)
109-
dynamodb_client = dynamodb_store._get_dynamodb_client(
113+
dynamodb_client = dynamodb_online_store._get_dynamodb_client(
110114
dynamodb_store_config.region, dynamodb_store_config.endpoint_url
111115
)
112116
assert dynamodb_client.meta.region_name == aws_region
@@ -126,15 +130,14 @@ def test_dynamodb_table_dynamodb_client():
126130
assert dynamodb_client.meta.endpoint_url == endpoint_url
127131

128132

129-
def test_online_store_config_dynamodb_resource():
133+
def test_dynamodb_online_store_config_dynamodb_resource(dynamodb_online_store):
130134
"""Test DynamoDBOnlineStoreConfig configure DynamoDB Resource with endpoint_url."""
131135
aws_region = "us-west-2"
132136
endpoint_url = "http://localhost:8000"
133-
dynamodb_store = DynamoDBOnlineStore()
134137
dynamodb_store_config = DynamoDBOnlineStoreConfig(
135138
region=aws_region, endpoint_url=endpoint_url
136139
)
137-
dynamodb_resource = dynamodb_store._get_dynamodb_resource(
140+
dynamodb_resource = dynamodb_online_store._get_dynamodb_resource(
138141
dynamodb_store_config.region, dynamodb_store_config.endpoint_url
139142
)
140143
assert dynamodb_resource.meta.client.meta.region_name == aws_region
@@ -156,36 +159,123 @@ def test_dynamodb_table_dynamodb_resource():
156159

157160
@mock_dynamodb2
158161
@pytest.mark.parametrize("n_samples", [5, 50, 100])
159-
def test_online_read(repo_config, n_samples):
162+
def test_dynamodb_online_store_online_read(
163+
repo_config, dynamodb_online_store, n_samples
164+
):
160165
"""Test DynamoDBOnlineStore online_read method."""
161-
_create_test_table(PROJECT, f"{TABLE_NAME}_{n_samples}", REGION)
166+
db_table_name = f"{TABLE_NAME}_online_read_{n_samples}"
167+
_create_test_table(PROJECT, db_table_name, REGION)
162168
data = _create_n_customer_test_samples(n=n_samples)
163-
_insert_data_test_table(data, PROJECT, f"{TABLE_NAME}_{n_samples}", REGION)
169+
_insert_data_test_table(data, PROJECT, db_table_name, REGION)
164170

165171
entity_keys, features, *rest = zip(*data)
166-
dynamodb_store = DynamoDBOnlineStore()
167-
returned_items = dynamodb_store.online_read(
172+
returned_items = dynamodb_online_store.online_read(
168173
config=repo_config,
169-
table=MockFeatureView(name=f"{TABLE_NAME}_{n_samples}"),
174+
table=MockFeatureView(name=db_table_name),
170175
entity_keys=entity_keys,
171176
)
172177
assert len(returned_items) == len(data)
173178
assert [item[1] for item in returned_items] == list(features)
174179

175180

176181
@mock_dynamodb2
177-
def test_online_read_unknown_entity(repo_config):
182+
@pytest.mark.parametrize("n_samples", [5, 50, 100])
183+
def test_dynamodb_online_store_online_write_batch(
184+
repo_config, dynamodb_online_store, n_samples
185+
):
186+
"""Test DynamoDBOnlineStore online_write_batch method."""
187+
db_table_name = f"{TABLE_NAME}_online_write_batch_{n_samples}"
188+
_create_test_table(PROJECT, db_table_name, REGION)
189+
data = _create_n_customer_test_samples()
190+
191+
entity_keys, features, *rest = zip(*data)
192+
dynamodb_online_store.online_write_batch(
193+
config=repo_config,
194+
table=MockFeatureView(name=db_table_name),
195+
data=data,
196+
progress=None,
197+
)
198+
stored_items = dynamodb_online_store.online_read(
199+
config=repo_config,
200+
table=MockFeatureView(name=db_table_name),
201+
entity_keys=entity_keys,
202+
)
203+
assert stored_items is not None
204+
assert len(stored_items) == len(data)
205+
assert [item[1] for item in stored_items] == list(features)
206+
207+
208+
@mock_dynamodb2
209+
def test_dynamodb_online_store_update(repo_config, dynamodb_online_store):
210+
"""Test DynamoDBOnlineStore update method."""
211+
# create dummy table to keep
212+
db_table_keep_name = f"{TABLE_NAME}_keep_update"
213+
_create_test_table(PROJECT, db_table_keep_name, REGION)
214+
# create dummy table to delete
215+
db_table_delete_name = f"{TABLE_NAME}_delete_update"
216+
_create_test_table(PROJECT, db_table_delete_name, REGION)
217+
218+
dynamodb_online_store.update(
219+
config=repo_config,
220+
tables_to_delete=[MockFeatureView(name=db_table_delete_name)],
221+
tables_to_keep=[MockFeatureView(name=db_table_keep_name)],
222+
entities_to_delete=None,
223+
entities_to_keep=None,
224+
partial=None,
225+
)
226+
227+
# check only db_table_keep_name exists
228+
dynamodb_client = dynamodb_online_store._get_dynamodb_client(REGION)
229+
existing_tables = dynamodb_client.list_tables()
230+
existing_tables = existing_tables.get("TableNames", None)
231+
232+
assert existing_tables is not None
233+
assert len(existing_tables) == 1
234+
assert existing_tables[0] == f"test_aws.{db_table_keep_name}"
235+
236+
237+
@mock_dynamodb2
238+
def test_dynamodb_online_store_teardown(repo_config, dynamodb_online_store):
239+
"""Test DynamoDBOnlineStore teardown method."""
240+
db_table_delete_name_one = f"{TABLE_NAME}_delete_teardown_1"
241+
db_table_delete_name_two = f"{TABLE_NAME}_delete_teardown_2"
242+
_create_test_table(PROJECT, db_table_delete_name_one, REGION)
243+
_create_test_table(PROJECT, db_table_delete_name_two, REGION)
244+
245+
dynamodb_online_store.teardown(
246+
config=repo_config,
247+
tables=[
248+
MockFeatureView(name=db_table_delete_name_one),
249+
MockFeatureView(name=db_table_delete_name_two),
250+
],
251+
entities=None,
252+
)
253+
254+
# Check tables non exist
255+
dynamodb_client = dynamodb_online_store._get_dynamodb_client(REGION)
256+
existing_tables = dynamodb_client.list_tables()
257+
existing_tables = existing_tables.get("TableNames", None)
258+
259+
assert existing_tables is not None
260+
assert len(existing_tables) == 0
261+
262+
263+
@mock_dynamodb2
264+
def test_dynamodb_online_store_online_read_unknown_entity(
265+
repo_config, dynamodb_online_store
266+
):
178267
"""Test DynamoDBOnlineStore online_read method."""
179268
n_samples = 2
180-
_create_test_table(PROJECT, f"{TABLE_NAME}_{n_samples}", REGION)
269+
_create_test_table(PROJECT, f"{TABLE_NAME}_unknown_entity_{n_samples}", REGION)
181270
data = _create_n_customer_test_samples(n=n_samples)
182-
_insert_data_test_table(data, PROJECT, f"{TABLE_NAME}_{n_samples}", REGION)
271+
_insert_data_test_table(
272+
data, PROJECT, f"{TABLE_NAME}_unknown_entity_{n_samples}", REGION
273+
)
183274

184275
entity_keys, features, *rest = zip(*data)
185276
# Append a nonsensical entity to search for
186277
entity_keys = list(entity_keys)
187278
features = list(features)
188-
dynamodb_store = DynamoDBOnlineStore()
189279

190280
# Have the unknown entity be in the beginning, middle, and end of the list of entities.
191281
for pos in range(len(entity_keys)):
@@ -198,9 +288,9 @@ def test_online_read_unknown_entity(repo_config):
198288
)
199289
features_with_none = deepcopy(features)
200290
features_with_none.insert(pos, None)
201-
returned_items = dynamodb_store.online_read(
291+
returned_items = dynamodb_online_store.online_read(
202292
config=repo_config,
203-
table=MockFeatureView(name=f"{TABLE_NAME}_{n_samples}"),
293+
table=MockFeatureView(name=f"{TABLE_NAME}_unknown_entity_{n_samples}"),
204294
entity_keys=entity_keys_with_unknown,
205295
)
206296
assert len(returned_items) == len(entity_keys_with_unknown)
@@ -210,17 +300,16 @@ def test_online_read_unknown_entity(repo_config):
210300

211301

212302
@mock_dynamodb2
213-
def test_write_batch_non_duplicates(repo_config):
303+
def test_write_batch_non_duplicates(repo_config, dynamodb_online_store):
214304
"""Test DynamoDBOnline Store deduplicate write batch request items."""
215305
dynamodb_tbl = f"{TABLE_NAME}_batch_non_duplicates"
216306
_create_test_table(PROJECT, dynamodb_tbl, REGION)
217307
data = _create_n_customer_test_samples()
218308
data_duplicate = deepcopy(data)
219309
dynamodb_resource = boto3.resource("dynamodb", region_name=REGION)
220310
table_instance = dynamodb_resource.Table(f"{PROJECT}.{dynamodb_tbl}")
221-
dynamodb_store = DynamoDBOnlineStore()
222311
# Insert duplicate data
223-
dynamodb_store._write_batch_non_duplicates(
312+
dynamodb_online_store._write_batch_non_duplicates(
224313
table_instance, data + data_duplicate, progress=None
225314
)
226315
# Request more items than inserted

0 commit comments

Comments
 (0)