<commit_before>import json

from mock import Mock

from tests import SerializerTestCase, TestPreference, TestUser
from madprops.serializers import PropertySerializer
from rest_framework.serializers import ValidationError


class TestSerializer(PropertySerializer):
    class Meta:
        model = TestPreference
        read_only_props = ('user_token',)
        json_props = ('json1', )

    def validate(self, attrs):
        if attrs['name'] == 'a' and attrs['value'] == 'invalid':
            raise ValidationError('No, you *bad* test!')
        return attrs


class FieldToNative(SerializerTestCase):
    def setUp(self):
        self.preferences = {'k1': 'v1', 'k2': 'v2'}
        user = TestUser()
        user.preferences = self.preferences
        self.representation = TestSerializer().field_to_native(
            user, 'preferences')

    def test_converts_properties_to_dict(self):
        self.assertEqual(self.preferences, self.representation)


class FieldToNativeWhenObjectIsNone(SerializerTestCase):
    def setUp(self):
        self.representation = TestSerializer().field_to_native(
            None, 'preferences')

    def test_returns_None(self):
        self.assertIsNone(self.representation)


class FromNative(SerializerTestCase):
    def setUp(self):
        data = {'k1': 'v1'}
        context = {'view': Mock(kwargs={'user_id': 4})}
        serializer = TestSerializer(context=context)
        self.patch_from_native()
        self.preference = serializer.from_native(data)

    def test_passes_updated_data_to_parent_method(self):
        self.assertEqual(self.preference, TestPreference('k1', 'v1', 4))


class FromNativeForJsonProperty(SerializerTestCase):
    def setUp(self):
        data = {'json1': [1, 2]}
        context = {'view': Mock(kwargs={'user_id': 4})}
        serializer = TestSerializer(context=context)
        self.patch_from_native()
        self.preference = serializer.from_native(data)

    def test_converts_value_to_json(self):
        self.assertEqual(
            self.preference, TestPreference('json1', json.dumps([1, 2]), 4))


class Data(SerializerTestCase):
    def setUp(self):
        serializer = TestSerializer(
            many=True, instance=(
                TestPreference('a', 'b'), TestPreference('c', 'd')))
        self.data = serializer.data

    def test_converts_properties_to_dict(self):
        self.assertEqual(self.data, {'a': 'b', 'c': 'd'})


class DataForJsonValues(SerializerTestCase):
    def setUp(self):
        serializer = TestSerializer(
            many=True, instance=(
                TestPreference('json1', json.dumps([1, 3])),
                TestPreference('c', 'd'))
        )
        self.data = serializer.data

    def test_loads_json_properties(self):
        self.assertEqual(self.data, {'json1': [1, 3], 'c': 'd'})


class DataWhenObjectIsNone(SerializerTestCase):
    def setUp(self):
        serializer = TestSerializer(many=True)
        self.data = serializer.data

    def test_returns_empty_dict(self):
        self.assertEqual(self.data, {})


class ErrorsWhenDataIsNotDict(SerializerTestCase):
    def setUp(self):
        self.serializer = TestSerializer(data=[])

    def test_returns_error(self):
        self.assertEqual(
            self.serializer.errors,
            {'non_field_errors': ['Expected a dictionary.']}
        )


class ErrorsForObjectsUpdate(SerializerTestCase):
    def setUp(self):
        self.serializer = TestSerializer(
            data={'a': 'b', 'c': 'd'},
            instance=(TestPreference('a', 'aa'),),
            many=True,
            context={'view': Mock(kwargs={'user_id': 4})}
        )
        self.patch_from_native()
        self.serializer.errors

    def test_updates_properties(self):
        self.assertEqual(
            sorted(self.serializer.object),
            sorted([TestPreference('a', 'b'), TestPreference('c', 'd', 4)],)
        )


class ErrorsForObjectsCreate(SerializerTestCase):
    def setUp(self):
        self.serializer = TestSerializer(
            data={'a': 'b', 'c': 'd'},
            many=True,
            context={'view': Mock(kwargs={'user_id': 4})}
        )
        self.patch_from_native()
        self.serializer.errors

    def test_updates_properties(self):
        self.assertEqual(
            sorted(self.serializer.object),
            sorted([TestPreference('a', 'b', 4), TestPreference('c', 'd', 4)])
        )
<commit_msg>Both objects are now correctly linked with the parent<commit_after>import json

from mock import Mock

from tests import SerializerTestCase, TestPreference, TestUser
from madprops.serializers import PropertySerializer
from rest_framework.serializers import ValidationError


