def test_plot_creates_household_structure(self): """Assert plot creates 6 household_structures if residential habitable and three surveys (3 for each household).""" SurveyFactory() SurveyFactory() SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=2, status='residential_habitable') self.assertEqual(HouseholdStructure.objects.all().count(), 6)
def test_plot_creates_household3(self): """Assert plot creates 2 households if residential habitable and three surveys""" SurveyFactory() SurveyFactory() SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=2, status='residential_habitable') self.assertEqual(Household.objects.all().count(), 2)
def test_plot_creates_household1(self): """Assert plot creates one household if residential habitable""" SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=1, status='residential_habitable') self.assertEqual(Household.objects.all().count(), 1)
def test_max_households(self): SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') plot.household_count = 10 self.assertRaises(ValidationError, plot.save)
def test_enrolled1(self): """Assert enrolled is False if members but none consented.""" SurveyFactory() plot = PlotFactory(community=self.mapper().map_area, household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): household_log = HouseholdLog.objects.get( household_structure__household=household) household_log_entry = HouseholdLogEntryFactory( household_log=household_log) household_structure = household_log.household_structure RepresentativeEligibilityFactory( household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure, age_in_years=10) HouseholdMemberFactory(household_structure=household_structure, age_in_years=74) HouseholdMemberFactory(household_structure=household_structure, age_in_years=25) HouseholdMemberFactory(household_structure=household_structure, age_in_years=50) household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) self.assertFalse(household_structure.enrolled)
def test_eligible_members3(self): """Assert eligible_members set from True but stays True if an eligible member is added then removed but others exist.""" SurveyFactory() plot = PlotFactory(community=self.mapper().map_area, household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): household_log = HouseholdLog.objects.get( household_structure__household=household) household_log_entry = HouseholdLogEntryFactory( household_log=household_log) household_structure = household_log.household_structure RepresentativeEligibilityFactory( household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure, age_in_years=10) HouseholdMemberFactory(household_structure=household_structure, age_in_years=74) HouseholdMemberFactory(household_structure=household_structure, age_in_years=50) household_member = HouseholdMemberFactory( household_structure=household_structure, age_in_years=25) household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) household_member.age_in_years = 10 household_member.save() self.assertTrue(household_structure.eligible_members)
def test_plot_creates_households_with_unique_identifiers(self): """Assert plot creates unique household identifiers""" household_identifier = [] SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): household_identifier.append(household.household_identifier) PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): household_identifier.append(household.household_identifier) PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): household_identifier.append(household.household_identifier) PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): household_identifier.append(household.household_identifier) PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): household_identifier.append(household.household_identifier) self.assertEqual(household_identifier.sort(), list(set(household_identifier)).sort())
def setUp(self): StudySpecificFactory() StudySiteFactory() self.survey = SurveyFactory() self.plot = PlotFactory(community='test_community2', household_count=1, status='occupied') self.household_structure = HouseholdStructure.objects.get(household__plot=self.plot) self.household_member = HouseholdMemberFactory(household_structure=self.household_structure)
def test_create_household5(self): """Assert household.plot is not None when household is created.""" SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): self.assertIsNotNone(household.plot)
def test_plot_creates_households_with_identifiers2(self): """Assert plot creates two households with identifier""" SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=2, status='residential_habitable') for household in Household.objects.all(): self.assertIsNotNone(household.household_identifier)
def test_household_count1(self): """Asserts household count is 1 after 1 household is created.""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=1, status='residential_habitable') plot = Plot.objects.get(plot_identifier=plot.plot_identifier) self.assertEquals(plot.household_count, 1)
def test_plot_creates_household4(self): """Assert plot creates two additional households if household_count increased to 3""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=1, status='residential_habitable') plot.household_count = 3 plot.save() self.assertEqual(Household.objects.all().count(), 3)
def test_plot_identifier_history_updated(self): SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for plot in Plot.objects.all(): self.assertEquals( PlotIdentifierHistory.objects.filter( identifier=plot.plot_identifier).count(), 1)
def test_household_derives_identifier_from_plot(self): """Assert household_identifier is derived from plot""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): self.assertEqual( household.household_identifier.split('-')[0][:6], '{0}'.format(plot.plot_identifier.split('-')[0]))
def test_household_count2(self): """Asserts household count is 3 after 2 households are added after create.""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=1, status='residential_habitable') plot = Plot.objects.get(plot_identifier=plot.plot_identifier) plot.household_count = 3 plot.save() self.assertEquals(plot.household_count, 3)
def setUp(self): try: site_lab_profiles.register(BcppSubjectProfile()) except AlreadyRegisteredLabProfile: pass BcppAppConfiguration().prepare() site_lab_tracker.autodiscover() BcppSubjectVisitSchedule().build() site_rule_groups.autodiscover() SurveyFactory()
def test_enumerated_members1(self): """Assert enumerated_members defaults to False.""" SurveyFactory() plot = PlotFactory(community=self.mapper().map_area, household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): for household_structure in HouseholdStructure.objects.filter( household=household): self.assertFalse(household_structure.enumerated)
def test_household_identifier_history_updated(self): SurveyFactory() PlotFactory(community=self.mapper().get_map_area(), household_count=8, status='residential_habitable') for household in Household.objects.all(): # print household.household_identifier, household.plot.plot_identifier self.assertEquals( HouseholdIdentifierHistory.objects.filter( plot_identifier=household.plot.plot_identifier, identifier=household.household_identifier).count(), 1)
def test_cannot_delete_household_with_logentry(self): """Assert household cannot be deleted if has a household log entry.""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area(), household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): household_log = HouseholdLog.objects.get( household_structure__household=household) HouseholdLogEntryFactory(household_log=household_log) plot.household_count = 1 plot.save() self.assertEqual(Household.objects.all().count(), 3) plot = Plot.objects.get(plot_identifier=plot.plot_identifier) self.assertEquals(plot.household_count, 3)
def test_eligible_members1(self): """Assert eligible_members set to True if an eligible member is added.""" SurveyFactory() plot = PlotFactory(community=self.mapper().map_area, household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): household_log = HouseholdLog.objects.get( household_structure__household=household) household_log_entry = HouseholdLogEntryFactory( household_log=household_log) household_structure = household_log.household_structure RepresentativeEligibilityFactory( household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure) household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) self.assertTrue(household_structure.eligible_members)
def test_enumerated_members2(self): """Assert enumerated_members is True if a household_member is added, others stay False.""" SurveyFactory() plot = PlotFactory(community=self.mapper().map_area, household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): household_log = HouseholdLog.objects.get( household_structure__household=household) household_structure = household_log.household_structure RepresentativeEligibilityFactory( household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure) household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) self.assertTrue(household_structure.enumerated) for household_structure in HouseholdStructure.objects.filter( household=household).exclude(pk=household_structure.pk): self.assertFalse(household_structure.enumerated)
def test_enrolled2(self): """Assert enrolled is True if eligible member consents.""" SurveyFactory() plot = PlotFactory(community=self.mapper().map_area, household_count=3, status='residential_habitable') for household in Household.objects.filter(plot=plot): household_log = HouseholdLog.objects.get( household_structure__household=household) household_log_entry = HouseholdLogEntryFactory( household_log=household_log) household_structure = household_log.household_structure RepresentativeEligibilityFactory( household_structure=household_structure) HouseholdMemberFactory(household_structure=household_structure, age_in_years=10) HouseholdMemberFactory(household_structure=household_structure, age_in_years=74) household_member = HouseholdMemberFactory( household_structure=household_structure, age_in_years=25, initials='NN') HouseholdMemberFactory(household_structure=household_structure, age_in_years=50) household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) dob = date.today() - relativedelta(years=25) enrollment_checklist = EnrollmentChecklistFactory( household_member=household_member, report_datetime=household_member.created, dob=dob, initials='NN') self.assertFalse(household_structure.enrolled) self.assertTrue(enrollment_checklist.is_eligible) household_member = HouseholdMember.objects.get(pk=household_member.pk) SubjectConsentFactory(household_member=household_member, dob=dob, initials='NN') household_structure = HouseholdStructure.objects.get( pk=household_structure.pk) self.assertTrue(household_structure.enrolled)
def test_identifier_code(self): """Assert plot creates an identifier prefixed with the community code""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area()) self.assertEquals(self.mapper.map_code, plot.plot_identifier[:2])
def test_identifier(self): """Assert plot creates an identifier""" SurveyFactory() plot = PlotFactory(community=self.mapper().get_map_area()) self.assertIsNotNone(plot.plot_identifier)
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()