def test_create_upload_model_csv_invalid_permissions(
    workspace: Workspace,
    user: User,
    authenticated_api_client,
    s3ff_field_value_factory,
    permission: WorkspaceRoleChoice,
    status_code: int,
):
    """Test that a user with insufficient permissions is forbidden from a POST request."""
    if permission is not None:
        workspace.set_user_permission(user, permission)

    # Generate field value
    data_file = data_dir / 'airports.csv'
    upload = local_csv_upload(data_file, workspace, user)
    field_value = s3ff_field_value_factory(upload.blob)

    table_name = f't{uuid.uuid4().hex}'
    r: Response = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/uploads/csv/',
        {
            'field_value': field_value,
            'edge': False,
            'table_name': table_name,
            'columns': {
                'latitude': 'number',
                'longitude': 'number',
                'altitude': 'number',
                'timezone': 'number',
                'year built': 'number',
            },
        },
        format='json',
    )
    assert r.status_code == status_code
def test_query_rest_retrieve_results(
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    valid_query,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    query_info = valid_query['response'].json()
    query_id = query_info['id']
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/queries/{query_id}/results/')
    assert r.status_code == status_code
    if success:
        r_json = r.json()
        assert r_json['id'] == query_id
        assert r_json['workspace'] == str(workspace)
        assert r_json['user'] == str(user)

        results = r_json['results']
        expected_results = valid_query['nodes']
        assert len(results) == len(expected_results)
        for row in results:
            assert row in expected_results
def airports_csv(
    workspace: Workspace, user: User, authenticated_api_client, s3ff_field_value_factory
) -> Dict:
    workspace.set_user_permission(user, WorkspaceRoleChoice.WRITER)

    # Upload file
    data_file = data_dir / 'airports.csv'
    upload = local_csv_upload(data_file, workspace, user)

    # Model creation request
    table_name = f't{uuid.uuid4().hex}'
    field_value = s3ff_field_value_factory(upload.blob)
    r: Response = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/uploads/csv/',
        {
            'field_value': field_value,
            'edge': False,
            'table_name': table_name,
            'columns': {
                'latitude': 'number',
                'longitude': 'number',
                'altitude': 'number',
                'timezone': 'number',
                'year built': 'number',
            },
        },
        format='json',
    )
    WorkspaceRole.objects.filter(workspace=workspace, user=user).delete()
    return {
        'response': r,
        'table_name': table_name,
        'data_file': data_file,
    }
def test_upload_rest_list(
    workspace: Workspace,
    user: User,
    upload_factory: UploadFactory,
    authenticated_api_client: APIClient,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    """Test listing all uploads on a workspace for which the user has permission."""
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    upload_ids = [
        upload_factory(workspace=workspace, user=user).pk for _ in range(3)
    ]
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/uploads/')
    assert r.status_code == status_code

    if success:
        r_json = r.json()
        assert r_json['count'] == len(upload_ids)
        for upload in r_json['results']:
            assert upload['id'] in upload_ids
    else:
        assert r.data == {'detail': 'Not found.'}
def test_upload_rest_retrieve(
    workspace: Workspace,
    user: User,
    upload_factory: UploadFactory,
    authenticated_api_client: APIClient,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    """Test retrieval of an upload on a private workspace."""
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    upload: Upload = upload_factory(workspace=workspace, user=user)
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/uploads/{upload.pk}/')
    assert r.status_code == status_code

    if success:
        assert r.data == {
            'id': upload.pk,
            'blob': upload.blob,
            'user': user.username,
            'created': TIMESTAMP_RE,
            'modified': TIMESTAMP_RE,
            'error_messages': upload.error_messages,
            'data_type': upload.data_type,
            'status': upload.status,
            'workspace': workspace_re(workspace),
        }
    else:
        assert r.data == {'detail': 'Not found.'}
def test_query_rest_retrieve_results_mutating(
        workspace: Workspace, user: User, authenticated_api_client: APIClient,
        mutating_query):
    workspace.set_user_permission(user, WorkspaceRoleChoice.READER)
    query_info = mutating_query['response'].json()
    query_id = query_info['id']
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/queries/{query_id}/results/')
    assert r.status_code == 400
    assert r.data == 'The given query could not be executed, and has no results'
def test_query_rest_retrieve_mutating(workspace: Workspace, user: User,
                                      authenticated_api_client: APIClient,
                                      mutating_query):
    workspace.set_user_permission(user, WorkspaceRoleChoice.READER)

    query_info = mutating_query['response'].json()
    query_id = query_info['id']
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/queries/{query_id}/')
    assert r.status_code == 200
    r_json = r.json()
    assert len(r_json['error_messages']) > 0
    assert r_json['status'] == AqlQuery.Status.FAILED
def test_upload_valid_csv_task_response(
    workspace: Workspace, user: User, authenticated_api_client, airports_csv
):
    """Test just the response of the model creation, not the task itself."""
    # Get upload info
    workspace.set_user_permission(user, WorkspaceRoleChoice.WRITER)
    r = airports_csv['response']
    data_file = airports_csv['data_file']
    table_name = airports_csv['table_name']

    # Since we're running with celery_task_always_eager=True, this job is finished
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/uploads/{r.json()["id"]}/'
    )

    r_json = r.json()
    assert r.status_code == 200
    assert r_json['status'] == Upload.Status.FINISHED
    assert r_json['error_messages'] is None

    # Check that table is created
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/tables/{table_name}/'
    )
    assert r.status_code == 200

    # Check that data was ingested correctly
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/tables/{table_name}/rows/'
    )

    assert r.status_code == 200
    r_json = r.json()
    results = r_json['results']

    # Get source data rows
    with open(data_file) as file_stream:
        rows = [row for row in csv.DictReader(file_stream)]

    # Check rows themselves
    assert r_json['count'] == len(rows)
    for i, row in enumerate(rows):
        result = results[i]

        # Convert these keys so we can compare documents
        for key in ['latitude', 'longitude', 'altitude', 'timezone', 'year built']:
            row[key] = str_to_number(row[key])

        # Assert documents match
        assert result == dict_to_fuzzy_arango_doc(row)
