Exemplo n.º 1
0
    def transmute_from_with_inherited_mapping(self):
        data = '{"test": "sample", "second": "other"}'

        result = JsonTransmuter.transmute_from(data, TestExtendedModel)

        expect(result.test).to.equal('sample')
        expect(result.second).to.equal('other')
Exemplo n.º 2
0
    def transmute_from_with_child_mapping(self):
        data = '{"other": "sample", "child": {"test": "sample stuff"}}'

        result = JsonTransmuter.transmute_from(data, TestChildMapping)

        require(result.child).not_to.be_none()
        expect(result.child.test).to.equal('sample stuff')
Exemplo n.º 3
0
    def transmute_to_and_from_with_excluded_items(self):
        class MixedMappedModel(JsonMappedModel):
            __mapping__ = {
                'test': Attr('test', int),
                'another': Attr('another', int, serialize=False)
            }

        mapping = MixedMappedModel()
        mapping.test = 1
        mapping.another = 2

        without_result = '{"test": 1}'
        result_without = JsonTransmuter.transmute_to(mapping)
        expect(result_without).to.equal(without_result)

        # Make sure we can override the serialization preferences
        result_with = JsonTransmuter.transmute_to(
            mapping,
            to_string=False,
            serialize_all=True
        )
        expect(result_with.get('test')).to.equal(1)
        expect(result_with.get('another')).to.equal(2)

        result = JsonTransmuter.transmute_from(
            {'test': 100, 'another': 200},
            MixedMappedModel
        )
        expect(result.test).to.equal(100)
        expect(result.another).to.equal(200)
Exemplo n.º 4
0
    def deserialize(self, raw_config, config_cls):
        import yaml

        config_dict = yaml.load(raw_config)
        for k in config_dict.keys():
            config_dict[k] = os.environ.get(k.upper()) or config_dict[k]

        return JsonTransmuter.transmute_from(config_dict, config_cls)
Exemplo n.º 5
0
    def transmute_from_with_list_of_child_mappings(self):
        data = '{"children": [{"test": "sample1"}, {"test": "sample2"}]}'

        result = JsonTransmuter.transmute_from(data, TestListChildMapping)

        require(result.children).not_to.be_none()
        expect(len(result.children)).to.equal(2)
        expect(result.children[0].test).to.equal('sample1')
        expect(result.children[1].test).to.equal('sample2')
Exemplo n.º 6
0
        def transmute_attribute_with_type(self, attr_type, attr_data,
                                          attr_result):
            class FlexMapping(JsonMappedModel):
                __mapping__ = {
                    'test': Attr('test', attr_type)
                }

            data = '{{"test": {0} }}'.format(attr_data)
            result = JsonTransmuter.transmute_from(data, FlexMapping)
            expect(result.test).to.equal(attr_result)
Exemplo n.º 7
0
    def transmute_to_with_old_attr_style(self):
        class OldStyleMappedModel(JsonMappedModel):
            __mapping__ = {
                'test': ['test', int]
            }

        mapping = OldStyleMappedModel()
        mapping.test = 0

        expected_result = '{"test": 0}'

        result = JsonTransmuter.transmute_to(mapping)
        expect(result).to.equal(expected_result)

        result = JsonTransmuter.transmute_from(
            {'test': 1},
            OldStyleMappedModel
        )
        expect(result.test).to.equal(1)
Exemplo n.º 8
0
    def deserialize(self, raw_config, config_cls):
        import yaml

        config_dict = yaml.load(raw_config)
        return JsonTransmuter.transmute_from(config_dict, config_cls)
Exemplo n.º 9
0
 def deserialize(self, raw_config, config_cls):
     return JsonTransmuter.transmute_from(raw_config.decode('utf-8'),
                                          config_cls)
Exemplo n.º 10
0
 def deserialize(self, raw_config, config_cls):
     return JsonTransmuter.transmute_from(raw_config, config_cls)
Exemplo n.º 11
0
 def deserialize(self, raw_config, config_cls):
     config_dict = self.parse(raw_config)
     return JsonTransmuter.transmute_from(config_dict, config_cls)