def test_get_now_application_list_filter_by_mine_region(self, test_client, db_session, auth_headers): """Should return the records filtered by mine_region""" mine = MineFactory(mine_region='SE') mine2 = MineFactory(mine_region='NW') now_submission_1 = NOWSubmissionFactory(mine=mine) identity_1 = NOWApplicationIdentityFactory(now_submission=now_submission_1, mine=mine) now_submission_2 = NOWSubmissionFactory(mine=mine) identity_2 = NOWApplicationIdentityFactory(now_submission=now_submission_2, mine=mine) now_submission_3 = NOWSubmissionFactory(mine=mine2) identity_3 = NOWApplicationIdentityFactory(now_submission=now_submission_3, mine=mine2) now_submission_4 = NOWSubmissionFactory(mine=mine2) identity_4 = NOWApplicationIdentityFactory(now_submission=now_submission_4, mine=mine2) get_resp = test_client.get( f'now-applications?mine_region={mine.mine_region}', headers=auth_headers['full_auth_header']) assert get_resp.status_code == 200, get_resp.response get_data = json.loads(get_resp.data.decode()) assert len(get_data['records']) == 2 assert all( str(submission.now_application_guid) in map(lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_1, now_submission_2]) assert all( str(submission.now_application_guid) not in map(lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_3, now_submission_4])
def test_put_redundant_mine_name(test_client, db_session, auth_headers): existing_name = MineFactory().mine_name mine = MineFactory() test_tenure_data = { "mine_name": existing_name, } put_resp = test_client.put(f'/mines/{mine.mine_guid}', json=test_tenure_data, headers=auth_headers['full_auth_header']) assert put_resp.status_code == 400
def test_get_incidents_search_filter(self, test_client, db_session, auth_headers): """Should respect incidents search query param""" MineFactory(mine_no='12345') MineFactory(mine_no="678910", mine_name='steve') MineFactory(mine_name="7891011") get_resp = test_client.get(f'/incidents?search=78910', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert len(get_data['records']) == 2 assert all(map( lambda v: v['mine_name'] in ["7891011","steve"] , get_data['records']))
def test_get_mines_basic_info_single(test_client, db_session, auth_headers): mine_guid = MineFactory().mine_guid MineFactory() form_data = {'mine_guids': [str(mine_guid)]} post_resp = test_client.post('/mines/basicinfo', json=form_data, headers=auth_headers['full_auth_header']) post_data = json.loads(post_resp.data.decode()) assert post_resp.status_code == 200, str(post_resp.response) assert len(post_data) == 1
def test_get_variances_application_filter_by_major_mine(self, test_client, db_session, auth_headers): """Should filter variances by major vs regional mine""" major_mine = MineFactory(major_mine_ind=True) regional_mine = MineFactory(major_mine_ind=False) VarianceFactory(mine=major_mine) VarianceFactory(mine=regional_mine) get_resp = test_client.get(f'/variances?major=f', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert all(map( lambda v: v['mine_name'] == regional_mine.mine_name, get_data['records']))
def test_simple_search_parties_and_mines(test_client, db_session, auth_headers): searchString = 'Abbo' mine = MineFactory(mine_name="Test") party = PartyFactory(person=True, first_name="Rod", party_name="Stewart") for x in range(3): PartyFactory(person=True, first_name='Abbot') MineFactory(mine_name='Abbot Mines') get_resp = test_client.get(f'/search/simple?search_term={searchString}', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert len(get_data['search_results']) == 6 assert get_resp.status_code == 200
def test_get_incidents_region_filter(self, test_client, db_session, auth_headers): """Should respect incidents region query param""" region_code = "NW" mine_with_region_nw = MineFactory(mine_region='NW') mine_with_region_sw = MineFactory(mine_region="SW") batch_size = 3 MineIncidentFactory.create_batch(size=batch_size) MineIncidentFactory(mine=mine_with_region_nw) MineIncidentFactory(mine=mine_with_region_sw) get_resp = test_client.get(f'/incidents?region={region_code}', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert all(map( lambda v: v['mine_name'] == mine_with_region_nw.mine_name, get_data['records']))
def test_post_mine_report_with_bad_permit_guid(test_client, db_session, auth_headers): mine = MineFactory(mine_reports=ONE_REPORT) mine2 = MineFactory(mine_reports=ONE_REPORT) data = { 'mine_report_definition_guid': 1, 'submission_year': '2019', 'permit_guid': mine2.mine_permit[0].permit_guid, 'due_date': '2019-07-05 20:27:45.11929+00', } post_resp = test_client.post(f'/mines/{mine.mine_guid}/reports', headers=auth_headers['full_auth_header'], json=data) assert post_resp.status_code == 400
def test_post_permit(test_client, db_session, auth_headers): mine = MineFactory() party_guid = PartyFactory(company=True).party_guid no_of_permits = len(mine.mine_permit) PERMIT_NO = 'mx-test-999' data = { 'permittee_party_guid': str(party_guid), 'permit_no': PERMIT_NO, 'permit_status_code': 'O', 'received_date': '1999-12-12', 'issue_date': '1999-12-21', 'authorization_end_date': '2012-12-02' } post_resp = test_client.post(f'/mines/{mine.mine_guid}/permits', headers=auth_headers['full_auth_header'], json=data) post_data = json.loads(post_resp.data.decode()) updated_mine = Mine.find_by_mine_guid(str(mine.mine_guid)) permittees = MinePartyAppointment.find_by_permit_guid( updated_mine.mine_permit[0].permit_guid) assert post_resp.status_code == 200 assert updated_mine.mine_permit[0].permit_no == PERMIT_NO assert permittees[0].party_guid == party_guid assert len(updated_mine.mine_permit) == no_of_permits + 1
def test_get_all_expected_documents_by_mine_guid_after_insert(test_client, db_session, auth_headers): mine = MineFactory() get_resp = test_client.get( '/documents/expected/mines/' + str(mine.mine_guid), headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) org_exp_document_list_len = len(get_data['expected_mine_documents']) req_guid = RandomRequiredDocument().req_document_guid new_expected_document = [{ 'req_document_guid': req_guid, 'document_name': 'a name' }] post_documents = {'documents': new_expected_document} post_resp = test_client.post( '/documents/expected/mines/' + str(mine.mine_guid), json=post_documents, headers=auth_headers['full_auth_header']) post_data = json.loads(post_resp.data.decode()) assert post_resp.status_code == 200 get_resp = test_client.get( '/documents/expected/mines/' + str(mine.mine_guid), headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert len(get_data['expected_mine_documents']) == org_exp_document_list_len + 1 assert all(ed['mine_guid'] == str(mine.mine_guid) for ed in get_data['expected_mine_documents'])
def test_update_new_item_in_list(db_session): mine = MineFactory(mine_permit=5) permit = PermitFactory() partial_mine_permit_dict = marshal( {'mine_permit': mine.mine_permit}, api.model('test_list', {'mine_permit': fields.List(fields.Nested(PERMIT_MODEL))})) new_permit_dict = marshal(permit, PERMIT_MODEL) for pa in permit.permit_amendments: db_session.delete(pa) db_session.delete(permit) del new_permit_dict['mine_guid'] del new_permit_dict['permit_guid'] #FOR TESTING ONLY, lets Permitid stay because it's correct and #postgres sequence doesn't get increased by factory object creation partial_mine_permit_dict['mine_permit'].append(new_permit_dict) mine.deep_update_from_dict(partial_mine_permit_dict, _edit_key=PERMIT_EDIT_GROUP) mine = Mine.query.filter_by(mine_guid=mine.mine_guid).first() assert len(mine.mine_permit) == 6 assert all(len(p.permit_amendments) > 0 for p in mine.mine_permit)
def test_post_mine_incidents_happy(test_client, db_session, auth_headers): test_mine_guid = MineFactory().mine_guid now_time_string = datetime.now().strftime("%Y-%m-%d %H:%M") data = { 'determination_type_code': 'NDO', 'incident_timestamp': now_time_string, 'reported_timestamp': now_time_string, 'incident_description': "Someone got a paper cut", } post_resp = test_client.post(f'/mines/{test_mine_guid}/incidents', json=data, headers=auth_headers['full_auth_header']) assert post_resp.status_code == 201, post_resp.response post_data = json.loads(post_resp.data.decode()) assert post_data['mine_guid'] == str(test_mine_guid) assert post_data['determination_type_code'] == data[ 'determination_type_code'] assert post_data['incident_timestamp'] == now_time_string # datetime.fromisoformat is in python 3.7 # assert datetime.fromisoformat(post_data['incident_timestamp']) == datetime.strptime( # data['incident_timestamp'], '%Y-%m-%d %H:%M') assert post_data['incident_description'] == data['incident_description']
def test_delete_mine_type_success(test_client, db_session, auth_headers): mine = MineFactory() type_guid = mine.mine_type[0].mine_type_guid delete_resp = test_client.delete(f'/mines/mine-types/{type_guid}', headers=auth_headers['full_auth_header']) assert delete_resp.status_code == 204
def test_happy_get_from_NRIS(test_client, auth_headers, setup_info, db_session): mine = MineFactory() with mock.patch('requests.get') as nris_data_mock: nris_data_mock.side_effect = [ MockResponse(setup_info.get('NRIS_Mock_data'), 200) ] get_resp = test_client.get(f'/mines/{mine.mine_no}/compliance/summary', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) expected = setup_info.get('expected_data') assert get_resp.status_code == 200, get_resp.response assert get_data['last_inspection'] == expected['last_inspection'] assert get_data['last_inspector'] == expected['last_inspector'] assert get_data['num_open_orders'] == expected['num_open_orders'] assert get_data['num_overdue_orders'] == expected['num_overdue_orders'] assert get_data['last_12_months']['num_advisories'] == expected[ 'last_12_months']['num_advisories'] assert get_data['last_12_months']['num_warnings'] == expected[ 'last_12_months']['num_warnings'] pairs = zip(get_data['orders'], expected['orders']) assert any(x != y for x, y in pairs)
def test_get_mine_application_list_success(self, test_client, db_session, auth_headers): """Should return the records for the mine with a 200 response code""" batch_size = 5 other_applications = NOWApplicationIdentityFactory.create_batch( size=batch_size) mine = MineFactory(minimal=True) now_submission_1 = NOWSubmissionFactory(mine=mine) identity_1 = NOWApplicationIdentityFactory( now_submission=now_submission_1, mine=mine) now_submission_2 = NOWSubmissionFactory(mine=mine) identity_2 = NOWApplicationIdentityFactory( now_submission=now_submission_2, mine=mine) get_resp = test_client.get( f'now-applications?mine_guid={mine.mine_guid}', headers=auth_headers['full_auth_header']) assert get_resp.status_code == 200, get_resp.response get_data = json.loads(get_resp.data.decode()) assert len(get_data['records']) == 2 assert all( str(submission.now_application_guid) not in map( lambda x: x['now_application_guid'], get_data['records']) for submission in other_applications) assert all( str(submission.now_application_guid) in map( lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_1, now_submission_1])
def test_get_mine_application_list_filter_by_status( self, test_client, db_session, auth_headers): """Should return the records filtered by status""" mine = MineFactory(minimal=True) now_submission_1 = NOWSubmissionFactory(mine=mine, status='Accepted') identity_1 = NOWApplicationIdentityFactory( now_submission=now_submission_1, mine=mine) now_submission_2 = NOWSubmissionFactory(mine=mine, status='Withdrawn') identity_2 = NOWApplicationIdentityFactory( now_submission=now_submission_2, mine=mine) now_submission_3 = NOWSubmissionFactory(mine=mine, status='Withdrawn') identity_3 = NOWApplicationIdentityFactory( now_submission=now_submission_3, mine=mine) now_submission_4 = NOWSubmissionFactory(mine=mine, status='Withdrawn') identity_4 = NOWApplicationIdentityFactory( now_submission=now_submission_4, mine=mine) get_resp = test_client.get( f'now-applications?mine_guid={mine.mine_guid}&now_application_status_description=Accepted&now_application_status_description=Withdrawn', headers=auth_headers['full_auth_header']) assert get_resp.status_code == 200, get_resp.response get_data = json.loads(get_resp.data.decode()) assert len(get_data['records']) == 2 assert all( str(submission.now_application_guid) in map( lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_1, now_submission_2]) assert all( str(submission.now_application_guid) not in map( lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_3, now_submission_4])
def test_delete_mine_type_invalid_mine_type_guid(test_client, db_session, auth_headers): mine = MineFactory() delete_resp = test_client.delete( f'/mines/{mine.mine_guid}/mine-types/{uuid.uuid4()}', headers=auth_headers['full_auth_header']) assert delete_resp.status_code == 404
def test_put_a_reclamation_invoice(self, test_client, db_session, auth_headers): """Should return the edited reclamation invoice with a 200 response code""" mine = MineFactory(minimal=True) permit = PermitFactory(mine=mine) reclamation_invoice = permit.reclamation_invoices[0] old_amount = reclamation_invoice.amount old_vendor = reclamation_invoice.vendor data = { "project_id": "DUCK-467985", "amount": 999999.99, "vendor": "Rubber Ducky Restoration Services", "documents": [] } post_resp = test_client.put( f'/securities/reclamation-invoices/{reclamation_invoice.reclamation_invoice_guid}', json=data, headers=auth_headers['full_auth_header']) assert post_resp.status_code == 200, post_resp.response post_data = json.loads(post_resp.data.decode()) assert post_data['amount'] != str(old_amount) assert post_data['vendor'] != old_vendor
def test_get_mine_incidents_by_guid(test_client, db_session, auth_headers): test_guid = MineFactory().mine_incidents[0].mine_incident_guid get_resp = test_client.get(f'/mines/incidents/{test_guid}', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert get_data['mine_incident_guid'] == str(test_guid)
def test_post_mine_incidents_dangerous_occurrence_happy( test_client, db_session, auth_headers): test_mine_guid = MineFactory().mine_guid do_subparagraph_count = 2 do_ids = [ sub.compliance_article_id for sub in SampleDangerousOccurrenceSubparagraphs(do_subparagraph_count) ] now_time_string = datetime.now().strftime("%Y-%m-%d %H:%M") data = { 'determination_type_code': 'DO', 'incident_timestamp': now_time_string, 'reported_timestamp': now_time_string, 'incident_description': "Someone got a really bad paper cut", 'dangerous_occurrence_subparagraph_ids': do_ids } post_resp = test_client.post(f'/mines/{test_mine_guid}/incidents', json=data, headers=auth_headers['full_auth_header']) assert post_resp.status_code == 201, post_resp.response post_data = json.loads(post_resp.data.decode()) assert post_data['mine_guid'] == str(test_mine_guid) assert post_data['determination_type_code'] == data[ 'determination_type_code'] assert post_data['incident_timestamp'] == now_time_string assert post_data['incident_description'] == data['incident_description'] assert set(post_data['dangerous_occurrence_subparagraph_ids']) == set( data['dangerous_occurrence_subparagraph_ids'])
def test_get_mine_application_list_filter_by_noticeofworktype(self, test_client, db_session, auth_headers): """Should return the records filtered by noticeofworktype""" mine = MineFactory(minimal=True) now_submission_1 = NOWSubmissionFactory(mine=mine, noticeofworktype='dog') identity_1 = NOWApplicationIdentityFactory( now_submission=now_submission_1, mine=mine, submission_only=True) now_submission_2 = NOWSubmissionFactory(mine=mine, noticeofworktype='dog') identity_2 = NOWApplicationIdentityFactory( now_submission=now_submission_2, mine=mine, submission_only=True) now_submission_3 = NOWSubmissionFactory(mine=mine, noticeofworktype='cat') identity_3 = NOWApplicationIdentityFactory( now_submission=now_submission_3, mine=mine, submission_only=True) now_submission_4 = NOWSubmissionFactory(mine=mine, noticeofworktype='parrot') identity_4 = NOWApplicationIdentityFactory( now_submission=now_submission_4, mine=mine, submission_only=True) get_resp = test_client.get( f'now-applications?mine_guid={mine.mine_guid}¬ice_of_work_type_description=dog', headers=auth_headers['full_auth_header']) assert get_resp.status_code == 200, get_resp.response get_data = json.loads(get_resp.data.decode()) assert len(get_data['records']) == 2 assert all( str(submission.now_application_guid) in map(lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_1, now_submission_2]) assert all( str(submission.now_application_guid) not in map(lambda x: x['now_application_guid'], get_data['records']) for submission in [now_submission_3, now_submission_4])
def test_delete_mine_report(test_client, db_session, auth_headers): mine = MineFactory(mine_reports=ONE_REPORT) delete_resp = test_client.delete( f'/mines/{mine.mine_guid}/reports/{mine.mine_reports[0].mine_report_guid}', headers=auth_headers['full_auth_header']) assert delete_resp.status_code == 204, delete_resp.response
def test_get_variances_application_filter_by_mine_region(self, test_client, db_session, auth_headers): """Should filter variances by mine region""" region_code = "NW" mine_with_region_nw = MineFactory(mine_region='NW') mine_with_region_sw = MineFactory(mine_region="SW") batch_size = 3 VarianceFactory.create_batch(size=batch_size) VarianceFactory(mine=mine_with_region_nw) VarianceFactory(mine=mine_with_region_sw) get_resp = test_client.get(f'/variances?region={region_code}',headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert all(map( lambda v: v['mine_name'] == mine_with_region_nw.mine_name, get_data['records']))
def test_post_a_favorite(test_client, db_session, auth_headers): mine1_guid = MineFactory().mine_guid get_resp = test_client.post('/mines/' + str(mine1_guid) + '/subscribe', headers=auth_headers['view_only_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_data == {"mine_guid": str(mine1_guid)} assert get_resp.status_code == 200
def test_get_all_reports_for_mine(test_client, db_session, auth_headers): mine = MineFactory(mine_reports=THREE_REPORTS) get_resp = test_client.get(f'/mines/{mine.mine_guid}/reports', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert len(get_data['records']) == THREE_REPORTS assert get_resp.status_code == 200
def test_post_mine_incidents_including_optional_fields(test_client, db_session, auth_headers): test_mine_guid = MineFactory().mine_guid now_time_string = datetime.now().strftime("%Y-%m-%d %H:%M") data = { 'determination_type_code': 'NDO', 'incident_timestamp': now_time_string, 'reported_timestamp': now_time_string, 'incident_description': 'Someone got a paper cut', 'mine_determination_type_code': 'NDO', 'mine_determination_representative': 'Billy' } post_resp = test_client.post( f'/mines/{test_mine_guid}/incidents', json=data, headers=auth_headers['full_auth_header']) assert post_resp.status_code == 201, post_resp.response post_data = json.loads(post_resp.data.decode()) assert post_data['mine_guid'] == str(test_mine_guid) assert post_data['determination_type_code'] == data['determination_type_code'] assert post_data['incident_timestamp'] == now_time_string assert post_data['incident_description'] == data['incident_description'] assert post_data['mine_determination_type_code'] == data['mine_determination_type_code'] assert post_data['mine_determination_representative'] == data[ 'mine_determination_representative']
def test_get_mines(test_client, db_session, auth_headers): mine_guid = MineFactory().mine_guid get_resp = test_client.get('/mines', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 assert get_data['mines'][0]['mine_guid'] == str(mine_guid)
def test_mine_tailings_find_by_mine_guid(db_session): batch_size = 2 mine_guid = MineFactory( mine_tailings_storage_facilities=batch_size).mine_guid mine_tsfs = MineTailingsStorageFacility.find_by_mine_guid(str(mine_guid)) assert len(mine_tsfs) == batch_size assert all(tsf.mine_guid == mine_guid for tsf in mine_tsfs)
def test_put_mine_mine_status(test_client, db_session, auth_headers): mine_guid = MineFactory().mine_guid test_mine_data = {"mine_status": "CLD, CM", "mine_note": ""} put_resp = test_client.put(f'/mines/{mine_guid}', json=test_mine_data, headers=auth_headers['full_auth_header']) assert put_resp.status_code == 200
def test_get_mine_by_mine_no(test_client, db_session, auth_headers): mine_no = MineFactory().mine_no get_resp = test_client.get(f'/mines/{mine_no}', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_data['mine_no'] == mine_no assert get_resp.status_code == 200