def get_registration_xform(self): """ Gets a registration xform. (currently loaded in from fixture) Returns it without saving. """ reg_xform = _load_registration_survey_object() return XForm(xml=reg_xform.to_xml())
def test_excel_date_conversion(self): """Convert date from 01/01/1900 to 01-01-1900""" date_md_form = """ | survey | | | type | name | label | | | today | today | Today | | | text | name | Name | | | date | tdate | Date | | | start | start | | | | end | end | | | | dateTime | now | Now | | choices | | | list name | name | label | | settings | | | form_title | form_id | | | Dates | dates | """ self._create_user_and_login() data = {'name': 'data'} survey = self.md_to_pyxform_survey(date_md_form, kwargs=data) survey['sms_keyword'] = survey['id_string'] project = get_user_default_project(self.user) xform = XForm(created_by=self.user, user=self.user, xml=survey.to_xml(), json=survey.to_json(), project=project) xform.save() date_csv = open(os.path.join(self.fixtures_dir, 'date.csv'), 'rb') date_csv.seek(0) csv_reader = ucsv.DictReader(date_csv, encoding='utf-8-sig') xl_dates = [] xl_datetime = [] # xl dates for row in csv_reader: xl_dates.append(row.get('tdate')) xl_datetime.append(row.get('now')) csv_import.submit_csv(self.user.username, xform, date_csv) # converted dates conv_dates = [ instance.json.get('tdate') for instance in Instance.objects.filter( xform=xform).order_by('date_created') ] conv_datetime = [ instance.json.get('now') for instance in Instance.objects.filter( xform=xform).order_by('date_created') ] self.assertEqual(xl_dates, ['3/1/2019', '2/26/2019']) self.assertEqual( xl_datetime, [u'6/12/2020 13:20', u'2019-03-11T16:00:51.147+02:00']) self.assertEqual( conv_datetime, [u'2020-06-12T13:20:00.000000', u'2019-03-11T16:00:51.147+02:00']) self.assertEqual(conv_dates, ['2019-03-01', '2019-02-26'])
def test_delete_instance_metadata_without_perms(self): user = User(username="******") instance = Instance() instance.xform = XForm() # user.has_perms.return_value = False request = MagicMock(user=user, method='DELETE') obj = MagicMock(content_object=instance) self.assertFalse( self.permissions.has_object_permission(request, self.view, obj))
def test_delete_instance_metadata_without_perms(self, has_perms_mock): """ Test that a user cannot delete an instance if they are not allowed through the XForm or the Project """ has_perms_mock.return_value = False user = User(username="******") instance = Instance(user=User(username="******")) instance.xform = XForm() request = MagicMock(user=user, method='DELETE') obj = MagicMock(content_object=instance) self.assertFalse( self.permissions.has_object_permission(request, self.view, obj))
def test_get_survey_element(self): """ Test XForm.get_survey_element() """ markdown_xlsform = """ | survey | | | type | name | label | | | begin group | a | Group A | | | select one fruits | fruita | Fruit A | | | select one fruity | fruity | Fruit Y | | | end group | | | | | begin group | b | Group B | | | select one fruits | fruitz | Fruit Z | | | select_multiple fruity | fruitb | Fruit B | | | end group | | | | choices | | | list name | name | label | | | fruits | orange | Orange | | | fruits | mango | Mango | | | fruity | orange | Orange | | | fruity | mango | Mango | """ kwargs = {'name': 'favs', 'title': 'Fruits', 'id_string': 'favs'} survey = self.md_to_pyxform_survey(markdown_xlsform, kwargs) xform = XForm() xform._survey = survey # pylint: disable=W0212 # non existent field self.assertIsNone(xform.get_survey_element("non_existent")) # get fruita element by name fruita = xform.get_survey_element('fruita') self.assertEqual(fruita.get_abbreviated_xpath(), "a/fruita") # get exact choices element from choice abbreviated xpath fruita_o = xform.get_survey_element("a/fruita/orange") self.assertEqual(fruita_o.get_abbreviated_xpath(), "a/fruita/orange") fruity_m = xform.get_survey_element("a/fruity/mango") self.assertEqual(fruity_m.get_abbreviated_xpath(), "a/fruity/mango") fruitb_o = xform.get_survey_element("b/fruitb/orange") self.assertEqual(fruitb_o.get_abbreviated_xpath(), "b/fruitb/orange") self.assertEqual(xform.get_child_elements('NoneExistent'), [])
def test_upload_to_with_project_and_xform_instance(self): model_instance = Project(created_by=self.user) metadata = MetaData(data_type="media") metadata.content_object = model_instance filename = "filename" self.assertEquals(upload_to(metadata, filename), "{}/{}/{}".format(self.user.username, 'formid-media', filename)) model_instance = XForm(user=self.user, created_by=self.user) metadata = MetaData(data_type="media") metadata.content_object = model_instance filename = "filename" self.assertEquals(upload_to(metadata, filename), "{}/{}/{}".format(self.user.username, 'formid-media', filename))
def get_simple_xform(self): survey_object = _load_simple_survey_object() return XForm(xml=survey_object.to_xml()) i = self.get_simple_instance(custom_values) i.save() return i
def test_submit_csv_param_sanity_check(self): resp = csv_import.submit_csv(u'userX', XForm(), 123456) self.assertIsNotNone(resp.get('error'))