Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
smotornyuk committed Nov 22, 2019
1 parent 8a650ab commit 9097ebc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 30 deletions.
1 change: 1 addition & 0 deletions ckan/controllers/error.py
Expand Up @@ -11,6 +11,7 @@
from ckan.lib.base import render
from ckan.lib.helpers import literal


class ErrorController(BaseController):
"""Generates error documents as and when they are required.
Expand Down
14 changes: 9 additions & 5 deletions ckan/lib/helpers.py
Expand Up @@ -776,11 +776,11 @@ def _create_link_text(text, **kwargs):
return text

icon = kwargs.pop('icon', None)
class_ = _link_class(kwargs)
cls = _link_class(kwargs)
return link_to(
_create_link_text(text, **kwargs),
url_for(*args, **kwargs),
class_=class_
cls=cls
)


Expand All @@ -791,7 +791,11 @@ def _preprocess_dom_attrs(attrs):
like `class` that cannot be used because it special meaning in
Python.
"""
return {key.rstrip('_'): value for key, value in attrs.items()}
return {
key.rstrip('_'): value
for key, value in attrs.items()
if value is not None
}


def _make_safe_id_component(idstring):
Expand All @@ -806,7 +810,7 @@ def _make_safe_id_component(idstring):
Whitespace is transformed into underscores, and then
anything which is not a hyphen or a character that
matches \w (alphanumerics and underscore) is removed.
matches \\w (alphanumerics and underscore) is removed.
"""
# Transform all whitespace to underscore
Expand Down Expand Up @@ -1351,7 +1355,7 @@ def truncate(text, length=30, indicator='...', whole_word=False):
if i <= 0:
# Entire text before break is one word, or we miscalculated.
return text[:short_length] + indicator
return text[:i+1] + indicator
return text[:i + 1] + indicator


@core_helper
Expand Down
46 changes: 28 additions & 18 deletions ckan/lib/pagination.py
Expand Up @@ -145,7 +145,8 @@ def __init__(
self.page_count = ((self.item_count - 1) / self.items_per_page) + 1
self.last_page = self.first_page + self.page_count - 1

# Make sure that the requested page number is the range of valid pages
# Make sure that the requested page number is the range of
# valid pages
if self.page > self.last_page:
self.page = self.last_page
elif self.page < self.first_page:
Expand Down Expand Up @@ -241,8 +242,7 @@ def pager(
dotdot_attr={u"class": u"pager_dotdot"},
**kwargs
):
"""
Return string with links to other pages (e.g. "1 2 [3] 4 5 6 7").
"""Return string with links to other pages (e.g. "1 2 [3] 4 5 6 7").
format:
Format string that defines how the pager is rendered. The string
Expand Down Expand Up @@ -374,45 +374,53 @@ def pager(
Default: { 'class':'pager_dotdot' }
onclick (optional)
This paramter is a string containing optional Javascript code
that will be used as the 'onclick' action of each pager link.
It can be used to enhance your pager with AJAX actions loading another
page into a DOM object.
This paramter is a string containing optional Javascript
code that will be used as the 'onclick' action of each
pager link. It can be used to enhance your pager with
AJAX actions loading another page into a DOM object.
In this string the variable '$partial_url' will be replaced by
the URL linking to the desired page with an added 'partial=1'
parameter (or whatever you set 'partial_param' to).
In addition the '$page' variable gets replaced by the
respective page number.
Note that the URL to the destination page contains a 'partial_param'
parameter so that you can distinguish between AJAX requests (just
refreshing the paginated area of your page) and full requests (loading
the whole new page).
Note that the URL to the destination page contains a
'partial_param' parameter so that you can distinguish
between AJAX requests (just refreshing the paginated area
of your page) and full requests (loading the whole new
page).
[Backward compatibility: you can use '%s' instead of '$partial_url']
[Backward compatibility: you can use '%s' instead of
'$partial_url']
jQuery example:
"$('#my-page-area').load('$partial_url'); return false;"
Yahoo UI example:
"YAHOO.util.Connect.asyncRequest('GET','$partial_url',{
success:function(o){YAHOO.util.Dom.get('#my-page-area').innerHTML=o.responseText;}
},null); return false;"
success:function(o){
YAHOO.util.Dom.get(
'#my-page-area'
).innerHTML=o.responseText;
}
},null); return false;"
scriptaculous example:
"new Ajax.Updater('#my-page-area', '$partial_url',
{asynchronous:true, evalScripts:true}); return false;"
ExtJS example:
"Ext.get('#my-page-area').load({url:'$partial_url'}); return false;"
"Ext.get('#my-page-area').load({url:'$partial_url'});
return false;"
Custom example:
"my_load_page($page)"
Additional keyword arguments are used as arguments in the links.
Otherwise the link will be created with url_for() which points
to the page you are currently displaying.
"""
self.curpage_attr = curpage_attr
self.separator = separator
Expand All @@ -430,7 +438,7 @@ def pager(
return u""

# Replace ~...~ in token format by range of pages
result = re.sub(r"~(\d+)~", self._range, format)
result = re.sub(u"~(\\d+)~", self._range, format)

# Interpolate '%' variables
result = Template(result).safe_substitute(
Expand Down Expand Up @@ -460,7 +468,7 @@ def pager(

return Markup(result)

#### Private methods ####
# Private methods
def _range(self, regexp_match):
"""
Return range of linked pages (e.g. '1 2 [3] 4 5 6 7 8').
Expand Down Expand Up @@ -563,7 +571,9 @@ def _pagerlink(self, page, text):
# updates)
link_params[self.partial_param] = 1
partial_url = url_generator(**link_params)
try: # if '%s' is used in the 'onclick' parameter (backwards compatibility)
try:
# if '%s' is used in the 'onclick' parameter
# (backwards compatibility)
onclick_action = self.onclick % (partial_url,)
except TypeError:
onclick_action = Template(self.onclick).safe_substitute(
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/legacy/functional/api/test_package_search.py
Expand Up @@ -2,7 +2,7 @@

from six.moves.urllib.parse import quote

import webhelpers
from dominate.util import escape

import ckan.lib.search as search
from ckan.tests.legacy import setup_test_search_index
Expand Down Expand Up @@ -64,7 +64,7 @@ def test_04_post_json(self):
assert res_dict['result']['count'] == 1, res_dict['result']['count']

def test_06_uri_q_tags(self):
query = webhelpers.util.html_escape('annakarenina tags:russian tags:tolstoy')
query = escape('annakarenina tags:russian tags:tolstoy')
offset = self.base_url + '?q=%s' % query
res = self.app.get(offset, status=200)
res_dict = self.data_from_res(res)
Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/legacy/lib/test_helpers.py
Expand Up @@ -64,7 +64,7 @@ def test_time_ago_in_words_from_str(self):
two_months_ago = datetime.datetime.now() - datetime.timedelta(days=65)
two_months_ago_str = two_months_ago.isoformat()
res = h.time_ago_in_words_from_str(two_months_ago_str)
assert_equal(res, '2 months')
assert_equal(res, '2 months ago')

def test_gravatar(self):
email = 'zephod@gmail.com'
Expand Down
8 changes: 4 additions & 4 deletions ckan/tests/lib/test_helpers.py
Expand Up @@ -420,13 +420,13 @@ def test_internal_tag_link(self):
def test_internal_tag_linked_with_quotes(self):
"""Asserts links like 'tag:"test-tag"' work"""
data = 'tag:"test-tag" foobar'
output = '<p><a href="/dataset/?tags=test-tag">tag:&#34;test-tag&#34;</a> foobar</p>'
output = '<p><a href="/dataset/?tags=test-tag">tag:&quot;test-tag&quot;</a> foobar</p>'
eq_(h.render_markdown(data), output)

def test_internal_tag_linked_with_quotes_and_space(self):
"""Asserts links like 'tag:"test tag"' work"""
data = 'tag:"test tag" foobar'
output = '<p><a href="/dataset/?tags=test+tag">tag:&#34;test tag&#34;</a> foobar</p>'
output = '<p><a href="/dataset/?tags=test+tag">tag:&quot;test tag&quot;</a> foobar</p>'
eq_(h.render_markdown(data), output)

def test_internal_tag_with_no_opening_quote_only_matches_single_word(self):
Expand All @@ -450,7 +450,7 @@ def test_internal_tag_with_no_closing_quote_does_not_match(self):
def test_tag_names_match_simple_punctuation(self):
"""Asserts punctuation and capital letters are matched in the tag name"""
data = 'tag:"Test- _." foobar'
output = '<p><a href="/dataset/?tags=Test-+_.">tag:&#34;Test- _.&#34;</a> foobar</p>'
output = '<p><a href="/dataset/?tags=Test-+_.">tag:&quot;Test- _.&quot;</a> foobar</p>'
eq_(h.render_markdown(data), output)

def test_tag_names_do_not_match_commas(self):
Expand All @@ -471,7 +471,7 @@ def test_tag_names_dont_match_non_space_whitespace(self):
def test_tag_names_with_unicode_alphanumeric(self):
"""Asserts that unicode alphanumeric characters are captured"""
data = u'tag:"Japanese katakana \u30a1" blah'
output = u'<p><a href="/dataset/?tags=Japanese+katakana+%E3%82%A1">tag:&#34;Japanese katakana \u30a1&#34;</a> blah</p>'
output = u'<p><a href="/dataset/?tags=Japanese+katakana+%E3%82%A1">tag:&quot;Japanese katakana \u30a1&quot;</a> blah</p>'
eq_(h.render_markdown(data), output)

def test_normal_link(self):
Expand Down

0 comments on commit 9097ebc

Please sign in to comment.