Skip to content

Commit

Permalink
test whether embed code works and whether data_dict is complete
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Nov 30, 2012
1 parent 50ca34f commit c6d5389
Showing 1 changed file with 33 additions and 10 deletions.
43 changes: 33 additions & 10 deletions ckan/tests/functional/test_preview_interface.py
Expand Up @@ -16,22 +16,31 @@ def __init__(self):
self.calls = defaultdict(int)

def can_preview(self, data_dict):
assert(isinstance(data_dict['resource'], dict))
assert(isinstance(data_dict['package'], dict))
assert('on_same_domain' in data_dict['resource'])

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

def setup_template_variables(self, context, data_dict):
self.calls['setup_template_variables'] += 1

def preview_template(self, context, data_dict):
assert(isinstance(data_dict['resource'], dict))
assert(isinstance(data_dict['package'], dict))

self.calls['preview_templates'] += 1
return 'tests/mock_resource_preview_template.html'


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

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

Expand All @@ -46,14 +55,25 @@ def setup_class(cls):

create_test_data.CreateTestData.create()

cls.package = model.Package.get('annakarenina')
cls.resource = cls.package.resources[0]
cls.url = h.url_for(controller='package',
action='resource_read',
id=cls.package.name,
resource_id=cls.resource.id)
cls.preview_url = h.url_for(controller='package',
action='resource_datapreview',
id=cls.package.id,
resource_id=cls.resource.id)

@classmethod
def teardown_class(cls):
model.repo.rebuild_db()
plugins.unload(cls.plugin)

def test_hook(self):
testpackage = model.Package.get('annakarenina')
resource_dict = model_dictize.resource_dictize(testpackage.resources[0], {'model': model})
testpackage = self.package
resource_dict = model_dictize.resource_dictize(self.resource, {'model': model})
resource_dict['format'] = 'mock'

context = {
Expand All @@ -63,20 +83,14 @@ def test_hook(self):
}

# no preview for type "plain text"
preview_url = h.url_for(controller='package',
action='resource_datapreview',
id=testpackage.id,
resource_id=testpackage.resources[0].id)
preview_url = self.preview_url
result = self.app.get(preview_url, status=409)
assert 'No preview' in result.body, result.body

l.action.update.resource_update(context, resource_dict)

#there should be a preview for type "json"
preview_url = h.url_for(controller='package',
action='resource_datapreview',
id=testpackage.id,
resource_id=testpackage.resources[0].id)
preview_url = self.preview_url
result = self.app.get(preview_url, status=200)

assert 'mock-preview' in result.body
Expand All @@ -99,3 +113,12 @@ def test_hook(self):
assert self.plugin.calls['can_preview'] == 3, self.plugin.calls
assert self.plugin.calls['setup_template_variables'] == 1, self.plugin.calls
assert self.plugin.calls['preview_templates'] == 1, self.plugin.calls

def test_iframe_is_shown(self):
result = self.app.get(self.url)
assert 'data-module="data-viewer"' in result.body, result.body
assert '<iframe' in result.body, result.body

def test_iframe_url_is_correct(self):
result = self.app.get(self.url)
assert self.preview_url in result.body, (self.preview_url, result.body)

0 comments on commit c6d5389

Please sign in to comment.