コード例 #1
0
    def test_mapping_creation_with_mappings_correct(self):

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass,
                          {"name": lambda x: "{0} {1}".format(x.name, x.surname),
                           "date": lambda x: "{0} Hi!".format(str(x.date))})
        mapper.create_map(FromTestClass, ToTestClassTwo,
                          {"all": lambda x: "{0}{1}{2}".format(x.name, x.surname, x.date)})
        mapper.create_map(ToTestClassTwo, ToTestClassEmpty)

        # Act
        result1 = mapper.map(from_class, ToTestClass)
        result2 = mapper.map(from_class, ToTestClassTwo)
        result3 = mapper.map(result2, ToTestClassEmpty)

        # Assert
        self.assertTrue(isinstance(result1, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result1.name, "{0} {1}".format(from_class.name, from_class.surname),
                         "Name mapping must be equal")
        self.assertEqual(result1.date, "{0} Hi!".format(from_class.date), "Date mapping must be equal")
        self.assertNotIn("surname", result1.__dict__, "To class must not contain surname")

        self.assertTrue(isinstance(result2, ToTestClassTwo), "Type must be ToTestClassTwo")
        self.assertEqual(result2.all,
                         "{0}{1}{2}".format(from_class.name, from_class.surname, from_class.date),
                         "There must be concatenated all properties of fromTestClass")
        self.assertNotIn("name", result2.__dict__, "To class must not contain name")
        self.assertNotIn("surname", result2.__dict__, "To class must not contain surname")
        self.assertNotIn("date", result2.__dict__, "To class must not contain date")

        self.assertTrue(isinstance(result3, ToTestClassEmpty), "Type must be ToTestClassEmpty")
        self.assertTrue(len(result3.__dict__) == 0, "There must be no attributes")
コード例 #2
0
    def test_mapping_creation_none_target(self):
        """ Test mapping creation with none target """

        # Arrange
        exc = None
        from_class = None
        mappings = \
            {
                "name": lambda x: x.name + " " + x.surname,
                "date": lambda x: str(x.date) + " Happy new year!"
            }

        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass, mappings)

        # Act
        try:
            mapper.map(from_class, ToTestClass)
        except AttributeError as ex:
            exc = ex

        # Assert
        self.assertIsNotNone(exc, "AttributeError must be thrown")
        self.assertEqual("'NoneType' object has no attribute '__dict__'",
                         str(exc))
コード例 #3
0
    def test_mapping_creation_with_mappings_correct(self):
        """ Test mapping creation with mappings """

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass,
                          {"name": lambda x: "{0} {1}".format(x.name, x.surname),
                           "date": lambda x: "{0} Hi!".format(str(x.date))})
        mapper.create_map(FromTestClass, ToTestClassTwo,
                          {"all": lambda x: "{0}{1}{2}".format(x.name, x.surname, x.date)})
        mapper.create_map(ToTestClassTwo, ToTestClassEmpty)

        # Act
        result1 = mapper.map(from_class, ToTestClass)
        result2 = mapper.map(from_class, ToTestClassTwo)
        result3 = mapper.map(result2, ToTestClassEmpty)

        # Assert
        self.assertTrue(isinstance(result1, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result1.name, "{0} {1}".format(from_class.name, from_class.surname),
                         "Name mapping must be equal")
        self.assertEqual(result1.date, "{0} Hi!".format(from_class.date), "Date mapping must be equal")
        self.assertNotIn("surname", result1.__dict__, "To class must not contain surname")

        self.assertTrue(isinstance(result2, ToTestClassTwo), "Type must be ToTestClassTwo")
        self.assertEqual(result2.all,
                         "{0}{1}{2}".format(from_class.name, from_class.surname, from_class.date),
                         "There must be concatenated all properties of fromTestClass")
        self.assertNotIn("name", result2.__dict__, "To class must not contain name")
        self.assertNotIn("surname", result2.__dict__, "To class must not contain surname")
        self.assertNotIn("date", result2.__dict__, "To class must not contain date")

        self.assertTrue(isinstance(result3, ToTestClassEmpty), "Type must be ToTestClassEmpty")
        self.assertTrue(len(result3.__dict__) == 0, "There must be no attributes")
コード例 #4
0
    def test_mapping_included_field_by_mapping(self):
        """Test mapping with included fields by mapping"""
        #Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(
            FromTestClass,
            ToTestClass,
            mapping={
                '_actor_name':
                lambda o: "{0} acted by {1}".format(o.name, o._actor_name)
            })

        #Act
        result = mapper.map(FromTestClass())

        #Assert
        print(result)
        self.assertTrue(isinstance(result, ToTestClass),
                        "Type must be ToTestClass")
        self.assertEqual(result.name, from_class.name,
                         "Name mapping must be equal")
        self.assertEqual(result.date, from_class.date,
                         "Date mapping must be equal")
        self.assertEqual(
            result._actor_name,
            "{0} acted by {1}".format(from_class.name, from_class._actor_name),
            "Private is copied if explicitly mapped")
        self.assertNotIn("surname", result.__dict__,
                         "To class must not contain surname")
