Skip to content

Commit

Permalink
Merge branch 'poc-flask-views' into poc-flask-views.common-url_for
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed May 26, 2016
2 parents 243e437 + 8d2418f commit fe72b99
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 41 deletions.
5 changes: 5 additions & 0 deletions ckan/pastertemplates/template/bin/travis-build.bash_tmpl
Expand Up @@ -22,6 +22,11 @@ echo "Creating the PostgreSQL user and database..."
sudo -u postgres psql -c "CREATE USER ckan_default WITH PASSWORD 'pass';"
sudo -u postgres psql -c 'CREATE DATABASE ckan_test WITH OWNER ckan_default;'

echo "SOLR config..."
# Solr is multicore for tests on ckan master, but it's easier to run tests on
# Travis single-core. See https://github.com/ckan/ckan/issues/2972
sed -i -e 's/solr_url.*/solr_url = http:\/\/127.0.0.1:8983\/solr/' ckan/test-core.ini

echo "Initialising the database..."
cd ckan
paster db init -c test-core.ini
Expand Down
28 changes: 28 additions & 0 deletions ckan/tests/helpers.py
Expand Up @@ -414,3 +414,31 @@ def wrapper(*args, **kwargs):
return return_value
return nose.tools.make_decorator(func)(wrapper)
return decorator


def find_flask_app(test_app):
'''
Helper function to recursively search the wsgi stack in `test_app` until
the flask_app is discovered.
Relies on each layer of the stack having a reference to the app they
wrap in either a .app attribute or .apps list.
'''
if isinstance(test_app, ckan.config.middleware.CKANFlask):
return test_app

try:
app = test_app.apps['flask_app'].app
except (AttributeError, KeyError):
pass
else:
return find_flask_app(app)

try:
app = test_app.app
except AttributeError:
print('No .app attribute. '
'Have all layers of the stack got '
'a reference to the app they wrap?')
else:
return find_flask_app(app)
10 changes: 5 additions & 5 deletions ckanext/datapusher/logic/action.py
Expand Up @@ -74,7 +74,7 @@ def datapusher_submit(context, data_dict):
'entity_id': res_id,
'entity_type': 'resource',
'task_type': 'datapusher',
'last_updated': str(datetime.datetime.now()),
'last_updated': str(datetime.datetime.utcnow()),
'state': 'submitting',
'key': 'datapusher',
'value': '{}',
Expand Down Expand Up @@ -119,7 +119,7 @@ def datapusher_submit(context, data_dict):
'details': str(e)}
task['error'] = json.dumps(error)
task['state'] = 'error'
task['last_updated'] = str(datetime.datetime.now()),
task['last_updated'] = str(datetime.datetime.utcnow()),
p.toolkit.get_action('task_status_update')(context, task)
raise p.toolkit.ValidationError(error)

Expand All @@ -134,7 +134,7 @@ def datapusher_submit(context, data_dict):
'status_code': r.status_code}
task['error'] = json.dumps(error)
task['state'] = 'error'
task['last_updated'] = str(datetime.datetime.now()),
task['last_updated'] = str(datetime.datetime.utcnow()),
p.toolkit.get_action('task_status_update')(context, task)
raise p.toolkit.ValidationError(error)

Expand All @@ -143,7 +143,7 @@ def datapusher_submit(context, data_dict):

task['value'] = value
task['state'] = 'pending'
task['last_updated'] = str(datetime.datetime.now()),
task['last_updated'] = str(datetime.datetime.utcnow()),
p.toolkit.get_action('task_status_update')(context, task)

return True
Expand Down Expand Up @@ -175,7 +175,7 @@ def datapusher_hook(context, data_dict):
})

task['state'] = status
task['last_updated'] = str(datetime.datetime.now())
task['last_updated'] = str(datetime.datetime.utcnow())

resubmit = False

Expand Down
17 changes: 9 additions & 8 deletions ckanext/datapusher/tests/test_action.py
Expand Up @@ -30,7 +30,7 @@ def _pending_task(self, resource_id):
'entity_id': resource_id,
'entity_type': 'resource',
'task_type': 'datapusher',
'last_updated': str(datetime.datetime.now()),
'last_updated': str(datetime.datetime.utcnow()),
'state': 'pending',
'key': 'datapusher',
'value': '{}',
Expand Down Expand Up @@ -163,13 +163,14 @@ def test_resubmits_if_upload_changes_in_the_meantime(
**self._pending_task(resource['id']))

# Update the resource, set a new last_modified (changes on file upload)
helpers.call_action('resource_update', {},
id=resource['id'],
package_id=dataset['id'],
url='http://example.com/file.csv',
format='CSV',
last_modified=datetime.datetime.now().isoformat()
)
helpers.call_action(
'resource_update', {},
id=resource['id'],
package_id=dataset['id'],
url='http://example.com/file.csv',
format='CSV',
last_modified=datetime.datetime.utcnow().isoformat()
)
# Not called
eq_(len(mock_datapusher_submit.mock_calls), 1)

Expand Down
29 changes: 1 addition & 28 deletions ckanext/example_flask_iroutes/tests/test_routes.py
@@ -1,41 +1,14 @@
from nose.tools import eq_, ok_

from ckan.config.middleware import CKANFlask
import ckan.plugins as plugins
import ckan.tests.helpers as helpers


class TestFlaskIRoutes(helpers.FunctionalTestBase):
@classmethod
def _find_flask_app(cls, test_app):
'''Recursively search the wsgi stack until the flask_app is
discovered.
Relies on each layer of the stack having a reference to the app they
wrap in either a .app attribute or .apps list.
'''
if isinstance(test_app, CKANFlask):
return test_app

try:
app = test_app.apps['flask_app'].app
except (AttributeError, KeyError):
pass
else:
return cls._find_flask_app(app)

try:
app = test_app.app
except AttributeError:
print('No .app attribute. '
'Have all layers of the stack got '
'a reference to the app they wrap?')
else:
return cls._find_flask_app(app)

def setup(self):
self.app = helpers._get_test_app()
flask_app = self._find_flask_app(self.app)
flask_app = helpers.find_flask_app(self.app)

# Blueprints can't be registered after the app has been setup. For
# some reason, if debug is True, the app will have exited its initial
Expand Down

0 comments on commit fe72b99

Please sign in to comment.