Skip to content

Commit

Permalink
[ckan#4850] fix tests to match pytest style
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Dec 25, 2019
2 parents a3cbca9 + 152e65a commit b01ac36
Show file tree
Hide file tree
Showing 15 changed files with 182 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ckan/config/resource_formats.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@
["KMZ", "KMZ File", "application/vnd.google-earth.kmz+xml", ["application/vnd.google-earth.kmz"]],
["MDB", "Access Database", "application/x-msaccess", []],
["MOP", "MOPAC Input format", "chemical/x-mopac-input", []],
["MP3", "MPEG-1 Audio Layer-3", "audio/mpeg", ["audio/mpeg3", "audio/x-mpeg-3", "video/mpeg", "video/x-mpeg"]],
["MP4", "MPEG Video File", "video/mp4", ["application/mp4"]],
["MrSID", "MrSID", "image/x-mrsid", []],
["MXD", "ESRI ArcGIS project file", "application/x-mxd", []],
["NetCDF", "NetCDF File", "application/netcdf", []],
["N3", "N3 Triples", "application/x-n3", []],
["OGG", "Ogg Vorbis Audio File", "audio/ogg", []],
["ODB", "OpenDocument Database", "application/vnd.oasis.opendocument.database", []],
["ODC", "OpenDocument Chart", "application/vnd.oasis.opendocument.chart", []],
["ODF", "OpenDocument Math Formula", "application/vnd.oasis.opendocument.formula", []],
Expand Down Expand Up @@ -73,7 +76,9 @@
["TORRENT", "Torrent", "application/x-bittorrent", ["bittorrent"]],
["TSV", "Tab Separated Values File", "text/tab-separated-values", ["text/tsv"]],
["TXT", "Text File", "text/plain", []],
["WAV", "Wave file", "audio/x-wav", ["audio/wav"]],
["WCS", "Web Coverage Service", "wcs", []],
["WebM", "WEBM video", "video/webm", []],
["WFS", "Web Feature Service", null, []],
["WMS", "Web Mapping Service", "WMS", ["wms"]],
["WMTS", "Web Map Tile Service", null, []],
Expand Down
4 changes: 4 additions & 0 deletions ckan/tests/test_coding_standards.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ def find_unprefixed_string_literals(filename):
u"ckanext/example_theme_docs/v21_custom_jquery_plugin/plugin.py",
u"ckanext/imageview/plugin.py",
u"ckanext/imageview/tests/test_view.py",
u'ckanext/audioview/plugin.py',
u'ckanext/audioview/tests/test_view.py',
u'ckanext/videoview/plugin.py',
u'ckanext/videoview/tests/test_view.py',
u"ckanext/multilingual/plugin.py",
u"ckanext/multilingual/tests/test_multilingual_plugin.py",
u"ckanext/reclineview/plugin.py",
Expand Down
Empty file added ckanext/audioview/__init__.py
Empty file.
41 changes: 41 additions & 0 deletions ckanext/audioview/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# encoding: utf-8

from six import text_type
import ckan.plugins as p

ignore_empty = p.toolkit.get_validator('ignore_empty')

DEFAULT_AUDIO_FORMATS = 'wav ogg mp3'


class AudioView(p.SingletonPlugin):
'''This plugin makes views of audio resources, using an <audio> tag'''

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)

def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
self.formats = config.get(
'ckan.preview.audio_formats',
DEFAULT_AUDIO_FORMATS).split()

def info(self):
return {'name': 'audio_view',
'title': p.toolkit._('Audio'),
'icon': 'file-audio-o',
'schema': {'audio_url': [ignore_empty, text_type]},
'iframed': False,
'always_available': True,
'default_title': p.toolkit._('Audio'),
}

def can_view(self, data_dict):
return (data_dict['resource'].get('format', '').lower()
in self.formats)

def view_template(self, context, data_dict):
return 'audio_view.html'

def form_template(self, context, data_dict):
return 'audio_form.html'
Empty file.
30 changes: 30 additions & 0 deletions ckanext/audioview/tests/test_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: utf-8

from ckan.lib.helpers import url_for
from ckan.tests import factories

import ckan.plugins as p

import pytest

@pytest.mark.ckan_config('ckan.views.default_views', '')
@pytest.mark.ckan_config("ckan.plugins", "audio_view")
@pytest.mark.usefixtures("clean_db", "with_plugins")
def test_view_shown_on_resource_page_with_audio_url(app):

dataset = factories.Dataset()

resource = factories.Resource(package_id=dataset['id'],
format='wav')

resource_view = factories.ResourceView(
resource_id=resource['id'],
view_type='audio_view',
audio_url='http://example.wav')

url = url_for('resource.read',
id=dataset['name'], resource_id=resource['id'])

response = app.get(url)

assert resource_view['audio_url'] in response
3 changes: 3 additions & 0 deletions ckanext/audioview/theme/templates/audio_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% import 'macros/form.html' as form %}

