<commit_before># vim= ai ts=4 sts=4 et sw=4 encoding=utf-8
import unittest
from datetime import datetime

from mock import Mock, patch

from mangrove.datastore.database import DatabaseManager
from mangrove.errors.MangroveException import IncorrectDate, GeoCodeFormatException, RegexMismatchException, RequiredFieldNotPresentException
from mangrove.form_model.field import DateField, GeoCodeField, field_to_json, HierarchyField, TelephoneNumberField, Field, ShortCodeField
from mangrove.errors.MangroveException import AnswerTooBigException, AnswerTooSmallException, \
    AnswerTooLongException, AnswerTooShortException, AnswerWrongType, AnswerHasTooManyValuesException
from mangrove.form_model.field import TextField, IntegerField, SelectField, ExcelDate
from mangrove.form_model import field
from mangrove.form_model.validation import NumericRangeConstraint, TextLengthConstraint, RegexConstraint


class TestField(unittest.TestCase):
    def setUp(self):
        self.dbm = Mock(spec=DatabaseManager)

    def test_should_create_text_field_type_for_default_english_language(self):
        expected_json = {
            "defaultValue": "some default value",
            "label": "What is your name",
            "name": "field1_Name",
            "instruction": "Answer is word or phrase",
            "code": "Q1",
            "constraints": [("length", {"min": 1, "max": 20})],
            "type": "text",
            "required": True,
        }
        field = TextField(name="field1_Name", code="Q1", label="What is your name",
                          defaultValue="some default value", constraints=[TextLengthConstraint(1, 20)], instruction="Answer is word or phrase")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

        field.set_value("abc")
        self.assertEqual("abc", field.convert_to_unicode())

    def test_should_create_list_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your location",
            "name": "loc",
            "instruction": "Answer is list",
            "code": "Q1",
            "type": "list",
            "required": True,
        }
        field = HierarchyField(name="loc", code="Q1", label="What is your location", instruction="Answer is list")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

        field.set_value(["abc", "def"])
        self.assertEqual("abc,def", field.convert_to_unicode())


    def test_should_create_integer_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your age",
            "name": "Age",
            "code": "Q2",
            "type": "integer",
            "required": True,
            "instruction": "test_instruction"
        }
        field = IntegerField(name="Age", code="Q2", label="What is your age",
                             instruction="test_instruction")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)
        field.set_value(123)
        self.assertEqual("123", field.convert_to_unicode())

    def test_should_set_new_name_for_field(self):
        new_name = "newName"
        instruction = "test_instruction"
        expected_json = {
            "label": "What is your age",
            "name": new_name,
            "code": "Q2",
            
            "type": "integer",
            "required": True,
            "instruction": instruction
        }
        field = Field(type="integer", name="oldName", code="Q2", label="What is your age",
                      instruction=instruction)
        field.set_name(new_name)
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

    def test_should_set_new_instruction_for_field(self):
        new_instruction = "new_instruction"
        expected_json = {
            "label": "What is your age",
            "name": "name",
            "code": "Q2",
            
            "type": "integer",
            "required": True,
            "instruction": new_instruction
        }
        field = Field(type="integer", name="name", code="Q2", label="What is your age",
                      instruction="instruction")
        field.set_instruction(new_instruction)
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

    def test_should_create_integer_field_type_with_range(self):
        expected_json = {
            "label": "What is your age",
            "name": "Age",
            "code": "Q2",
            "constraints": [('range', {"min": 15, "max": 120})],
            
            "type": "integer",
            "required": True,
            "instruction": "test_instruction"
        }
        field = IntegerField(name="Age", code="Q2", label="What is your age",
                             constraints=[NumericRangeConstraint(min=15, max=120)],
                             instruction="test_instruction")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

    def test_should_create_select_one_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your favorite color",
            "name": "color",
            "choices": [{"text": "RED", "val": 1}, {"text": "YELLOW", "val": 2},
                        {"text": 'green', "val": 3}],
            "code": "Q3",
            
            "type": "select1",
            "required": True,
            "instruction": None
        }
        field = SelectField(name="color", code="Q3", label="What is your favorite color",
                            options=[("RED", 1), ("YELLOW", 2), ('green', 3)])
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

        field.set_value(field.validate('b'))
        self.assertEqual("YELLOW", field.convert_to_unicode())

    def test_should_create_multi_select_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your favorite color",
            "name": "color",
            "choices": [{"text": "RED", "val": 1}, {"text": "YELLOW", "val": 2},
                        {"text": 'green'}],
            "code": "Q3",
            
            "type": "select",
            "required": True,
            "instruction": "test_instruction"
        }
        field = SelectField(name="color", code="Q3", label="What is your favorite color",
                            options=[("RED", 1), ("YELLOW", 2), ('green')], single_select_flag=False,
                             instruction="test_instruction")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)
        field.set_value(field.validate('ab'))
        self.assertEqual("RED,YELLOW", field.convert_to_unicode())

    def test_should_remove_spaces_if_present_in_answer_for_multi_select(self):
        field = SelectField(name="color", code="Q3", label="What is your favorite color",
                            options=[("RED", 1), ("YELLOW", 2), ('green')], single_select_flag=False,
                             instruction="test_instruction")
        self.assertEqual(['RED', 'YELLOW'], field.validate('a b'))

    def test_should_add_entity_field_with_default_constraints(self):
        expected_json = {
            "defaultValue": "",
            "label": "What is your name",
            "name": "field1_Name",
            "code": "Q1",
            "type": "short_code",
            "constraints": [("length", {"max": 20}), ("short_code", "^[a-zA-Z0-9]+$")],
            "required": True,
            "instruction": "test_instruction"
        }
        field = ShortCodeField(name="field1_Name", code="Q1", label="What is your name",
                          instruction="test_instruction")
        actual_json = field._to_json()
        self.assertDictEqual(actual_json, expected_json)

    def test_should_create_text_field_from_dictionary(self):
        field_json = {
            "defaultValue": "",
            "label": "What is your name",
            "name": "field1_Name",<commit_msg>Ajay| #3067 | Fixing failing ut<commit_after># vim= ai ts=4 sts=4 et sw=4 encoding=utf-8
import unittest
from datetime import datetime

from mock import Mock, patch

from mangrove.datastore.database import DatabaseManager
from mangrove.errors.MangroveException import IncorrectDate, GeoCodeFormatException, RegexMismatchException, RequiredFieldNotPresentException
from mangrove.form_model.field import DateField, GeoCodeField, field_to_json, HierarchyField, TelephoneNumberField, Field, ShortCodeField
from mangrove.errors.MangroveException import AnswerTooBigException, AnswerTooSmallException, \
    AnswerTooLongException, AnswerTooShortException, AnswerWrongType, AnswerHasTooManyValuesException
from mangrove.form_model.field import TextField, IntegerField, SelectField, ExcelDate
from mangrove.form_model import field
from mangrove.form_model.validation import NumericRangeConstraint, TextLengthConstraint, RegexConstraint


class TestField(unittest.TestCase):
    def setUp(self):
        self.dbm = Mock(spec=DatabaseManager)

    def test_should_create_text_field_type_for_default_english_language(self):
        expected_json = {
            "defaultValue": "some default value",
            "label": "What is your name",
            "name": "field1_Name",
            "instruction": "Answer is word or phrase",
            "code": "Q1",
            "constraints": [("length", {"min": 1, "max": 20})],
            "type": "text",
            "required": True,
        }
        field = TextField(name="field1_Name", code="Q1", label="What is your name",
                          defaultValue="some default value", constraints=[TextLengthConstraint(1, 20)], instruction="Answer is word or phrase")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

        field.set_value("abc")
        self.assertEqual("abc", field.convert_to_unicode())

    def test_should_create_list_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your location",
            "name": "loc",
            "instruction": "Answer is list",
            "code": "Q1",
            "type": "list",
            "required": True,
        }
        field = HierarchyField(name="loc", code="Q1", label="What is your location", instruction="Answer is list")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

        field.set_value(["abc", "def"])
        self.assertEqual("abc,def", field.convert_to_unicode())


    def test_should_create_integer_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your age",
            "name": "Age",
            "code": "Q2",
            "type": "integer",
            "required": True,
            "instruction": "test_instruction"
        }
        field = IntegerField(name="Age", code="Q2", label="What is your age",
                             instruction="test_instruction")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)
        field.set_value(123)
        self.assertEqual("123", field.convert_to_unicode())

    def test_should_set_new_name_for_field(self):
        new_name = "newName"
        instruction = "test_instruction"
        expected_json = {
            "label": "What is your age",
            "name": new_name,
            "code": "Q2",
            
            "type": "integer",
            "required": True,
            "instruction": instruction
        }
        field = Field(type="integer", name="oldName", code="Q2", label="What is your age",
                      instruction=instruction)
        field.set_name(new_name)
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

    def test_should_set_new_instruction_for_field(self):
        new_instruction = "new_instruction"
        expected_json = {
            "label": "What is your age",
            "name": "name",
            "code": "Q2",
            
            "type": "integer",
            "required": True,
            "instruction": new_instruction
        }
        field = Field(type="integer", name="name", code="Q2", label="What is your age",
                      instruction="instruction")
        field.set_instruction(new_instruction)
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

    def test_should_create_integer_field_type_with_range(self):
        expected_json = {
            "label": "What is your age",
            "name": "Age",
            "code": "Q2",
            "constraints": [('range', {"min": 15, "max": 120})],
            
            "type": "integer",
            "required": True,
            "instruction": "test_instruction"
        }
        field = IntegerField(name="Age", code="Q2", label="What is your age",
                             constraints=[NumericRangeConstraint(min=15, max=120)],
                             instruction="test_instruction")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

    def test_should_create_select_one_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your favorite color",
            "name": "color",
            "choices": [{"text": "RED", "val": 1}, {"text": "YELLOW", "val": 2},
                        {"text": 'green', "val": 3}],
            "code": "Q3",
            
            "type": "select1",
            "required": True,
            "instruction": None
        }
        field = SelectField(name="color", code="Q3", label="What is your favorite color",
                            options=[("RED", 1), ("YELLOW", 2), ('green', 3)])
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)

        field.set_value(field.validate('b'))
        self.assertEqual("YELLOW", field.convert_to_unicode())

    def test_should_create_multi_select_field_type_for_default_english_language(self):
        expected_json = {
            "label": "What is your favorite color",
            "name": "color",
            "choices": [{"text": "RED", "val": 1}, {"text": "YELLOW", "val": 2},
                        {"text": 'green'}],
            "code": "Q3",
            
            "type": "select",
            "required": True,
            "instruction": "test_instruction"
        }
        field = SelectField(name="color", code="Q3", label="What is your favorite color",
                            options=[("RED", 1), ("YELLOW", 2), ('green')], single_select_flag=False,
                             instruction="test_instruction")
        actual_json = field._to_json()
        self.assertEqual(actual_json, expected_json)
        field.set_value(field.validate('ab'))
        self.assertEqual("RED,YELLOW", field.convert_to_unicode())

    def test_should_remove_spaces_if_present_in_answer_for_multi_select(self):
        field = SelectField(name="color", code="Q3", label="What is your favorite color",
                            options=[("RED", 1), ("YELLOW", 2), ('green')], single_select_flag=False,
                             instruction="test_instruction")
        self.assertEqual(['RED', 'YELLOW'], field.validate('a b'))

    def test_should_add_entity_field_with_default_constraints(self):
        expected_json = {
            "defaultValue": "",
            "label": "What is your name",
            "name": "field1_Name",
            "code": "Q1",
            "type": "short_code",
            "constraints": [("length", {"max": 20}), ("short_code", "^[a-zA-Z0-9]+$")],
            "required": False,
            "instruction": "test_instruction"
        }
        field = ShortCodeField(name="field1_Name", code="Q1", label="What is your name",
                          instruction="test_instruction")
        actual_json = field._to_json()
        self.assertDictEqual(actual_json, expected_json)

    def test_should_create_text_field_from_dictionary(self):
        field_json = {
            "defaultValue": "",
            "label": "What is your name",
            "name": "field1_Name",
{'fraction_non_alphanumeric': 0.1277365548537438, 'fraction_numerical': 0.00846262341325811, 'mean_word_length': 2.4174350377200335, 'pattern_counts': {'":': 160, '<': 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': '5381619', 'n_tokens_mistral': 4686, 'n_tokens_neox': 4438, 'n_words': 965}