Skip to content

Commit

Permalink
functional test for preview plugin interface
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Nov 8, 2012
1 parent 6b003a6 commit d87c115
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 15 deletions.
17 changes: 17 additions & 0 deletions ckan/templates/tests/mock_json_resource_preview_template.html
@@ -0,0 +1,17 @@
{% extends 'dataviewer/base.html' %}

{% block page %}
<div>
<pre data-module="mock-json-preview">
<div class="loading">
{{ _('Loading...') }}
</div>
</pre>
</div>
{% endblock %}

{% block scripts %}
{{ super() }}

<script src="mock-json-preview.js" ?>
{% endblock %}
53 changes: 38 additions & 15 deletions ckan/tests/functional/test_preview_interface.py
@@ -1,10 +1,12 @@
from ckan.tests import *
import ckan.lib.helpers as h
import ckan.logic as l
import ckan.model as model
from ckan.lib.create_test_data import CreateTestData
from ckan.tests.functional.base import FunctionalTestCase
import ckan.plugins as plugins
import ckan.tests.mock_plugin as mock
from ckan.lib.dictization.model_dictize import resource_dictize


class MockResourcePreviewExtension(mock.MockSingletonPlugin):
Expand All @@ -16,7 +18,7 @@ def __init__(self):

def can_preview(self, data_dict):
self.calls['can_preview'] += 1
return True
return data_dict['resource']['format'].lower() == 'mock'

def setup_template_variables(self, context, data_dict):
self.calls['setup_template_variables'] += 1
Expand All @@ -26,11 +28,23 @@ def preview_template(self, context, data_dict):
return 'tests/mock_resource_preview_template.html'


class JsonMockResourcePreviewExtension(MockResourcePreviewExtension):
def can_preview(self, data_dict):
return data_dict['resource']['format'].lower() == 'json'

def preview_template(self, context, data_dict):
self.calls['preview_templates'] += 1
return 'tests/mock_json_resource_preview_template.html'


class TestPluggablePreviews(FunctionalTestCase):
@classmethod
def setup_class(cls):
cls.plugin = MockResourcePreviewExtension()
plugins.load(cls.plugin)
json_plugin = JsonMockResourcePreviewExtension()
plugins.load(json_plugin)

CreateTestData.create()

@classmethod
Expand All @@ -39,30 +53,39 @@ def teardown_class(cls):
plugins.unload(cls.plugin)

def test_hook(self):
'''
TODO:
* Modify mock plugin to only preview resources of type 'mock'
* Create a dataset with two resources (logic function), one of type mock and one
of another type, and check that the mock stuff is only rendered on
the relevant one and not the other
* (?) create resources of type csv, json and pdf and look if the relevant bits
are rendered (these tests should probably go in their relevant extensions)
'''

testpackage = model.Package.get('annakarenina')
resource_dict = resource_dictize(testpackage.resources[0], {'model': model})
resource_dict['format'] = 'mock'

context = {
'model': model,
'session': model.Session,
'user': model.User.get('testsysadmin').name
}

testpackage = model.Package.get('annakarenina')
l.action.update.resource_update(context, resource_dict)
print testpackage.resources[0].format

offset = h.url_for(controller='package',
preview_url = h.url_for(controller='package',
action='resource_datapreview',
id=testpackage.id,
resource_id=testpackage.resources[0].id)
result = self.app.get(offset, status=200)
result = self.app.get(preview_url, status=200)

assert 'mock-preview' in result.body
assert 'mock-preview.js' in result.body

assert self.plugin.calls['can_preview'] == 1, plugin.call
assert self.plugin.calls['setup_template_variables'] == 1, plugin.calls
assert self.plugin.calls['preview_templates'] == 1, plugin.calls

preview_url = h.url_for(controller='package',
action='resource_datapreview',
id=testpackage.id,
resource_id=testpackage.resources[1].id)
result = self.app.get(preview_url, status=200)

assert 'mock-preview' not in result.body, result.body
assert 'mock-preview.js' not in result.body, result.body
assert 'mock-json-preview' in result.body
assert 'mock-json-preview.js' in result.body

0 comments on commit d87c115

Please sign in to comment.