コード例 #5
0
    def test_mapping_creation_complex_without_mappings_correct(self):
        """ Test mapping creation for complex class without mappings """

        # Arrange
        from_class = FromTestComplexClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestComplexClass, ToTestComplexClass)
        mapper.create_map(FromTestComplexChildClass, ToTestComplexChildClass)

        # Act
        result = mapper.map(from_class)

        # Assert
        self.assertTrue(isinstance(result, ToTestComplexClass),
                        "Target types must be same")
        self.assertTrue(isinstance(result.student, ToTestComplexChildClass),
                        "Target types must be same")
        self.assertEqual(result.name, from_class.name,
                         "Name mapping must be equal")
        self.assertEqual(result.date, from_class.date,
                         "Date mapping must be equal")
        self.assertEqual(result.student.full_name,
                         from_class.student.full_name,
                         "StudentName mapping must be equal")
        self.assertEqual(len(result.knows), len(from_class.knows),
                         "number of entries must be the same for Knows")
        self.assertTrue(
            all(isinstance(k, ToTestComplexChildClass) for k in result.knows),
            "Children target types must be same")
        self.assertEqual(result.knows[0].full_name,
                         from_class.knows[0].full_name,
                         "StudentName(0) mapping must be equal")
        self.assertEqual(result.knows[1].full_name,
                         from_class.knows[1].full_name,
                         "StudentName(1) mapping must be equal")
コード例 #6
0
    def test_mapping_with_case_insensitivity(self):
        """ Test mapping with case insensitivity """

        # Arrange
        class ToTestClass2(object):
            """ To Test Class 2 """

            def __init__(self):
                self.name = ""

        class FromTestClass2(object):
            """ From Test Class 2 """

            def __init__(self):
                self.Name = "Name"

        from_class = FromTestClass2()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass2, ToTestClass2)

        # Act
        result = mapper.map(FromTestClass2(), ToTestClass2, ignore_case=True)

        # Assert
        self.assertEqual(result.name, from_class.Name, "Name mapping must be equal")
コード例 #7
0
    def test_mapping_included_field(self):
        """Test mapping with included fields"""
        #Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass)

        #Act
        result = mapper.map(FromTestClass(),
                            excluded=['name'],
                            included=['name', '_actor_name'])

        #Assert
        print(result)
        self.assertTrue(isinstance(result, ToTestClass),
                        "Type must be ToTestClass")
        self.assertEqual(
            result.name, '',
            "Name must not be copied despite of inclusion, as exclusion take precedence"
        )
        self.assertEqual(result.date, from_class.date,
                         "Date mapping must be equal")
        self.assertEqual(result._actor_name, from_class._actor_name,
                         "Private is copied if explicitly included")
        self.assertNotIn("surname", result.__dict__,
                         "To class must not contain surname")
コード例 #8
0
    def test_mapping_creation_no_mapping_defined(self):
        # Arrange
        exc = False
        msg = "No mapping defined for FromTestClass -> ToTestClass"
        from_class = FromTestClass()

        mapper = ObjectMapper()

        # Act
        try:
            mapper.map(from_class, ToTestClass)
        except ObjectMapperException as ex:
            self.assertEqual(ex.message, msg, "Exception message must be correct")
            exc = True

        # Assert
        self.assertTrue(exc, "Exception must be thrown")
コード例 #9
0
    def test_mapping_creation_invalid_mapping_function(self):

        # Arrange
        exc = False
        msg = "Invalid mapping function while setting property ToTestClass.date"
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass, {"date": lambda x: x.be + x.de})

        # Act
        try:
            mapper.map(FromTestClass(), ToTestClass)
        except ObjectMapperException as ex:
            self.assertEqual(ex.message, msg, "Exception message must be correct")
            exc = True

        # Assert
        self.assertTrue(exc, "Exception must be thrown")
