Skip to content

Commit

Permalink
First go at fixing the legacy tests
Browse files Browse the repository at this point in the history
Wrapping calls to url_for (and functions that call it firther down the
line) with the Flask test_request_context, update functional tests to
use the helpers._get_test_app function rather than old paster ones.
Still some failures, related to

* Template rendering
* Request object
* PEP8
  • Loading branch information
amercader committed Jun 2, 2016
1 parent a41fd3b commit 878394b
Show file tree
Hide file tree
Showing 21 changed files with 417 additions and 164 deletions.
5 changes: 4 additions & 1 deletion ckan/tests/legacy/__init__.py
Expand Up @@ -33,6 +33,7 @@
import ckan.model as model
from ckan import ckan_nose_plugin
from ckan.common import json
from ckan.tests import helpers

# evil hack as url_for is passed out
url_for = h.url_for
Expand Down Expand Up @@ -235,7 +236,8 @@ class WsgiAppCase(BaseCase):
# Either that, or this file got imported somehow before the tests started
# running, meaning the pylonsapp wasn't setup yet (which is done in
# pylons.test.py:begin())
app = paste.fixture.TestApp(wsgiapp)
#app = paste.fixture.TestApp(wsgiapp)
app = helpers._get_test_app()


def config_abspath(file_path):
Expand Down Expand Up @@ -422,6 +424,7 @@ def call_action_api(app, action, apikey=None, status=200, **kwargs):
params = json.dumps(kwargs)
response = app.post('/api/3/action/{0}'.format(action), params=params,
extra_environ={'Authorization': str(apikey)}, status=status)

assert '/api/3/action/help_show?name={0}'.format(action) \
in response.json['help']

Expand Down
2 changes: 1 addition & 1 deletion ckan/tests/legacy/functional/api/base.py
Expand Up @@ -188,7 +188,7 @@ def loads(self, chars):
raise Exception, "Couldn't loads string '%s': %s" % (chars, inst)

