def test_default_init(monkeypatch): def requests_mock(*args, **kwargs): response = requests.models.Response() response.status_code = 200 return response monkeypatch.setattr( requests, "get", requests_mock) # apply the monkeypatch for requests.get to mock_get rubrix._client = None if "RUBRIX_API_URL" in os.environ: del os.environ["RUBRIX_API_URL"] if "RUBRIX_API_KEY" in os.environ: del os.environ["RUBRIX_API_KEY"] rubrix.init() assert isinstance(rubrix._client._client, Client) assert rubrix._client._client.base_url == "http://localhost:6900" expected_token = "blablabla" rubrix.init(api_key=expected_token) assert isinstance(rubrix._client._client, AuthenticatedClient) assert rubrix._client._client.token == expected_token
def main(config): rb.init(api_url=config["rubrix_uri"], api_key=config["rubrix_api_key"]) rb.set_workspace(config["rubrix_workspace"]) connection_uri = config["connection_uri"] engine = sa.create_engine(connection_uri) result_list = [] with engine.connect() as connection: # Long Covid Patients; notes 1 month after DX or test, Outpatient physician notes query = """ select pn.*, e.service_delivery_location from sbm_covid19_documents.physician_notes pn join sbm_covid19_hi.PH_F_Encounter e on pn.encounter_id = e.encounter_number where document_id in ( select distinct document_id from sbm_covid19_documents.physician_notes p join sbm_covid19_analytics_build.pui_covid_result_overview pcro on p.patient_id = pcro.mrn join sbm_covid19_hi_cdm_build.map2_condition_occurrence co on pcro.person_id = co.person_id and co.condition_source_concept_code = 'U09.9' where p.document_datetime >= pcro.positive_datetime + interval '1 month' and pcro.positive_datetime is not null) and document_type not in ('Ambulatory Patient Summary') and e.classification_display = 'Outpatient' """ cursor = connection.execute(query) report_list = [] for row in cursor: result_list += [row["file_text"]] meta_data_dict = {} columns = ["patient_id", "mapped_document_type", "document_type", "service_delivery_location", "patient_id", "encounter_id"] for column in columns: meta_data_dict[column] = row[column] rb_obj = rb.TextClassificationRecord(inputs={"text": row["file_text"]}, metadata=meta_data_dict, id=row["document_id"], multi_label=True) report_list += [rb_obj] reports_to_commit = [] for i in range(len(report_list)): reports_to_commit += [report_list[i]] if i > 0 and i % 50 == 0: rb.log(records=reports_to_commit, name="long_covid_with_metadata") reports_to_commit = [] else: pass if len(reports_to_commit): rb.log(records=reports_to_commit, name="long_covid_with_metadata")
def test_init_token_auth_fail(mock_response_token_401): """Testing initalization with failed authentication It checks an Exception is raised with the correct message. Parameters ---------- mock_response_401 Mocked correct http response """ rubrix._client = None # assert empty client with pytest.raises(Exception, match="Authentification error: invalid credentials."): rubrix.init(api_url="fake_url", api_key="422")
def test_init_token_correct(mock_response_200): """Testing correct token initalization It checks if the _client created is a RubrixClient object. Parameters ---------- mock_response_200 Mocked correct http response """ rubrix._client = None # assert empty client rubrix.init(api_key="fjkjdf333") assert isinstance(rubrix._client, RubrixClient)
def test_init_correct(mock_response_200): """Testing correct default initalization It checks if the _client created is a RubrixClient object. Parameters ---------- mock_response_200 Mocked correct http response """ rubrix.init() assert isinstance(rubrix._client, RubrixClient)
def test_init_token_incorrect(mock_response_500): """Testing incorrect token initalization It checks an Exception is raised with the correct message. Parameters ---------- mock_response_500 Mocked correct http response """ rubrix._client = None # assert empty client with pytest.raises(Exception, match="Unidentified error, it should not get here."): rubrix.init(api_key="422")
def test_init_token_incorrect(mock_response_500): """Testing incorrect token initalization It checks an Exception is raised with the correct message. Parameters ---------- mock_response_500 Mocked correct http response """ rubrix._client = None # assert empty client with pytest.raises( Exception, match= "Connection error: Indetermined error connecting to Rubrix Server. The API answered with a 500 code: b", ): rubrix.init(api_key="422")
def test_default_init(mock_response_200): rubrix._client = None if "RUBRIX_API_URL" in os.environ: del os.environ["RUBRIX_API_URL"] if "RUBRIX_API_KEY" in os.environ: del os.environ["RUBRIX_API_KEY"] rubrix.init() assert isinstance(rubrix._client._client, Client) assert rubrix._client._client.base_url == "http://localhost:6900" expected_token = "blablabla" rubrix.init(api_key=expected_token) assert isinstance(rubrix._client._client, AuthenticatedClient) assert rubrix._client._client.token == expected_token
def test_init_evironment_no_url_token(token_env_var, mock_response_200): """Testing initalization with token provided via environment variable and api_url via args It checks a non-secured Client is created Parameters ---------- token_env_var Fixture to set the fake token in the env variable mock_response_200 Mocked correct http response """ rubrix._client = None # assert empty client rubrix.init(api_url="http://anotherfakeurl.com") assert isinstance(rubrix._client, RubrixClient) assert isinstance(rubrix._client._client, Client) assert rubrix._client._client.base_url == "http://anotherfakeurl.com"
def test_init_evironment_url(api_url_env_var, mock_response_200): """Testing initalization with api_url provided via environment variable It checks the url in the environment variable gets passed to client. Parameters ---------- api_url_env_var Fixture to set the fake url in the env variable mock_response_200 Mocked correct http response """ rubrix._client = None # assert empty client rubrix.init() assert isinstance(rubrix._client, RubrixClient) assert isinstance(rubrix._client._client, Client) assert rubrix._client._client.base_url == "http://fakeurl.com"
def test_trailing_slash(api_url_env_var_trailing_slash, mock_response_200): """Testing initalization with provided api_url via environment variable and argument It checks the trailing slash is removed in all cases Parameters ---------- api_url_env_var Fixture to set the fake url in the env variable, with trailing slash mock_response_200 Mocked correct http response """ rubrix._client = None # assert empty client # Environment variable case rubrix.init(api_url="http://anotherfakeurl.com/") assert rubrix._client._client.base_url == "http://anotherfakeurl.com" rubrix._client = None # assert empty client # Argument case rubrix.init() assert rubrix._client._client.base_url == "http://fakeurl.com"