Skip to content

Commit

Permalink
Fix ckanext tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Jun 2, 2016
1 parent 878394b commit 3193049
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 107 deletions.
6 changes: 4 additions & 2 deletions ckanext/example_idatasetform/tests/test_controllers.py
@@ -1,5 +1,5 @@
from nose.tools import assert_equal
from routes import url_for
from ckan.lib.helpers import url_for

import ckan.plugins as plugins
import ckan.tests.helpers as helpers
Expand All @@ -12,8 +12,10 @@
def _get_package_edit_page(app, package_name):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
with app.flask_app.test_request_context():
url = url_for(controller='package', action='edit', id=package_name)
response = app.get(
url=url_for(controller='package', action='edit', id=package_name),
url,
extra_environ=env,
)
return env, response
Expand Down
57 changes: 38 additions & 19 deletions ckanext/example_igroupform/tests/test_controllers.py
@@ -1,5 +1,5 @@
from nose.tools import assert_equal
from routes import url_for
from ckan.lib.helpers import url_for

import ckan.plugins as plugins
import ckan.tests.helpers as helpers
Expand All @@ -17,8 +17,10 @@
def _get_group_new_page(app, group_type):
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
with app.flask_app.test_request_context():
url = url_for('%s_new' % group_type)
response = app.get(
url_for('%s_new' % group_type),
url,
extra_environ=env,
)
return env, response
Expand All @@ -41,7 +43,9 @@ def test_about(self):
group = factories.Group(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_about' % custom_group_type,

with app.flask_app.test_request_context():
url = url_for('%s_about' % custom_group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
response.mustcontain(group_name)
Expand All @@ -52,8 +56,10 @@ def test_bulk_process(self):
group = factories.Group(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_bulk_process' % custom_group_type,
id=group_name)

with app.flask_app.test_request_context():
url = url_for('%s_bulk_process' % custom_group_type,
id=group_name)
try:
response = app.get(url=url, extra_environ=env)
except Exception as e:
Expand All @@ -67,8 +73,10 @@ def test_delete(self):
group = factories.Group(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_action' % custom_group_type, action='delete',
id=group_name)

with app.flask_app.test_request_context():
url = url_for('%s_action' % custom_group_type, action='delete',
id=group_name)
response = app.get(url=url, extra_environ=env)


Expand All @@ -89,8 +97,10 @@ def test_about(self):
group = factories.Organization(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_about' % custom_group_type,
id=group_name)

with app.flask_app.test_request_context():
url = url_for('%s_about' % custom_group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
response.mustcontain(group_name)

Expand All @@ -100,8 +110,10 @@ def test_bulk_process(self):
group = factories.Organization(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_bulk_process' % custom_group_type,
id=group_name)

with app.flask_app.test_request_context():
url = url_for('%s_bulk_process' % custom_group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)

def test_delete(self):
Expand All @@ -110,8 +122,10 @@ def test_delete(self):
group = factories.Organization(user=user, type=custom_group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_action' % custom_group_type, action='delete',
id=group_name)

with app.flask_app.test_request_context():
url = url_for('%s_action' % custom_group_type, action='delete',
id=group_name)
response = app.get(url=url, extra_environ=env)


Expand Down Expand Up @@ -193,8 +207,9 @@ def _get_group_edit_page(app, group_type, group_name=None):
group = factories.Group(user=user, type=group_type)
group_name = group['name']
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % group_type,
id=group_name)
with app.flask_app.test_request_context():
url = url_for('%s_edit' % group_type,
id=group_name)
response = app.get(url=url, extra_environ=env)
return env, response, group_name

Expand All @@ -214,8 +229,10 @@ def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % custom_group_type,
id='doesnt_exist')

with app.flask_app.test_request_context():
url = url_for('%s_edit' % custom_group_type,
id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)

Expand Down Expand Up @@ -255,8 +272,10 @@ def test_group_doesnt_exist(self):
app = self._get_test_app()
user = factories.User()
env = {'REMOTE_USER': user['name'].encode('ascii')}
url = url_for('%s_edit' % group_type,
id='doesnt_exist')

with app.flask_app.test_request_context():
url = url_for('%s_edit' % group_type,
id='doesnt_exist')
app.get(url=url, extra_environ=env,
status=404)

Expand Down
54 changes: 33 additions & 21 deletions ckanext/example_itranslation/tests/test_plugin.py
Expand Up @@ -17,48 +17,60 @@ def teardown_class(cls):

def test_translated_string_in_extensions_templates(self):
app = self._get_test_app()
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index',
locale='fr'),
)

with app.flask_app.test_request_context():
url = plugins.toolkit.url_for(controller='home', action='index',
locale='fr')

response = app.get(url)
assert_true('This is a itranslated string' in response.body)
assert_false('This is an untranslated string' in response.body)

# double check the untranslated strings
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index'),
)

with app.flask_app.test_request_context():
url = plugins.toolkit.url_for(controller='home', action='index')

response = app.get(url)
assert_true('This is an untranslated string' in response.body)
assert_false('This is a itranslated string' in response.body)

