def test_get_incidents(self, test_client, db_session, auth_headers): """Should return all records and a 200 response code""" batch_size = 5 MineIncidentFactory.create_batch(size=batch_size) get_resp = test_client.get('/incidents?per_page=25', 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']) == batch_size
def test_get_incidents_date_sort(self, test_client, db_session, auth_headers): """Should respect incidents date sort""" sort_field = "incident_timestamp" sort_dir = "desc" batch_size = 5 MineIncidentFactory.create_batch(size=batch_size) get_resp = test_client.get(f'/incidents?sort_field={sort_field}&sort_dir={sort_dir}', 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']) == batch_size assert all(get_data['records'][i]['incident_timestamp'] >= get_data['records'][i+1]['incident_timestamp'] for i in range(len(get_data['records'])-1))
def test_get_incidents_status_filter(self, test_client, db_session, auth_headers): """Should respect incidents status query param""" batch_size = 5 status_pre="PRE"; MineIncidentFactory.create_batch(size=batch_size, status_code=status_pre) status_fin = "FIN" MineIncidentFactory.create_batch(size=batch_size, status_code=status_fin) get_resp = test_client.get(f"/incidents?incident_status={status_pre}", 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']) == batch_size assert all(map(lambda v: v['status_code'] == status_pre, get_data['records']))
def test_get_incidents_non_existent_mine_guid(self, test_client, db_session, auth_headers): """Should return a 404 when the provided mine guid does not exist""" batch_size = 3 fake_guid = uuid.uuid4() MineIncidentFactory.create_batch(size=batch_size) get_resp = test_client.get(f'/mines/{fake_guid}/incidents', headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 404
def test_get_incidents_year_filter(self, test_client, db_session, auth_headers): """Should respect incidents year query param""" batch_size = 5 MineIncidentFactory.create_batch(size=batch_size) random_time_past = random.uniform(-time.time(), time.time()) random_date_time = datetime.fromtimestamp(random_time_past) MineIncidentFactory(incident_timestamp=random_date_time) incident_year = str(random_date_time.year) get_resp = test_client.get( f"/incidents?year={incident_year}", 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']) == 1 assert all(map(lambda v: v['incident_timestamp'][0:4] == incident_year, get_data['records']))
def test_get_incidents_for_a_mine(self, test_client, db_session, auth_headers): """Should return the correct number of records and a 200 staus code""" batch_size = 3 mine = MineFactory(minimal=True) MineIncidentFactory.create_batch(size=batch_size, mine=mine) MineIncidentFactory.create_batch(size=batch_size) get_resp = test_client.get(f'/mines/{mine.mine_guid}/incidents', 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']) == batch_size
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_get_incidents_sort_and_filter_multiple_fields(self, test_client, db_session, auth_headers): """Should respect incidents sort and filter by multiple fields""" batch_size = 5 status_pre="PRE"; MineIncidentFactory.create_batch(size=batch_size, status_code=status_pre) status_fin = "FIN" MineIncidentFactory.create_batch(size=batch_size, status_code=status_fin) sort_field = "mine_name" sort_dir = "desc" get_resp = test_client.get(f'/incidents?sort_field={sort_field}&sort_dir={sort_dir}&incident_status={status_pre}', 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']) == batch_size assert all(map(lambda v: v['status_code'] == status_pre, get_data['records'])) assert all( get_data['records'][i]['mine_name'] >= get_data['records'][i + 1]['mine_name'] for i in range(len(get_data['records']) - 1))
def test_file_removal_not_on_incident(self, test_client, db_session, auth_headers): """Should return a 404""" incident = MineIncidentFactory() mine_document_guid = MineDocumentFactory().mine_document_guid delete_resp = test_client.delete( f'/mines/{incident.mine_guid}/incidents/{incident.mine_incident_guid}/documents/{mine_document_guid}', headers=auth_headers['full_auth_header']) assert delete_resp.status_code == 404
def test_get_mine_incidents_wrong_mine_guid(self, test_client, db_session, auth_headers): """Should return a 404 when the mine_incident does not exist on the provided mine""" mine_incident = MineIncidentFactory() mine = MineFactory() get_resp = test_client.get( f'/mines/{mine.mine_guid}/mine_incidents/{mine_incident.mine_incident_guid}', headers=auth_headers['full_auth_header']) assert get_resp.status_code == 404
def test_get_mine_incident(self, test_client, db_session, auth_headers): """Should return the correct mine incident and a 200 response code""" mine_incident = MineIncidentFactory() get_resp = test_client.get( f'/mines/{mine_incident.mine_guid}/incidents/{mine_incident.mine_incident_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( mine_incident.mine_incident_guid)
def test_get_incidents_codes_filter(self, test_client, db_session, auth_headers): """Should respect incidents codes query param""" code1 = SampleDangerousOccurrenceSubparagraphs(1) code2 = SampleDangerousOccurrenceSubparagraphs(2) code3 = SampleDangerousOccurrenceSubparagraphs(1) batch_size = 5 MineIncidentFactory.create_batch( size=batch_size, determination_type_code='DO', dangerous_occurrence_subparagraphs=code1) MineIncidentFactory.create_batch( size=batch_size, determination_type_code='DO', dangerous_occurrence_subparagraphs=code2) MineIncidentFactory.create_batch( size=batch_size, determination_type_code='DO', dangerous_occurrence_subparagraphs=code3) get_resp = test_client.get( f"/incidents?codes={code2[0].compliance_article_id}%2C{code2[1].compliance_article_id}", headers=auth_headers['full_auth_header']) get_data = json.loads(get_resp.data.decode()) assert get_resp.status_code == 200 # The intersection of two lists assert all( map( lambda v: set(v['dangerous_occurrence_subparagraph_ids']) & set([ code2[0].compliance_article_id, code2[1]. compliance_article_id ]), get_data['records']))
def test_file_removal(self, test_client, db_session, auth_headers): """Should dissociate a document from a Mine Incident""" incident = MineIncidentFactory() incident_document = incident.documents[0] document_count = len(incident.documents) assert incident_document is not None delete_resp = test_client.delete( f'/mines/{incident.mine_guid}/incidents/{incident.mine_incident_guid}/documents/{incident_document.mine_document_guid}', headers=auth_headers['full_auth_header']) assert delete_resp.status_code == 204 assert len(incident.documents) == document_count - 1
def test_get_incidents_determination_filter(self, test_client, db_session, auth_headers): """Should respect incidents determination query param""" batch_size = 5 MineIncidentFactory.create_batch(size=batch_size, determination_type_code="PEN") MineIncidentFactory.create_batch(size=batch_size, determination_type_code="DO") MineIncidentFactory.create_batch(size=batch_size, determination_type_code="NDO") get_resp = test_client.get(f"/incidents?determination=DO&determination=NDO", 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*batch_size assert all(map(lambda v: v['determination_type_code'] in ["DO","NDO"], get_data['records']))
def test_put_file(self, test_client, db_session, auth_headers): """Should add a document to a Mine Incident""" mine = MineFactory() incident = MineIncidentFactory(mine=mine) document_count = len(incident.documents) data = { 'document_manager_guid': uuid.uuid4(), 'filename': 'my_document.pdf', 'mine_incident_document_type': RandomIncidentDocumentType() } # FIXME: This endpoint is set up with a required param that is ignored put_resp = test_client.put( f'/mines/{mine.mine_guid}/incidents/{incident.mine_incident_guid}/documents/12345', headers=auth_headers['full_auth_header'], data=data) put_data = json.loads(put_resp.data.decode()) assert put_resp.status_code == 200, put_resp.response assert len(put_data['documents']) == document_count + 1
def test_post_mine_incident(self, test_client, db_session, auth_headers): """Should return the new mine incident record""" mine = MineFactory() incident = MineIncidentFactory(mine=mine) test_incident_data = { 'incident_timestamp': '2019-01-01 00:00', 'reported_timestamp': '2019-01-01 00:00', 'incident_description': incident.incident_description, } post_resp = test_client.post(f'/mines/{mine.mine_guid}/incidents', json=test_incident_data, headers=auth_headers['full_auth_header']) post_data = json.loads(post_resp.data.decode()) assert post_resp.status_code == 201, post_resp.response assert post_data['incident_timestamp'] == test_incident_data[ 'incident_timestamp'] assert post_data['incident_description'] == test_incident_data[ 'incident_description'] assert post_data['reported_timestamp'] == str( test_incident_data['reported_timestamp'])
def test_put_mine_incident(self, test_client, db_session, auth_headers): """Should return the updated mine_incident record""" mine_incident = MineIncidentFactory() incident = MineIncidentFactory() data = { 'incident_timestamp': '2019-01-01 00:00', 'reported_timestamp': '2019-01-01 00:00', 'incident_description': incident.incident_description, 'reported_by_name': incident.reported_by_name, 'reported_by_email': incident.reported_by_email, 'reported_by_phone_no': incident.reported_by_phone_no, 'reported_by_phone_ext': incident.reported_by_phone_ext, 'emergency_services_called': incident.emergency_services_called, 'number_of_injuries': incident.number_of_injuries, 'number_of_fatalities': incident.number_of_fatalities, 'reported_to_inspector_party_guid': incident.reported_to_inspector_party_guid, 'responsible_inspector_party_guid': incident.responsible_inspector_party_guid, 'determination_inspector_party_guid': incident.determination_inspector_party_guid, 'proponent_incident_no': incident.proponent_incident_no, 'determination_type_code': incident.determination_type_code, 'followup_investigation_type_code': incident.followup_investigation_type_code, 'followup_inspection': incident.followup_inspection, 'followup_inspection_date': '2019-01-01', 'status_code': incident.status_code, 'dangerous_occurrence_subparagraph_ids': incident.dangerous_occurrence_subparagraph_ids, 'recommendations': incident.recommendations } put_resp = test_client.put( f'/mines/{mine_incident.mine_guid}/incidents/{mine_incident.mine_incident_guid}', headers=auth_headers['full_auth_header'], json=data) put_data = json.loads(put_resp.data.decode()) assert put_resp.status_code == 200, put_resp.response assert put_data['incident_timestamp'] == data['incident_timestamp'] assert put_data['reported_timestamp'] == data['reported_timestamp'] assert put_data['incident_description'] == data['incident_description'] assert put_data['reported_by_name'] == data['reported_by_name'] assert put_data['reported_by_email'] == data['reported_by_email'] assert put_data['reported_by_phone_no'] == data['reported_by_phone_no'] assert put_data['reported_by_phone_ext'] == data[ 'reported_by_phone_ext'] assert put_data['emergency_services_called'] == data[ 'emergency_services_called'] assert put_data['number_of_injuries'] == data['number_of_injuries'] assert put_data['number_of_fatalities'] == data['number_of_fatalities'] assert put_data['reported_to_inspector_party_guid'] == data[ 'reported_to_inspector_party_guid'] assert put_data['responsible_inspector_party_guid'] == data[ 'responsible_inspector_party_guid'] assert put_data['determination_inspector_party_guid'] == data[ 'determination_inspector_party_guid'] assert put_data['proponent_incident_no'] == data[ 'proponent_incident_no'] assert put_data['determination_type_code'] == data[ 'determination_type_code'] assert put_data['followup_investigation_type_code'] == data[ 'followup_investigation_type_code'] assert put_data['followup_inspection'] == data['followup_inspection'] assert put_data['followup_inspection_date'] == data[ 'followup_inspection_date'] assert put_data['status_code'] == data['status_code'] assert all(x in data['dangerous_occurrence_subparagraph_ids'] for x in put_data['dangerous_occurrence_subparagraph_ids']) assert all(x in data['recommendations'] for x in put_data['recommendations'])