示例#1
0
    def setUp(self):
        self.suspected = CaseType(domain=self.domain,
                                  name='Suspected',
                                  description='A suspected case',
                                  fully_generated=True)
        self.suspected.save()
        self.suspected_name = CaseProperty(case_type=self.suspected,
                                           name='Patient name',
                                           description='Name of patient',
                                           deprecated=False,
                                           data_type='plain',
                                           group='')
        self.suspected_name.save()
        self.suspected_date = CaseProperty(
            case_type=self.suspected,
            name='Date opened',
            description='Date the case was opened',
            deprecated=False,
            data_type='date',
            group='')
        self.suspected_date.save()

        self.confirmed = CaseType(domain=self.domain,
                                  name='Confirmed',
                                  description='A confirmed case',
                                  fully_generated=True)
        self.confirmed.save()
        self.confirmed_name = CaseProperty(case_type=self.confirmed,
                                           name='Patient name',
                                           description='Name of patient',
                                           deprecated=False,
                                           data_type='plain',
                                           group='')
        self.confirmed_name.save()
        self.confirmed_date = CaseProperty(
            case_type=self.confirmed,
            name='Date opened',
            description='Date the case was opened',
            deprecated=False,
            data_type='date',
            group='')
        self.confirmed_date.save()
        self.confirmed_test = CaseProperty(
            case_type=self.confirmed,
            name='Test',
            description='Type of test performed',
            deprecated=False,
            data_type='plain',
            group='')
        self.confirmed_test.save()
示例#2
0
def _create_properties_for_case_types(domain, case_type_to_prop):
    current_case_types, current_properties = _get_current_case_types_and_properties(domain)
    new_case_properties = []

    for case_type, props in case_type_to_prop.items():
        if not case_type:
            continue

        try:
            case_type_obj = current_case_types[case_type]
        except KeyError:
            case_type_obj = CaseType.objects.create(domain=domain, name=case_type)

        for prop in props:
            # don't add any properites to parent cases
            if '/' in prop:
                continue

            if (case_type not in current_properties or
                    prop not in current_properties[case_type]):
                new_case_properties.append(CaseProperty(
                    case_type=case_type_obj, name=prop
                ))

    CaseProperty.objects.bulk_create(new_case_properties)
示例#3
0
 def setUpClass(cls):
     cls.domain = create_domain(cls.domain_name)
     cls.couch_user = WebUser.create(None, "test", "foobar")
     cls.couch_user.add_domain_membership(cls.domain_name, is_admin=True)
     cls.couch_user.save()
     cls.case_type_obj = CaseType(name='caseType', domain=cls.domain_name)
     cls.case_type_obj.save()
     CaseProperty(case_type=cls.case_type_obj, name='property').save()
示例#4
0
    def test_already_existing_property(self, mock):
        mock.return_value = {'type': ['property']}
        case_type = CaseType(domain=self.domain, name='type')
        case_type.save()
        CaseProperty(case_type=case_type, name='property').save()

        self.assertEqual(CaseType.objects.filter(domain=self.domain).count(), 1)
        self.assertEqual(CaseProperty.objects.filter(case_type__domain=self.domain).count(), 1)

        with self.assertNumQueries(3):
            generate_data_dictionary(self.domain)

        self.assertEqual(CaseType.objects.filter(domain=self.domain).count(), 1)
        self.assertEqual(CaseProperty.objects.filter(case_type__domain=self.domain).count(), 1)
示例#5
0
 def setUpClass(cls):
     super().setUpClass()
     cls.domain = create_domain(cls.domain_name)
     cls.case_type = CaseType(name="caseType", domain=cls.domain_name)
     cls.case_type.save()
     cls.date_property = CaseProperty(case_type=cls.case_type,
                                      data_type="date",
                                      name="dob")
     cls.date_property.save()
     cls.valid_date = datetime.date.today().isoformat()
     cls.select_property = CaseProperty.objects.create(
         case_type=cls.case_type, data_type="select", name="status")
     cls.valid_choices = ["todo", "in-progress", "complete"]
     for choice in cls.valid_choices:
         CasePropertyAllowedValue.objects.create(
             case_property=cls.select_property, allowed_value=choice)