コード例 #10
0
    def test_mapping_creation_invalid_mapping_function(self):
        """ Test mapping creation with invalid mapping function """

        # Arrange
        exc = False
        msg = "Invalid mapping function while setting property ToTestClass.date"
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass, {"date": lambda x: x.be + x.de})

        # Act
        try:
            mapper.map(FromTestClass())
        except ObjectMapperException as ex:
            self.assertEqual(str(ex), msg, "Exception message must be correct")
            exc = True

        # Assert
        self.assertTrue(exc, "Exception must be thrown")
コード例 #11
0
    def test_mapping_creation_no_mapping_defined(self):
        """ Test mapping creation with no mapping defined """

        # Arrange
        exc = False
        from_class = FromTestClass()
        msg = NO_MAPPING_FOUND_EXCEPTION_MESSAGE.format(from_class.__module__, from_class.__class__.__name__)
        mapper = ObjectMapper()

        # Act
        try:
            mapper.map(from_class)
        except ObjectMapperException as ex:
            self.assertEqual(str(ex), msg, "Exception message must be correct")
            exc = True

        # Assert
        self.assertTrue(exc, "Exception must be thrown")
コード例 #12
0
    def test_automatic_constructor_mapping__on_mapping_creation(self):
        mapper = ObjectMapper()

        mapper.create_map(FromTestConstructorAutoMapping,
                          ToTestConstructorAutoMapping)

        result = mapper.map(FromTestConstructorAutoMapping(),
                            ToTestConstructorAutoMapping)
        self.assertEqual(result.full_name, 'ololo')
コード例 #13
0
    def test_mapping_creation_no_mapping_defined(self):
        """ Test mapping creation with no mapping defined """

        # Arrange
        exc = False
        msg = "No mapping defined for FromTestClass -> ToTestClass"
        from_class = FromTestClass()

        mapper = ObjectMapper()

        # Act
        try:
            mapper.map(from_class, ToTestClass)
        except ObjectMapperException as ex:
            self.assertEqual(str(ex), msg, "Exception message must be correct")
            exc = True

        # Assert
        self.assertTrue(exc, "Exception must be thrown")
コード例 #14
0
    def test_mapping_creation_with_constructor_parameters_correct(self):
        mapper = ObjectMapper()

        mapper.create_map(FromTestConstructorArguments,
                          ToTestConstructorArguments,
                          constructor_mapping={'full_name': lambda o: o.name})

        result = mapper.map(FromTestConstructorArguments(),
                            ToTestConstructorArguments)
        self.assertEqual(result.full_name, 'ololo')
コード例 #15
0
class OrderRepository(object):
    def __init__(self):
        self.mapper = ObjectMapper()
        self.mapper.create_map(Order, OrderData)

    def all(self):
        return OrderData.objects.all()

    def save(self, order):
        order_data = self.mapper.map(order, OrderData)
        order_data.save()
コード例 #16
0
    def test_mapping_creation_none_target(self):
        # Arrange
        exc = False
        from_class = None
        mappings = \
            {
                "name": lambda x: x.name + " " + x.surname,
                "date": lambda x: str(x.date) + " Happy new year!"
            }

        mapper = ObjectMapper()

        # Act
        try:
            mapper.create_map(FromTestClass, ToTestClass, mappings)
            mapper.map(from_class, ToTestClass)
        except AttributeError:
            exc = True

        # Assert
        self.assertTrue(exc, "AttributeError must be thrown")
コード例 #17
0
class OrderRepository(object):

    def __init__(self):
        self.mapper = ObjectMapper()
        self.mapper.create_map(Order, OrderData)

    def all(self):
        return OrderData.objects.all()

    def save(self, order):
        order_data = self.mapper.map(order, OrderData)
        order_data.save()
コード例 #18
0
    def test_mapping_creation_without_mappings_correct(self):

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass)

        # Act
        result = mapper.map(FromTestClass(), ToTestClass)

        # Assert
        self.assertTrue(isinstance(result, ToTestClass), "Target types must be same")
        self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
        self.assertEqual(result.date, from_class.date, "Date mapping must be equal")
        self.assertNotIn("surname", result.__dict__, "To class must not contain surname")
