<commit_before>
import unittest

from schematics.models import Model
from schematics.types import IntType, StringType
from schematics.exceptions import ValidationError


class TestDataIntegrity(unittest.TestCase):

    def test_dont_serialize_invalid_data(self):
        """
        Serialization must always contain just the subset of valid
        data from the model.

        """
        class Player(Model):
            code = StringType(max_length=4)

        p1 = Player({'code': 'invalid1'})
        self.assertRaises(ValidationError, p1.validate)
        self.assertEqual(p1.serialize(), {'code': None})<commit_msg>Model setter integrity test<commit_after>
import unittest

from schematics.models import Model
from schematics.types import IntType, StringType
from schematics.exceptions import ValidationError


class TestDataIntegrity(unittest.TestCase):

    def test_dont_serialize_invalid_data(self):
        """
        Serialization must always contain just the subset of valid
        data from the model.

        """
        class Player(Model):
            code = StringType(max_length=4)

        p1 = Player({'code': 'invalid1'})
        self.assertRaises(ValidationError, p1.validate)
        self.assertEqual(p1.serialize(), {'code': None})

    def test_dont_overwrite_with_invalid_data(self):
        """
        Model-level validators are black-boxes and we should not assume
        that we can set the instance data at any time.

        """
        class Player(Model):
            id = IntType()

            def validate_id(self, context, value):
                if self.id:
                    raise ValidationError('Cannot change id')

        p1 = Player({'id': 4})
        p1.id = 3
        self.assertRaises(ValidationError, p1.validate)
        self.assertEqual(p1.id, 4)
{'fraction_non_alphanumeric': 0.08799114554510237, 'fraction_numerical': 0.009407858328721638, 'mean_word_length': 2.7049180327868854, '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': '6683864', 'n_tokens_mistral': 506, 'n_tokens_neox': 479, 'n_words': 136}