<commit_before>import unittest
from typing import Dict

from hgijson.json.models import JsonPropertyMapping
from hgijson.models import PropertyMapping
from hgijson.tests._models import SimpleModel
from hgijson.tests._serializers import ComplexModelSerializer, SimpleModelDeserializer, ComplexModelDeserializer, \
    SimpleModelSerializer
from hgijson.tests.json._helpers import create_complex_model_with_json_representation, \
    create_simple_model_with_json_representation


class TestSerializer(unittest.TestCase):
    """
    Tests for `Serializer`.
    """
    def setUp(self):
        self.simple_model = create_simple_model_with_json_representation()[0]
        self.simple_model_as_json = create_simple_model_with_json_representation()[1]
        self.complex_model = create_complex_model_with_json_representation()[0]
        self.complex_model_as_json = create_complex_model_with_json_representation()[1]

    def test_serialize_with_no_mapping(self):
        mappings = [PropertyMapping()]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), {})

    def test_serialize_using_names(self):
        mappings = [JsonPropertyMapping("serialized_a", "a"), JsonPropertyMapping("serialized_b", "b")]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), self.simple_model_as_json)

    def test_serialize_using_object_property_getter(self):
        mappings = [JsonPropertyMapping("serialized_b_plus_one", object_property_getter=lambda model: model.b + 1)]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), {"serialized_b_plus_one": self.simple_model.b + 1})

    def test_serialize_using_serialized_property_setter(self):
        def serialized_property_setter(container: Dict, value: Dict):
            container["augmented_key_%s" % value] = value

        mappings = [JsonPropertyMapping(None, "b", json_property_setter=serialized_property_setter)]
        serializer = ComplexModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.complex_model), {"augmented_key_5": 5})

    def test_serialize_using_serializer_cls(self):
        def serialized_property_setter_for_d(container: Dict, value: Dict):
            container["serialized_d"] = value

        mappings = [PropertyMapping("d", serialized_property_setter=serialized_property_setter_for_d,
                                    serializer_cls=SimpleModelSerializer)]
        serializer = ComplexModelSerializer(mappings)

        complex_model = self.complex_model
        expected = {"serialized_d": [{
            "serialized_a": i,
            "serialized_b": complex_model.b + i
        } for i in range(len(complex_model.d))]}
        self.assertDictEqual(serializer.serialize(complex_model), expected)

    def test_serialize_with_iterable_with_no_items(self):
        serialized = SimpleModelSerializer().serialize([])
        self.assertCountEqual(serialized, [])

    def test_serialize_with_iterable(self):
        complex_models = [create_complex_model_with_json_representation(i)[0] for i in range(10)]
        complex_models_as_json = [create_complex_model_with_json_representation(i)[1] for i in range(10)]
        serialized = ComplexModelSerializer().serialize(complex_models)
        self.assertEqual(serialized, complex_models_as_json)

    def test_serialize_optional_when_not_set(self):
        mappings = [JsonPropertyMapping("serialized_a", "a", optional=True),
                    JsonPropertyMapping("serialized_b", "b", optional=True)]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(SimpleModel()), {})

    def test_serialize_optional_when_set(self):
        mappings = [JsonPropertyMapping("serialized_a", "a", optional=True),
                    JsonPropertyMapping("serialized_b", "b", optional=True)]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), self.simple_model_as_json)


class TestDeserializer(unittest.TestCase):
    """
    Tests for `Deserializer`.
    """
    def setUp(self):
        self.simple_model = create_simple_model_with_json_representation()[0]
        self.simple_model_as_json = create_simple_model_with_json_representation()[1]
        self.complex_model = create_complex_model_with_json_representation()[0]<commit_msg>Added a test to ensure similar issue as #2 is handled correctly with serialisation.<commit_after>import unittest
from typing import Dict

from hgijson.json.models import JsonPropertyMapping
from hgijson.models import PropertyMapping
from hgijson.tests._models import SimpleModel
from hgijson.tests._serializers import ComplexModelSerializer, SimpleModelDeserializer, ComplexModelDeserializer, \
    SimpleModelSerializer