コード例 #19
0
    def test_mapping_to_custom_metaclass_correct(self):
        # Arrange
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClassWithCustomMetaClass)
        from_class = FromTestClass()

        # Act
        result = mapper.map(from_class)

        # Assert
        self.assertTrue(isinstance(result, ToTestClassWithCustomMetaClass), "Target types must be same")
        self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
        self.assertEqual(result.date, from_class.date, "Date mapping must be equal")
        self.assertEqual(result._actor_name, "", "Private should not be copied by default")
        self.assertNotIn("surname", result.__dict__, "To class must not contain surname")
コード例 #20
0
    def test_mapping_creation_none_target(self):
        """ Test mapping creation with none target """

        # Arrange
        exc = None
        from_class = None
        mappings = \
            {
                "name": lambda x: x.name + " " + x.surname,
                "date": lambda x: str(x.date) + " Happy new year!"
            }

        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass, mappings)

        # Act
        try:
            mapper.map(from_class, ToTestClass)
        except AttributeError as ex:
            exc = ex

        # Assert
        self.assertIsNotNone(exc, "AttributeError must be thrown")
        self.assertEqual("'NoneType' object has no attribute '__dict__'", str(exc))
コード例 #21
0
class PostRepository(object):
    def __init__(self):
        self.mapper = ObjectMapper()
        self.mapper.create_map(Post, PostData)

    def all(self):
        return PostData.objects.all()

    def allOrder(self):
        return PostData.objects.filter(
            published_date__lte=timezone.now()).order_by('published_date')

    def save(self, post):
        post_data = self.mapper.map(post, PostData)
        post_data.save()
コード例 #22
0
    def test_mapping_creation_with_mapping_suppression(self):

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass,
                          {"name": None})

        # Act
        result1 = mapper.map(from_class, ToTestClass)

        # Assert
        self.assertTrue(isinstance(result1, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result1.name, "", "Name must not be mapped")
        self.assertEqual(result1.date, from_class.date, "Date is set by property name")
        self.assertNotIn("surname", result1.__dict__, "To class must not contain surname")
コード例 #23
0
    def test_mapping_excluded_field(self):
        """Test mapping with excluded fields"""
        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass)

        #Act
        result = mapper.map(FromTestClass(), excluded=['date'])

        # Assert
        print(result)
        self.assertTrue(isinstance(result, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result.name, from_class.name, "Name mapping must be equal")
        self.assertEqual(result.date, '', "Date mapping must be equal")
        self.assertNotIn("surname", result.__dict__, "To class must not contain surname")
コード例 #24
0
    def test_mapping_creation_with_mapping_suppression(self):
        """ Test mapping creation with mapping suppression """

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass,
                          {"name": None})

        # Act
        result1 = mapper.map(from_class)

        # Assert
        self.assertTrue(isinstance(result1, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result1.name, "", "Name must not be mapped")
        self.assertEqual(result1.date, from_class.date, "Date is set by property name")
        self.assertNotIn("surname", result1.__dict__, "To class must not contain surname")
コード例 #25
0
    def test_mapping_creation_with_partial_mapping_correct(self):
        """ Test mapping creation with partial mapping """

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass,
                          {"name": lambda x: "{0} {1}".format(x.name, x.surname)})

        # Act
        result1 = mapper.map(from_class)

        # Assert
        self.assertTrue(isinstance(result1, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result1.name, "{0} {1}".format(from_class.name, from_class.surname),
                         "Name mapping must be equal")
        self.assertEqual(result1.date, from_class.date, "Date mapping must be equal")
        self.assertNotIn("surname", result1.__dict__, "To class must not contain surname")
コード例 #26
0
    def test_mapping_creation_with_partial_mapping_correct(self):
        """ Test mapping creation with partial mapping """

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass,
                          {"name": lambda x: "{0} {1}".format(x.name, x.surname)})

        # Act
        result1 = mapper.map(from_class, ToTestClass)

        # Assert
        self.assertTrue(isinstance(result1, ToTestClass), "Type must be ToTestClass")
        self.assertEqual(result1.name, "{0} {1}".format(from_class.name, from_class.surname),
                         "Name mapping must be equal")
        self.assertEqual(result1.date, from_class.date, "Date mapping must be equal")
        self.assertNotIn("surname", result1.__dict__, "To class must not contain surname")
コード例 #27
0
class ProductRepository(object):

    def __init__(self):
        self.mapper = ObjectMapper()
        self.mapper.create_map(Product, ProductData)

    def get(self, id):
        # product_data = Product_data.objects.get(id)
        product = Product(name='Fake', description='Fake', price=11)
        product.id = 1
        return product

    def save(self, product):
        product_data = self.mapper.map(product, ProductData)
        product_data.save()

    def all(self):
        return ProductData.objects.all()
