def test_empty_json(self):
     config = {'form_configs': [{}]}
     repeater = Dhis2Repeater()
     repeater.dhis2_config = Dhis2Config.wrap(config)
     with self.assertRaises(BadValueError) as e:
         repeater.save()
     self.assertEqual(str(e.exception), "Property xmlns is required.")
    def test_config_empty_datavalue_map(self):
        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': [{}]
            }])
        }
        form = Dhis2ConfigForm(data=config)
        self.assertTrue(form.is_valid())

        repeater = Dhis2Repeater()
        repeater.dhis2_config = Dhis2Config.wrap(form.cleaned_data)
        with self.assertRaises(BadValueError) as e:
            repeater.save()
        self.assertEqual(str(e.exception),
                         "Property data_element_id is required.")
    def test_full_config(self):
        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/example_question'
                    }
                }]
            }])
        }
        form = Dhis2ConfigForm(data=config)
        self.assertTrue(form.is_valid())

        repeater = Dhis2Repeater()
        repeater.dhis2_config = Dhis2Config.wrap(form.cleaned_data)
        repeater.save()
    def test_org_unit_id_migration(self):
        config = {
            'form_configs':
            json.dumps([{
                'xmlns':
                'test_xmlns',
                'program_id':
                'test program',
                'org_unit_id':
                'dhis2_location_id',
                '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()
        org_unit_value_source = dict(
            repeater.dhis2_config.form_configs[0].org_unit_id)
        self.assertDictEqual(org_unit_value_source,
                             {'value': 'dhis2_location_id'})
Example #5
0
    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()
Example #6
0
 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.')
Example #7
0
    def test_get_programs_by_id(self):
        program_id = 'test program'

        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": 'test 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",
        }
        config = {
            'form_configs': json.dumps([{
                'xmlns': 'test_xmlns',
                'program_id': program_id,
                'event_status': 'COMPLETED',
                'event_location': {
                    'form_question': '/data/event_location'
                },
                '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(domain=DOMAIN)
        conn = ConnectionSettings(domain=DOMAIN, url="http://dummy.com")
        conn.save()
        repeater.dhis2_config.form_configs = [Dhis2FormConfig.wrap(fc) for fc in data['form_configs']]
        repeater.connection_settings_id = conn.id
        repeater.save()

        info = CaseTriggerInfo(
            domain=DOMAIN,
            case_id=None,
            owner_id='test_location',
            form_question_values=get_form_question_values(form),
        )

        programs = get_programs_by_id(info, repeater.dhis2_config)

        self.assertDictEqual(
            programs[program_id]['geometry'],
            {'type': 'Point', 'coordinates': [-33.6543, 19.1234]}
        )
Example #8
0
 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
     )
Example #10
0
 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()