Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DynamoDB: Add software test for decoding list of objects #246

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions tests/io/dynamodb/manager.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import typing as t
from pathlib import Path

from yarl import URL
Expand Down Expand Up @@ -37,3 +38,31 @@ def load_product_catalog(self):
self.adapter.dynamodb_client.batch_write_item(RequestItems=data)
table.load()
return table

def load_records(self, table_name: str, records: t.List[t.Dict[str, t.Any]]):
table = self.adapter.dynamodb_resource.Table(table_name)
try:
table.delete()
except Exception: # noqa: S110
pass

table = self.adapter.dynamodb_resource.create_table(
TableName=table_name,
KeySchema=[
{"AttributeName": "Id", "KeyType": "HASH"},
],
AttributeDefinitions=[
{"AttributeName": "Id", "AttributeType": "N"},
],
ProvisionedThroughput={
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1,
},
TableClass="STANDARD",
)
table.wait_until_exists()

for record in records:
self.adapter.dynamodb_client.put_item(TableName=table_name, Item=record)
table.load()
return table
54 changes: 54 additions & 0 deletions tests/io/dynamodb/test_copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from cratedb_toolkit.io.dynamodb.copy import DynamoDBFullLoad

RECORD_UTM = {
"Id": {"N": "101"},
"utmTags": {
"L": [
{
"M": {
"date": {"S": "2024-08-28T20:05:42.603Z"},
"utm_adgroup": {"L": [{"S": ""}, {"S": ""}]},
"utm_campaign": {"S": "34374686341"},
"utm_medium": {"S": "foobar"},
"utm_source": {"S": "google"},
}
}
]
},
}
Comment on lines +3 to +18
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on this input data (DynamoDB table record), and a SQL DDL like

CREATE TABLE IF NOT EXISTS foobar (data OBJECT(DYNAMIC));

[...]



def test_dynamodb_copy_list_of_objects(caplog, cratedb, dynamodb, dynamodb_test_manager):
"""
CLI test: Invoke `ctk load table` for DynamoDB.
"""

# Define source and target URLs.
cratedb_url = f"{cratedb.get_connection_url()}/testdrive/demo"
dynamodb_url = f"{dynamodb.get_connection_url()}/demo?region=us-east-1"

# Populate source database with data.
dynamodb_test_manager.load_records(table_name="demo", records=[RECORD_UTM])

# Run transfer command.
table_loader = DynamoDBFullLoad(dynamodb_url=dynamodb_url, cratedb_url=cratedb_url)
table_loader.start()

# Verify data in target database.
assert cratedb.database.table_exists("testdrive.demo") is True
assert cratedb.database.refresh_table("testdrive.demo") is True
assert cratedb.database.count_records("testdrive.demo") == 1

results = cratedb.database.run_sql("SELECT * FROM testdrive.demo;", records=True) # noqa: S608
assert results[0]["data"] == {
"Id": 101.0,
"utmTags": [
{
"date": "2024-08-28T20:05:42.603Z",
"utm_adgroup": ["", ""],
"utm_campaign": "34374686341",
"utm_medium": "foobar",
"utm_source": "google",
}
],
}
Comment on lines +42 to +54
Copy link
Member Author

@amotl amotl Aug 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.... that's the outcome that proves data converges into a CrateDB record well, afaics.