<commit_before>from django.test import TransactionTestCase

from chamber.shortcuts import change_and_save
from chamber.models.fields import generate_random_upload_path
from germanium.tools import assert_equal, assert_raises, assert_true, assert_is_none

from chamber.exceptions import PersistenceException

from test_chamber.models import TestFieldsModel, CSVRecord


class ModelFieldsTestCase(TransactionTestCase):

    def setUp(self):
        self.inst = TestFieldsModel()
        super(ModelFieldsTestCase, self).setUp()

    def test_random_file_path_should_be_generated_from_class_name(self):
        instance = CSVRecord()
        filename = 'filename.txt'
        path = generate_random_upload_path(instance, filename)
        assert_true(path.startswith('csvrecord/'))
        assert_true(path.endswith('/{}'.format(filename)))

    def test_decimal_field(self):
        change_and_save(self.inst, decimal=3)
        assert_equal(self.inst.decimal, 3)
        assert_raises(PersistenceException, change_and_save, self.inst, decimal=2.99)
        assert_raises(PersistenceException, change_and_save, self.inst, decimal=10.00001)

    def test_subchoices_field(self):
        change_and_save(self.inst, state_reason=TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_equal(self.inst.state_reason, TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_raises(PersistenceException, change_and_save, self.inst,<commit_msg>Added a test for PersistenceException __str__ method<commit_after>from django.test import TransactionTestCase

from chamber.shortcuts import change_and_save
from chamber.models.fields import generate_random_upload_path
from germanium.tools import assert_equal, assert_raises, assert_true, assert_is_none

from chamber.exceptions import PersistenceException

from test_chamber.models import TestFieldsModel, CSVRecord


class ModelFieldsTestCase(TransactionTestCase):

    def setUp(self):
        self.inst = TestFieldsModel()
        super(ModelFieldsTestCase, self).setUp()

    def test_random_file_path_should_be_generated_from_class_name(self):
        instance = CSVRecord()
        filename = 'filename.txt'
        path = generate_random_upload_path(instance, filename)
        assert_true(path.startswith('csvrecord/'))
        assert_true(path.endswith('/{}'.format(filename)))

    def test_decimal_field(self):
        change_and_save(self.inst, decimal=3)
        assert_equal(self.inst.decimal, 3)
        assert_raises(PersistenceException, change_and_save, self.inst, decimal=2.99)
        assert_raises(PersistenceException, change_and_save, self.inst, decimal=10.00001)
        try:
            change_and_save(self.inst, decimal=11.1)
            assert_true(False, 'Previous `change_and_save` suppose to raise an exception')
        except Exception as ex:
            assert_true('decimal: ' in str(ex), 'Exception message suppose contains a field name.')

    def test_subchoices_field(self):
        change_and_save(self.inst, state_reason=TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_equal(self.inst.state_reason, TestFieldsModel.STATE_REASON.SUB_OK_2)
        assert_raises(PersistenceException, change_and_save, self.inst,
{'fraction_non_alphanumeric': 0.08097928436911488, 'fraction_numerical': 0.00973006905210295, 'mean_word_length': 4.610915492957746, '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': '1557200', 'n_tokens_mistral': 961, 'n_tokens_neox': 927, 'n_words': 170}