示例#6
0
    def test_builder_for_registry(self):
        case_type_for_registry = CaseType(domain=self.domain,
                                          name='registry_prop',
                                          fully_generated=True)
        case_type_for_registry.save()
        CaseProperty(case_type=case_type_for_registry,
                     name='registry_property',
                     deprecated=False,
                     data_type='plain',
                     group='').save()
        user = create_user("admin", "123")
        registry = create_registry_for_test(user,
                                            self.domain,
                                            invitations=[
                                                Invitation('foo',
                                                           accepted=True),
                                                Invitation('user-reports',
                                                           accepted=True),
                                            ],
                                            name='registry')
        registry_data_source = get_sample_registry_data_source(
            registry_slug=registry.slug)
        registry_data_source.save()
        registry.schema = RegistrySchemaBuilder(["registry_prop"]).build()
        registry.save()

        builder = RegistryCaseDataSourceHelper(self.domain, registry.slug,
                                               'case',
                                               case_type_for_registry.name)

        expected_property_names = [
            'closed', 'closed_on', 'registry_property', 'computed/owner_name',
            'computed/user_name', 'commcare_project'
        ]
        self.assertEqual(expected_property_names,
                         list(builder.data_source_properties.keys()))
        registry_prop = builder.data_source_properties['registry_property']
        self.assertEqual('registry_property', registry_prop.get_id())
        self.assertEqual('registry property', registry_prop.get_text())
示例#7
0
    def test_update_data_dictionary(self):
        self.assertEqual({}, get_data_dictionary(self.linked_domain))

        # Update linked domain
        update_data_dictionary(self.domain_link)

        # Linked domain should now have master domain's data dictionary
        linked_data_dictionary = get_data_dictionary(self.linked_domain)

        def expected_property_type(description, data_type):
            return {
                'description': description,
                'deprecated': False,
                'data_type': data_type,
                'group': '',
            }

        patient_name = expected_property_type('Name of patient', 'plain')
        date_opened = expected_property_type('Date the case was opened',
                                             'date')
        test_performed = expected_property_type('Type of test performed',
                                                'plain')

        suspected_properties = {
            'Patient name': patient_name,
            'Date opened': date_opened
        }
        confirmed_properties = suspected_properties.copy()
        confirmed_properties.update({'Test': test_performed})

        def expected_case_type(domain, description, properties):
            return {
                'domain': domain,
                'description': description,
                'fully_generated': True,
                'properties': properties
            }

        self.assertEqual(
            linked_data_dictionary, {
                'Suspected':
                expected_case_type(self.linked_domain, 'A suspected case',
                                   suspected_properties),
                'Confirmed':
                expected_case_type(self.linked_domain, 'A confirmed case',
                                   confirmed_properties)
            })

        # Master domain's data dictionary should be untouched
        original_data_dictionary = get_data_dictionary(self.domain)
        self.assertEqual(
            original_data_dictionary, {
                'Suspected':
                expected_case_type(self.domain, 'A suspected case',
                                   suspected_properties),
                'Confirmed':
                expected_case_type(self.domain, 'A confirmed case',
                                   confirmed_properties)
            })

        # Change the original domain and update the linked domain.
        self.suspected_date.delete()
        del suspected_properties['Date opened']

        self.confirmed_date.delete()
        del confirmed_properties['Date opened']

        self.archived = CaseType(domain=self.domain,
                                 name='Archived',
                                 description='An archived case',
                                 fully_generated=True)
        self.archived.save()
        self.archived_name = CaseProperty(case_type=self.archived,
                                          name='Patient name',
                                          description='Name of patient',
                                          deprecated=False,
                                          data_type='plain',
                                          group='')
        self.archived_name.save()
        self.archived_reason = CaseProperty(case_type=self.archived,
                                            name='Reason',
                                            description='Reason for archiving',
                                            deprecated=False,
                                            data_type='plain',
                                            group='')
        self.archived_reason.save()
        update_data_dictionary(self.domain_link)

        reason_archived = expected_property_type('Reason for archiving',
                                                 'plain')
        archived_properties = suspected_properties.copy()
        archived_properties.update({'Reason': reason_archived})

        # Checked that the linked domain has the new state.
        linked_data_dictionary = get_data_dictionary(self.linked_domain)
        self.assertEqual(
            linked_data_dictionary, {
                'Suspected':
                expected_case_type(self.linked_domain, 'A suspected case',
                                   suspected_properties),
                'Confirmed':
                expected_case_type(self.linked_domain, 'A confirmed case',
                                   confirmed_properties),
                'Archived':
                expected_case_type(self.linked_domain, 'An archived case',
                                   archived_properties)
            })
        self.addCleanup(self.archived_name.delete)
        self.addCleanup(self.archived_reason.delete)
        self.addCleanup(self.archived.delete)