def assert_json_response(self, res, expected_in_body=None):
content_type = res.header_dict['Content-Type']
content_type = res.headers['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(res.body)
if expected_in_body:
Expand Down
27 changes: 14 additions & 13 deletions ckan/tests/legacy/functional/api/model/test_package.py
Expand Up @@ -11,6 +11,7 @@
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase

import ckan.tests.legacy as tests
from ckan.tests import helpers

# Todo: Remove this ckan.model stuff.
import ckan.model as model
Expand Down Expand Up @@ -63,7 +64,7 @@ def test_register_post_ok(self):
assert_equal(pkg['extras'], self.package_fixture_data['extras'])

# Check the value of the Location header.
location = res.header('Location')
location = res.headers['Location']

assert offset in location
res = self.app.get(location, status=self.STATUS_200_OK)
Expand Down Expand Up @@ -131,7 +132,7 @@ def test_register_post_with_group(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
extra_environ={'Authorization':str(user.apikey)})

# Check the database record.
Expand All @@ -157,7 +158,7 @@ def test_register_post_with_group_not_authorized(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_403_ACCESS_DENIED,
res = self.app.post(offset, data, status=self.STATUS_403_ACCESS_DENIED,
extra_environ=self.extra_environ)
del package_fixture_data['groups']

Expand All @@ -171,7 +172,7 @@ def test_register_post_with_group_not_found(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_404_NOT_FOUND,
res = self.app.post(offset, data, status=self.STATUS_404_NOT_FOUND,
extra_environ=self.extra_environ)
del package_fixture_data['groups']

Expand All @@ -185,7 +186,7 @@ def test_register_post_with_group_sysadmin(self):
package_fixture_data = self.package_fixture_data
package_fixture_data['groups'] = groups
data = self.dumps(package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
extra_environ={'Authorization':str(user.apikey)})
# Check the database record.
model.Session.remove()
Expand All @@ -205,7 +206,7 @@ def test_register_post_json(self):
assert not self.get_package_by_name(self.package_fixture_data['name'])
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
res = self.post_json(offset, data, status=self.STATUS_201_CREATED,
res = self.app.post(offset, data, status=self.STATUS_201_CREATED,
extra_environ=self.admin_extra_environ)
# Check the database record.
model.Session.remove()
Expand All @@ -217,7 +218,7 @@ def test_register_post_bad_content_type(self):
assert not self.get_package_by_name(self.package_fixture_data['name'])
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)
res = self.http_request(offset, data,
res = self.app.post(offset, data,
content_type='something/unheard_of',
status=[self.STATUS_400_BAD_REQUEST,
self.STATUS_201_CREATED],
Expand Down Expand Up @@ -251,7 +252,7 @@ def test_register_post_indexerror(self):
"""
Test that we can't add a package if Solr is down.
"""
bad_solr_url = 'http://127.0.0.1/badsolrurl'
bad_solr_url = 'http://example.com/badsolrurl'
original_settings = SolrSettings.get()[0]
try:
SolrSettings.init(bad_solr_url)
Expand All @@ -260,7 +261,7 @@ def test_register_post_indexerror(self):
offset = self.package_offset()
data = self.dumps(self.package_fixture_data)

self.post_json(offset, data, status=500, extra_environ=self.admin_extra_environ)
self.app.post(offset, data, status=500, extra_environ=self.admin_extra_environ)
model.Session.remove()
finally:
SolrSettings.init(original_settings)
Expand All @@ -271,7 +272,7 @@ def test_register_post_tag_too_long(self):
assert not self.get_package_by_name(pkg['name'])
offset = self.package_offset()
data = self.dumps(pkg)
res = self.post_json(offset, data, status=self.STATUS_409_CONFLICT,
res = self.app.post(offset, data, status=self.STATUS_409_CONFLICT,
extra_environ=self.admin_extra_environ)
assert 'length is more than maximum 100' in res.body, res.body
assert 'tagok' not in res.body
Expand Down Expand Up @@ -641,7 +642,7 @@ def test_entity_update_indexerror(self):
"""
Test that we can't update a package if Solr is down.
"""
bad_solr_url = 'http://127.0.0.1/badsolrurl'
bad_solr_url = 'http://example.com/badsolrurl'
original_settings = SolrSettings.get()[0]
try:
SolrSettings.init(bad_solr_url)
Expand Down Expand Up @@ -712,8 +713,8 @@ def test_entity_delete_ok_without_request_headers(self):
assert self.get_package_by_name(self.package_fixture_data['name'])
# delete it
offset = self.package_offset(self.package_fixture_data['name'])
res = self.delete_request(offset, status=self.STATUS_200_OK,
extra_environ=self.admin_extra_environ)
res = self.app.delete(offset, status=self.STATUS_200_OK,
extra_environ=self.admin_extra_environ)
package = self.get_package_by_name(self.package_fixture_data['name'])
self.assert_equal(package.state, 'deleted')
model.Session.remove()
Expand Down
16 changes: 8 additions & 8 deletions ckan/tests/legacy/functional/api/model/test_relationships.py
@@ -1,12 +1,12 @@
from nose.tools import assert_equal
from nose.tools import assert_equal
from nose.plugins.skip import SkipTest

from ckan import model
from ckan.lib.create_test_data import CreateTestData

from ckan.tests.legacy.functional.api.base import BaseModelApiTestCase
from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase
from ckan.tests.legacy.functional.api.base import Api1TestCase as Version1TestCase
from ckan.tests.legacy.functional.api.base import Api2TestCase as Version2TestCase

class RelationshipsTestCase(BaseModelApiTestCase):

Expand Down Expand Up @@ -113,7 +113,7 @@ def test_01_create_and_read_relationship(self):
assert len(rels) == 1
self.check_relationship_dict(rels[0],
'annakarenina', 'parent_of', 'warandpeace', self.comment)

def test_02_create_relationship_way_2(self):
# Create a relationship using 2nd way
self.create_annakarenina_parent_of_war_and_peace(way=2)
Expand Down Expand Up @@ -187,7 +187,7 @@ def test_create_relationship_unknown(self):
def create_annakarenina_parent_of_war_and_peace(self, way=1):
# Create package relationship.
# More than one 'way' to create a package.
# Todo: Redesign this in a RESTful style, so that a relationship is
# Todo: Redesign this in a RESTful style, so that a relationship is
# created by posting a relationship to a relationship **register**.
assert way in (1, 2, 3, 4)
if way == 1:
Expand Down Expand Up @@ -218,7 +218,7 @@ def create_annakarenina_parent_of_war_and_peace(self, way=1):
assert_equal(rel['type'], 'child_of')
assert_equal(rel['subject'], self.ref_package(self.war))
assert_equal(rel['object'], self.ref_package(self.anna))

# Check the model, directly.
rels = self.anna.get_relationships()
assert len(rels) == 1, rels
Expand Down Expand Up @@ -269,7 +269,7 @@ def get_relationships(self, package1_name=u'annakarenina', type='relationships',
if type:
allowable_statuses.append(404)
res = self.app.get(offset, status=allowable_statuses)
if res.status == 200:
if res.status_int == 200:
res_dict = self.data_from_res(res) if res.body else []
return res_dict
else:
Expand Down Expand Up @@ -298,7 +298,7 @@ def check_relationships_rest(self, pkg1_name, pkg2_name=None,
expected_relationships=[]):
rels = self.get_relationships(package1_name=pkg1_name,
package2_name=pkg2_name)
self.assert_len_relationships(rels, expected_relationships)
self.assert_len_relationships(rels, expected_relationships)
for rel in rels:
the_expected_rel = None
for expected_rel in expected_relationships:
Expand Down
4 changes: 2 additions & 2 deletions ckan/tests/legacy/functional/api/test_resource.py
Expand Up @@ -43,7 +43,7 @@ def teardown_class(self):
def test_good_input(self):
offset = self.base_url + '/format_autocomplete?incomplete=cs'
result = self.app.get(offset, status=200)
content_type = result.header_dict['Content-Type']
content_type = result.headers['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(result.body)
assert 'ResultSet' in res_json, res_json
Expand All @@ -56,7 +56,7 @@ def test_good_input(self):
def test_missing_format(self):
offset = self.base_url + '/format_autocomplete?incomplete=incorrectformat'
result = self.app.get(offset, status=200)
content_type = result.header_dict['Content-Type']
content_type = result.headers['Content-Type']
assert 'application/json' in content_type, content_type
res_json = self.loads(result.body)
assert 'ResultSet' in res_json, res_json
Expand Down
20 changes: 16 additions & 4 deletions ckan/tests/legacy/functional/api/test_user.py
Expand Up @@ -23,8 +23,12 @@ def teardown_class(cls):
model.repo.rebuild_db()

def test_autocomplete(self):

with self.app.flask_app.test_request_context():
url = url_for(controller='api', action='user_autocomplete', ver=2)

response = self.app.get(
url=url_for(controller='api', action='user_autocomplete', ver=2),
url,
params={
'q': u'sysadmin',
},
Expand All @@ -33,11 +37,15 @@ def test_autocomplete(self):
print response.json
assert set(response.json[0].keys()) == set(['id', 'name', 'fullname'])
assert_equal(response.json[0]['name'], u'testsysadmin')
assert_equal(response.header('Content-Type'), 'application/json;charset=utf-8')
assert_equal(response.headers['Content-Type'], 'application/json;charset=utf-8')

def test_autocomplete_multiple(self):

with self.app.flask_app.test_request_context():
url = url_for(controller='api', action='user_autocomplete', ver=2)

response = self.app.get(
url=url_for(controller='api', action='user_autocomplete', ver=2),
url,
params={
'q': u'tes',
},
Expand All @@ -47,8 +55,12 @@ def test_autocomplete_multiple(self):
assert_equal(len(response.json), 2)

def test_autocomplete_limit(self):

with self.app.flask_app.test_request_context():
url = url_for(controller='api', action='user_autocomplete', ver=2)

response = self.app.get(
url=url_for(controller='api', action='user_autocomplete', ver=2),
url,
params={
'q': u'tes',
'limit': 1
Expand Down

0 comments on commit 878394b

Please sign in to comment.