def __init__(self, export_plan=None, community=None, exception_cls=None): super(Communities, self).__init__(export_plan=export_plan, community=community, exception_cls=exception_cls) site_mappers.autodiscover() if community: self.mappers = [site_mappers.get(community)]
def setUp(self): try: site_lab_profiles.register(BcppSubjectProfile()) except AlreadyRegisteredLabProfile: pass BcppAppConfiguration() site_lab_tracker.autodiscover() BcppSubjectVisitSchedule().build() self.survey1 = Survey.objects.get(survey_name='BCPP Year 1') # see app_configuration site_mappers.autodiscover() mapper = site_mappers.get(site_mappers.get_as_list()[0]) self.community = mapper().get_map_area() gps_degrees_s, gps_minutes_s, gps_degrees_e, gps_minutes_e = mapper().test_location self.plot = PlotFactory(community=self.community, gps_degrees_s=gps_degrees_s, gps_minutes_s=gps_minutes_s, gps_degrees_e=gps_degrees_e, gps_minutes_e=gps_minutes_e, household_count=2, status='residential_habitable') self.household1 = Household.objects.filter(plot=self.plot).order_by('created')[0] self.household_structure1 = HouseholdStructure.objects.get(household=self.household1, survey=self.survey1) self.household2 = Household.objects.filter(plot=self.plot).order_by('created')[1] self.household_member1 = HouseholdMemberFactory(household_structure=self.household_structure1, age_in_years=26, present_today='Yes', member_status='RESEARCH') self.household_member2 = HouseholdMemberFactory(household_structure=self.household_structure1, age_in_years=36, present_today='Yes') self.household_member3 = HouseholdMemberFactory(household_structure=self.household_structure1, age_in_years=12, present_today='Yes') self.household_member4 = HouseholdMemberFactory(household_structure=self.household_structure1, age_in_years=99, present_today='Yes') self.dashboard_type = 'household' self.dashboard_model = 'household' self.dashboard_id = self.household1.pk self.household_member1.eligible_subject = True self.subject_consent = SubjectConsentFactory( household_member=self.household_member1, first_name=self.household_member1.first_name, initials=self.household_member1.initials, registered_subject=self.household_member1.registered_subject)
def test_p2(self): """Confirms all models have a get_by_natural_key manager method.""" self.startup() print 'get a community name from the mapper classes' site_mappers.autodiscover() mapper = site_mappers.get(site_mappers.get_as_list()[0]) print 'No. of SURVEY = ' + str(Survey.objects.all().count()) plot = PlotFactory(community=mapper().get_map_area(), status='residential_habitable') plot.save() print 'No. of HOUSEHOLDS = ' + str(Household.objects.all().count()) household = Household.objects.get(plot=plot) self.assertEquals(HouseholdStructure.objects.all().count(), 3) self.assertEquals(Survey.objects.all().count(), 3) household_structure = HouseholdStructure.objects.get( survey=Survey.objects.all()[0]) print 'No. of HOUSEHOLDS_STRUCTURE = ' + str( HouseholdStructure.objects.all().count()) HouseholdRefusalFactory(household_structure=household_structure) print 'No. of HOUSEHOLDS_REFUSALS = ' + str( HouseholdRefusal.objects.all().count()) print 'HOUSEHOLD_REFUSAL=' + str(HouseholdRefusal.objects.all()[0]) HouseholdRefusal.objects.get( household_structure=household_structure).delete() print 'No. of HOUSEHOLDS_REFUSALS = ' + str( HouseholdRefusal.objects.all().count()) household_structure.failed_enumeration_attempts = 3 household_assesment = HouseholdAssessmentFactory( household_structure=household_structure) household_log = HouseholdLog.objects.get( household_structure=household_structure) household_log_entry1 = HouseholdLogEntryFactory( household_log=household_log, report_datetime=date.today()) household_log_entry2 = HouseholdLogEntryFactory( household_log=household_log, report_datetime=date.today() + timedelta(days=1)) plot_log = PlotLogFactory(plot=plot) plot_log_entry1 = PlotLogEntryFactory(plot_log=plot_log, report_datetime=datetime.now()) plot_log_entry2 = PlotLogEntryFactory(plot_log=plot_log, report_datetime=datetime.now() + timedelta(days=1)) instances = [] instances.append(plot) instances.append(household) instances.append(household_structure) instances.append(household_assesment) instances.append(household_log) instances.append(plot_log_entry1) instances.append(plot_log_entry2) instances.append(plot_log) instances.append(household_log_entry1) instances.append(household_log_entry2) print 'INSTANCE: ' + str(instances) for obj in instances: print 'test natural key on {0}'.format(obj._meta.object_name) natural_key = obj.natural_key() get_obj = obj.__class__.objects.get_by_natural_key(*natural_key) self.assertEqual(obj.pk, get_obj.pk) # pp = pprint.PrettyPrinter(indent=4) for obj in instances: print 'test serializing/deserializing {0}'.format( obj._meta.object_name) outgoing_transaction = SerializeToTransaction().serialize( obj.__class__, obj, False, True, 'default') # pp.pprint(FieldCryptor('aes', 'local').decrypt(outgoing_transaction.tx)) for transaction in serializers.deserialize( "json", FieldCryptor('aes', 'local').decrypt(outgoing_transaction.tx)): self.assertEqual(transaction.object.pk, obj.pk)
def setUp(self): site_mappers.autodiscover() self.mapper = site_mappers.get(site_mappers.get_as_list()[0])
def test_all_forms(self): adminuser = User.objects.create_user('django', '*****@*****.**', 'pass') adminuser.save() adminuser.is_staff = True adminuser.is_active = True adminuser.is_superuser = True adminuser.save() self.client.login(username=adminuser.username, password='******') content_type_map = ContentTypeMap.objects.get( content_type__model='SubjectConsent'.lower()) membership_form = MembershipFormFactory( content_type_map=content_type_map) schedule_group = ScheduleGroupFactory(membership_form=membership_form, group_name='enrollment', grouping_key='ELIGIBILITY') visit_tracking_content_type_map = ContentTypeMap.objects.get( content_type__model='subjectvisit') visit_definition = VisitDefinitionFactory( code='1000', title='Enrollment', grouping='subject', visit_tracking_content_type_map=visit_tracking_content_type_map) visit_definition.schedule_group.add(schedule_group) survey = SurveyFactory() print('get site mappers') site_mappers.autodiscover() print('get one mapper') mapper = site_mappers.get(site_mappers.get_as_list()[0]) print('mapper is {0}'.format(mapper().get_map_area())) print('Create a plot') plot = PlotFactory(community=mapper().get_map_area()) household = HouseholdFactory(plot=plot) household_structure = HouseholdStructure.objects.get( household=household, survey=survey) HouseholdMemberFactory(household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure) household_member = HouseholdMemberFactory( household_structure=household_structure) n = 0 # collect inline models inline_models = [] models = [] for model, model_admin in admin.site._registry.iteritems(): if self.app_label == model._meta.app_label: models.append(model) inline_models = inline_models + [ m.model for m in model_admin.inlines ] print('Inline models are {0}'.format(', '.join( [m._meta.object_name for m in inline_models]))) for model, model_admin in admin.site._registry.iteritems(): if self.app_label == model._meta.app_label: if model in models and model not in inline_models: n += 1 model_name = model._meta.object_name print('{0}_{1}_add'.format(model._meta.app_label, model_name.lower())) url = reverse('admin:{0}_{1}_add'.format( model._meta.app_label, model_name.lower())) response = self.client.get(url) print(' assert response=200') self.assertEqual(response.status_code, 200) print(' assert template') self.assertTemplateUsed(response, 'admin/change_form.html') factory_mod = __import__( '{0}.tests.factories'.format(self.app_label), fromlist=['{0}Factory'.format(model_name)]) factory = getattr(factory_mod, '{0}Factory'.format(model_name)) print(' instantiate the factory') model_instance = factory() print(' get admin change url for pk={0}'.format( model_instance.id)) url = reverse('admin:{0}_{1}_change'.format( model_instance._meta.app_label, model_instance._meta.object_name.lower()), args=(model_instance.id, )) print(' url = {0}'.format(url)) if model_admin.inlines: for inline_admin in model_admin.inlines: print(' inline model {0}'.format( inline_admin.model)) print(' {0}_{1}_add'.format( inline_admin.model._meta.app_label, inline_admin.model._meta.object_name.lower())) url = reverse('admin:{0}_{1}_add'.format( inline_admin.model._meta.app_label, inline_admin.model._meta.object_name.lower())) response = self.client.get(url) print(' assert response=200') self.assertEqual(response.status_code, 200) print(' assert template') self.assertTemplateUsed(response, 'admin/change_form.html') factory_mod = __import__( 'bcpp_subject.tests.factories', fromlist=[ '{0}Factory'.format( inline_admin.model._meta.object_name) ]) factory = getattr( factory_mod, '{0}Factory'.format( inline_admin.model._meta.object_name)) print(' instantiate the factory {0}'.format( factory)) factory( **{ convert_from_camel(model_instance._meta.object_name): model_instance, 'subject_visit': model_instance.subject_visit }) factory( **{ convert_from_camel(model_instance._meta.object_name): model_instance, 'subject_visit': model_instance.subject_visit }) print(' post url') response = self.client.post(url, model_instance.__dict__) self.assertEqual(response.status_code, 200) print('tested {0} forms'.format(n))
def test_p1(self): adminuser = User.objects.create_user('django', '*****@*****.**', 'pass') adminuser.save() adminuser.is_staff = True adminuser.is_active = True adminuser.is_superuser = True adminuser.save() self.client.login(username=adminuser.username, password='******') print 'create a survey' survey1 = SurveyFactory() survey2 = SurveyFactory() print 'get site mappers' site_mappers.autodiscover() print 'get one mapper' mapper = site_mappers.get(site_mappers.get_as_list()[0]) print 'mapper is {0}'.format(mapper().get_map_area()) print 'create a plot model instance for community {0}'.format( mapper().get_map_area()) self.assertEqual(Household.objects.all().count(), 0) print 'Create a plot' plot = PlotFactory(community=mapper().get_map_area()) plot.save() print 'create a household on this plot for survey={0}'.format(survey1) household = HouseholdFactory(plot=plot) print 'assert hs created' self.assertRaises(IntegrityError, HouseholdStructureFactory, household=household, survey=survey1) household_structure1 = HouseholdStructure.objects.get( household=household, survey=survey1) print 'add members' hm1 = HouseholdMemberFactory(household_structure=household_structure1, first_name='ERIK', initials='EW') hm2 = HouseholdMemberFactory(household_structure=household_structure1, first_name='ERIK', initials='E1W') hm3 = HouseholdMemberFactory(household_structure=household_structure1, first_name='ERIK', initials='E2W') print 'change members' hm1.save() hm2.save() hm3.save() print 'resave household on this plot for survey={0}'.format(survey2) household.save() self.assertRaises(IntegrityError, HouseholdStructureFactory, household=household, survey=survey2) household_structure2 = HouseholdStructure.objects.get( household=household, survey=survey2) print 'assert using different hs' self.assertNotEqual(household_structure1, household_structure2) print 'add members' HouseholdMemberFactory(household_structure=household_structure2) HouseholdMemberFactory(household_structure=household_structure2, first_name='ERIK', initials='E1W') HouseholdMemberFactory(household_structure=household_structure2, first_name='ERIK', initials='E2W') HouseholdMemberFactory(household_structure=household_structure2, first_name='ERIK', initials='EW') print 'change members' hm1.save() hm2.save() hm3.save()
def get_site_code(self): return site_mappers.get(self.community).map_code