Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Feb 12, 2024
1 parent 6654fe6 commit 49ebdc2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 65 deletions.
23 changes: 20 additions & 3 deletions ckanext/scheming/tests/test_arbitrary_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from flask import render_template
from bs4 import BeautifulSoup

from ckanext.scheming.helpers import scheming_get_arbitrary_schema
import ckan.plugins.toolkit as tk


class TestArbitrarySchema:
def test_arbitrary_schema_structure(self):
schema = scheming_get_arbitrary_schema("ckanext_notifier")
schema = tk.h.scheming_get_arbitrary_schema("ckanext_notifier")

assert schema["scheming_version"]
assert schema["schema_id"] == "ckanext_notifier"
Expand All @@ -16,7 +16,7 @@ def test_arbitrary_schema_structure(self):

@pytest.mark.usefixtures("with_request_context")
def test_render_arbitrary_schema(self, app):
schema = scheming_get_arbitrary_schema("ckanext_notifier")
schema = tk.h.scheming_get_arbitrary_schema("ckanext_notifier")

result = render_template(
"scheming/snippets/render_fields.html",
Expand All @@ -28,3 +28,20 @@ def test_render_arbitrary_schema(self, app):
soup = BeautifulSoup(result)

assert len(soup.select("div.form-group")) == 3


@pytest.mark.usefixtures("with_plugins")
class TestGetArbitrarySchemaHelper:
def test_get_all_arbitrary_schemas(self):
assert tk.h.scheming_arbitrary_schemas()

@pytest.mark.ckan_config("scheming.arbitrary_schemas", "")
def test_get_all_arbitrary_schemas_if_none(self):
assert not tk.h.scheming_arbitrary_schemas()

def test_get_specific_schema(self):
assert tk.h.scheming_get_arbitrary_schema("ckanext_notifier")

@pytest.mark.ckan_config("scheming.arbitrary_schemas", "")
def test_get_specific_schema_if_none(self):
assert not tk.h.scheming_get_arbitrary_schema("ckanext_notifier")
112 changes: 50 additions & 62 deletions ckanext/scheming/tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from mock import patch, Mock

import datetime

import pytest
import six

from ckanext.scheming.helpers import (
Expand All @@ -17,8 +15,6 @@
scheming_get_presets,
scheming_datastore_choices,
scheming_display_json_value,
scheming_get_arbitrary_schema,
scheming_arbitrary_schemas,
)

from ckanapi import NotFound
Expand All @@ -31,7 +27,9 @@ def test_pass_through_gettext(self, _):
assert "hello1" == scheming_language_text("hello")

def test_only_one_language(self):
assert "hello" == scheming_language_text({"zh": "hello"}, prefer_lang="en")
assert "hello" == scheming_language_text(
{"zh": "hello"}, prefer_lang="en"
)

def test_matching_language(self):
assert "hello" == scheming_language_text(
Expand All @@ -44,7 +42,7 @@ def test_first_when_no_matching_language(self):
)

def test_decodes_utf8(self):
assert "\xa1Hola!" == scheming_language_text(six.b("\xc2\xa1Hola!"))
assert u"\xa1Hola!" == scheming_language_text(six.b("\xc2\xa1Hola!"))

@patch("ckanext.scheming.helpers.lang")
def test_no_user_lang(self, lang):
Expand All @@ -71,35 +69,35 @@ def test_scheming_get_presets(self):
presets = scheming_get_presets()
assert sorted(
(
"title",
"tag_string_autocomplete",
"select",
"resource_url_upload",
"organization_url_upload",
"resource_format_autocomplete",
"multiple_select",
"multiple_checkbox",
"multiple_text",
"date",
"datetime",
"datetime_tz",
"dataset_slug",
"dataset_organization",
"json_object",
"markdown",
"radio",
u'title',
u'tag_string_autocomplete',
u'select',
u'resource_url_upload',
u'organization_url_upload',
u'resource_format_autocomplete',
u'multiple_select',
u'multiple_checkbox',
u'multiple_text',
u'date',
u'datetime',
u'datetime_tz',
u'dataset_slug',
u'dataset_organization',
u'json_object',
u'markdown',
u'radio',
)
) == sorted(presets.keys())

def test_scheming_get_preset(self):
preset = scheming_get_preset("date")
preset = scheming_get_preset(u"date")
assert sorted(
(
("display_snippet", "date.html"),
("form_snippet", "date.html"),
(u"display_snippet", u"date.html"),
(u"form_snippet", u"date.html"),
(
"validators",
"scheming_required isodate convert_to_json_if_date",
u"validators",
u"scheming_required isodate convert_to_json_if_date",
),
)
) == sorted(preset.items())
Expand All @@ -112,7 +110,9 @@ def test_no_choices_on_not_found(self, LocalCKAN):
lc.action.datastore_search.side_effect = NotFound()
LocalCKAN.return_value = lc
assert (
scheming_datastore_choices({"datastore_choices_resource": "not-found"})
scheming_datastore_choices(
{"datastore_choices_resource": "not-found"}
)
== []
)
lc.action.datastore_search.assert_called_once()
Expand All @@ -123,7 +123,9 @@ def test_no_choices_on_not_authorized(self, LocalCKAN):
lc.action.datastore_search.side_effect = NotFound()
LocalCKAN.return_value = lc
assert (
scheming_datastore_choices({"datastore_choices_resource": "not-allowed"})
scheming_datastore_choices(
{"datastore_choices_resource": "not-allowed"}
)
== []
)
lc.action.datastore_search.assert_called_once()
Expand All @@ -134,7 +136,9 @@ def test_no_choices_on_not_authorized(self, LocalCKAN):
lc.action.datastore_search.side_effect = NotFound()
LocalCKAN.return_value = lc
assert (
scheming_datastore_choices({"datastore_choices_resource": "not-allowed"})
scheming_datastore_choices(
{"datastore_choices_resource": "not-allowed"}
)
== []
)
lc.action.datastore_search.assert_called_once()
Expand Down Expand Up @@ -171,10 +175,9 @@ def test_call_with_all_params(self, LocalCKAN):
"datastore_choices_resource": "all-params",
"datastore_choices_limit": 5,
"datastore_choices_columns": {"value": "a", "label": "b"},
"datastore_additional_choices": [
{"value": "none", "label": "None"},
{"value": "na", "label": "N/A"},
],
"datastore_additional_choices":
[{"value": "none", "label": "None"},
{"value": "na", "label": "N/A"}]
}
) == [
{"value": "none", "label": "None"},
Expand All @@ -191,56 +194,41 @@ def test_call_with_all_params(self, LocalCKAN):

class TestJSONHelpers(object):
def test_display_json_value_default(self):

value = {"a": "b"}

assert scheming_display_json_value(value) == '{\n "a": "b"\n}'

def test_display_json_value_indent(self):

value = {"a": "b"}

assert scheming_display_json_value(value, indent=4) == '{\n "a": "b"\n}'
assert (
scheming_display_json_value(value, indent=4)
== '{\n "a": "b"\n}'
)

def test_display_json_value_no_indent(self):

value = {"a": "b"}

assert scheming_display_json_value(value, indent=None) == '{"a": "b"}'

def test_display_json_value_keys_are_sorted(self):

value = {"c": "d", "a": "b"}
if six.PY3:
expected = '{\n "a": "b",\n "c": "d"\n}'
else:
expected = '{\n "a": "b", \n "c": "d"\n}'

assert scheming_display_json_value(value, indent=4) == expected
assert (
scheming_display_json_value(value, indent=4) == expected
)

def test_display_json_value_json_error(self):

date = datetime.datetime.now()
value = ("a", date)

assert scheming_display_json_value(value) == ("a", date)


@pytest.mark.usefixtures("with_plugins")
class TestGetArbitrarySchemaHelper(object):
def test_get_all_arbitrary_schemas(self):
schemas = scheming_arbitrary_schemas()

assert schemas

@pytest.mark.ckan_config("scheming.arbitrary_schemas", "")
def test_get_all_arbitrary_schemas_if_none(self):
schemas = scheming_arbitrary_schemas()

assert not schemas

def test_get_specific_schema(self):
schema = scheming_get_arbitrary_schema("ckanext_notifier")

assert schema

@pytest.mark.ckan_config("scheming.arbitrary_schemas", "")
def test_get_specific_schema_if_none(self):
schema = scheming_get_arbitrary_schema("ckanext_notifier")

assert not schema

0 comments on commit 49ebdc2

Please sign in to comment.