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()
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)
def save_case_property(name, case_type, domain=None, data_type=None, description=None, group=None, deprecated=None): """ Takes a case property to update and returns an error if there was one """ if not name: return ugettext('Case property must have a name') prop = CaseProperty.get_or_create(name=name, case_type=case_type, domain=domain) if data_type: prop.data_type = data_type if description: prop.description = description if group: prop.group = group if deprecated is not None: prop.deprecated = deprecated try: prop.full_clean() except ValidationError as e: return six.text_type(e) prop.save()
def update_data_dictionary(domain_link): if domain_link.is_remote: master_results = remote_get_data_dictionary(domain_link) else: master_results = local_get_data_dictionary(domain_link.master_domain) # Start from an empty set of CaseTypes and CaseProperties in the linked domain. CaseType.objects.filter(domain=domain_link.linked_domain).delete() # Create CaseType and CaseProperty as necessary for case_type_name, case_type_desc in master_results.items(): case_type_obj = CaseType.get_or_create(domain_link.linked_domain, case_type_name) case_type_obj.description = case_type_desc['description'] case_type_obj.fully_generated = case_type_desc['fully_generated'] case_type_obj.save() for case_property_name, case_property_desc in case_type_desc['properties'].items(): case_property_obj = CaseProperty.get_or_create(case_property_name, case_type_obj.name, domain_link.linked_domain) case_property_obj.description = case_property_desc['description'] case_property_obj.deprecated = case_property_desc['deprecated'] case_property_obj.data_type = case_property_desc['data_type'] case_property_obj.group = case_property_desc['group'] case_property_obj.save()
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()
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)
def setup_data_dictionary(domain, case_type_name, prop_list=None, allowed_values=None): prop_list = prop_list or [] allowed_values = allowed_values or {} CaseType.get_or_create(domain, case_type_name) for prop_name, data_type in prop_list: prop = CaseProperty.get_or_create(prop_name, case_type_name, domain) prop.data_type = data_type prop.save() if prop_name in allowed_values: for value in allowed_values[prop_name]: CasePropertyAllowedValue.objects.get_or_create( case_property=prop, allowed_value=value)
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)
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())
def save_case_property(name, case_type, domain=None, data_type=None, description=None, group=None, deprecated=None, fhir_resource_prop_path=None, fhir_resource_type=None, remove_path=False): """ Takes a case property to update and returns an error if there was one """ if not name: return ugettext('Case property must have a name') prop = CaseProperty.get_or_create(name=name, case_type=case_type, domain=domain) if data_type: prop.data_type = data_type if description: prop.description = description if group: prop.group = group if deprecated is not None: prop.deprecated = deprecated try: prop.full_clean() except ValidationError as e: return str(e) if fhir_resource_type and fhir_resource_prop_path: update_fhir_resource_property(prop, fhir_resource_type, fhir_resource_prop_path, remove_path) prop.save()
def save_case_property(name, case_type, domain=None, data_type=None, description=None, group=None, deprecated=None): """ Takes a case property to update and returns an error if there was one """ if not name: return ugettext('Case property must have a name') prop = CaseProperty.get_or_create( name=name, case_type=case_type, domain=domain ) if data_type: prop.data_type = data_type if description: prop.description = description if group: prop.group = group if deprecated is not None: prop.deprecated = deprecated try: prop.full_clean() except ValidationError as e: return six.text_type(e) prop.save()
class TestUpdateDataDictionary(BaseLinkedDomainTest): 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() def tearDown(self): self.suspected.delete() self.confirmed.delete() 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)