{{ form.input('audio_url', id='field-audio_url', label=_('Audio url'), placeholder=_('eg. http://example.com/audio.mp3 (if blank uses resource url)'), value=data.audio_url, error=errors.audio_url, classes=['control-full', 'control-large']) }}
9 changes: 9 additions & 0 deletions ckanext/audioview/theme/templates/audio_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% set res_url = resource_view.get('audio_url') or resource.get('url') %}

<audio style="margin:auto; max-height:100%; display:block; min-width: 100%;"
controls src="{{ resource_view.audio_url or resource.get('url') }}">
{% trans %}
Your browser does not support the <code>audio</code> element.
But don't worry, you can <a href="{{ res_url }}" alt="video url">download it</a>.
{% endtrans %}
</audio>
Empty file added ckanext/videoview/__init__.py
Empty file.
42 changes: 42 additions & 0 deletions ckanext/videoview/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# encoding: utf-8

from six import text_type
import ckan.plugins as p

ignore_empty = p.toolkit.get_validator('ignore_empty')

DEFAULT_VIDEO_FORMATS = 'mp4 ogg webm'


class VideoView(p.SingletonPlugin):
'''This plugin makes views of video resources, using a <video> tag'''

p.implements(p.IConfigurer, inherit=True)
p.implements(p.IResourceView, inherit=True)

def update_config(self, config):
p.toolkit.add_template_directory(config, 'theme/templates')
self.formats = config.get(
'ckan.preview.video_formats',
DEFAULT_VIDEO_FORMATS).split()

def info(self):
return {'name': 'video_view',
'title': p.toolkit._('Video'),
'icon': 'file-video-o',
'schema': {'video_url': [ignore_empty, text_type],
'poster_url': [ignore_empty, text_type]},
'iframed': False,
'always_available': True,
'default_title': p.toolkit._('Video'),
}

def can_view(self, data_dict):
return (data_dict['resource'].get('format', '').lower()
in self.formats)

def view_template(self, context, data_dict):
return 'video_view.html'

def form_template(self, context, data_dict):
return 'video_form.html'
Empty file.
30 changes: 30 additions & 0 deletions ckanext/videoview/tests/test_view.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# encoding: utf-8

from ckan.lib.helpers import url_for
from ckan.tests import helpers, factories

import ckan.plugins as p
import pytest


@pytest.mark.ckan_config('ckan.views.default_views', '')
@pytest.mark.ckan_config("ckan.plugins", "video_view")
@pytest.mark.usefixtures("clean_db", "with_plugins")
def test_view_shown_on_resource_page_with_video_url(app):

dataset = factories.Dataset()

resource = factories.Resource(package_id=dataset['id'],
format='mp4')

resource_view = factories.ResourceView(
resource_id=resource['id'],
view_type='video_view',
video_url='https://example/video.mp4')

url = url_for('resource.read',
id=dataset['name'], resource_id=resource['id'])

response = app.get(url)

assert resource_view['video_url'] in response
4 changes: 4 additions & 0 deletions ckanext/videoview/theme/templates/video_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{% import 'macros/form.html' as form %}

{{ form.input('video_url', id='field-video_url', label=_('Video url'), placeholder=_('eg. http://example.com/video.mpeg (if blank uses resource url)'), value=data.video_url, error=errors.video_url, classes=['control-full', 'control-large']) }}
{{ form.input('poster_url', id='field-poster_url', label=_('Poster url'), placeholder=_('eg. http://example.com/poster.jpg'), value=data.poster_url, error=errors.poster_url, classes=['control-full', 'control-large']) }}
12 changes: 12 additions & 0 deletions ckanext/videoview/theme/templates/video_view.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{% set res_url = resource_view.get('video_url') or resource.get('url') %}

<video style="margin:auto; max-height:100%; width: 250px; min-width: 100%; display:block"
src="{{ resource_view.video_url or resource.get('url') }}"
type="{{ resource.get('format') }}" controls
poster="{{ resource_view.poster_url }}">
{% trans %}
Sorry, your browser doesn't support embedded videos,
but don't worry, you can <a href="{{ res_url }}" alt="video url">download it</a>
and watch it with your favorite video player!
{% endtrans %}
</video>
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ def parse_version(s):
'recline_map_view = ckanext.reclineview.plugin:ReclineMapView',
'datatables_view = ckanext.datatablesview.plugin:DataTablesView',
'image_view = ckanext.imageview.plugin:ImageView',
'audio_view = ckanext.audioview.plugin:AudioView',
'video_view = ckanext.videoview.plugin:VideoView',
'webpage_view = ckanext.webpageview.plugin:WebPageView',
# FIXME: Remove deprecated resource previews below. You should use the
# versions as *_view instead.
Expand Down

0 comments on commit b01ac36

Please sign in to comment.