def test_run_prefix__success(mocker): """Test run prefix success.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**MOCKED_UPLOADS), ) mocked_add_samples_to_project = mocker.patch.object( APIClient, "add_samples_to_project", return_value=UploadSamples(**{}), ) res = runner.invoke( run_prefix, [ str(uuid4()), "gncv://batch", "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 0 mocked_login.assert_called_once() mocked_get_sample_sheet.assert_called_once() mocked_add_samples_to_project.assert_called_once() assert "Number of samples assigned to the project" in res.output assert "Assigning metadata to the uploaded samples." not in res.output
def test_run_prefix__filter_success(mocker): """Test run prefix when filter returns some paths.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_uploads_copy = copy.deepcopy(MOCKED_UPLOADS) del mocked_uploads_copy["results"][1] mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**mocked_uploads_copy), ) mocked_add_samples_to_project = mocker.patch.object( APIClient, "add_samples_to_project", ) res = runner.invoke( run_prefix, [ str(uuid4()), "gncv://batch", "--status", "unassigned", "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 0 mocked_login.assert_called_once() mocked_get_sample_sheet.assert_called_once() mocked_add_samples_to_project.assert_called_once() assert "Number of samples assigned to the project" in res.output assert "Assigning metadata to the uploaded samples." not in res.output
def test_run_prefix__filter_empty(mocker): """Test run prefix when all paths are filtered out.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**MOCKED_EMPTY_UPLOADS), ) mocked_add_samples_to_project = mocker.patch.object( APIClient, "add_samples_to_project", return_value=UploadSamples(**{}), ) res = runner.invoke( run_prefix, [ str(uuid4()), "gncv://batch", "--status", "assigned", "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_get_sample_sheet.assert_called_once() mocked_add_samples_to_project.assert_not_called() assert "No matching paths found" in res.output
def test_run_prefix__assigning_samples_failed(mocker): """Test run prefix when assigning samples fails.""" runner = CliRunner() mocked_login = mocker.patch.object(APIClient, "login", return_value=None) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**MOCKED_UPLOADS), ) mocked_add_samples_to_project = mocker.patch.object( APIClient, "add_samples_to_project", side_effect=APIClientError(message="", status_code=404), ) res = runner.invoke( run_prefix, [ str(uuid4()), "gncv://batch", "--email", "*****@*****.**", "--password", "123", ], ) assert res.exit_code == 1 mocked_login.assert_called_once() mocked_get_sample_sheet.assert_called_once() mocked_add_samples_to_project.assert_called_once() assert "You do not have the sufficient permission" not in res.output assert "There was an error assigning" in res.output
def test_upload_and_run_immediately_something_went_wrong( credentials, mocker, project_id, recording, vcr ): """Upload and assign right away did't work.""" runner = CliRunner() with runner.isolated_filesystem(): os.mkdir("cli_test_data") with open("cli_test_data/test.fastq.gz", "w") as fastq_file: fastq_file.write("AAABBB") mocked_get_credentials = mocker.patch( "gencove.command.upload.main.get_s3_client_refreshable", side_effect=get_s3_client_refreshable, ) mocked_upload_file = mocker.patch( "gencove.command.upload.main.upload_file", side_effect=upload_file ) if not recording: # Mock get_upload credentials only if using the cassettes, since # we mock the return value. upload_details_response = get_vcr_response( "/api/v2/uploads-post-data/", vcr ) mocked_get_upload_details = mocker.patch.object( APIClient, "get_upload_details", return_value=UploadsPostData(**upload_details_response), ) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet( **{"meta": {"next": None}, "results": []} ), ) mocked_assign_sample = mocker.patch.object( APIClient, "add_samples_to_project" ) res = runner.invoke( upload, [ "cli_test_data", *credentials, "--run-project-id", project_id, ], ) assert res.exit_code == 0 mocked_get_credentials.assert_called_once() if not recording: mocked_get_upload_details.assert_called_once() mocked_upload_file.assert_called_once() mocked_get_sample_sheet.assert_called() mocked_assign_sample.assert_not_called()
def test_list_empty(mocker, credentials): """Test user has no uploads.""" runner = CliRunner() mocked_get_projects = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(results=[], meta=dict(next=None)), ) res = runner.invoke(list_uploads, credentials) assert res.exit_code == 0 mocked_get_projects.assert_called_once() assert "" in res.output
def test_list_uploads(mocker, credentials, recording, vcr): """Test list uploads being outputed to the shell.""" runner = CliRunner() if not recording: sample_sheet_response = get_vcr_response("/api/v2/sample-sheet/", vcr) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**sample_sheet_response), ) res = runner.invoke(list_uploads, credentials) assert res.exit_code == 0 if not recording: mocked_get_sample_sheet.assert_called() uploads = sample_sheet_response["results"] uploads = "\n".join([ "\t".join([ upload["client_id"], upload["fastq"]["r1"]["upload"], upload["fastq"]["r1"]["destination_path"], ]) for upload in uploads ]) assert f"{uploads}\n" == res.output
def test_upload_and_run_immediately_without_progressbar( credentials, mocker, project_id, recording, vcr ): """Upload and assign right away.""" # pylint: disable=too-many-locals runner = CliRunner() with runner.isolated_filesystem(): os.mkdir("cli_test_data") with open("cli_test_data/test.fastq.gz", "w") as fastq_file: fastq_file.write("AAABBB") mocked_get_credentials = mocker.patch( "gencove.command.upload.main.get_s3_client_refreshable", side_effect=get_s3_client_refreshable, ) mocked_upload_file = mocker.patch( "gencove.command.upload.main.upload_file", side_effect=upload_file ) mocked_regular_progress_bar = mocker.patch( "gencove.command.upload.main.get_regular_progress_bar", side_effect=get_regular_progress_bar, ) if not recording: # Mock get_upload credentials only if using the cassettes, since # we mock the return value. upload_details_response = get_vcr_response( "/api/v2/uploads-post-data/", vcr ) mocked_get_upload_details = mocker.patch.object( APIClient, "get_upload_details", return_value=UploadsPostData(**upload_details_response), ) sample_sheet_response = get_vcr_response( "/api/v2/sample-sheet/", vcr ) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**sample_sheet_response), ) project_sample_response = get_vcr_response( "/api/v2/project-samples/", vcr, operator.contains ) mocked_assign_sample = mocker.patch.object( APIClient, "add_samples_to_project", return_value=UploadSamples(**project_sample_response), ) res = runner.invoke( upload, [ "cli_test_data", *credentials, "--run-project-id", project_id, "--no-progress", ], ) assert res.exit_code == 0 mocked_get_credentials.assert_called_once() mocked_upload_file.assert_called_once() mocked_regular_progress_bar.assert_not_called() if not recording: mocked_get_upload_details.assert_called_once() mocked_get_sample_sheet.assert_called() mocked_assign_sample.assert_called_once()
def test_upload_and_run_immediately_with_stdout( credentials, mocker, project_id, recording, vcr ): """Upload and assign right away, then print out the results.""" # pylint: disable=too-many-locals runner = CliRunner() with runner.isolated_filesystem(): os.mkdir("cli_test_data") with open("cli_test_data/test.fastq.gz", "w") as fastq_file: fastq_file.write("AAABBB") mocked_get_credentials = mocker.patch( "gencove.command.upload.main.get_s3_client_refreshable", side_effect=get_s3_client_refreshable, ) if not recording: # Mock get_upload credentials only if using the cassettes, since # we mock the return value. upload_details_response = get_vcr_response( "/api/v2/uploads-post-data/", vcr ) mocked_get_upload_details = mocker.patch.object( APIClient, "get_upload_details", return_value=UploadsPostData(**upload_details_response), ) sample_sheet_response = get_vcr_response( "/api/v2/sample-sheet/", vcr ) mocked_get_sample_sheet = mocker.patch.object( APIClient, "get_sample_sheet", return_value=SampleSheet(**sample_sheet_response), ) project_sample_response = get_vcr_response( "/api/v2/project-samples/", vcr, operator.contains ) mocked_assign_sample = mocker.patch.object( APIClient, "add_samples_to_project", return_value=UploadSamples(**project_sample_response), ) # These mocks needs to be here and without side effect otherwise # a weird behavior on the stdout will happend and the test fails. mocked_upload_file = mocker.patch( "gencove.command.upload.main.upload_file" ) mocker.patch( "gencove.command.upload.main.get_regular_progress_bar", ) res = runner.invoke( upload, [ "cli_test_data", *credentials, "--run-project-id", project_id, "--output", "-", ], ) assert res.exit_code == 0 mocked_get_credentials.assert_called_once() if not recording: mocked_upload_file.assert_called_once() mocked_get_upload_details.assert_called_once() mocked_get_sample_sheet.assert_called() mocked_assign_sample.assert_called_once() # r2 fastq key is not present on the file assert ( project_sample_response["uploads"][0]["fastq"]["r2"] is None ) del project_sample_response["uploads"][0]["fastq"]["r2"] assert ( json.dumps(project_sample_response["uploads"], indent=4) in res.output )