def test_clinic_scores_when_no_submissions_for_period(self): self.setup_test_data() self.create_adolescent_client_submissions_v2() period = '1may_31jul_2015' clinic = Clinic.get(Clinic.id == 1) scores = clinic.get_scores(period) scores_1 = scores['one'] self.assertEqual(scores_1[constants.ADOLESCENT_CLIENT], { 'aggregate_score': None, 'num_questions': 2, 'num_responses': 0, 'num_pending_responses': 6, }) self.assertEqual(scores_1[constants.HEALTH_CARE_PROVIDER], { 'aggregate_score': None, 'num_questions': 5, 'num_responses': 0, 'num_pending_responses': 5, }) self.assertEqual(scores_1['totals'], { 'total_scores': None, 'total_questions': 13, 'total_responses': 0, 'total_pending_responses': 17, 'total_percentage': None, 'meets_threshold': False, 'score_classification': None })
def test_clinic_with_submissions_from_different_periods_v3(self): self.setup_test_data() self.create_adolescent_client_submissions_v3() health_centre = Clinic.get(Clinic.id == 4) period = '2016' scores = health_centre.get_scores(period) scores_1 = scores['one'] self.assertEqual(scores_1[constants.ADOLESCENT_CLIENT], { 'aggregate_score': 1.0, 'num_questions': 2, 'num_responses': 1, 'num_pending_responses': 5, }) self.assertEqual(scores_1[constants.HEALTH_CARE_PROVIDER], { 'aggregate_score': None, 'num_questions': 5, 'num_responses': 0, 'num_pending_responses': 5, }) self.assertEqual(scores_1['totals'], { 'total_scores': 1.0, 'total_questions': 13, 'total_responses': 1, 'total_pending_responses': 16, 'total_percentage': 7.6923076923076925, 'meets_threshold': False, 'score_classification': constants.BAD })
def parse_municipalities_from_submissions(): submissions = Submission.all() for submission in submissions: try: with transaction.manager: clinic_code = submission.raw_data[constants.CLINIC_IDENTIFIER] clinic = Clinic.get(Clinic.code == clinic_code) state_name = submission.raw_data[constants.STATE_IDENTIFIER] municipality_name = submission.raw_data[ constants.MUNICIPALITY_IDENTIFIER] state_params = {'name': state_name} state = State.get_or_create(State.name == state_name, **state_params) municipality_params = {'name': municipality_name} municipality = Municipality.get_or_create( Municipality.name == municipality_name, **municipality_params) municipality.state = state if clinic.municipality is None: clinic.municipality = municipality DBSession.add_all([municipality, clinic]) except (NoResultFound, KeyError): pass
def test_get_active_characteristics_filters_by_period(self): self.setup_test_data() period_1 = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic_a = Clinic.get(Clinic.name == 'Clinic A') period2 = ReportingPeriod( title='Period 2', start_date=datetime.datetime(2014, 1, 1), end_date=datetime.datetime(2014, 1, 1)) DBSession.add(period2) DBSession.flush() clinic_char1 = ClinicCharacteristics( clinic_id=clinic_a.id, characteristic_id='one', period_id=period_1.id) clinic_char2 = ClinicCharacteristics( clinic_id=clinic_a.id, characteristic_id='one', period_id=period2.id) DBSession.add_all([clinic_char1, clinic_char2]) characteristics = clinic_a.get_active_characteristics(period_1) self.assertEqual(len(characteristics), 1)
def handle_submission(self): from whoahqa.models import ClinicSubmission clinic_code, characteristics, xform_id = \ ClinicReportHandler.parse_data(self.submission.raw_data) # Check if we have a valid clinic with said id try: clinic = Clinic.get(Clinic.code == clinic_code) except NoResultFound: raise ClinicNotFound else: for characteristic in characteristics: if characteristic: clinic_submission = ClinicSubmission.get_or_create( ClinicSubmission.submission_id == self.submission.id, ClinicSubmission.clinic_id == clinic.id, ClinicSubmission.characteristic == characteristic, clinic_id=clinic.id, submission=self.submission, characteristic=characteristic, xform_id=xform_id) clinic_submission.period = ( self.submission.raw_data[constants.PERIOD_IDENTIFIER]) clinic_submission.valid = (not bool( int(self.submission.raw_data[ constants. INVALID_CHARACTERISTICS_FLAGS[characteristic]]))) clinic_submission.save() clinic.update_reports()
def test_municipality_manager_can_edit_clinics(self): municipality = Municipality.get(Municipality.name == "Brazilia") clinic = Clinic.get(Clinic.code == '1A2B') url = self.request.route_path('clinics', traverse=(clinic.id, 'edit_clinic')) headers = self._login_user('manager_a') params = MultiDict({ 'municipality': municipality.id, 'name': "New Clinic Name", 'code': clinic.code }) response = self.testapp.post(url, params, headers=headers) self.assertEqual(response.status_code, 200) clinic = Clinic.get(Clinic.code == '1A2B') self.assertEqual(clinic.name, "New Clinic Name")
def test_clinic_show_allows_owner(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.name == "Clinic A") url = self.request.route_path('clinics', traverse=(clinic.id, period.id)) headers = self._login_user('manager_a') response = self.testapp.get(url, headers=headers) self.assertEqual(response.status_code, 200)
def test_assign_to_user(self): self.setup_test_data() user = OnaUser.get(OnaUser.username == 'manager_a').user clinic = Clinic.get(Clinic.name == "Clinic B") clinic.assign_to(user) user = DBSession.merge(user) clinic = DBSession.merge(clinic) self.assertEqual(clinic.user, user)
def test_clinic_report_is_generated_if_none_existent_v3(self): count = ClinicReport.count() clinic = Clinic.get(Clinic.id == 4) period = ReportingPeriod.get(ReportingPeriod.title == 'Period 2017') ClinicReport.get_or_generate(clinic, period) self.assertEqual(ClinicReport.count(), count + 1)
def test_get_period_submissions(self): self.setup_test_data() self.create_submissions() clinic = Clinic.get(Clinic.name == 'Clinic A') period = ReportingPeriod.get(ReportingPeriod.title == "Period 1") submissions = clinic.get_period_clinic_submissions(period.form_xpath) self.assertEqual(len(submissions), 50)
def test_characteristics_returns_200(self): clinic = Clinic.get(Clinic.name == "Clinic A") period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') url = self.request.route_path('clinics', traverse=(clinic.id, period.id, 'characteristics')) headers = self._login_user('super') response = self.testapp.get(url, headers=headers) self.assertEqual(response.status_code, 200)
def test_calculate_characteristic_scores_with_period(self): self.setup_test_data() self.create_adolescent_client_submissions_v2() health_centre = Clinic.get(Clinic.id == 3) period = '1may_31jul_2015' scores = health_centre.get_scores(period) scores_1 = scores['one'] self.assertEqual(scores_1[constants.ADOLESCENT_CLIENT], { 'aggregate_score': 1.3333333333333333, 'num_questions': 2, 'num_responses': 3, 'num_pending_responses': 3, }) self.assertEqual(scores_1[constants.HEALTH_CARE_PROVIDER], { 'aggregate_score': None, 'num_questions': 5, 'num_responses': 0, 'num_pending_responses': 5, }) self.assertEqual(scores_1['totals'], { 'total_scores': 1.3333333333333333, 'total_questions': 13, 'total_responses': 3, 'total_pending_responses': 14, 'total_percentage': 10.256410256410255, 'meets_threshold': False, 'score_classification': constants.BAD }) # Test scores of characteristic with an invalid entry score_14 = scores['fourteen'] self.assertEqual(score_14[constants.ADOLESCENT_CLIENT], { 'aggregate_score': 3.0, 'num_questions': 6, 'num_responses': 2, 'num_pending_responses': 4, }) self.assertEqual(score_14[constants.HEALTH_FACILITY_MANAGER], { 'aggregate_score': None, 'num_questions': 2, 'num_responses': 0, 'num_pending_responses': 1, }) self.assertEqual(score_14['totals'], { 'total_scores': 3.0, 'total_questions': 10, 'total_responses': 2, 'total_pending_responses': 8, 'total_percentage': 30.0, 'meets_threshold': False, 'score_classification': constants.BAD })
def test_render_unauthorized_when_forbidden_and_authenticated(self): # TODO: move setup_test_data call to setUp self.setup_test_data() self._create_user('john') clinic = Clinic.get(Clinic.name == "Clinic A") period = ReportingPeriod.get(ReportingPeriod.title == "Period 1") url = self.request.route_url('clinics', traverse=(clinic.id, period.id)) headers = self._login_user('john') response = self.testapp.get(url, headers=headers, status=403) self.assertEqual(response.status_code, 403)
def test_calculate_characteristic_scores_with_period_v3(self): self.setup_test_data() self.create_adolescent_client_submissions_v3() period = "2017" clinic = Clinic.get(Clinic.id == 4) scores = clinic.get_scores(period) scores_1 = scores['one'] self.assertEqual(scores_1[constants.ADOLESCENT_CLIENT], { 'aggregate_score': 0.6666666666666666, 'num_questions': 2, 'num_responses': 3, 'num_pending_responses': 3, }) self.assertEqual(scores_1[constants.HEALTH_CARE_PROVIDER], { 'aggregate_score': None, 'num_questions': 5, 'num_responses': 0, 'num_pending_responses': 5, }) self.assertEqual(scores_1['totals'], { 'total_scores': 0.6666666666666666, 'total_questions': 13, 'total_responses': 3, 'total_pending_responses': 14, 'total_percentage': 5.128205128205128, 'meets_threshold': False, 'score_classification': constants.BAD }) # test with invalid entry scores_16 = scores['sixteen'] self.assertEqual(scores_16[constants.ADOLESCENT_CLIENT], { 'aggregate_score': 3.0, 'num_questions': 6, 'num_responses': 2, 'num_pending_responses': 4, }) self.assertEqual(scores_16[constants.HEALTH_FACILITY_MANAGER], { 'aggregate_score': None, 'num_questions': 13, 'num_responses': 0, 'num_pending_responses': 1, }) self.assertEqual(scores_16['totals'], { 'total_scores': 3.0, 'total_questions': 41, 'total_responses': 2, 'total_pending_responses': 15, 'total_percentage': 7.317073170731707, 'meets_threshold': False, 'score_classification': constants.BAD })
def test_national_official_can_view_clinics(self): self._create_dash_user("national", "national", "*****@*****.**", groups.NATIONAL_OFFICIAL) user = User.newest() period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.name == "Clinic A") url = self.request.route_path('clinics', traverse=(clinic.id, period.id)) headers = self._login_dashboard_user(user) response = self.testapp.get(url, headers=headers) self.assertEqual(response.status_code, 200)
def test_key_indicator_scores_for_period_without_submissions_v3(self): self.setup_test_data() self.create_adolescent_client_submissions_v3() health_centre = Clinic.get(Clinic.id == 4) period = '2018' key_indicator_scores = health_centre.get_key_indicator_scores(period) self.assertEqual(key_indicator_scores, { 'accessible': 0, 'equitable': 0, 'acceptable': 0, 'appropriate': 0, 'effective': 0})
def test_show(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.id == 1) period.__parent__ = clinic self.request.context = period response = self.clinic_views.show() self.assertIsInstance(response['clinic'], Clinic) self.assertEqual(response['clinic'].id, clinic.id) self.assertIn('client_tools', response) self.assertEqual( response['characteristics'], tuple_to_dict_list(("id", "description", "number"), constants.CHARACTERISTICS))
def test_calculate_key_indicator_when_responses_exist(self): """ should return a valid value when responses exist """ self.setup_test_data() self.create_submissions() clinic_a = Clinic.get(Clinic.id == 1) key_indicator_scores = clinic_a.get_key_indicator_scores('') self.assertEqual(key_indicator_scores, { 'accessible': 0, 'equitable': 0, 'acceptable': 0, 'appropriate': 0, 'effective': 0})
def test_calculate_key_indicator_scores_with_period(self): self.setup_test_data() self.create_adolescent_client_submissions_v2() health_centre = Clinic.get(Clinic.id == 3) period = '1may_31jul_2015' key_indicator_scores = health_centre.get_key_indicator_scores(period) self.assertEqual(key_indicator_scores, { 'accessible': 19.02777777777778, 'equitable': 7.228327228327228, 'acceptable': 22.108843537414963, 'appropriate': 2.4390243902439024, 'effective': 6.515151515151515})
def test_calculate_key_indicator_scores_with_period_v3(self): self.setup_test_data() self.create_adolescent_client_submissions_v3() health_centre = Clinic.get(Clinic.id == 4) period = '2017' key_indicator_scores = health_centre.get_key_indicator_scores(period) self.assertEqual(key_indicator_scores, { 'accessible': 20.83333333333333, 'equitable': 7.000407000407002, 'acceptable': 22.006802721088434, 'appropriate': 7.317073170731707, 'effective': 8.806818181818182})
def test_get_num_responses_per_characteristic_xform_id(self): self.setup_test_data() self.create_submissions() clinic_a = Clinic.get(Clinic.name == 'Clinic A') result = clinic_a.get_num_responses_per_characteristic_xform_id( '1may_31jul_2015') self.assertEqual(len(result), 17) self.assertIn( {'count': 3, 'characteristic': constants.THREE, 'xform_id': constants.ADOLESCENT_CLIENT}, result) self.assertIn( {'count': 3, 'characteristic': constants.ONE, 'xform_id': constants.ADOLESCENT_CLIENT}, result)
def test_select_characteristics(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.id == 1) period.__parent__ = clinic self.request.context = period params = MultiDict([('characteristic_id', 'one'), ('characteristic_id', 'two'), ('characteristic_id', 'three')]) self.request.POST = params response = self.clinic_views.select_characteristics() self.assertIsInstance(response, HTTPFound) self.assertEqual( response.location, self.request.route_url('clinics', traverse=(clinic.id, period.id)))
def test_register_clinic_doesnt_save_clinics_with_same_codes(self): ona_user = OnaUser.get(OnaUser.username == 'manager_a') municipality = Municipality.get(Municipality.name == "Brazilia") clinic = Clinic.get(Clinic.code == '1A2B') params = MultiDict({ 'municipality': "{}".format(municipality.id), 'name': "New Clinic Name", 'code': clinic.code }) self.request.method = 'POST' self.request.ona_user = ona_user self.request.POST = params self.clinic_views.register_clinic() flash_error = self.request.session.pop_flash('error')[0] self.assertTrue(flash_error.find("exists") != -1)
def test_characteristics_list_with_characteristic_type_filter(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.id == 1) period.__parent__ = clinic self.request.context = period params = MultiDict({'char_type': 'equitable'}) self.request.GET = params response = self.clinic_views.characteristics_list() self.assertIsInstance(response['period'], ReportingPeriod) self.assertIsInstance(response['clinic'], Clinic) self.assertEqual(response['clinic'].id, clinic.id) self.assertEqual(len(response['characteristics']), 3), self.assertEqual(response['indicator_labels'], dict(constants.INDICATOR_LABELS)), self.assertEqual(response['characteristic_indicator_mapping'], constants.CHARACTERISTIC_INDICATOR_MAPPING)
def test_characteristics_list(self): period = ReportingPeriod.get(ReportingPeriod.title == 'Period 1') clinic = Clinic.get(Clinic.id == 1) period.__parent__ = clinic self.request.context = period response = self.clinic_views.characteristics_list() self.assertIsInstance(response['period'], ReportingPeriod) self.assertIsInstance(response['clinic'], Clinic) self.assertEqual(response['clinic'].id, clinic.id) self.assertEqual( response['characteristics'], tuple_to_dict_list(("id", "description", "number"), constants.CHARACTERISTICS)), self.assertEqual(response['indicator_labels'], dict(constants.INDICATOR_LABELS)), self.assertEqual(response['characteristic_indicator_mapping'], constants.CHARACTERISTIC_INDICATOR_MAPPING)
def test_date_created_is_automatically_populated_on_create(self): self.setup_test_data() clinic_a = Clinic.get(Clinic.id == 1) self.assertEquals( clinic_a.date_created.date(), datetime.datetime.today().date())
def test_is_assigned_returns_false_if_not_assigned(self): self.setup_test_data() clinic_b = Clinic.get(Clinic.id == 2) self.assertFalse(clinic_b.is_assigned)
def test_is_assigned_returns_true_if_assigned(self): self.setup_test_data() clinic_a = Clinic.get(Clinic.id == 1) self.assertTrue(clinic_a.is_assigned)
def create_clinic_report_v3(self): clinic = Clinic.get(Clinic.id == 4) period = ReportingPeriod.get(ReportingPeriod.title == 'Period 2017') with transaction.manager: ClinicReport.get_or_generate(clinic, period)