Skip to content

Commit

Permalink
tests: TestTourStepCreate
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Aug 10, 2023
1 parent 084fc2a commit 006389a
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
2 changes: 1 addition & 1 deletion ckanext/tour/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def tour_show(context, data_dict):
tk.check_access("tour_show", context, data_dict)

return tour_model.Tour.get(data_dict["id"]).dictize(context) # type: ignore
return tour_model.Tour.get(data_dict["id"]).dictize(context) # type: ignore


@tk.side_effect_free
Expand Down
1 change: 1 addition & 0 deletions ckanext/tour/logic/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ def tour_remove(context, data_dict):
def tour_list(context, data_dict):
return {"success": False}


def tour_show(context, data_dict):
return {"success": False}
3 changes: 2 additions & 1 deletion ckanext/tour/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def tour_create(

@validator_args
def tour_step_schema(
not_empty, ignore_missing, unicode_safe, default, one_of, tour_tour_exist
not_empty, ignore, ignore_missing, unicode_safe, default, one_of, tour_tour_exist
) -> Schema:
image_schema = tour_step_image_schema()
image_schema["tour_step_id"] = [ignore_missing]
Expand All @@ -58,6 +58,7 @@ def tour_step_schema(
"url": [ignore_missing, unicode_safe],
"image": image_schema,
"tour_id": [not_empty, unicode_safe, tour_tour_exist],
"__extras": [ignore],
}


Expand Down
27 changes: 12 additions & 15 deletions ckanext/tour/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ckan.tests import factories

import ckanext.tour.tests.factories as tour_factories
from ckanext.tour.tests.helpers import CSV_DATA, FakeFileStorage

fake = Faker()

Expand Down Expand Up @@ -38,7 +39,7 @@ class SysadminFactory(factories.Sysadmin):


@pytest.fixture
def validation_setup(monkeypatch, ckan_config, tmpdir):
def mock_storage(monkeypatch, ckan_config, tmpdir):
monkeypatch.setitem(ckan_config, "ckan.storage_path", str(tmpdir))
monkeypatch.setattr(uploader, "get_storage_path", lambda: str(tmpdir))

Expand Down Expand Up @@ -67,20 +68,16 @@ def validation_setup(monkeypatch, ckan_config, tmpdir):
# yield _prepare_data


# @pytest.fixture
# def rd_study_file_data():
# def _prepare_data(**kwargs):
# data = {
# "entity_type": rd_model.DataRequestFile.Entity.data_request,
# "entity_id": None,
# "mimetype": "text/csv",
# "name": "data.csv",
# "upload": FakeFileStorage(BytesIO(CSV_DATA), "data.csv"),
# "data_request": None,
# }
@pytest.fixture
def tour_image_data():
def _prepare_data(**kwargs):
data = {
"upload": FakeFileStorage(BytesIO(CSV_DATA), "data.csv"),
"url": None,
}

# data.update(**kwargs)
data.update(**kwargs)

# return data
return data

# yield _prepare_data
yield _prepare_data
62 changes: 59 additions & 3 deletions ckanext/tour/tests/logic/test_action.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,72 @@
from turtle import position
import pytest

import ckan.plugins.toolkit as tk
from ckan.tests.helpers import call_action

import ckanext.tour.model as tour_model

@pytest.mark.usefixtures("with_plugins", "clean_db", "validation_setup")

@pytest.mark.usefixtures("with_plugins", "clean_db", "mock_storage")
class TestTourCreate:
def test_basic_create(self, tour_factory):
tour = tour_factory()

pass

assert tour["id"]
assert tour["anchor"]
assert tour["author_id"]
assert tour["created_at"]
assert tour["modified_at"]
assert tour["title"]
assert tour["page"] is None
assert tour["state"] == tour_model.Tour.State.active
assert tour["steps"][0]["id"]
assert tour["steps"][0]["element"]
assert tour["steps"][0]["image"]
assert tour["steps"][0]["intro"]
assert tour["steps"][0]["position"]
assert tour["steps"][0]["title"]
assert tour["steps"][0]["tour_id"] == tour["id"]


@pytest.mark.usefixtures("with_plugins", "clean_db", "mock_storage")
class TestTourStepCreate:
def test_basic_create(self, tour_factory, tour_step_factory):
tour = tour_factory(steps=[])
tour_step = tour_step_factory(tour_id=tour["id"])

tour = call_action("tour_show", id=tour["id"])

assert tour["steps"][0]["id"] == tour_step["id"]

def test_wrong_position(self, tour_factory, tour_step_factory):
tour = tour_factory(steps=[])

with pytest.raises(tk.ValidationError, match="Value must be one of"):
tour_step_factory(tour_id=tour["id"], position="xxx")

def test_upload_image(self, tour_factory, tour_step_factory, tour_image_data):
tour = tour_factory(steps=[])

assert tour_step_factory(tour_id=tour["id"], image=[tour_image_data()])

def test_upload_multiple_image(
self, tour_factory, tour_step_factory, tour_image_data
):
tour = tour_factory(steps=[])

with pytest.raises(tk.ValidationError, match="only 1 image for step allowed"):
tour_step_factory(
tour_id=tour["id"], image=[tour_image_data(), tour_image_data()]
)

def test_missing_element(
self, tour_factory, tour_step_factory, tour_image_data
):
tour = tour_factory(steps=[])

with pytest.raises(tk.ValidationError, match="Missing value"):
tour_step_factory(tour_id=tour["id"], element=None)

# def test_create_without_studies(self, user):
# with pytest.raises(
Expand Down

0 comments on commit 006389a

Please sign in to comment.