fixed bug in camel case transformer
This commit is contained in:
parent
726298b155
commit
20c980dbe6
@ -1,3 +1,5 @@
|
||||
from typing import Iterable
|
||||
|
||||
from image_prediction.formatter.formatter import Formatter
|
||||
|
||||
|
||||
@ -14,6 +16,11 @@ class Snake2CamelCaseKeyFormatter(Formatter):
|
||||
|
||||
def __format(self, data):
|
||||
|
||||
# If we wanted to do this properly, we would need handlers for all expected types and dispatch based
|
||||
# on a type comparison. This is too much engineering for the limited use-case of this class though.
|
||||
if isinstance(data, Iterable) and not isinstance(data, dict) and not isinstance(data, str):
|
||||
return type(data)(map(self.__format, data))
|
||||
|
||||
if not isinstance(data, dict):
|
||||
return data
|
||||
|
||||
|
||||
@ -1,15 +1,31 @@
|
||||
import pytest
|
||||
|
||||
from image_prediction.compositor.compositor import TransformerCompositor
|
||||
from image_prediction.formatter.formatters.camel_case import Snake2CamelCaseKeyFormatter
|
||||
from image_prediction.formatter.formatters.enum import EnumFormatter
|
||||
from image_prediction.formatter.formatters.identity import IdentityFormatter
|
||||
|
||||
|
||||
def test_single_formatter(metadata):
|
||||
def test_identity(metadata):
|
||||
compositor = TransformerCompositor(IdentityFormatter())
|
||||
assert metadata == compositor(metadata)
|
||||
assert compositor(metadata) == metadata
|
||||
|
||||
|
||||
def test_two_formatters(metadata, metadata_formatted):
|
||||
def test_composition(metadata, metadata_formatted):
|
||||
compositor = TransformerCompositor(IdentityFormatter(), EnumFormatter())
|
||||
assert metadata_formatted == list(compositor(metadata))
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def compositor_test_enum_metadata(info_label_map):
|
||||
return [{info_label_map.WIDTH: 100, info_label_map.PAGE_WIDTH: 200}]
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def compositor_test_camelcase_metadata(info_label_map):
|
||||
return [{"width": 100, "pageWidth": 200}]
|
||||
|
||||
|
||||
def test_enum_to_camel_case(compositor_test_enum_metadata, compositor_test_camelcase_metadata):
|
||||
compositor = TransformerCompositor(EnumFormatter(), Snake2CamelCaseKeyFormatter())
|
||||
assert list(compositor(compositor_test_enum_metadata)) == compositor_test_camelcase_metadata
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user