Beispiel #1
0
    def test_encoder_non_dict(self):
        class C:
            def serialize(self):
                return 0

        e = base.BaseEncoder()
        obj = {"data": C()}

        self.assertRaises(ValueError, e.encode, obj)
Beispiel #2
0
    def test_encoder_non_dict_no_metadata(self):
        class C:
            def serialize(self):
                return 0

        e = base.BaseEncoder(metadata=False)
        obj = {"data": C()}
        expected_result = {"data": 0}

        self.assertEqual(e.encode(obj), expected_result)
Beispiel #3
0
    def test_encoder_no_metadata(self):
        e = base.BaseEncoder(metadata=False)

        # Remove metadata from expected result
        expected_result = deepcopy(self.expected_result)
        expected_result.pop("__class__")
        expected_result["nested"].pop("__class__")
        expected_result["dict_var"]["nested"].pop("__class__")
        expected_result["list_var"][1].pop("__class__")

        obj = ExampleClass()
        self.assertEqual(e.encode(obj), expected_result)
Beispiel #4
0
    def test_encoder_hooks(self):
        def hook(obj):
            result = obj.serialize()
            result["extra"] = None
            return result

        e = base.BaseEncoder(hooks={ExampleClass.Nested: hook})

        # Add the "extra" field to expected result
        expected_result = deepcopy(self.expected_result)
        expected_result["nested"]["extra"] = None
        expected_result["dict_var"]["nested"]["extra"] = None
        expected_result["list_var"][1]["extra"] = None

        obj = ExampleClass()
        self.assertEqual(e.encode(obj), expected_result)
Beispiel #5
0
    def test_encoder(self):
        e = base.BaseEncoder()

        obj = ExampleClass()
        self.assertEqual(e.encode(obj), self.expected_result)