Best Python code snippet using lettuce_webdriver_python
test_chart_tools.py
Source:test_chart_tools.py  
...5from onadata.apps.main.tests.test_base import TestBase6from onadata.libs.utils.chart_tools import build_chart_data_for_field,\7    build_chart_data, utc_time_string_for_javascript, calculate_ranges8from onadata.apps.logger.models import XForm9def find_field_by_name(dd, field_name):10        return filter(11            lambda f: f.name == field_name, [e for e in dd.survey_elements])[0]12class TestChartTools(TestBase):13    def setUp(self):14        super(TestChartTools, self).setUp()15        # create an xform16        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",17                            "apps", "api", "tests", "fixtures", "forms",18                            "tutorial", "tutorial.xls")19        self._publish_xls_file_and_set_xform(path)20        # make a couple of submissions21        for i in range(1, 3):22            path = os.path.join(os.path.dirname(__file__), "..", "..", "..",23                                "apps", "api", "tests", "fixtures", "forms",24                                "tutorial", "instances", "{}.xml".format(i))25            self._make_submission(path)26    def test_build_chart_data_for_field_on_submission_time(self):27        data = build_chart_data_for_field(self.xform, '_submission_time')28        self.assertEqual(data['field_name'], '_submission_time')29        self.assertEqual(data['field_type'], 'datetime')30        self.assertEqual(data['data_type'], 'time_based')31    def test_build_chart_data_for_fields_with_accents(self):32        xls_path = os.path.join(33            self.this_directory, "fixtures",34            "sample_accent.xlsx")35        response = self._publish_xls_file(xls_path)36        self.assertEquals(response.status_code, 200)37        xform = XForm.objects.all()[0]38        self.assertEqual(xform.title, "sample_accent")39        dd = xform.data_dictionary()40        field = find_field_by_name(dd, u'tête')41        data = build_chart_data_for_field(self.xform, field)42        self.assertEqual(data['field_name'], u'words_with_accents-tête')43        field = find_field_by_name(dd, u'té')44        data = build_chart_data_for_field(self.xform, field)45        self.assertEqual(data['field_name'], u'words_with_accents-té')46        field = find_field_by_name(dd, u'père')47        data = build_chart_data_for_field(self.xform, field)48        self.assertEqual(data['field_name'], u'words_with_accents-père')49    def test_build_chart_data_for_field_on_select_one(self):50        field_name = 'gender'51        dd = self.xform.data_dictionary()52        field = find_field_by_name(dd, field_name)53        data = build_chart_data_for_field(self.xform, field)54        self.assertEqual(data['field_name'], field_name)55        self.assertEqual(data['field_type'], 'select one')56        self.assertEqual(data['data_type'], 'categorized')57        # map the list to a dict58        for d in data['data']:59            genders = d[field_name]60            count = d['count']61            self.assertEqual(type(genders), list)62            self.assertEqual(count, 1)63    def test_build_chart_data_for_field_on_grouped_field(self):64        dd = self.xform.data_dictionary()65        field = find_field_by_name(dd, 'a_text')66        data = build_chart_data_for_field(self.xform, field)67        self.assertEqual(data['field_name'], 'a_group-a_text')68        self.assertEqual(data['field_xpath'], 'a_text')69        self.assertEqual(data['field_type'], 'text')70        self.assertEqual(data['data_type'], 'categorized')71    def test_build_chart_data_output(self):72        data = build_chart_data(self.xform)73        self.assertIsInstance(data, list)74        # check expected fields75        expected_fields = sorted(['_submission_time', 'pizza_type', 'age',76                                  'gender', 'date', 'pizza_fan', 'net_worth',77                                  'start_time', 'end_time', 'today'])78        data_field_names = sorted([f['field_name'] for f in data])79        self.assertEqual(expected_fields, data_field_names)80    def test_build_chart_data_strips_none_from_dates(self):81        # make the 3rd submission that doesnt have a date82        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",83                            "apps", "api", "tests", "fixtures", "forms",84                            "tutorial", "instances", "3.xml")85        self._make_submission(path)86        dd = self.xform.data_dictionary()87        field = find_field_by_name(dd, 'date')88        data = build_chart_data_for_field(self.xform, field)89        # create a list with comparisons to the dict values90        values = [d['date'] is not None for d in data['data']]91        self.assertTrue(all(values))92    def test_build_chart_data_for_field_with_language(self):93        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",94                            "apps", "main", "tests", "fixtures",95                            "good_eats_multilang", "good_eats_multilang.xls")96        self._publish_xls_file_and_set_xform(path)97        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",98                            "apps", "main", "tests", "fixtures",99                            "good_eats_multilang", "1.xml")100        self._make_submission(path)101        dd = self.xform.data_dictionary()102        field = find_field_by_name(dd, 'food_type')103        data = build_chart_data_for_field(self.xform, field, language_index=1)104        self.assertEqual(data['field_label'], u"Type of Eat")105    def test_build_chart_data_for_field_with_language_on_non_lang_field(self):106        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",107                            "apps", "main", "tests", "fixtures",108                            "good_eats_multilang", "good_eats_multilang.xls")109        self._publish_xls_file_and_set_xform(path)110        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",111                            "apps", "main", "tests", "fixtures",112                            "good_eats_multilang", "1.xml")113        self._make_submission(path)114        dd = self.xform.data_dictionary()115        field = find_field_by_name(dd, 'submit_date')116        data = build_chart_data_for_field(self.xform, field, language_index=1)117        self.assertEqual(data['field_label'], 'submit_date')118    def test_build_chart_data_with_nonexisting_field_xpath(self):119        # make the 3rd submission that doesnt have a date120        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",121                            "apps", "api", "tests", "fixtures", "forms",122                            "tutorial", "instances", "3.xml")123        self._make_submission(path)124        dd = self.xform.data_dictionary()125        field = find_field_by_name(dd, 'date')126        field.name = 'informed_consent/pas_denfants_elig/q7b'127        data = build_chart_data_for_field(self.xform, field)128        # create a list with comparisons to the dict values129        values = [d['date'] is not None for d in data['data']]130        self.assertTrue(all(values))131    def test_build_chart_data_with_field_name_with_lengh_65(self):132        # make the 3rd submission that doesnt have a date133        path = os.path.join(os.path.dirname(__file__), "..", "..", "..",134                            "apps", "api", "tests", "fixtures", "forms",135                            "tutorial", "instances", "3.xml")136        self._make_submission(path)137        dd = self.xform.data_dictionary()138        field = find_field_by_name(dd, 'date')139        field.name = 'a' * 65140        data = build_chart_data_for_field(self.xform, field)141        self.assertEqual(data['field_name'], field.name)142    def mock_get_abbreviated_xpath(self):143        return 'informed_consent/pas_denfants_elig/date'144class TestChartUtilFunctions(unittest.TestCase):145    def test_utc_time_string_for_javascript(self):146        time_str = '2014-01-16T12:07:23.322+03'147        expected_time_str = '2014-01-16T12:07:23.322+0300'148        result = utc_time_string_for_javascript(time_str)149        self.assertEqual(result, expected_time_str)150    def test_raise_value_error_if_no_match(self):151        time_str = '2014-01-16T12:07:23.322'152        self.assertRaises(ValueError, utc_time_string_for_javascript, time_str)...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
