From 65b9503d504bdc4020124cc6f2a7fe72bef72cce Mon Sep 17 00:00:00 2001 From: Saqib Date: Wed, 2 Aug 2017 14:55:22 +0500 Subject: [PATCH] adds student_view_data to Image Explorer Block adds student_view_data tests bump version --- image_explorer/image_explorer.py | 17 +++++++++ setup.py | 2 +- tests/unit/__init__.py | 0 tests/unit/test_image_explorer.py | 60 +++++++++++++++++++++++++++++++ tests/utils.py | 12 +++++++ 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 tests/unit/__init__.py create mode 100644 tests/unit/test_image_explorer.py create mode 100644 tests/utils.py diff --git a/image_explorer/image_explorer.py b/image_explorer/image_explorer.py index 4ba3364..794e641 100644 --- a/image_explorer/image_explorer.py +++ b/image_explorer/image_explorer.py @@ -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: diff --git a/setup.py b/setup.py index e6e9147..c77d1fc 100644 --- a/setup.py +++ b/setup.py @@ -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=[ diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/unit/test_image_explorer.py b/tests/unit/test_image_explorer.py new file mode 100644 index 0000000..3b1d003 --- /dev/null +++ b/tests/unit/test_image_explorer.py @@ -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 = '

Test Descrption

' + self.image_explorer_xml = """ + + + {1} + + + +

Test Header

+

Test Body

+
+
+
+
+ """.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) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 0000000..9a755b7 --- /dev/null +++ b/tests/utils.py @@ -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)