def valid_query(workspace: Workspace, user: User,
                authenticated_api_client: APIClient):
    """Create a fixture representing the response of a POST request for AQL queries."""
    workspace.set_user_permission(user, WorkspaceRoleChoice.READER)
    node_table = populated_table(workspace, False)
    query_str = f'FOR document IN {node_table.name} RETURN document'
    r: Response = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/queries/', {'query': query_str},
        format='json')
    WorkspaceRole.objects.filter(workspace=workspace, user=user).delete()
    return {
        'response': r,
        'query': query_str,
        'nodes': list(node_table.get_rows())
    }
Beispiel #10
0
def mutating_query(workspace: Workspace, user: User,
                   authenticated_api_client: APIClient):
    """Create a fixture for a mutating AQL query that will have an error message post processing."""
    workspace.set_user_permission(user, WorkspaceRoleChoice.READER)
    node_table = populated_table(workspace, False)
    fake = Faker()
    query_str = f"INSERT {{ 'name': {fake.pystr()} }} INTO {node_table.name}"
    r: Response = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/queries/', {'query': query_str},
        format='json')
    WorkspaceRole.objects.filter(workspace=workspace, user=user).delete()
    return {
        'response': r,
        'query': query_str,
        'nodes': list(node_table.get_rows())
    }
def test_create_upload_model_invalid_field_value(
    workspace: Workspace, user: User, authenticated_api_client
):
    workspace.set_user_permission(user, WorkspaceRoleChoice.WRITER)
    r: Response = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/uploads/csv/',
        {
            'field_value': 'field_value',
            'edge': False,
            'table_name': 'table',
        },
        format='json',
    )

    assert r.status_code == 400
    assert r.json() == {'field_value': ['field_value is not a valid signed string.']}
def test_retrieve_table_type_annotations(
    workspace: Workspace, user: User, authenticated_api_client, airports_csv
):
    """Test that the type annotations can be retrieved successfully."""
    workspace.set_user_permission(user, WorkspaceRoleChoice.WRITER)
    table_name = airports_csv['table_name']
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/tables/{table_name}/annotations/'
    )

    assert r.json() == {
        'latitude': TableTypeAnnotation.Type.NUMBER,
        'longitude': TableTypeAnnotation.Type.NUMBER,
        'altitude': TableTypeAnnotation.Type.NUMBER,
        'timezone': TableTypeAnnotation.Type.NUMBER,
        'year built': TableTypeAnnotation.Type.NUMBER,
    }
def test_create_upload_model_invalid_columns(
    workspace: Workspace, user: User, authenticated_api_client
):
    workspace.set_user_permission(user, WorkspaceRoleChoice.WRITER)
    r: Response = authenticated_api_client.post(
        f'/api/workspaces/{workspace.name}/uploads/csv/',
        {
            # Not an issue to specify invalid field_value, as that's checked after columns,
            # so the request will return before that is checked
            'field_value': 'field_value',
            'edge': False,
            'table_name': 'table',
            'columns': {'foo': 'invalid'},
        },
        format='json',
    )

    assert r.status_code == 400
    assert r.json() == {'columns': {'foo': ['"invalid" is not a valid choice.']}}
Beispiel #14
0
def test_query_rest_retrieve(
    workspace: Workspace,
    user: User,
    authenticated_api_client: APIClient,
    valid_query,
    permission: WorkspaceRoleChoice,
    is_owner: bool,
    status_code: int,
    success: bool,
):
    if permission is not None:
        workspace.set_user_permission(user, permission)
    elif is_owner:
        workspace.set_owner(user)

    query_info = valid_query['response'].json()
    query_id = query_info['id']
    r: Response = authenticated_api_client.get(
        f'/api/workspaces/{workspace.name}/queries/{query_id}/')
    assert r.status_code == status_code
    if success:
        r_json = r.json()
        assert r_json['status'] == AqlQuery.Status.FINISHED