Exemple #1
0
    def test_build_with_multiple_superclasses(self):
        NamedJSONDecoder = MappingJSONDecoderClassBuilder(_Named, [
            JsonPropertyMapping("name", "name")
        ]).build()
        OfficeJSONDecoder = MappingJSONDecoderClassBuilder(_Office, [], (NamedJSONDecoder,)).build()
        IdentifiableJSONDecoder = MappingJSONDecoderClassBuilder(_Identifiable, [
            JsonPropertyMapping("id", "id")
        ]).build()
        EmployeeJSONDecoder = MappingJSONDecoderClassBuilder(_Employee, [
            JsonPropertyMapping("title", "title"),
            JsonPropertyMapping("office", "office", decoder_cls=OfficeJSONDecoder)
        ], (NamedJSONDecoder, IdentifiableJSONDecoder)).build()

        office = _Office()
        office.name = "Cambridge"

        employee = _Employee()
        employee.name = "Bob"
        employee.id = 42
        employee.title = "Software Dev"
        employee.office = office

        employee_as_json = {
            "name": employee.name,
            "id": employee.id,
            "title": employee.title,
            "office": {
                "name": office.name
            }
        }
        employee_as_json_string = json.dumps([employee_as_json])
        self.assertEqual(EmployeeJSONDecoder().decode(employee_as_json_string), [employee])
Exemple #2
0
    def test_build(self):
        decoder_builder = MappingJSONDecoderClassBuilder()
        decoder_builder.target_cls = SimpleModel
        decoder_builder.mappings = get_simple_model_json_property_mappings()

        decoder_cls = decoder_builder.build()
        decoder = decoder_cls()     # type: MappingJSONDecoder

        decoded = decoder.decode(json.dumps(self.simple_model_as_json))
        self.assertEqual(decoded, self.simple_model)
Exemple #3
0
 def test_build_with_superclass_string(self):
     ContainerJSONDecoder = MappingJSONDecoderClassBuilder(_Container, [
         JsonPropertyMapping("colour", "colour", object_constructor_parameter_name="colour"),
         JsonPropertyMapping("contains", "contains", decoder_cls=lambda: ContainerJSONDecoder, optional=True)
     ]).build()
     decoder = ContainerJSONDecoder()
     container = _Container("red", _Container("blue"))
     self.assertEqual(container, decoder.decode(json.dumps({"colour": "red", "contains": {"colour": "blue"}})))
Exemple #4
0
    def test_build_with_none_property(self):
        NamedJSONDecoder = MappingJSONDecoderClassBuilder(_Named, [
            JsonPropertyMapping("name", "name")
        ]).build()
        OfficeJSONDecoder = MappingJSONDecoderClassBuilder(_Office, [], (NamedJSONDecoder,)).build()
        EmployeeJSONDecoder = MappingJSONDecoderClassBuilder(_Employee, [
            JsonPropertyMapping("office", "office", decoder_cls=OfficeJSONDecoder)
        ], (NamedJSONDecoder, )).build()

        employee = _Employee()
        employee.name = "Bob"
        employee.office = None

        employee_as_json = {
            "name": employee.name,
            "id": employee.id,
            "title": employee.title,
            "office": None
        }
        employee_as_json_string = json.dumps([employee_as_json])
        self.assertEqual(EmployeeJSONDecoder().decode(employee_as_json_string), [employee])
Exemple #5
0
    def test_build_with_superclass(self):
        SimpleModelJSONDecoder = MappingJSONDecoderClassBuilder(
            target_cls=SimpleModel, mappings=get_simple_model_json_property_mappings()).build()

        decoder_builder = MappingJSONDecoderClassBuilder()
        decoder_builder.superclasses = (SimpleModelJSONDecoder, )
        decoder_builder.target_cls = ComplexModel
        decoder_builder.mappings = get_complex_model_json_property_mappings()

        decoder_cls = decoder_builder.build()
        decoder = decoder_cls()     # type: MappingJSONDecoder

        decoded = decoder.decode(json.dumps(self.complex_model_as_json))
        self.assertEqual(decoded, self.complex_model)
Exemple #6
0
 def setUp(self):
     self.values = set([create_simple_model_with_json_representation(i)[0] for i in range(10)])
     self.values_as_json = [create_simple_model_with_json_representation(i)[1] for i in range(10)]
     self.ValueJSONDecoder = MappingJSONDecoderClassBuilder(
         SimpleModel, get_simple_model_json_property_mappings()).build()