def reset_all_clinics(): # delete all Clinics user_clinics.delete().where( user_clinics.columns.clinic_id.in_(Clinic.all())) with transaction.manager: for cs in Clinic.all(): DBSession.delete(cs)
def test_calculate_aggregate_scores(self): clinic = Clinic(code="123", name="Test Clinic") characteristic_one = constants.CHARACTERISTIC_MAPPING[constants.ONE] score_xpath = characteristic_one.keys()[0] num_responses = 3 submissions = [json.loads(s) for s in self.submissions[1: 4]] scores = clinic.calculate_characteristic_aggregate_scores( score_xpath, num_responses, submissions) self.assertIsInstance(scores, float) self.assertEqual(scores, 1.3333333333333333)
def test_creates_unassigned_clinic_if_user_doesnt_exist(self): payload = self.clinic_registrations[0] raw_data = json.loads(payload) count = Clinic.count() submission = Submission(raw_data=raw_data) self.assertRaises( UserNotFound, ClinicRegistrationHandler(submission).handle_submission) # check that a clinic_submission record was NOT created self.assertEqual(Clinic.count(), count + 1)
def test_raises_submission_handler_error_if_invalid_json(self): # check current counts count = Submission.count() clinic_count = Clinic.count() payload = self.clinic_registrations[1] self.assertRaises(SubmissionHandlerError, Submission.create_from_json, payload) submission = Submission.newest() self.assertEqual(Submission.count(), count + 1) self.assertEqual(submission.raw_data, json.loads(payload)) # check that a clinic record was still created self.assertEqual(Clinic.count(), clinic_count + 1)
def test_get_item_returns_clinic_if_id_exists(self): self.setup_test_data() clinic = Clinic.newest() request = testing.DummyRequest() clinic = ClinicFactory(request).__getitem__(clinic.id) self.assertIsInstance(clinic, Clinic)
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 setUp(self): super(TestMunicipalityViews, self).setUp() self.request = testing.DummyRequest() self._create_user('municipality-manager') with transaction.manager: reporting_period = ReportingPeriod( title='Period 1', start_date=datetime.datetime(2015, 5, 1), end_date=datetime.datetime(2015, 7, 31)) reporting_period.save() municipality = Municipality(name="Brasillia") DBSession.add(municipality) for i in range(5): clinic = Clinic(name="Clinic {}".format(i), code="{}BCDE".format(i), municipality=municipality) DBSession.add(clinic) self.request.user = OnaUser.get( OnaUser.username == 'municipality-manager').user self.view = MunicipalityViews(self.request)
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 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 setUp(self): super(TestStateViews, self).setUp() self.request = testing.DummyRequest() self._create_user('state-official') with transaction.manager: reporting_period = ReportingPeriod( title='Period 1', start_date=datetime.datetime(2015, 5, 1), end_date=datetime.datetime(2015, 7, 31)) reporting_period.save() state = State(name="Sao Paolo") municipality1 = Municipality(name="Brasillia", parent=state) municipality2 = Municipality(name="Brasil", parent=state) DBSession.add_all([state, municipality1, municipality2]) for i in range(5): clinic = Clinic(name="Clinic {}".format(i), code="{}BCDE".format(i), municipality=municipality1) DBSession.add(clinic) self.request.user = OnaUser.get( OnaUser.username == 'state-official').user self.view = StateViews(self.request)
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 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 import_health_data(): file_name = os.path.relpath('whoahqa/data/clinics.csv') with open(file_name, 'rU') as source: rdr = UnicodeDictReader(source) existing_states = [ normalizeString(state.name) for state in State.all() ] existing_municipalities = [ normalizeString(municipality.name) for municipality in Municipality.all() ] existing_clinics = [clinic.code for clinic in Clinic.all()] with transaction.manager: for row in rdr: state = None municipality = None normalized_state = normalizeString(row['state']) normalized_municipality = normalizeString(row['municipality']) if normalized_state not in existing_states: existing_states.append(normalized_state) state = State(name=normalized_state) DBSession.add(state) if normalized_municipality not in existing_municipalities: existing_municipalities.append(normalized_municipality) if state is None: state = State.get(State.name == normalized_state) municipality = Municipality(name=normalized_municipality, parent=state) DBSession.add(municipality) if row['CNES'] not in existing_clinics: # import ipdb; ipdb.set_trace() existing_clinics.append(row['CNES']) if municipality is None: municipality = Municipality.get( Municipality.name == normalized_municipality) clinic = Clinic(name=row['facility_name'], code=row['CNES'], municipality=municipality) DBSession.add(clinic)
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_create_clinic_from_submission(self): # create user with matching id DBSession.add(User()) user = User.newest() payload = self.clinic_registrations[0] raw_data = json.loads(payload) raw_data[constants.USER_ID] = user.id submission = Submission(raw_data=raw_data) count = Clinic.count() ClinicRegistrationHandler(submission).handle_submission() # check that a clinic_submission record was created self.assertEqual(Clinic.count(), count + 1) clinic = Clinic.newest() self.assertEqual(clinic.code, '1A2B')
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_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_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_json_post_with_valid_clinic_id(self): clinic = Clinic(code='1A2B', name="Clinic A") DBSession.add(clinic) response = self.post_json(self.submissions[0]) # should return a 201 response code self.assertEqual(response.status_code, 201) self.assertEqual(response.body, 'Saved')
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_json_post_with_invalid_clinic_id(self): clinic = Clinic(code='1A2B', name="Clinic A") DBSession.add(clinic) response = self.post_json(self.submissions[5]) # should return a 202 response code self.assertEqual(response.status_code, 202) self.assertEqual(response.body, 'Accepted pending manual matching process')
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 manage_clinics(self): user = self.request.user if self.request.has_permission(perms.SUPER_USER, self.request.context): clinics = Clinic.all() else: clinics = user.location.clinics period = get_period_from_request(self.request) return {'clinics': clinics, 'period': period}
def national_report(self, period): clinics = Clinic.all() clinics = [ c for c in clinics if c.has_clinic_submissions_for_period(period.form_xpath) ] national_report = ClinicReport.get_clinic_reports(clinics, period) return national_report
def test_calculate_aggregate_scores_raises_value_error_for_0_submissions( self): clinic = Clinic(code="123", name="Test Clinic") characteristic_one = constants.CHARACTERISTIC_MAPPING[constants.ONE] score_xpath = characteristic_one.keys()[0] num_responses = 0 submissions = [json.loads(s) for s in self.submissions[1: 4]] self.assertRaises( ValueError, clinic.calculate_characteristic_aggregate_scores, score_xpath, num_responses, submissions)
def test_push_facilities_with_locations(self): # create dummy data self._create_state() self._create_municipality() municipality = Municipality.newest() state = State.newest() municipality.parent = state municipality.save() clinic1 = Clinic(id=1, name=u"Clinic A", code="1A2B", municipality=municipality) clinic1.save() # test push_facilities service response = push_facilities(self.request) self.assertEqual(1, len(response['rows']))
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)