From 006389a1b67208e82ea5d5be13d790cd4c62322c Mon Sep 17 00:00:00 2001 From: Oleksandr Cherniavskyi Date: Thu, 10 Aug 2023 16:49:21 +0300 Subject: [PATCH] tests: TestTourStepCreate --- ckanext/tour/logic/action.py | 2 +- ckanext/tour/logic/auth.py | 1 + ckanext/tour/logic/schema.py | 3 +- ckanext/tour/tests/conftest.py | 27 +++++------ ckanext/tour/tests/logic/test_action.py | 62 +++++++++++++++++++++++-- 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/ckanext/tour/logic/action.py b/ckanext/tour/logic/action.py index b7b9f28..759b8f1 100644 --- a/ckanext/tour/logic/action.py +++ b/ckanext/tour/logic/action.py @@ -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 diff --git a/ckanext/tour/logic/auth.py b/ckanext/tour/logic/auth.py index c1fd55f..db217d4 100644 --- a/ckanext/tour/logic/auth.py +++ b/ckanext/tour/logic/auth.py @@ -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} diff --git a/ckanext/tour/logic/schema.py b/ckanext/tour/logic/schema.py index d99cb42..5b41ccc 100644 --- a/ckanext/tour/logic/schema.py +++ b/ckanext/tour/logic/schema.py @@ -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] @@ -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], } diff --git a/ckanext/tour/tests/conftest.py b/ckanext/tour/tests/conftest.py index 120620c..66aa0f3 100644 --- a/ckanext/tour/tests/conftest.py +++ b/ckanext/tour/tests/conftest.py @@ -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() @@ -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)) @@ -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 diff --git a/ckanext/tour/tests/logic/test_action.py b/ckanext/tour/tests/logic/test_action.py index f96acf2..c0292e0 100644 --- a/ckanext/tour/tests/logic/test_action.py +++ b/ckanext/tour/tests/logic/test_action.py @@ -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(