Skip to content

Commit

Permalink
fix: allow set API_PREFIX for backend routes
Browse files Browse the repository at this point in the history
  • Loading branch information
VDigitall committed Apr 14, 2021
1 parent 298f652 commit c60c454
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 65 deletions.
1 change: 1 addition & 0 deletions .envrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export POSTGRES_DB=spoonbill
export POSTGRES_USER=spoonbilluser
export POSTGRES_PASSWORD=spoonbillpwd
export DB_HOST=127.0.0.1
export API_PREFIX=api/
62 changes: 32 additions & 30 deletions core/tests/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@

@pytest.mark.django_db
class TestUpload:
url_prefix = "/uploads/" if not settings.API_PREFIX else f"/{settings.API_PREFIX}uploads/"

def test_create_upload_wo_file(self, client):
response = client.post("/uploads/", {"attr": "value"})
response = client.post(self.url_prefix, {"attr": "value"})
assert response.status_code == 400
assert response.json() == {"detail": "File is required"}

def test_create_upload_successful(self, client, dataset, cleanup_upload_task, validation_task):
response = client.post("/uploads/", {"file": dataset})
response = client.post(self.url_prefix, {"file": dataset})
assert response.status_code == 201
upload = response.json()
assert set(upload.keys()) == {
Expand All @@ -43,127 +45,127 @@ def test_create_upload_successful(self, client, dataset, cleanup_upload_task, va
shutil.rmtree(f"{settings.MEDIA_ROOT}{upload_obj.id}")

def test_get_non_existed_upload(self, client):
response = client.get("/uploads/some-invalid-id/")
response = client.get(f"{self.url_prefix}some-invalid-id/")
assert response.status_code == 404
assert response.json() == {"detail": "Not found."}

def test_get_upload_successful(self, client, upload_obj):
response = client.get(f"/uploads/{upload_obj.id}/")
response = client.get(f"{self.url_prefix}{upload_obj.id}/")
assert response.status_code == 200
assert UploadSerializer(upload_obj).data == response.json()

def test_create_selections_successful(self, client, upload_obj):
create_data_selection(client, upload_obj, "uploads")
create_data_selection(client, upload_obj, self.url_prefix)

def test_get_selections_successful(self, client, upload_obj):
get_data_selections(client, upload_obj, "uploads")
get_data_selections(client, upload_obj, self.url_prefix)

def test_delete_table(self, client, upload_obj_validated):
selection = create_data_selection(client, upload_obj_validated, "uploads")
response = client.get(f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/")
selection = create_data_selection(client, upload_obj_validated, self.url_prefix)
response = client.get(f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/")
assert len(response.json()) == 2

table_data = response.json()[0]
assert table_data["include"]

response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{table_data['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{table_data['id']}/",
content_type="application/json",
data={"include": False},
)
assert response.status_code == 200
assert not response.json()["include"]

response = client.get(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{table_data['id']}/"
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{table_data['id']}/"
)
assert not response.json()["include"]

def test_list_tables(self, client, upload_obj):
selection = create_data_selection(client, upload_obj, "uploads")
response = client.get(f"/uploads/{upload_obj.id}/selections/{selection['id']}/tables/")
selection = create_data_selection(client, upload_obj, self.url_prefix)
response = client.get(f"{self.url_prefix}{upload_obj.id}/selections/{selection['id']}/tables/")
assert len(response.json()) == 2

def test_table_preview(self, client, upload_obj_validated):
selection = create_data_selection(client, upload_obj_validated, "uploads")
tables = client.get(f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, upload_obj_validated, self.url_prefix)
tables = client.get(f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()

response = client.get(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 1
data = response.json()[0]
assert set(data.keys()) == {"id", "name", "preview"}

def test_table_r_friendly_preview(self, client, upload_obj_validated):
selection = create_data_selection(client, upload_obj_validated, "uploads")
tables = client.get(f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, upload_obj_validated, self.url_prefix)
tables = client.get(f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()
response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/",
data={"headings_type": "es_r_friendly"},
content_type="application/json",
)

response = client.get(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 1
data = response.json()[0]
assert set(data.keys()) == {"id", "name", "preview", "headings"}

def test_table_split_preview(self, client, upload_obj_validated):
selection = create_data_selection(client, upload_obj_validated, "uploads")
tables = client.get(f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, upload_obj_validated, self.url_prefix)
tables = client.get(f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()

response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
data={"split": True},
content_type="application/json",
)
assert response.status_code == 200

response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/",
data={"headings_type": "es_r_friendly"},
content_type="application/json",
)
assert response.status_code == 200

response = client.get(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 3
data = response.json()[0]
assert set(data.keys()) == {"id", "name", "preview", "headings"}

def test_table_split_include_preview(self, client, upload_obj_validated):
selection = create_data_selection(client, upload_obj_validated, "uploads")
tables = client.get(f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, upload_obj_validated, self.url_prefix)
tables = client.get(f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/").json()

response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
data={"split": True},
content_type="application/json",
)
assert response.status_code == 200
array_tables = response.json()["array_tables"]

response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/",
data={"headings_type": "es_r_friendly"},
content_type="application/json",
)
assert response.status_code == 200

response = client.patch(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{array_tables[0]['id']}/",
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{array_tables[0]['id']}/",
data={"include": False},
content_type="application/json",
)
assert response.status_code == 200

response = client.get(
f"/uploads/{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{upload_obj_validated.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 2
data = response.json()[0]
Expand Down
65 changes: 35 additions & 30 deletions core/tests/test_urls.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
from django.conf import settings

from core.models import Url
from core.serializers import UrlSerializer
Expand All @@ -8,13 +9,15 @@

@pytest.mark.django_db
class TestUrl:
url_prefix = "/urls/" if not settings.API_PREFIX else f"/{settings.API_PREFIX}urls/"

def test_create_datasource_wo_url(self, client):
response = client.post("/urls/", {"attr": "value"})
response = client.post(f"{self.url_prefix}", {"attr": "value"})
assert response.status_code == 400
assert response.json() == {"detail": "Url is required"}

def test_create_datasource_successful(self, client, download_datasource_task):
response = client.post("/urls/", {"url": "https://example.org/dataset.json"})
response = client.post(f"{self.url_prefix}", {"url": "https://example.org/dataset.json"})
assert response.status_code == 201
url = response.json()
assert set(url.keys()) == {
Expand All @@ -41,125 +44,127 @@ def test_create_datasource_successful(self, client, download_datasource_task):
download_datasource_task.delay.assert_called_once_with(url_obj.id, model="Url")

def test_get_non_existed_datasource(self, client):
response = client.get("/urls/some-invalid-id/")
response = client.get(f"{self.url_prefix}some-invalid-id/")
assert response.status_code == 404
assert response.json() == {"detail": "Not found."}

def test_get_url_successful(self, client, url_obj):
response = client.get(f"/urls/{url_obj.id}/")
response = client.get(f"{self.url_prefix}{url_obj.id}/")
assert response.status_code == 200
assert UrlSerializer(url_obj).data == response.json()

def test_create_selections_successful(self, client, url_obj):
create_data_selection(client, url_obj, prefix="urls")
create_data_selection(client, url_obj, prefix=self.url_prefix)

def test_get_selections_successful(self, client, url_obj):
get_data_selections(client, url_obj, "urls")
get_data_selections(client, url_obj, self.url_prefix)

def test_delete_table(self, client, url_obj_w_files):
selection = create_data_selection(client, url_obj_w_files, "urls")
response = client.get(f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/")
selection = create_data_selection(client, url_obj_w_files, self.url_prefix)
response = client.get(f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/")
assert len(response.json()) == 2

table_data = response.json()[0]
assert table_data["include"]

response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{table_data['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{table_data['id']}/",
content_type="application/json",
data={"include": False},
)
assert response.status_code == 200
assert not response.json()["include"]

response = client.get(f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{table_data['id']}/")
response = client.get(
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{table_data['id']}/"
)
assert not response.json()["include"]

def test_list_tables(self, client, url_obj):
selection = create_data_selection(client, url_obj, "urls")
response = client.get(f"/urls/{url_obj.id}/selections/{selection['id']}/tables/")
selection = create_data_selection(client, url_obj, self.url_prefix)
response = client.get(f"{self.url_prefix}{url_obj.id}/selections/{selection['id']}/tables/")
assert len(response.json()) == 2

def test_table_preview(self, client, url_obj_w_files):
selection = create_data_selection(client, url_obj_w_files, "urls")
tables = client.get(f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, url_obj_w_files, self.url_prefix)
tables = client.get(f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()

response = client.get(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 1
data = response.json()[0]
assert set(data.keys()) == {"id", "name", "preview"}

def test_table_r_friendly_preview(self, client, url_obj_w_files):
selection = create_data_selection(client, url_obj_w_files, "urls")
tables = client.get(f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, url_obj_w_files, self.url_prefix)
tables = client.get(f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()
response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/",
data={"headings_type": "es_r_friendly"},
content_type="application/json",
)

response = client.get(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 1
data = response.json()[0]
assert set(data.keys()) == {"id", "name", "preview", "headings"}

def test_table_split_preview(self, client, url_obj_w_files):
selection = create_data_selection(client, url_obj_w_files, "urls")
tables = client.get(f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, url_obj_w_files, self.url_prefix)
tables = client.get(f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()

response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
data={"split": True},
content_type="application/json",
)
assert response.status_code == 200

response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/",
data={"headings_type": "es_r_friendly"},
content_type="application/json",
)
assert response.status_code == 200

response = client.get(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 3
data = response.json()[0]
assert set(data.keys()) == {"id", "name", "preview", "headings"}

def test_table_split_include_preview(self, client, url_obj_w_files):
selection = create_data_selection(client, url_obj_w_files, "urls")
tables = client.get(f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()
selection = create_data_selection(client, url_obj_w_files, self.url_prefix)
tables = client.get(f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/").json()

response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/",
data={"split": True},
content_type="application/json",
)
assert response.status_code == 200
array_tables = response.json()["array_tables"]

response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/",
data={"headings_type": "es_r_friendly"},
content_type="application/json",
)
assert response.status_code == 200

response = client.patch(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{array_tables[0]['id']}/",
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{array_tables[0]['id']}/",
data={"include": False},
content_type="application/json",
)
assert response.status_code == 200

response = client.get(
f"/urls/{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
f"{self.url_prefix}{url_obj_w_files.id}/selections/{selection['id']}/tables/{tables[0]['id']}/preview/"
)
assert len(response.json()) == 2
data = response.json()[0]
Expand Down
6 changes: 3 additions & 3 deletions core/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def reason(self):


def create_data_selection(client, parent, prefix=None):
url = f"/{prefix}/{parent.id}/selections/"
url = f"{prefix}{parent.id}/selections/"
response = client.post(url, content_type="application/json", data=data_selection)
assert response.status_code == 201
json_data = response.json()
Expand All @@ -57,15 +57,15 @@ def get_data_selections(client, parent, prefix=None):
json_data = create_data_selection(client, parent, prefix)

# get list of selections
url = f"/{prefix}/{parent.id}/selections/"
url = f"{prefix}{parent.id}/selections/"
response = client.get(url)
assert response.status_code == 200
json_resp = response.json()
assert len(json_resp) == 1
assert json_resp[0] == json_data

# get single selection by id
url = f"/{prefix}/{parent.id}/selections/{json_data['id']}/"
url = f"{prefix}{parent.id}/selections/{json_data['id']}/"
response = client.get(url)
assert response.status_code == 200
json_resp = response.json()
Expand Down

0 comments on commit c60c454

Please sign in to comment.