Skip to content

Commit

Permalink
[2237] add environment variables and add way to segment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kindly committed Mar 16, 2012
1 parent 8c8cb8a commit 9e3e4ce
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
26 changes: 26 additions & 0 deletions ckan/ckan_nose_plugin.py
@@ -1,10 +1,13 @@
from nose.plugins import Plugin
from inspect import isclass
import hashlib
import os
import sys
import re
import pkg_resources
from paste.deploy import loadapp
from pylons import config
import unittest
import time

class CkanNose(Plugin):
Expand Down Expand Up @@ -54,12 +57,35 @@ def options(self, parser, env):
action='store_true',
dest='docstrings',
help='set this to display test docstrings instead of module names')
parser.add_option(
'--segments',
dest='segments',
help='A string containing a hex digits that represent which of'
'the 16 test segments to run. i.e 15af will run segments 1,5,a,f')

def wantClass(self, cls):
name = cls.__name__

wanted = (not cls.__name__.startswith('_')
and (issubclass(cls, unittest.TestCase)
or re.search('(?:^|[\b_\./-])[Tt]est', name)
))

if self.segments and str(hashlib.md5(name).hexdigest())[0] not in self.segments:
return False

return wanted

def finalize(self, report):
if self.segments:
print 'Segments: %s' % self.segments

def configure(self, settings, config):
CkanNose.settings = settings
if settings.is_ckan:
self.enabled = True
self.is_first_test = True
self.segments = settings.segments

def describeTest(self, test):
if not CkanNose.settings.docstrings:
Expand Down
11 changes: 10 additions & 1 deletion ckan/config/environment.py
Expand Up @@ -75,6 +75,10 @@ def find_controller(self, controller):
plugin.update_config(config)

# This is set up before globals are initialized
site_id = os.environ.get('CKAN_SITE_ID')
if site_id:
config['ckan.site_id'] = site_id

site_url = config.get('ckan.site_url', '')
ckan_host = config['ckan.host'] = urlparse(site_url).netloc
if config.get('ckan.site_id') is None:
Expand Down Expand Up @@ -125,7 +129,12 @@ def template_loaded(template):
warnings.filterwarnings('ignore', "^Did not recognize type 'BIGINT' of column 'size'", sqlalchemy.exc.SAWarning)
warnings.filterwarnings('ignore', "^Did not recognize type 'tsvector' of column 'search_vector'", sqlalchemy.exc.SAWarning)

engine = sqlalchemy.engine_from_config(config, 'sqlalchemy.')
ckan_db = os.environ.get('CKAN_DB')

if ckan_db:
engine = sqlalchemy.create_engine(ckan_db)
else:
engine = sqlalchemy.engine_from_config(config, 'sqlalchemy.')

if not model.meta.engine:
model.init_model(engine)
Expand Down
16 changes: 8 additions & 8 deletions ckan/tests/functional/test_datastore.py
Expand Up @@ -39,15 +39,15 @@ def test_read(self):
assert_equal(res.status, 200)
assert_equal(res.body, '""')
headers = dict(res.headers)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-test.ckan.net/%s?'
% resource_id)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-%s/%s?'
% (config['ckan.site_id'], resource_id))

offset = url_for('datastore_read', id=resource_id, url='/_search')
res = self.app.get(offset)
assert_equal(res.status, 200)
headers = dict(res.headers)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-test.ckan.net/%s/_search?'
% resource_id)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-%s/%s/_search?'
% (config['ckan.site_id'], resource_id))

def test_update(self):
dataset = model.Package.by_name(CreateTestData.pkg_names[0])
Expand Down Expand Up @@ -79,14 +79,14 @@ def test_update(self):
# assert res.status in [401,302], res.status
assert res.status == 200
headers = dict(res.headers)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-test.ckan.net/%s?'
% resource_id)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-%s/%s?'
% (config['ckan.site_id'], resource_id))


offset = url_for('datastore_write', id=resource_id, url='/_mapping')
res = self.app.post(offset)
assert res.status == 200
headers = dict(res.headers)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-test.ckan.net/%s/_mapping?'
% resource_id)
assert_equal(headers['X-Accel-Redirect'], '/elastic/ckan-%s/%s/_mapping?'
% (config['ckan.site_id'], resource_id))

10 changes: 6 additions & 4 deletions ckan/tests/logic/test_action.py
Expand Up @@ -2,6 +2,7 @@
import json
from pprint import pprint
from nose.tools import assert_equal, assert_raises
from pylons import config

import ckan
from ckan.lib.create_test_data import CreateTestData
Expand Down Expand Up @@ -1273,16 +1274,17 @@ def test_27_get_site_user_not_authorized(self):
user = model.User.get('test.ckan.net')
assert not user

site_id = config.get('ckan.site_id')
user = get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
assert user['name'] == 'test.ckan.net'
assert user['name'] == site_id

user = model.User.get('test.ckan.net')
user = model.User.get(site_id)
assert user

user=get_action('get_site_user')({'model': model, 'ignore_auth': True}, {})
assert user['name'] == 'test.ckan.net'
assert user['name'] == site_id

user = model.Session.query(model.User).filter_by(name='test.ckan.net').one()
user = model.Session.query(model.User).filter_by(name=site_id).one()
assert user

def test_28_group_package_show(self):
Expand Down

0 comments on commit 9e3e4ce

Please sign in to comment.