def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/test/" and not kwargs: return MockResponse(page1, url) if url == "https://api.refine.bio/v1/test/" and kwargs["params"] == {"offset": 2, "limit": 2}: return MockResponse(page2, url) if url == "https://api.refine.bio/v1/processors/" and kwargs["params"] == {"limit": 10}: return MockResponse( { "count": 20, "next": "foo", "previous": None, "results": [processor(i) for i in range(10)], }, url, ) if url == "https://api.refine.bio/v1/processors/" and kwargs["params"] == { "offset": 10, "limit": 10, }: return MockResponse( { "count": 20, "next": "foo", "previous": None, "results": [processor(i) for i in range(10, 20)], }, url, )
def mocked_success_requests(*args, **kwargs): if args[0] == 'https://pi-test.sagepay.com/api/v1/merchant-session-keys': return MockResponse({ 'merchantSessionKey': 'unique-key', 'expiry': '2015-08-11T11:45:16.285+01:00' }, 201) else: return MockResponse({}, 201)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/token/": return MockResponse(token, url) if url == "https://api.refine.bio/v1/token/123456789/": return MockResponse(token, url) if url == "https://api.refine.bio/v1/token/test/": return MockResponse(token2, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/samples/SRR5445147/": return MockResponse(sample_1, url) if url == "https://api.refine.bio/v1/samples/GSM1562009/": return MockResponse(sample_2, url) # this request needs to exist because the result's organism index is None for both test samples # so a request will be made to make sure that that value should actually be None if url == "https://api.refine.bio/v1/computational_results/1339595/": return MockResponse(result, url) if url == "https://api.refine.bio/v1/samples/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/samples/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/samples/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url) if url == "https://api.refine.bio/v1/search/": return MockResponse(e_search_1, "e_search_2") if url == "e_search_2": return MockResponse(e_search_2, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/qn_targets/MUSTELA_PUTORIUS_FURO/": return MockResponse(qn_target, url) if url == "https://api.refine.bio/v1/qn_targets/HUMAN/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/qn_targets/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/qn_targets/": return MockResponse(qn_target_organisms, url)
def test_download_compendium_extract(self, mock_get, mock_download, mock_unpack): mock_get.return_value = MockResponse( { "count": 1, "next": None, "previous": None, "results": [{ "id": 123, "computed_file": { "download_url": "test_url" } }], }, "url", ) pyrefinebio.download_compendium("./", "test_organism", extract=True) expected_path = os.path.abspath("compendium-123.zip") mock_download.assert_called_with("test_url", expected_path, True) mock_unpack.assert_called_with(expected_path)
def test_download_compendium(self, mock_get, mock_download): mock_get.return_value = MockResponse( { "count": 1, "next": None, "previous": None, "results": [{ "computed_file": { "download_url": "test_url" } }], }, "url", ) pyrefinebio.download_compendium("test", "test_organism") mock_get.assert_called_with( "compendia", params={ "primary_organism__name": "test_organism", "quant_sf_only": False, "latest_version": True, }, ) mock_download.assert_called_with("test_url", os.path.abspath("test"), True)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/transcriptome_indices/1/": return MockResponse(index_1, url) if url == "https://api.refine.bio/v1/transcriptome_indices/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/transcriptome_indices/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/transcriptome_indices/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/processors/1/": return MockResponse(processor_1, url) if url == "https://api.refine.bio/v1/processors/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/processors/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/processors/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def test_download_dataset_notify_me(self, mock_post, mock_get, mock_download): mr = MockResponse( { "id": "test_dataset", "is_processed": True, "download_url": "test_url" }, "url") mock_get.return_value = mr mock_post.return_value = mr dataset_dict = {"test": ["foo", "bar"]} pyrefinebio.download_dataset("test", "*****@*****.**", dataset_dict=dataset_dict, notify_me=True) mock_post.assert_called_with( "dataset", payload={ "data": dataset_dict, "aggregate_by": "EXPERIMENT", "email_address": "*****@*****.**", "start": True, "quantile_normalize": True, "quant_sf_only": False, "notify_me": True, }, ) mock_download.assert_called_with("test_url", os.path.abspath("test"), True)
def test_dataset_notify_me(self, mock_put_by_endpoint, mock_post_by_endpoint): """ Test requesting to be notified """ mock_post_by_endpoint.return_value = MockResponse(dataset, "") # First test that notify_me is false by default ds = pyrefinebio.Dataset( data={"GSE68833": ["ALL"]}, aggregate_by="EXPERIMENT", email_address="*****@*****.**", ) ds.save() self.assertEqual(mock_post_by_endpoint.call_count, 1) # We need to use assertEqual here because None is False-y args, kwargs = mock_post_by_endpoint.call_args self.assertEqual(kwargs["payload"].get("notify_me", None), False) self.assertIsNotNone(ds.id) # Now set notify_me true ds.notify_me = True ds.save() self.assertEqual(mock_put_by_endpoint.call_count, 1) args, kwargs = mock_put_by_endpoint.call_args self.assertTrue(kwargs["payload"].get("notify_me", None))
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/organisms/GORILLA/": return MockResponse(gorilla, url) if url == "https://api.refine.bio/v1/organisms/HUMAN/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/organisms/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/organisms/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/experiments/SRP150473/": return MockResponse(experiment, url) if url == "https://api.refine.bio/v1/experiments/bad-accession-code/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/experiments/force-500-error/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/search/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def test_host_and_ip(self, mock_get: MagicMock): mock_get.return_value = MockResponse({'data': [ { 'ip': '192.168.99.99', 'machine_type': 'unknown', 'name': 'something' } ]}, 200) haip = self.host.get_host_and_ip(name='pi-garage') print(haip)
def test_download_compendium_no_token(self, mock_compendia_get, mock_computed_file_get): mock_compendia_get.return_value = MockResponse( { "count": 1, "next": None, "previous": None, "results": [{ "computed_file": { "download_url": None } }], }, "url", ) mock_computed_file_get.return_value = MockResponse( {"download_url": None}, "url") with self.assertRaises(pyrefinebio.exceptions.DownloadError): pyrefinebio.download_compendium("test", "test_organism")
def test_iteration(self, mock_request): T = pyrefinebio.Processor response = MockResponse(page1, "https://api.refine.bio/v1/test/") paginatedList = pyrefinebio.util.create_paginated_list(T, response) actual = [item0, item1, item2, item3] i = 0 for processor in paginatedList: self.assertObject(processor, actual[i]) i += 1
def test_dataset_save(self, mock_put, mock_post): mock_post.return_value = MockResponse(dataset, "") mock_put.return_value = MockResponse(dataset, "") ds = pyrefinebio.Dataset( data={"test-experiment": ["sample-1", "sample-2"]}, email_address="test-email") ds = ds.save() mock_post.assert_called_with( "dataset", payload={ "data": { "test-experiment": ["sample-1", "sample-2"] }, "email_address": "test-email", "notify_me": False, }, ) ds.email_address = "changed-email" ds.save() mock_put.assert_called_with( "dataset/test-dataset", payload={ "data": { "test-experiment": ["sample-1", "sample-2"] }, "aggregate_by": "EXPERIMENT", "scale_by": "NONE", "email_address": "changed-email", "quantile_normalize": True, "quant_sf_only": False, "svd_algorithm": "NONE", # We want email notifications to be opt-out "notify_me": False, }, )
def mock_request(method, url, data, **kwargs): if method == "POST": data = json.loads(data) ds = {} for key, value in dataset.items(): if data.get(key): ds[key] = data.get(key) else: ds[key] = value return MockResponse(ds, url) if url == "https://api.refine.bio/v1/dataset/test-dataset/": return MockResponse(dataset, url) if url == "https://api.refine.bio/v1/dataset/non-processed-dataset/": return MockResponse(non_processed_dataset, url) if url == "https://api.refine.bio/v1/dataset/": return MockResponse(dataset, url) if url == "https://api.refine.bio/v1/dataset/500/": return MockResponse(dataset, url, status=500)
def test_dataset_download_no_size_header(self, mock_get, mock_input, mock_copy): ds = pyrefinebio.Dataset(download_url="test_download_url") mr = MockResponse(None, "test_download_url", headers={}) setattr(mr, "raw", "raw") mock_get.return_value.__enter__.return_value = mr mock_input.return_value = "n" ds.download("test_path") mock_get.assert_called_with("test_download_url", stream=True) mock_copy.assert_not_called()
def test_dataset_download(self, mock_get, mock_open, mock_copy): ds = pyrefinebio.Dataset(download_url="test_download_url") mr = MockResponse(None, "test_download_url", headers={"content-length": 3}) setattr(mr, "raw", "raw") mock_get.return_value.__enter__.return_value = mr mock_open.return_value.__enter__.return_value = "file" ds.download("test_path") mock_get.assert_called_with("test_download_url", stream=True) mock_open.assert_called_with(os.path.abspath("test_path"), "wb") mock_copy.assert_called_with("raw", "file")
def test_indexing(self, mock_request): T = pyrefinebio.Processor response = MockResponse(page1, "https://api.refine.bio/v1/test/") paginatedList = pyrefinebio.util.create_paginated_list(T, response) self.assertObject(paginatedList[0], item0) self.assertObject(paginatedList[1], item1) self.assertObject(paginatedList[2], item2) self.assertObject(paginatedList[3], item3) self.assertObject(paginatedList[-1], item3) self.assertObject(paginatedList[-2], item2) with self.assertRaises(IndexError): paginatedList[4] with self.assertRaises(IndexError): paginatedList[-5]
def test_download_dataset_experiments(self, mock_post, mock_get, mock_download): mr = MockResponse( { "id": "test_dataset", "is_processed": True, "download_url": "test_url" }, "url") mock_get.return_value = mr mock_post.return_value = mr experiment2 = pyrefinebio.Experiment( accession_code="test-experiment-2") pyrefinebio.download_dataset( "test", "*****@*****.**", experiments=["test-experiment-1", experiment2]) mock_post.assert_called_with( "dataset", payload={ "data": { "test-experiment-1": ["ALL"], "test-experiment-2": ["ALL"] }, "aggregate_by": "EXPERIMENT", "email_address": "*****@*****.**", "start": True, "quantile_normalize": True, "quant_sf_only": False, "notify_me": False, }, ) mock_download.assert_called_with("test_url", os.path.abspath("test"), True)
def test_download_dataset_extract(self, mock_post, mock_get, mock_download, mock_unpack): mr = MockResponse( { "id": "test_dataset", "is_processed": True, "download_url": "test_url" }, "url") mock_get.return_value = mr mock_post.return_value = mr dataset_dict = {"test": ["foo", "bar"]} pyrefinebio.download_dataset("./", "*****@*****.**", dataset_dict=dataset_dict, extract=True) expected_path = os.path.abspath("dataset-test_dataset.zip") mock_download.assert_called_with("test_url", expected_path, True) mock_unpack.assert_called_with(expected_path)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/original_files/1/": return MockResponse(og_file_1, url) if url == "https://api.refine.bio/v1/original_files/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/original_files/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/jobs/processor/29708302/": return MockResponse(processor_job, url) if url == "https://api.refine.bio/v1/jobs/downloader/7381811/": return MockResponse(downloader_job, url) if url == "https://api.refine.bio/v1/original_files/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/computed_files/1/": return MockResponse(computed_file_1, url) if url == "https://api.refine.bio/v1/computed_files/2/": return MockResponse(computed_file_2, url) if url == "https://api.refine.bio/v1/computed_files/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/computed_files/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/computed_files/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url) if url == "https://api.refine.bio/v1/computational_results/2784485/": return MockResponse(result, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/compendia/1/": return MockResponse(compendium_object_1, url) if url == "https://api.refine.bio/v1/compendia/42/": return MockResponse(compendium_object_2, url) if url == "https://api.refine.bio/v1/compendia/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/compendia/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/compendia/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def mock_request(method, url, **kwargs): if url == "https://api.refine.bio/v1/jobs/downloader/1/": return MockResponse(job_1, url) if url == "https://api.refine.bio/v1/jobs/downloader/2/": return MockResponse(job_2, url) if url == "https://api.refine.bio/v1/jobs/downloader/0/": return MockResponse(None, url, status=404) if url == "https://api.refine.bio/v1/jobs/downloader/500/": return MockResponse(None, url, status=500) if url == "https://api.refine.bio/v1/jobs/downloader/": return MockResponse(search_1, "search_2") if url == "search_2": return MockResponse(search_2, url)
def mock_get(*args: Any, **kwargs: Any) -> MockResponse: return MockResponse(OIDCConfig)
def mock_get(*args: Any, **kwargs: Any) -> MockResponse: return MockResponse({}, DummyExc)
def mock_get(args: Any, **kwargs: Any) -> MockResponse: nonlocal call_count call_count += 1 return MockResponse(OIDCConfig)