def test_sync_to_sql(self): doc = CustomDataFieldsDefinition( domain='botswana', field_type='UserFields', fields=[ CustomDataField( slug='color', is_required=True, label='Color', choices=['red', 'orange', 'yellow'], ), CustomDataField( slug='size', is_required=False, label='Size', regex='^[0-9]+$', regex_msg='Εισαγάγετε', ), ], ) doc.save() self.assertIsNone( Command.diff_couch_and_sql( doc.to_json(), SQLCustomDataFieldsDefinition.objects.get( couch_id=doc.get_id))) doc.fields[0].label = 'Hue' doc.save() self.assertEquals( 'Hue', SQLCustomDataFieldsDefinition.objects.get( couch_id=doc.get_id).get_fields()[0].label)
def setUpClass(cls): super(TestIndexedLocationsFixture, cls).setUpClass() cls.user = create_restore_user(cls.domain, 'user', '123') cls.loc_fields = CustomDataFieldsDefinition.get_or_create(cls.domain, LocationFieldsView.field_type) cls.loc_fields.fields = [ CustomDataField(slug='is_test', index_in_fixture=True), CustomDataField(slug='favorite_color'), ] cls.loc_fields.save() cls.field_slugs = [f.slug for f in cls.loc_fields.fields] for location in cls.locations.values(): location.metadata = { 'is_test': 'no', 'favorite_color': 'blue', } location.save()
def setUpClass(cls): super(TestLocationsExport, cls).setUpClass() cls.loc_fields = CustomDataFieldsDefinition.get_or_create( cls.domain, LocationFieldsView.field_type) cls.loc_fields.fields = [ CustomDataField(slug=slug) for slug in cls.custom_fields ] cls.loc_fields.save() cls.boston = cls.locations['Boston'] cls.boston.metadata = { field: '{}-试验'.format(field) for field in cls.custom_fields + ['不知道'] } cls.boston.save() exporter = LocationExporter(cls.domain) writer = MockExportWriter() exporter.write_data(writer) cls.headers = dict(exporter.get_headers()) cls.city_headers = cls.headers['city'][0] cls.boston_data = [ row for row in writer.data['city'] if row[0] == cls.boston.location_id ][0]
def test_migration(self): doc = CustomDataFieldsDefinition( domain='botswana', field_type='UserFields', fields=[ CustomDataField( slug='color', is_required=True, label='Color', choices=['red', 'orange', 'yellow'], ), ], ) doc.save(sync_to_sql=False) call_command('populate_custom_data_fields') self.assertIsNone( Command.diff_couch_and_sql( doc.to_json(), SQLCustomDataFieldsDefinition.objects.get( couch_id=doc.get_id))) # Additional call should apply any updates doc = CustomDataFieldsDefinition.get(doc.get_id) doc.field_type = 'LocationFields' doc.save(sync_to_sql=False) call_command('populate_custom_data_fields') self.assertIsNone( Command.diff_couch_and_sql( doc.to_json(), SQLCustomDataFieldsDefinition.objects.get( couch_id=doc.get_id)))
def test_download_reupload_no_changes(self): # Make sure there's a bunch of data loc_fields = CustomDataFieldsDefinition.get_or_create( self.domain, 'LocationFields') loc_fields.fields = [ CustomDataField(slug='favorite_color'), CustomDataField(slug='language') ] loc_fields.save() self.locations['City111'].latitude = Decimal('42.36') self.locations['City111'].longitude = Decimal('71.06') self.locations['City111'].external_id = '123' self.locations['County11'].metadata = { 'favorite_color': 'purple', 'language': 'en' } self.locations['City111'].save() self.locations['County11'].external_id = '321' self.locations['County11'].metadata = {'favorite_color': 'blue'} self.locations['County11'].save() # Export locations exporter = LocationExporter(self.domain) writer = MockExportWriter() exporter.write_data(writer) # Re-upload that export worksheets = [] for sheet_title, headers in exporter.get_headers(): rows = [[val if val is not None else '' for val in row] for row in writer.data[sheet_title]] sheet = IteratorJSONReader(headers + rows) sheet.title = sheet_title worksheets.append(sheet) mock_importer = Mock() mock_importer.worksheets = worksheets with patch('corehq.apps.locations.models.SQLLocation.save') as save_location, \ patch('corehq.apps.locations.models.LocationType.save') as save_type: result = new_locations_import(self.domain, mock_importer, self.user) # The upload should succeed and not perform any updates assert_errors(result, []) self.assertFalse(save_location.called) self.assertFalse(save_type.called)
def get_fields(self, spec): return [ CustomDataField( slug=slug, is_required=False, label=label, choices=choices, is_multiple_choice=bool(choices), ) for slug, label, choices in spec ]
def update_custom_data_models(domain_link, limit_types=None): if domain_link.is_remote: master_results = remote_custom_data_models(domain_link, limit_types) else: master_results = local_custom_data_models(domain_link.master_domain, limit_types) for field_type, field_definitions in master_results.items(): model = CustomDataFieldsDefinition.get_or_create(domain_link.linked_domain, field_type) model.fields = [CustomDataField.wrap(field_def) for field_def in field_definitions] model.save()
def prepare_domain(domain_name): from corehq.apps.commtrack.tests.util import bootstrap_domain domain = bootstrap_domain(domain_name) previous = None for name, administrative in [ ("MOHSW", True), ("MSDZONE", True), ("REGION", True), ("DISTRICT", True), ("FACILITY", False) ]: previous, _ = LocationType.objects.get_or_create( domain=domain_name, name=name, parent_type=previous, administrative=administrative, ) generator.instantiate_accounting() account = BillingAccount.get_or_create_account_by_domain( domain.name, created_by="automated-test", )[0] plan = DefaultProductPlan.get_default_plan( edition=SoftwarePlanEdition.ADVANCED ) commtrack = domain.commtrack_settings commtrack.actions.append( CommtrackActionConfig(action='receipts', keyword='delivered', caption='Delivered') ) commtrack.save() subscription = Subscription.new_domain_subscription( account, domain.name, plan ) subscription.is_active = True subscription.save() ils_config = ILSGatewayConfig(enabled=True, domain=domain.name, all_stock_data=True) ils_config.save() fields_definition = CustomDataFieldsDefinition.get_or_create(domain.name, 'LocationFields') fields_definition.fields.append(CustomDataField( slug='group', label='Group', is_required=False, choices=['A', 'B', 'C'], is_multiple_choice=False )) fields_definition.save() return domain
def setUpClass(cls): super(TestUserSetupUtils, cls).setUpClass() cls.domain_obj = Domain(name=cls.domain) cls.domain_obj.save() cls.location_types, cls.locations = setup_enikshay_locations( cls.domain) with mock.patch( 'corehq.apps.locations.tasks._create_or_unarchive_users', mock.MagicMock()): for location_type in cls.location_types.values(): if location_type.code in AGENCY_LOCATION_TYPES: location_type.has_user = True location_type.save() # set up data fields user_fields = CustomDataFieldsDefinition.get_or_create( cls.domain, CUSTOM_USER_DATA_FIELD_TYPE) usertypes = [ 'to', 'tbhv', 'mo-phi', 'sts', 'stls', 'lt-dmc', 'lt-cdst', 'dto', 'deo', 'cto', 'sto', 'drtb-hiv' ] user_fields.fields = [ CustomDataField( slug='usertype', is_multiple_choice=True, choices=usertypes), CustomDataField(slug='is_test'), CustomDataField(slug='nikshay_id'), ] + [ CustomDataField(slug=slug, is_required=False) for slug, _, __ in AGENCY_USER_FIELDS ] user_fields.save() location_fields = CustomDataFieldsDefinition.get_or_create( cls.domain, LocationFieldsView.field_type) location_fields.fields = [ CustomDataField(slug='nikshay_code', is_required=True) ] + [ CustomDataField(slug=slug) for slug, _, __ in AGENCY_LOCATION_FIELDS ] location_fields.save() cls.web_user = WebUser.create(cls.domain, 'blah', 'password')
def test_diff(self): # Start with identical data def_args = {'domain': 'some-domain', 'field_type': 'ProductFields'} field1_args = { 'slug': 'texture', 'is_required': True, 'label': 'Texture', 'choices': ['soft', 'spiky'] } obj = SQLCustomDataFieldsDefinition(**def_args) obj.save() obj.set_fields([SQLField(**field1_args)]) doc = CustomDataFieldsDefinition( fields=[CustomDataField(**field1_args)], **def_args, ).to_json() self.assertIsNone(Command.diff_couch_and_sql(doc, obj)) # Difference in top-level attribute doc['domain'] = 'other-domain' self.assertEqual( Command.diff_couch_and_sql(doc, obj), "domain: couch value 'other-domain' != sql value 'some-domain'") # Difference in numer of sub-models doc['domain'] = 'some-domain' field2_args = { 'slug': 'temp', 'is_required': False, 'label': 'F', 'choices': ['32', '212'] } doc['fields'] = [ CustomDataField(**field1_args).to_json(), CustomDataField(**field2_args).to_json() ] self.assertEqual(Command.diff_couch_and_sql(doc, obj), "fields: 2 in couch != 1 in sql") # Different in sub-model attribute field2_args['label'] = 'C' obj.set_fields([SQLField(**field1_args), SQLField(**field2_args)]) self.assertEqual(Command.diff_couch_and_sql(doc, obj), "label: couch value 'F' != sql value 'C'") # Difference in sub-model ordering field2_args['label'] = 'F' obj.set_fields([SQLField(**field2_args), SQLField(**field1_args)]) self.assertEqual( Command.diff_couch_and_sql(doc, obj).split("\n"), [ "slug: couch value 'texture' != sql value 'temp'", "is_required: couch value 'True' != sql value 'False'", "label: couch value 'Texture' != sql value 'F'", "choices: couch value '['soft', 'spiky']' != sql value '['32', '212']'", "slug: couch value 'temp' != sql value 'texture'", "is_required: couch value 'False' != sql value 'True'", "label: couch value 'F' != sql value 'Texture'", "choices: couch value '['32', '212']' != sql value '['soft', 'spiky']'", ]) # Identical data obj.set_fields([SQLField(**field1_args), SQLField(**field2_args)]) self.assertIsNone(Command.diff_couch_and_sql(doc, obj))