Exemple #1
0
    def test_map_object_allow_empty_strings(self):
        # given:
        json_object = json.loads('''{
            "pet_name": "Champ",
            "favourite_food": ""
        }''')

        # when:
        pet_json = JsonMapper(json_object).map({
            'name': ['pet_name'],
            'fav_food': ['favourite_food']
        })

        # then:
        self.assertEqual('Champ', pet_json.get('name'))
        self.assertEqual('', pet_json.get('fav_food'))
Exemple #2
0
    def test_map_list_of_objects_with_nested_anchoring(self):
        # given:
        json_object = json.loads('''{
            "social_network": {
                "friends": [
                    {
                        "name": "Carl",
                        "age": 24
                    },
                    {
                        "name": "Tina",
                        "age": 22
                    },
                    {
                        "name": "Oscar",
                        "age": 22
                    }
                ]
            }
        }''')

        # when:
        people_json = JsonMapper(json_object).map({
            '$on': 'social_network',
            'people': {
                '$on': 'friends',
                'person_name': ['name'],
                'person_age': ['age']
            }
        })

        # then:
        people = people_json.get('people')
        self.assertIsNotNone(people)
        self.assertEqual(3, len(people))
Exemple #3
0
    def test_map_object_ignore_missing_fields(self):
        # given:
        json_object = json.loads('''{
            "first_name": "Juan",
            "last_name": "dela Cruz"
        }''')

        # when:
        person_json = JsonMapper(json_object).map({
            'fname': ['first_name'],
            'mname': ['middle_name'],
            'lname': ['last_name']
        })

        # then:
        self.assertEqual('Juan', person_json.get('fname'))
        self.assertEqual('dela Cruz', person_json.get('lname'))
        self.assertTrue('mname' not in person_json)
Exemple #4
0
    def test_map_object_using_spec_based_anchor(self):
        # given:
        json_object = json.loads('''{
            "shipping_info": {
                "recipient": "Kamado Tanjiro",
                "address": "Tokyo, Japan"
            }
        }''')

        # when:
        delivery_json = JsonMapper(json_object).map({
            '$on': 'shipping_info',
            'name': ['recipient'],
            'location': ['address']
        })

        # then:
        self.assertEqual('Kamado Tanjiro', delivery_json.get('name'))
        self.assertEqual('Tokyo, Japan', delivery_json.get('location'))
Exemple #5
0
    def test_map_object_using_field_chaining(self):
        # given:
        json_object = json.loads('''{
            "name": "John Doe",
            "address": {
                "city": "London",
                "country": "UK"
            }
        }''')

        # when:
        profile_json = JsonMapper(json_object).map({
            'user.profile': ['name'],
            'user.address_city': ['address.city'],
            'user.address_country': ['address.country']
        })

        # then:
        self.assertIsNotNone(profile_json)
        self.assertEqual('John Doe', profile_json.get('user', {}).get('profile'))
        self.assertEqual('London', profile_json.get('user', {}).get('address_city'))
        self.assertEqual('UK', profile_json.get('user', {}).get('address_country'))
Exemple #6
0
    def test_map_with_object_literal(self):
        # given:
        json_object = json.loads('''{
            "description": "test"
        }''')

        # when:
        metadata = {'authored_by': 'me'}
        result = JsonMapper(json_object).map({
            'text': ['description'],
            'metadata': ['$object', metadata],
            'empty': ['$object', {}]
        })

        # then:
        self.assertEqual(metadata, result.get('metadata'))
        self.assertFalse('empty' in result.keys())
Exemple #7
0
    def test_map_object_with_nested_spec(self):
        # given:
        json_object = json.loads('''{
            "first_name": "Vanessa",
            "last_name": "Doofenshmirtz"
        }''')

        # when:
        profile_json = JsonMapper(json_object).map({
            'profile': {
                'first_name': ['first_name'],
                'last_name': ['last_name']
            }
        })

        # then:
        profile = profile_json.get('profile')
        self.assertIsNotNone(profile)
        self.assertEqual('Vanessa', profile.get('first_name'))
        self.assertEqual('Doofenshmirtz', profile.get('last_name'))
Exemple #8
0
    def test_map_object_using_chained_spec_based_anchors(self):
        # given:
        json_object = json.loads('''{
            "user": {
                "profile": {
                    "name": "Kamado Nezuko",
                    "location": "hako no naka"
                }
            }
        }''')

        # when:
        person_json = JsonMapper(json_object).map({
            '$on': 'user',
            'person': {
                '$on': 'profile',
                'known_by': ['name']
            }
        })

        # then:
        self.assertEqual('Kamado Nezuko', person_json.get('person', {}).get('known_by'))