<commit_before>from unittest import TestCase
from pyxform.builder import create_survey_element_from_dict
from pyxform.xls2json import SurveyReader, print_pyobj_to_json
from pyxform import Survey, InputQuestion

class BuilderTests(TestCase):
    
    def test_create_table_from_dict(self):
        d = {
            u"type" : u"loop",
            u"name" : u"my_loop",
            u"label" : {u"English" : u"My Loop"},
            u"columns" : [
                {
                    u"name" : u"col1",
                    u"label" : {u"English" : u"column 1"},
                    },
                {
                    u"name" : u"col2",
                    u"label" : {u"English" : u"column 2"},
                    },
                ],
            u"children" : [
                {
                    u"type": u"integer",
                    u"name": u"count",
                    u"label": {u"English": u"How many are there in this group?"}
                    },
                ]
            }
        g = create_survey_element_from_dict(d)

        expected_dict = {
            u'name': u'my_loop',
            u'label': {u'English': u'My Loop'},
            u'type' : u'group',
            u'children': [
                {
                    u'name': u'col1',
                    u'label': {u'English': u'column 1'},
                    u'type' : u'group',
                    u'children': [
                        {
                            u'name': u'count',
                            u'label': {u'English': u'How many are there in this group?'},
                            u'type': u'integer'
                            }
                        ]
                    },
                {
                    u'name': u'col2',
                    u'label': {u'English': u'column 2'},
                    u'type' : u'group',
                    u'children': [
                        {
                            u'name': u'count',
                            u'label': {u'English': u'How many are there in this group?'},
                            u'type': u'integer'
                            }
                        ]
                    }
                ]
            }

        self.assertEqual(g.to_dict(), expected_dict)

    def test_specify_other(self):
        excel_reader = SurveyReader("pyxform/tests/specify_other.xls")
        d = excel_reader.to_dict()
        survey = create_survey_element_from_dict(d)
        expected_dict = {
            u'name': 'specify_other',
            u'type': u'survey',
            u'children': [
                {
                    u'name': u'sex',
                    u'label': {u'English': u'What sex are you?'},
                    u'type': u'select one',
                    u'children': [
                        {
                            u'name': u'male',
                            u'label': {u'English': u'Male'}
                            },
                        {
                            u'name': u'female',
                            u'label': {u'English': u'Female'}
                            },
                        {
                            u'name': u'other',
                            u'label': u'Other'
                            }
                        ]
                    },
                {
                    u'name': u'sex_other',
                    u'bind': {u'relevant': u"selected(../sex, 'other')"},
                    u'label': u'Specify other.',
                    u'type': u'text'}
                ]
            }
        self.assertEqual(survey.to_dict(), expected_dict)

    def test_include(self):
        excel_reader = SurveyReader("pyxform/tests/include.xls")
        d = excel_reader.to_dict()
        survey = create_survey_element_from_dict(d)
        expected_dict = {
            u'name': 'include',
            u'type': u'survey',
            u'children': [
                {
                    u'name': u'name',
                    u'label': {u'English': u"What's your name?"},
                    u'type': u'text'
                    },
                    {
                        u'name': u'good_day',
                        u'label': {u'english': u'have you had a good day today?'},
                        u'type': u'select one',
                        u'children': [
                            {
                                u'name': u'yes',
                                u'label': {u'english': u'yes'}
                                },
                            {
                                u'name': u'no',
                                u'label': {u'english': u'no'}
                                }
                            ]}]}

        self.assertEqual(survey.to_dict(), expected_dict)

    def test_include_json(self):
        survey_out = Survey(
            type=u"survey"
            )
        question = InputQuestion(name=u"age")
        question.set(InputQuestion.TYPE, u"integer")
        question.set(InputQuestion.LABEL, u"How old are you?")
        survey_out.add_child(question)
        survey_out_dict = survey_out.to_dict()
        print_pyobj_to_json(survey_out_dict, "pyxform/tests/how_old_are_you.json")
            
        excel_reader = SurveyReader("pyxform/tests/include_json.xls")
        d = excel_reader.to_dict()
        survey_in = create_survey_element_from_dict(d)

        for k, v in survey_in.to_dict().items():
            if k!="name": self.assertEqual(v, survey_out_dict[k])

    def test_loop(self):
        excel_reader = SurveyReader("pyxform/tests/loop.xls")
        d = excel_reader.to_dict()
        survey = create_survey_element_from_dict(d)

        expected_dict = {
            u'name': 'loop',
            u'type': u'survey',
            u'children': [
                {
                    u'name': u'available_toilet_types',
                    u'label': {u'english': u'What type of toilets are on the premises?'},
                    u'type': u'select all that apply',<commit_msg>Added setUp and tearDown methods.<commit_after>from unittest import TestCase
from pyxform.builder import create_survey_element_from_dict
from pyxform.xls2json import SurveyReader, print_pyobj_to_json
from pyxform import Survey, InputQuestion

class BuilderTests(TestCase):

    def setUp(self):
        survey_out = Survey(
            type=u"survey"
            )
        question = InputQuestion(name=u"age")
        question.set(InputQuestion.TYPE, u"integer")
        question.set(InputQuestion.LABEL, u"How old are you?")
        survey_out.add_child(question)
        self.survey_out_dict = survey_out.to_dict()
        print_pyobj_to_json(self.survey_out_dict, "pyxform/tests/how_old_are_you.json")

    def tearDown(self):
        import os
        os.remove("pyxform/tests/how_old_are_you.json")

    def test_create_table_from_dict(self):
        d = {
            u"type" : u"loop",
            u"name" : u"my_loop",
            u"label" : {u"English" : u"My Loop"},
            u"columns" : [
                {
                    u"name" : u"col1",
                    u"label" : {u"English" : u"column 1"},
                    },
                {
                    u"name" : u"col2",
                    u"label" : {u"English" : u"column 2"},
                    },
                ],
            u"children" : [
                {
                    u"type": u"integer",
                    u"name": u"count",
                    u"label": {u"English": u"How many are there in this group?"}
                    },
                ]
            }
        g = create_survey_element_from_dict(d)

        expected_dict = {
            u'name': u'my_loop',
            u'label': {u'English': u'My Loop'},
            u'type' : u'group',
            u'children': [
                {
                    u'name': u'col1',
                    u'label': {u'English': u'column 1'},
                    u'type' : u'group',
                    u'children': [
                        {
                            u'name': u'count',
                            u'label': {u'English': u'How many are there in this group?'},
                            u'type': u'integer'
                            }
                        ]
                    },
                {
                    u'name': u'col2',
                    u'label': {u'English': u'column 2'},
                    u'type' : u'group',
                    u'children': [
                        {
                            u'name': u'count',
                            u'label': {u'English': u'How many are there in this group?'},
                            u'type': u'integer'
                            }
                        ]
                    }
                ]
            }

        self.assertEqual(g.to_dict(), expected_dict)

    def test_specify_other(self):
        excel_reader = SurveyReader("pyxform/tests/specify_other.xls")
        d = excel_reader.to_dict()
        survey = create_survey_element_from_dict(d)
        expected_dict = {
            u'name': 'specify_other',
            u'type': u'survey',
            u'children': [
                {
                    u'name': u'sex',
                    u'label': {u'English': u'What sex are you?'},
                    u'type': u'select one',
                    u'children': [
                        {
                            u'name': u'male',
                            u'label': {u'English': u'Male'}
                            },
                        {
                            u'name': u'female',
                            u'label': {u'English': u'Female'}
                            },
                        {
                            u'name': u'other',
                            u'label': u'Other'
                            }
                        ]
                    },
                {
                    u'name': u'sex_other',
                    u'bind': {u'relevant': u"selected(../sex, 'other')"},
                    u'label': u'Specify other.',
                    u'type': u'text'}
                ]
            }
        self.assertEqual(survey.to_dict(), expected_dict)

    def test_include(self):
        excel_reader = SurveyReader("pyxform/tests/include.xls")
        d = excel_reader.to_dict()
        survey = create_survey_element_from_dict(d)
        expected_dict = {
            u'name': 'include',
            u'type': u'survey',
            u'children': [
                {
                    u'name': u'name',
                    u'label': {u'English': u"What's your name?"},
                    u'type': u'text'
                    },
                    {
                        u'name': u'good_day',
                        u'label': {u'english': u'have you had a good day today?'},
                        u'type': u'select one',
                        u'children': [
                            {
                                u'name': u'yes',
                                u'label': {u'english': u'yes'}
                                },
                            {
                                u'name': u'no',
                                u'label': {u'english': u'no'}
                                }
                            ]}]}

        self.assertEqual(survey.to_dict(), expected_dict)

    def test_include_json(self):            
        excel_reader = SurveyReader("pyxform/tests/include_json.xls")
        d = excel_reader.to_dict()
        survey_in = create_survey_element_from_dict(d)

        for k, v in survey_in.to_dict().items():
            if k!="name": self.assertEqual(v, self.survey_out_dict[k])

    def test_loop(self):
        excel_reader = SurveyReader("pyxform/tests/loop.xls")
        d = excel_reader.to_dict()
        survey = create_survey_element_from_dict(d)

        expected_dict = {
            u'name': 'loop',
            u'type': u'survey',
            u'children': [
                {
                    u'name': u'available_toilet_types',
                    u'label': {u'english': u'What type of toilets are on the premises?'},
                    u'type': u'select all that apply',
{'fraction_non_alphanumeric': 0.11875666365947675, 'fraction_numerical': 0.001476256868695153, 'mean_word_length': 1.036746283614498, 'pattern_counts': {'":': 10, '<': 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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5300249', 'n_tokens_mistral': 3430, 'n_tokens_neox': 3264, 'n_words': 578}