Skip to content

Commit

Permalink
Dependencies: Make MongoDB subsystem not strictly depend on Zyp
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 18, 2024
1 parent 116f0c4 commit 5befd3e
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ jobs:
pip install "setuptools>=64" --upgrade
# Install package in editable mode.
pip install --use-pep517 --prefer-binary --editable=.[mongodb,develop,test]
pip install --use-pep517 --prefer-binary --editable=.[develop,test]
- name: Run linters and software tests
run: poe test -- -m 'dynamodb'
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:
pip install "setuptools>=64" --upgrade
# Install package in editable mode.
pip install --use-pep517 --prefer-binary --editable=.[mongodb,develop,test]
pip install --use-pep517 --prefer-binary --editable=.[mongodb,zyp,develop,test]
- name: Run linters and software tests
run: poe test -- -m 'mongodb'
Expand Down
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- MongoDB: Use `bson` package to parse BSON CANONICAL representation
- MongoDB: Complete and verify BSON data type mapping end-to-end
- MongoDB: Use improved decoding machinery also for `MongoDBCDCTranslator`
- Dependencies: Make MongoDB subsystem not strictly depend on Zyp

## 2024/09/10 v0.0.15
- Added Zyp Treatments, a slightly tailored transformation subsystem
Expand Down
13 changes: 6 additions & 7 deletions src/commons_codec/transform/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
from sqlalchemy_cratedb.support import quote_relation_name

from commons_codec.model import SQLOperation
from zyp.model.base import DictOrList
from zyp.model.collection import CollectionTransformation

logger = logging.getLogger(__name__)


Document = t.Mapping[str, t.Any]
DocumentCollection = t.List[Document]


logger = logging.getLogger(__name__)


def date_converter(value):
if isinstance(value, int):
return value
Expand All @@ -45,15 +43,16 @@ class MongoDBCrateDBConverter:
Extracted from cratedb-toolkit, earlier migr8.
"""

transformation: t.Union[CollectionTransformation, None] = None
transformation: t.Any = None

def decode_documents(self, data: t.Iterable[Document]) -> Iterable[Document]:
"""
Decode MongoDB Extended JSON, considering CrateDB specifics.
"""
data = map(self.decode_value, data)
# TODO: This is currently untyped. Types are defined in Zyp, see `zyp.model.base`.
if self.transformation is not None:
data = self.transformation.apply(t.cast(DictOrList, data))
data = self.transformation.apply(data)
return data

def decode_document(self, data: Document) -> Document:
Expand Down
3 changes: 3 additions & 0 deletions tests/transform/mongodb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import pytest

pytest.importorskip("bson", reason="Skipping MongoDB/BSON tests because 'bson' package is not installed")
5 changes: 1 addition & 4 deletions tests/transform/mongodb/test_mongodb_cdc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@

import datetime

from commons_codec.model import SQLOperation

pytest.importorskip("pymongo")

from bson import ObjectId, Timestamp

from commons_codec.model import SQLOperation
from commons_codec.transform.mongodb import MongoDBCDCTranslator

MSG_OPERATION_UNKNOWN = {
Expand Down
5 changes: 3 additions & 2 deletions tests/transform/mongodb/test_mongodb_convert.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# ruff: noqa: E402
import pytest

pytestmark = pytest.mark.mongodb

from commons_codec.transform.mongodb import MongoDBCrateDBConverter, date_converter
from zyp.model.bucket import BucketTransformation, ValueConverter
from zyp.model.collection import CollectionTransformation
from zyp.model.treatment import Treatment

pytestmark = pytest.mark.mongodb


def test_date_converter_int():
"""
Expand Down
3 changes: 3 additions & 0 deletions tests/transform/mongodb/test_mongodb_full.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ruff: noqa: E402
import pytest

pytestmark = pytest.mark.mongodb

from commons_codec.model import SQLOperation
from commons_codec.transform.mongodb import MongoDBFullLoadTranslator
from tests.transform.mongodb.data import RECORD_IN_ALL_TYPES, RECORD_OUT_ALL_TYPES
Expand Down

0 comments on commit 5befd3e

Please sign in to comment.