class TestSerializer(PropertySerializer):
    class Meta:
        model = TestPreference
        read_only_props = ('user_token',)
        json_props = ('json1', )

    def validate(self, attrs):
        if attrs['name'] == 'a' and attrs['value'] == 'invalid':
            raise ValidationError('No, you *bad* test!')
        return attrs


class FieldToNative(SerializerTestCase):
    def setUp(self):
        self.preferences = {'k1': 'v1', 'k2': 'v2'}
        user = TestUser()
        user.preferences = self.preferences
        self.representation = TestSerializer().field_to_native(
            user, 'preferences')

    def test_converts_properties_to_dict(self):
        self.assertEqual(self.preferences, self.representation)


class FieldToNativeWhenObjectIsNone(SerializerTestCase):
    def setUp(self):
        self.representation = TestSerializer().field_to_native(
            None, 'preferences')

    def test_returns_None(self):
        self.assertIsNone(self.representation)


class FromNative(SerializerTestCase):
    def setUp(self):
        data = {'k1': 'v1'}
        context = {'view': Mock(kwargs={'user_id': 4})}
        serializer = TestSerializer(context=context)
        self.patch_from_native()
        self.preference = serializer.from_native(data)

    def test_passes_updated_data_to_parent_method(self):
        self.assertEqual(self.preference, TestPreference('k1', 'v1', 4))


class FromNativeForJsonProperty(SerializerTestCase):
    def setUp(self):
        data = {'json1': [1, 2]}
        context = {'view': Mock(kwargs={'user_id': 4})}
        serializer = TestSerializer(context=context)
        self.patch_from_native()
        self.preference = serializer.from_native(data)

    def test_converts_value_to_json(self):
        self.assertEqual(
            self.preference, TestPreference('json1', json.dumps([1, 2]), 4))


class Data(SerializerTestCase):
    def setUp(self):
        serializer = TestSerializer(
            many=True, instance=(
                TestPreference('a', 'b'), TestPreference('c', 'd')))
        self.data = serializer.data

    def test_converts_properties_to_dict(self):
        self.assertEqual(self.data, {'a': 'b', 'c': 'd'})


class DataForJsonValues(SerializerTestCase):
    def setUp(self):
        serializer = TestSerializer(
            many=True, instance=(
                TestPreference('json1', json.dumps([1, 3])),
                TestPreference('c', 'd'))
        )
        self.data = serializer.data

    def test_loads_json_properties(self):
        self.assertEqual(self.data, {'json1': [1, 3], 'c': 'd'})


class DataWhenObjectIsNone(SerializerTestCase):
    def setUp(self):
        serializer = TestSerializer(many=True)
        self.data = serializer.data

    def test_returns_empty_dict(self):
        self.assertEqual(self.data, {})


class ErrorsWhenDataIsNotDict(SerializerTestCase):
    def setUp(self):
        self.serializer = TestSerializer(data=[])

    def test_returns_error(self):
        self.assertEqual(
            self.serializer.errors,
            {'non_field_errors': ['Expected a dictionary.']}
        )


class ErrorsForObjectsUpdate(SerializerTestCase):
    def setUp(self):
        self.serializer = TestSerializer(
            data={'a': 'b', 'c': 'd'},
            instance=(TestPreference('a', 'aa'),),
            many=True,
            context={'view': Mock(kwargs={'user_id': 4})}
        )
        self.patch_from_native()
        self.serializer.errors

    def test_updates_properties(self):
        self.assertEqual(
            sorted(self.serializer.object),
            sorted([TestPreference('a', 'b', 4), TestPreference('c', 'd', 4)],)
        )


class ErrorsForObjectsCreate(SerializerTestCase):
    def setUp(self):
        self.serializer = TestSerializer(
            data={'a': 'b', 'c': 'd'},
            many=True,
            context={'view': Mock(kwargs={'user_id': 4})}
        )
        self.patch_from_native()
        self.serializer.errors

    def test_updates_properties(self):
        self.assertEqual(
            sorted(self.serializer.object),
            sorted([TestPreference('a', 'b', 4), TestPreference('c', 'd', 4)])
        )

{'fraction_non_alphanumeric': 0.12903588705141186, 'fraction_numerical': 0.006862414219822252, 'mean_word_length': 2.9162995594713657, 'pattern_counts': {'":': 0, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2069022', 'n_tokens_mistral': 2627, 'n_tokens_neox': 2568, 'n_words': 503}