def test_translated_string_in_core_templates(self):
app = self._get_test_app()
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index',
locale='fr'),
)

with app.flask_app.test_request_context():
url = plugins.toolkit.url_for(controller='home', action='index',
locale='fr')

response = app.get(url)
assert_true('Overwritten string in ckan.mo' in response.body)
assert_false('Connexion' in response.body)

# double check the untranslated strings
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index'),
)

with app.flask_app.test_request_context():
url = plugins.toolkit.url_for(controller='home', action='index')

response = app.get(url)
assert_true('Log in' in response.body)
assert_false('Overwritten string in ckan.mo' in response.body)

# check that we have only overwritten 'fr'
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index',
locale='de'),
)

with app.flask_app.test_request_context():
url = plugins.toolkit.url_for(controller='home', action='index',
locale='de')

response = app.get(url)
assert_true('Einloggen' in response.body)
assert_false('Overwritten string in ckan.mo' in response.body)

def test_english_translation_replaces_default_english_string(self):
app = self._get_test_app()
response = app.get(
url=plugins.toolkit.url_for(controller='home', action='index'),
)

with app.flask_app.test_request_context():
url = plugins.toolkit.url_for(controller='home', action='index')

response = app.get(url)
assert_true('Replaced' in response.body)
assert_false('Register' in response.body)
7 changes: 4 additions & 3 deletions ckanext/imageview/tests/test_view.py
@@ -1,4 +1,4 @@
from routes import url_for
from ckan.lib.helpers import url_for

import ckan.plugins as p

Expand Down Expand Up @@ -37,8 +37,9 @@ def test_view_shown_on_resource_page_with_image_url(self):
resource_id=resource['id'],
image_url='http://some.image.png')

url = url_for(controller='package', action='resource_read',
id=dataset['name'], resource_id=resource['id'])
with app.flask_app.test_request_context():
url = url_for(controller='package', action='resource_read',
id=dataset['name'], resource_id=resource['id'])

response = app.get(url)

Expand Down
58 changes: 30 additions & 28 deletions ckanext/multilingual/tests/test_multilingual_plugin.py
@@ -1,12 +1,13 @@
import ckan.plugins as p
import ckanext.multilingual.plugin as mulilingual_plugin
import ckan.lib.helpers

from ckan.lib.helpers import url_for
import ckan.lib.create_test_data
import ckan.logic.action.update
import ckan.model as model
import ckan.tests.legacy
import ckan.tests.legacy.html_check
import routes
from ckan.tests import helpers
import paste.fixture
import pylons.test

Expand All @@ -16,8 +17,8 @@
class TestDatasetTermTranslation(ckan.tests.legacy.html_check.HtmlCheckMethods):
'Test the translation of datasets by the multilingual_dataset plugin.'
@classmethod
def setup(cls):
cls.app = paste.fixture.TestApp(pylons.test.pylonsapp)
def setup_class(cls):
cls.app = helpers._get_test_app()

if not p.plugin_loaded('multilingual_dataset'):
p.load('multilingual_dataset')
Expand Down Expand Up @@ -59,7 +60,7 @@ def setup(cls):
context, data_dict)

@classmethod
def teardown(cls):
def teardown_class(cls):
p.unload('multilingual_dataset')
p.unload('multilingual_group')
p.unload('multilingual_tag')
Expand All @@ -74,29 +75,30 @@ def test_user_read_translation(self):

# It is testsysadmin who created the dataset, so testsysadmin whom
# we'd expect to see the datasets for.
for user_name in ('testsysadmin',):
offset = routes.url_for(
controller='user', action='read', id=user_name)
for (lang_code, translations) in (
('de', _create_test_data.german_translations),
('fr', _create_test_data.french_translations),
('en', _create_test_data.english_translations),
('pl', {})):
response = self.app.get(
offset,
status=200,
extra_environ={'CKAN_LANG': lang_code,
'CKAN_CURRENT_URL': offset})
terms = ('A Novel By Tolstoy')
for term in terms:
if term in translations:
assert translations[term] in response, response
elif term in _create_test_data.english_translations:
assert (_create_test_data.english_translations[term]
in response)
else:
assert term in response
assert 'this should not be rendered' not in response
with self.app.flask_app.test_request_context():
for user_name in ('testsysadmin',):
offset = url_for(
controller='user', action='read', id=user_name)
for (lang_code, translations) in (
('de', _create_test_data.german_translations),
('fr', _create_test_data.french_translations),
('en', _create_test_data.english_translations),
('pl', {})):
response = self.app.get(
offset,
status=200,
extra_environ={'CKAN_LANG': lang_code,
'CKAN_CURRENT_URL': offset})
terms = ('A Novel By Tolstoy')
for term in terms:
if term in translations:
assert translations[term] in response, response
elif term in _create_test_data.english_translations:
assert (_create_test_data.english_translations[term]
in response)
else:
assert term in response
assert 'this should not be rendered' not in response

def test_org_read_translation(self):
for (lang_code, translations) in (
Expand Down

0 comments on commit 3193049

Please sign in to comment.