Skip to content

Commit

Permalink
adds student_view_data to Image Explorer Block
Browse files Browse the repository at this point in the history
adds student_view_data tests

bump version
  • Loading branch information
msaqib52 committed Aug 9, 2017
1 parent 31e6da3 commit 65b9503
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
17 changes: 17 additions & 0 deletions image_explorer/image_explorer.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ def student_view(self, context):

return fragment

def student_view_data(self, context=None):
"""
Returns a JSON representation of the Image Explorer Xblock, that can be
retrieved using Course Block API.
"""
xmltree = etree.fromstring(self.data)

description = self._get_description(xmltree)
background = self._get_background(xmltree)
hotspots = self._get_hotspots(xmltree)

return {
'description': description,
'background': background,
'hotspots': hotspots,
}

@XBlock.json_handler
def publish_event(self, data, suffix=''):
try:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def package_data(pkg, root_list):

setup(
name='xblock-image-explorer',
version='0.3',
version='0.4.0',
description='XBlock - Image Explorer',
packages=['image_explorer'],
install_requires=[
Expand Down
Empty file added tests/unit/__init__.py
Empty file.
60 changes: 60 additions & 0 deletions tests/unit/test_image_explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import unittest
from lxml import etree

from xblock.field_data import DictFieldData

from image_explorer.image_explorer import ImageExplorerBlock
from ..utils import MockRuntime

class TestImageExplorerBlock(unittest.TestCase):
"""
Tests for XBlock Image Explorer.
"""
def setUp(self):
"""
Test case setup
"""
super(TestImageExplorerBlock, self).setUp()
self.runtime = MockRuntime()
self.image_url = 'http://example.com/test.jpg'
self.image_explorer_description = '<p>Test Descrption</p>'
self.image_explorer_xml = """
<image_explorer schema_version='1'>
<background src='{0}' />
<description>{1}</description>
<hotspots>
<hotspot x='370' y='20' item-id='hotspotA'>
<feedback width='300' height='240'>
<header><p>Test Header</p></header>
<body><p>Test Body</p></body>
</feedback>
</hotspot>
</hotspots>
</image_explorer>
""".format(self.image_url, self.image_explorer_description)

self.image_explorer_data = {'data': self.image_explorer_xml}
self.image_explorer_block = ImageExplorerBlock(
self.runtime,
DictFieldData(self.image_explorer_data),
None
)

def test_student_view_data(self):
"""
Test the student_view_data results.
"""
xmltree = etree.fromstring(self.image_explorer_xml)
hotspots = self.image_explorer_block._get_hotspots(xmltree)
expected_image_explorer_data = {
'description': self.image_explorer_description,
'background': {
'src': self.image_url,
'height': None,
'width': None
},
'hotspots': hotspots,
}

student_view_data = self.image_explorer_block.student_view_data()
self.assertEqual(student_view_data, expected_image_explorer_data)
12 changes: 12 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Test mocks and helpers
from xblock.runtime import DictKeyValueStore, KvsFieldData
from xblock.test.tools import TestRuntime


class MockRuntime(TestRuntime):
"""
Provides a mock XBlock runtime object.
"""
def __init__(self, **kwargs):
field_data = kwargs.get('field_data', KvsFieldData(DictKeyValueStore()))
super(MockRuntime, self).__init__(field_data=field_data)

0 comments on commit 65b9503

Please sign in to comment.