Skip to content

Commit

Permalink
MongoDB: Allow running decoder without any transformation at all
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 18, 2024
1 parent 22b388b commit e6095fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/commons_codec/transform/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
import datetime as dt
import logging
import typing as t
from typing import Any, Iterable
from typing import Iterable

import bson
import dateutil.parser as dateparser
from attr import Factory
from attrs import define
from bson.json_util import _json_convert, object_hook
from pymongo.cursor import Cursor
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__)
Expand Down Expand Up @@ -45,13 +45,16 @@ class MongoDBCrateDBConverter:
Extracted from cratedb-toolkit, earlier migr8.
"""

transformation: CollectionTransformation = Factory(CollectionTransformation)
transformation: t.Union[CollectionTransformation, None] = None

def decode_documents(self, data: t.List[Document]) -> Iterable[dict[str, Any]]:
def decode_documents(self, data: t.Iterable[Document]) -> Iterable[Document]:
"""
Decode MongoDB Extended JSON, considering CrateDB specifics.
"""
return self.transformation.apply(map(self.decode_value, data))
data = map(self.decode_value, data)
if self.transformation is not None:
data = self.transformation.apply(t.cast(DictOrList, data))
return data

def decode_document(self, data: Document) -> Document:
"""
Expand Down
23 changes: 23 additions & 0 deletions tests/transform/mongodb/test_mongodb_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,29 @@ def test_date_converter_invalid():
assert ex.match("Unable to convert datetime value: None")


def test_convert_basic():
"""
Just a basic conversion, without transformation.
"""
data_in = {
"_id": {
"$oid": "56027fcae4b09385a85f9344",
},
"value": {
"id": 42,
},
}
data_out = {
"_id": "56027fcae4b09385a85f9344",
"value": {
"id": 42,
},
}

converter = MongoDBCrateDBConverter()
assert list(converter.decode_documents([data_in])) == [data_out]


def test_convert_with_treatment_ignore_complex_lists():
"""
The `ignore_complex_lists` treatment ignores lists of dictionaries, often having deviating substructures.
Expand Down

0 comments on commit e6095fc

Please sign in to comment.