コード例 #28
0
    def test_mapping_creation_with_custom_dir(self):
        """ Test mapping to objects with custom __dir__ behaviour """

        # Arrange
        _propNames = ['name', 'date']

        class ToCustomDirClass(object):
            def __dir__(self):
                props = list(self.__dict__.keys())
                props.extend(_propNames)
                return props

            def __init__(self):
                self.props = {k: None for k in _propNames}

            def __getattribute__(self, name):
                if name in _propNames:
                    return self.props[name]
                else:
                    return object.__getattribute__(self, name)

            def __setattr__(self, name, value):
                if name in _propNames:
                    self.props[name] = value
                else:
                    return object.__setattr__(self, name, value)

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToCustomDirClass)

        # Act
        result = mapper.map(FromTestClass(), ToCustomDirClass)

        # Assert
        self.assertTrue(isinstance(result, ToCustomDirClass),
                        "Target types must be same")
        self.assertEqual(result.name, from_class.name,
                         "Name mapping must be equal")
        self.assertEqual(result.date, from_class.date,
                         "Date mapping must be equal")
        self.assertNotIn("surname", dir(result),
                         "To class must not contain surname")
コード例 #29
0
    def test_mapping_with_none_source_and_allow_none_returns_none(self):
        """ Test mapping with none source and allow none returns none """

        # Arrange
        from_class = None
        mappings = \
            {
                "name": lambda x: x.name + " " + x.surname,
                "date": lambda x: str(x.date) + " Happy new year!"
            }

        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass, mappings)

        # Act
        result = mapper.map(from_class, ToTestClass, allow_none=True)

        # Assert
        self.assertEqual(None, result)
コード例 #30
0
    def test_mapping_with_none_source_and_allow_none_returns_none(self):
        """ Test mapping with none source and allow none returns none """

        # Arrange
        from_class = None
        mappings = \
            {
                "name": lambda x: x.name + " " + x.surname,
                "date": lambda x: str(x.date) + " Happy new year!"
            }

        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass, mappings)

        # Act
        result = mapper.map(from_class, ToTestClass, allow_none=True)

        # Assert
        self.assertEqual(None, result)
コード例 #31
0
    def test_mapping_creation_without_mappings_correct(self):
        """ Test mapping creation without mappings """

        # Arrange
        from_class = FromTestClass()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass, ToTestClass)

        # Act
        result = mapper.map(FromTestClass(), ToTestClass)

        # Assert
        self.assertTrue(isinstance(result, ToTestClass),
                        "Target types must be same")
        self.assertEqual(result.name, from_class.name,
                         "Name mapping must be equal")
        self.assertEqual(result.date, from_class.date,
                         "Date mapping must be equal")
        self.assertNotIn("surname", result.__dict__,
                         "To class must not contain surname")
コード例 #32
0
    def test_mapping_with_case_insensitivity(self):

        # Arrange
        class ToTestClass2:
            def __init__(self):
                self.name = ""

        class FromTestClass2:
            def __init__(self):
                self.Name = "Name"

        from_class = FromTestClass2()
        mapper = ObjectMapper()
        mapper.create_map(FromTestClass2, ToTestClass2)

        # Act
        result = mapper.map(FromTestClass2(), ToTestClass2, ignore_case=True)

        # Assert
        self.assertEqual(result.name, from_class.Name, "Name mapping must be equal")
コード例 #33
0
from mapper.object_mapper import ObjectMapper
from api.persistence.models.photo_database import PhotoDatabase
from api.persistence.models.history import History
from api.persistence.models.person import Person
from api.services.domain_models.photo_database_dm import PhotoDatabaseDomainModel
from api.services.domain_models.history_dm import HistoryDomainModel
from api.services.domain_models.person_dm import PersonDomainModel

mapper = ObjectMapper()

mapper.create_map(PhotoDatabase, PhotoDatabaseDomainModel)
mapper.create_map(
    History, HistoryDomainModel,
    {"person": lambda history: mapper.map(history.person, PersonDomainModel)})
mapper.create_map(
    Person, PersonDomainModel, {
        "photo_database":
        lambda person: mapper.map(person.photo_database,
                                  PhotoDatabaseDomainModel)
    })

mapper.create_map(PhotoDatabaseDomainModel, PhotoDatabase)
mapper.create_map(HistoryDomainModel, History)
mapper.create_map(PersonDomainModel, Person)