from hgijson.tests.json._helpers import create_complex_model_with_json_representation, \
    create_simple_model_with_json_representation


class TestSerializer(unittest.TestCase):
    """
    Tests for `Serializer`.
    """
    def setUp(self):
        self.simple_model = create_simple_model_with_json_representation()[0]
        self.simple_model_as_json = create_simple_model_with_json_representation()[1]
        self.complex_model = create_complex_model_with_json_representation()[0]
        self.complex_model_as_json = create_complex_model_with_json_representation()[1]

    def test_serialize_with_no_mapping(self):
        mappings = [PropertyMapping()]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), {})

    def test_serialize_using_names(self):
        mappings = [JsonPropertyMapping("serialized_a", "a"), JsonPropertyMapping("serialized_b", "b")]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), self.simple_model_as_json)

    def test_serialize_using_object_property_getter(self):
        mappings = [JsonPropertyMapping("serialized_b_plus_one", object_property_getter=lambda model: model.b + 1)]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), {"serialized_b_plus_one": self.simple_model.b + 1})

    def test_serialize_using_serialized_property_setter(self):
        def serialized_property_setter(container: Dict, value: Dict):
            container["augmented_key_%s" % value] = value

        mappings = [JsonPropertyMapping(None, "b", json_property_setter=serialized_property_setter)]
        serializer = ComplexModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.complex_model), {"augmented_key_5": 5})

    def test_serialize_using_serializer_cls(self):
        def serialized_property_setter_for_d(container: Dict, value: Dict):
            container["serialized_d"] = value

        mappings = [PropertyMapping("d", serialized_property_setter=serialized_property_setter_for_d,
                                    serializer_cls=SimpleModelSerializer)]
        serializer = ComplexModelSerializer(mappings)

        complex_model = self.complex_model
        expected = {"serialized_d": [{
            "serialized_a": i,
            "serialized_b": complex_model.b + i
        } for i in range(len(complex_model.d))]}
        self.assertDictEqual(serializer.serialize(complex_model), expected)

    def test_serialize_with_iterable_with_no_items(self):
        serialized = SimpleModelSerializer().serialize([])
        self.assertCountEqual(serialized, [])

    def test_serialize_with_iterable(self):
        complex_models = [create_complex_model_with_json_representation(i)[0] for i in range(10)]
        complex_models_as_json = [create_complex_model_with_json_representation(i)[1] for i in range(10)]
        serialized = ComplexModelSerializer().serialize(complex_models)
        self.assertEqual(serialized, complex_models_as_json)

    def test_serialize_optional_when_not_set(self):
        mappings = [JsonPropertyMapping("serialized_a", "a", optional=True),
                    JsonPropertyMapping("serialized_b", "b", optional=True)]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(SimpleModel()), {})

    def test_serialize_optional_when_set(self):
        mappings = [JsonPropertyMapping("serialized_a", "a", optional=True),
                    JsonPropertyMapping("serialized_b", "b", optional=True)]
        serializer = SimpleModelSerializer(mappings)
        self.assertDictEqual(serializer.serialize(self.simple_model), self.simple_model_as_json)

    def test_serialize_when_property_does_not_exist(self):
        mappings = [JsonPropertyMapping("serialized_a", "z")]
        serializer = SimpleModelSerializer(mappings)
        self.assertRaises(AttributeError, serializer.serialize, self.simple_model)


class TestDeserializer(unittest.TestCase):
    """
    Tests for `Deserializer`.
    """
    def setUp(self):
        self.simple_model = create_simple_model_with_json_representation()[0]
        self.simple_model_as_json = create_simple_model_with_json_representation()[1]
        self.complex_model = create_complex_model_with_json_representation()[0]
{'fraction_non_alphanumeric': 0.08996576807873342, 'fraction_numerical': 0.003744116388532306, 'mean_word_length': 4.548367952522256, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1347642', 'n_tokens_mistral': 2671, 'n_tokens_neox': 2611, 'n_words': 396}