-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MongoDB: Fix and verify Zyp transformations
- Loading branch information
Showing
8 changed files
with
92 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Zyp transformation defining a schema override on a top-level column. | ||
|
||
# Timestamps in Unixtime/Epoch format are sometimes stored as | ||
# int64 / BIGINT. This transformation defines an adjustment to | ||
# make the target schema use a native datetime/timestamp column. | ||
--- | ||
meta: | ||
type: zyp-project | ||
version: 1 | ||
collections: | ||
- address: | ||
container: testdrive | ||
name: demo | ||
schema: | ||
rules: | ||
- pointer: /timestamp | ||
type: DATETIME |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
from sqlalchemy import TIMESTAMP | ||
|
||
from tests.conftest import check_sqlalchemy2 | ||
|
||
pytestmark = pytest.mark.mongodb | ||
|
||
pymongo = pytest.importorskip("pymongo", reason="Skipping tests because pymongo is not installed") | ||
pytest.importorskip("rich", reason="Skipping tests because rich is not installed") | ||
|
||
from cratedb_toolkit.io.mongodb.api import mongodb_copy # noqa: E402 | ||
|
||
|
||
@pytest.fixture(scope="module", autouse=True) | ||
def check_prerequisites(): | ||
""" | ||
This subsystem needs SQLAlchemy 2.x. | ||
""" | ||
check_sqlalchemy2() | ||
|
||
|
||
def test_mongodb_copy_transform_timestamp(caplog, cratedb, mongodb): | ||
""" | ||
Verify MongoDB -> CrateDB data transfer with transformation. | ||
""" | ||
cratedb_url = f"{cratedb.get_connection_url()}/testdrive/demo" | ||
mongodb_url = f"{mongodb.get_connection_url()}/testdrive/demo" | ||
|
||
# Populate source database. | ||
client: pymongo.MongoClient = mongodb.get_connection_client() | ||
testdrive = client.get_database("testdrive") | ||
demo = testdrive.create_collection("demo") | ||
demo.insert_one({"device": "Hotzenplotz", "temperature": 42.42, "timestamp": 1563051934000}) | ||
|
||
# Run transfer command. | ||
mongodb_copy( | ||
mongodb_url, | ||
cratedb_url, | ||
transformation=Path("examples/zyp/zyp-int64-to-timestamp.yaml"), | ||
) | ||
|
||
# Verify data in target database. | ||
cratedb.database.refresh_table("testdrive.demo") | ||
results = cratedb.database.run_sql("SELECT * FROM testdrive.demo;", records=True) | ||
assert results[0]["timestamp"] == 1563051934000 | ||
|
||
# Verify schema in target database. | ||
columns = cratedb.database.describe_table_columns("testdrive.demo") | ||
timestamp_type = columns[3]["type"] | ||
assert isinstance(timestamp_type, TIMESTAMP) |