Commit 82ff3556 by Sven Marnach

Add unit tests for activetable.py.

parent 781f858f
......@@ -92,8 +92,9 @@ def parse_number_list(source):
"""
try:
lst = ast.literal_eval(source)
except SyntaxError as exc:
raise ParseError(exc.msg)
except (SyntaxError, ValueError) as exc:
msg = getattr(exc, 'msg', getattr(exc, 'message', None))
raise ParseError(msg)
if not isinstance(lst, list):
raise ParseError('not a list')
if not all(isinstance(x, numbers.Real) for x in lst):
......
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, unicode_literals
import unittest
import mock
from xblock.field_data import DictFieldData
from xblock.runtime import Runtime
from xblock.validation import Validation
from activetable.activetable import ActiveTableXBlock
class ActiveTableTest(unittest.TestCase):
def setUp(self):
self.runtime_mock = mock.Mock(spec=Runtime)
self.block = ActiveTableXBlock(self.runtime_mock, DictFieldData({}), mock.Mock())
def verify_validation(self, data, expect_success):
validation = Validation('xblock_id')
self.block.validate_field_data(validation, data)
self.assertEqual(bool(validation), expect_success)
def test_validate_field_data(self):
data = mock.Mock()
data.table_definition = 'invalid'
data.column_widths = ''
data.row_heights = ''
self.verify_validation(data, False)
data.table_definition = '[["header"], [6.283]]'
self.verify_validation(data, True)
data.column_widths = 'invalid'
self.verify_validation(data, False)
data.column_widths = '[1, 2, 3]'
self.verify_validation(data, True)
data.row_heights = 'invalid'
self.verify_validation(data, False)
data.row_heights = '[1, 2, 3]'
self.verify_validation(data, True)
......@@ -52,6 +52,6 @@ class ParserTest(unittest.TestCase):
def test_parse_number_list(self):
self.assertEquals(parse_number_list('[1, 2.3]'), [1, 2.3])
for string in [']', '123', '["123"]', '[1j]']:
for string in [']', '123', '["123"]', '[1j]', 'malformed']:
with self.assertRaises(ParseError):
parse_number_list(string)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment