def test_missing_event_date(self): config = {'form_configs': [{'program_id': 'test program'}]} repeater = Dhis2Repeater() repeater.dhis2_config = Dhis2Config.wrap(config) with self.assertRaises(BadValueError) as e: repeater.save() self.assertEqual(str(e.exception), 'Property event_date is required.')
def test_minimal_config(self): config = { 'form_configs': json.dumps([{ 'program_id': 'test program', 'event_date': { 'doc_type': 'FormQuestion', 'form_question': '/data/event_date' }, 'datavalue_maps': [{ 'data_element_id': 'dhis2_element_id', 'value': { 'doc_type': 'FormQuestion', 'form_question': '/data/example_question' } }] }]) } form = Dhis2ConfigForm(data=config) self.assertTrue(form.is_valid()) repeater = Dhis2Repeater() repeater.dhis2_config = Dhis2Config.wrap(form.cleaned_data) repeater.save()
def config_dhis2_repeater(request, domain, repeater_id): repeater = Dhis2Repeater.get(repeater_id) assert repeater.domain == domain, f'"{repeater.domain}" != "{domain}"' if request.method == 'POST': form = Dhis2ConfigForm(data=request.POST) if form.is_valid(): data = form.cleaned_data repeater.dhis2_config.form_configs = list( map(Dhis2FormConfig.wrap, data['form_configs'])) repeater.save() else: form_configs = json.dumps([ form_config.to_json() for form_config in repeater.dhis2_config.form_configs ]) form = Dhis2ConfigForm(data={ 'form_configs': form_configs, }) return render(request, 'dhis2/edit_config.html', { 'domain': domain, 'repeater_id': repeater_id, 'form': form })
def repeater(self): repeater = Dhis2Repeater.get(self.repeater_id) assert repeater.domain == self.domain return repeater
def tearDown(self): Dhis2Repeater.set_db(self.db)
def setUp(self): self.db = Dhis2Repeater.get_db() self.fakedb = FakeCouchDb() Dhis2Repeater.set_db(self.fakedb)
def repeater(self): repeater = Dhis2Repeater.get(self.repeater_id) assert repeater.domain == self.domain return repeater
def test_form_processing(self): form = { "domain": DOMAIN, "form": { "@xmlns": "test_xmlns", "event_date": "2017-05-25T21:06:27.012000", "completed_date": "2017-05-25T21:06:27.012000", "name": "test event", "meta": { "location": self.location.location_id, "timeEnd": "2017-05-25T21:06:27.012000", "timeStart": "2017-05-25T21:06:17.739000", "userID": self.user.user_id, "username": self.user.username } }, "received_on": "2017-05-26T09:17:23.692083Z", } config = { 'form_configs': json.dumps([{ 'xmlns': 'test_xmlns', 'program_id': 'test program', 'event_status': 'COMPLETED', 'completed_date': { 'doc_type': 'FormQuestion', 'form_question': '/data/completed_date', 'external_data_type': DHIS2_DATA_TYPE_DATE }, 'org_unit_id': { 'doc_type': 'FormUserAncestorLocationField', 'form_user_ancestor_location_field': LOCATION_DHIS_ID }, 'datavalue_maps': [ { 'data_element_id': 'dhis2_element_id', 'value': { 'doc_type': 'FormQuestion', 'form_question': '/data/name' } } ] }]) } config_form = Dhis2ConfigForm(data=config) self.assertTrue(config_form.is_valid()) data = config_form.cleaned_data repeater = Dhis2Repeater() repeater.dhis2_config.form_configs = [Dhis2FormConfig.wrap(fc) for fc in data['form_configs']] repeater.save() event = get_event(DOMAIN, repeater.dhis2_config.form_configs[0], form) self.assertDictEqual( { 'dataValues': [ { 'dataElement': 'dhis2_element_id', 'value': 'test event' } ], 'status': 'COMPLETED', 'completedDate': '2017-05-25', 'program': 'test program', 'eventDate': '2017-05-26', 'orgUnit': 'dhis2_location_id' }, event )
def test_form_processing(self): form = { "domain": "dhis2", "form": { "@xmlns": "test_xmlns", "event_date": "2017-05-25T21:06:27.012000", "name": "test event", "meta": { "location": self.location.location_id, "timeEnd": "2017-05-25T21:06:27.012000", "timeStart": "2017-05-25T21:06:17.739000", "userID": self.user.user_id, "username": self.user.username } }, "received_on": "2017-05-25T21:06:27.275256Z", } config = { 'form_configs': json.dumps([{ 'xmlns': 'test_xmlns', 'program_id': 'test program', 'event_date': { 'doc_type': 'FormQuestion', 'form_question': '/data/event_date' }, 'event_status': 'COMPLETED', 'org_unit_id': { 'doc_type': 'ConstantString', 'value': 'dhis2_location_id' }, 'datavalue_maps': [ { 'data_element_id': 'dhis2_element_id', 'value': { 'doc_type': 'FormQuestion', 'form_question': '/data/name' } } ] }]) } config_form = Dhis2ConfigForm(data=config) self.assertTrue(config_form.is_valid()) data = config_form.cleaned_data repeater = Dhis2Repeater() repeater.dhis2_config.form_configs = list(map(Dhis2FormConfig.wrap, data['form_configs'])) repeater.save() dhis_format = _to_dhis_format(repeater.dhis2_config.form_configs[0], form) self.assertDictEqual( { 'dataValues': [ { 'dataElement': 'dhis2_element_id', 'value': 'test event' } ], 'status': 'COMPLETED', 'program': 'test program', 'eventDate': '2017-05-25', 'orgUnit': 'dhis2_location_id' }, dhis_format )
def setUp(self): self.db = Dhis2Repeater.get_db() self.fakedb = FakeCouchDb() Dhis2Repeater.set_db(self.fakedb) self.form = { "domain": DOMAIN, "form": { "@xmlns": "test_xmlns", "event_date": "2017-05-25T21:06:27.012000", "completed_date": "2017-05-25T21:06:27.012000", "event_location": "-33.6543213 19.12344312 abcdefg", "name": "test event", "meta": { "location": '', "timeEnd": "2017-05-25T21:06:27.012000", "timeStart": "2017-05-25T21:06:17.739000", "userID": self.user.user_id, "username": self.user.username } }, "received_on": "2017-05-26T09:17:23.692083Z", } self.config = { 'form_configs': json.dumps([{ 'xmlns': 'test_xmlns', 'program_id': 'test program', 'event_status': 'COMPLETED', 'completed_date': { 'doc_type': 'FormQuestion', 'form_question': '/data/completed_date', 'external_data_type': DHIS2_DATA_TYPE_DATE }, 'org_unit_id': { 'doc_type': 'FormUserAncestorLocationField', 'form_user_ancestor_location_field': LOCATION_DHIS_ID }, 'event_location': { 'form_question': '/data/event_location' }, 'datavalue_maps': [{ 'data_element_id': 'dhis2_element_id', 'value': { 'doc_type': 'FormQuestion', 'form_question': '/data/name' } }] }]) } config_form = Dhis2ConfigForm(data=self.config) self.assertTrue(config_form.is_valid()) data = config_form.cleaned_data conn = ConnectionSettings.objects.create(url="http://dummy.com", domain=DOMAIN) self.repeater = Dhis2Repeater(domain=DOMAIN, connection_settings_id=conn.id) self.repeater.dhis2_config.form_configs = [ Dhis2FormConfig.wrap(fc) for fc in data['form_configs'] ] self.repeater.save()