<commit_before>from copy import deepcopy

from django.db import models

from river.models.fields.state import StateField, _get_cls_identifier, class_field_rl
from river.tests.base_test import BaseTestCase
from river.tests.models.testmodel import TestModel
from river.utils.error_code import ErrorCode
from river.utils.exceptions import RiverException

__author__ = 'ahmetdal'


class test_StateFIeld(BaseTestCase):
    def test_injections(self):
        self.assertTrue(hasattr(TestModel, 'proceedings'))
        self.assertTrue(hasattr(TestModel, 'proceeding'))
        self.assertTrue(hasattr(TestModel, 'is_workflow_completed'))
        self.assertTrue(hasattr(TestModel, 'proceed'))
        self.assertTrue(hasattr(TestModel, 'on_initial_state'))
        self.assertTrue(hasattr(TestModel, 'on_final_state'))
        self.assertTrue(hasattr(TestModel, 'get_initial_state'))
        self.assertTrue(hasattr(TestModel, 'get_available_proceedings'))
        self.assertTrue(hasattr(TestModel, 'initial_proceedings'))
        self.assertTrue(hasattr(TestModel, 'final_proceedings'))
        self.assertTrue(hasattr(TestModel, 'next_proceedings'))
        self.assertTrue(hasattr(TestModel, 'get_state'))
        self.assertTrue(hasattr(TestModel, 'set_state'))
        self.assertTrue(hasattr(TestModel, 'objects'))

    def test_triggering_multiple_time(self):
        class TestModelForTriggeringMultipleTime(models.Model):
            pass

        field = StateField()
        class_field_rl_cp = deepcopy(class_field_rl)

        field.contribute_to_class(TestModelForTriggeringMultipleTime, 'status')

        class_field_rl_cp.update({_get_cls_identifier(TestModelForTriggeringMultipleTime): 'status'})
        self.assertDictEqual(class_field_rl_cp, class_field_rl)

        field.contribute_to_class(TestModelForTriggeringMultipleTime, 'status')

        self.assertDictEqual(class_field_rl_cp, class_field_rl)

        try:
            field.contribute_to_class(TestModelForTriggeringMultipleTime, 'status2')
            self.assertFalse(True, "River exception with error code %d, must have been raised")
        except RiverException as re:
            self.assertEqual(ErrorCode.MULTIPLE_STATE_FIELDS, re.code)
        else:
            self.assertFalse(True, "River exception with error code %d, must have been raised")
<commit_msg>#40: Invoking contribute_to_class function multiple time in favor of testing it, caused status field clash error. It is fixed.<commit_after>from copy import deepcopy

import mock
from django.db import models

from river.models.fields.state import StateField, _get_cls_identifier, class_field_rl
from river.tests.base_test import BaseTestCase
from river.tests.models.testmodel import TestModel
from river.utils.error_code import ErrorCode
from river.utils.exceptions import RiverException

__author__ = 'ahmetdal'


class test_StateFIeld(BaseTestCase):
    def test_injections(self):
        self.assertTrue(hasattr(TestModel, 'proceedings'))
        self.assertTrue(hasattr(TestModel, 'proceeding'))
        self.assertTrue(hasattr(TestModel, 'is_workflow_completed'))
        self.assertTrue(hasattr(TestModel, 'proceed'))
        self.assertTrue(hasattr(TestModel, 'on_initial_state'))
        self.assertTrue(hasattr(TestModel, 'on_final_state'))
        self.assertTrue(hasattr(TestModel, 'get_initial_state'))
        self.assertTrue(hasattr(TestModel, 'get_available_proceedings'))
        self.assertTrue(hasattr(TestModel, 'initial_proceedings'))
        self.assertTrue(hasattr(TestModel, 'final_proceedings'))
        self.assertTrue(hasattr(TestModel, 'next_proceedings'))
        self.assertTrue(hasattr(TestModel, 'get_state'))
        self.assertTrue(hasattr(TestModel, 'set_state'))
        self.assertTrue(hasattr(TestModel, 'objects'))

    @mock.patch('django.db.models.fields.related.ForeignObject.contribute_to_class', mock.MagicMock())
    @mock.patch('river.models.fields.state.StateField._StateField__add_to_class', mock.MagicMock())
    def test_triggering_multiple_time(self):
        class TestModelForTriggeringMultipleTime(models.Model):
            pass

        field = StateField()
        class_field_rl_cp = deepcopy(class_field_rl)

        field.contribute_to_class(TestModelForTriggeringMultipleTime, 'status')

        class_field_rl_cp.update({_get_cls_identifier(TestModelForTriggeringMultipleTime): 'status'})
        self.assertDictEqual(class_field_rl_cp, class_field_rl)

        field.contribute_to_class(TestModelForTriggeringMultipleTime, 'status')

        self.assertDictEqual(class_field_rl_cp, class_field_rl)

        try:
            field.contribute_to_class(TestModelForTriggeringMultipleTime, 'status2')
            self.assertFalse(True, "River exception with error code %d, must have been raised")
        except RiverException as re:
            self.assertEqual(ErrorCode.MULTIPLE_STATE_FIELDS, re.code)
        else:
            self.assertFalse(True, "River exception with error code %d, must have been raised")

{'fraction_non_alphanumeric': 0.09206855320844959, 'fraction_numerical': 0.0007971303308090873, 'mean_word_length': 4.431818181818182, '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': '4612470', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1450, 'n_words': 254}