Пример #1
0
def test_import_invalid_json(
    tmp_path: Path,
    user,
    project_factory,
    samples_dir: Path,
    sample_scans,
):
    json_file = str(tmp_path / 'import.json')
    json_content = generate_import_json(samples_dir, sample_scans)

    # deliberately invalidate the data
    json_content['scans'][0]['site_id'] = 666

    with open(json_file, 'w') as fd:
        fd.write(json.dumps(json_content))

    project = project_factory(import_path=json_file)

    with pytest.raises(
        ValidationError,
        match=re.escape(
            "666 is not of type 'string'\n\nFailed validating 'type' in schema['properties']['scans']['items']['properties']['site_id']:\n    {'type': 'string'}\n\nOn instance['scans'][0]['site_id']:\n    666"  # noqa: E501
        ),
    ):
        import_data(user.id, project.id)
Пример #2
0
    def import_(self, request, **kwargs):
        project: Project = self.get_object()

        # tasks sent to celery must use serializable arguments
        import_data(project.id)

        return Response(status=status.HTTP_204_NO_CONTENT)
Пример #3
0
def test_import_invalid_csv(tmp_path: Path, user, project_factory, sample_scans):
    csv_file = str(tmp_path / 'import.csv')
    output, writer = generate_import_csv(sample_scans)

    # deliberately invalidate the data
    writer.writerow(
        {
            'xnat_experiment_id': 'NCANDA_DUMMY',
            # This value with no common prefix with the other scans will cause a ValueError
            'nifti_folder': 'NOT_A_REAL_FOLDER',
            'scan_id': '123',
            'scan_type': 'foobar',
            'experiment_note': '',
            'decision': '',
            'scan_note': '',
        }
    )

    with open(csv_file, 'w') as fd:
        fd.write(output.getvalue())

    project = project_factory(import_path=csv_file)

    with pytest.raises(ValueError, match='empty separator'):
        import_data(user.id, project.id)
Пример #4
0
def test_import_with_relative_path(project_factory):
    rel_import_csv = Path(__file__).parent / 'data' / 'relative_import.csv'
    project = project_factory(import_path=rel_import_csv)
    import_data(project.id)

    frame = Frame.objects.first()
    assert frame.raw_path == str(
        Path(__file__).parent / 'data' / 'example.nii.gz')
Пример #5
0
def test_import_s3_preserves_path(project_factory):
    s3_import_csv = Path(__file__).parent / 'data' / 's3_import.csv'
    project = project_factory(import_path=s3_import_csv)
    import_data(project.id)

    frame = Frame.objects.first()
    assert (
        frame.raw_path ==
        's3://miqa-sample/IXI_small/Guys/IXI002/0828-DTI/IXI002-Guys-0828-DTI-00.nii.gz'
    )
Пример #6
0
def test_import_csv(tmp_path, user, project_factory, sample_scans, authenticated_api_client):
    csv_file = str(tmp_path / 'import.csv')
    with open(csv_file, 'w') as fd:
        output, _writer = generate_import_csv(sample_scans)
        fd.write(output.getvalue())

    project = project_factory(import_path=csv_file)

    import_data(user.id, project.id)
    # Test that the API import succeeds
    resp = authenticated_api_client.post(f'/api/v1/projects/{project.id}/import')
    assert resp.status_code == 204
Пример #7
0
def test_import_json(
    tmp_path: Path,
    user,
    project_factory,
    samples_dir: Path,
    sample_scans,
    authenticated_api_client,
):
    json_file = str(tmp_path / 'import.json')
    with open(json_file, 'w') as fd:
        fd.write(json.dumps(generate_import_json(samples_dir, sample_scans)))

    project = project_factory(import_path=json_file)

    import_data(user.id, project.id)

    # Test that the API import succeeds
    resp = authenticated_api_client.post(f'/api/v1/projects/{project.id}/import')
    assert resp.status_code == 204
Пример #8
0
def test_import_invalid_csv(tmp_path: Path, user, project_factory,
                            sample_scans):
    csv_file = str(tmp_path / 'import.csv')
    output, writer = generate_import_csv(sample_scans)

    # deliberately invalidate the data
    writer.writerow({
        'project_name': 'testProject',
        'experiment_name': 'testExperiment',
        'scan_name': 'testScan',
        'scan_type': 'foobar',
        'frame_number': 0,
        'file_location': '/not/a/real/file.nii.gz',
    })

    with open(csv_file, 'w') as fd:
        fd.write(output.getvalue())

    project = project_factory(import_path=csv_file)

    with pytest.raises(APIException, match='Could not locate file'):
        import_data(user.id, project.id)
Пример #9
0
def test_import_invalid_json(
    tmp_path: Path,
    user,
    project_factory,
    samples_dir: Path,
    sample_scans,
):
    json_file = str(tmp_path / 'import.json')
    json_content = generate_import_json(samples_dir, sample_scans)

    # deliberately invalidate the data
    json_content['fake_key'] = 'foo'

    with open(json_file, 'w') as fd:
        fd.write(json.dumps(json_content))

    project = project_factory(import_path=json_file)

    with pytest.raises(
            APIException,
            match=re.escape('Invalid format of import file'),
    ):
        import_data(user.id, project.id)
Пример #10
0
def test_import_invalid_extension(user, project_factory):
    invalid_file = '/foo/bar.txt'
    project = project_factory(import_path=invalid_file)
    with pytest.raises(ValidationError, match=f'Invalid import file {invalid_file}'):
        import_data(user.id, project.id)
Пример #11
0
 def import_(self, request, **kwargs):
     import_data(None)
     return Response(status=status.HTTP_204_NO_CONTENT)