Commit ea0a61f3 authored by Javier Vegas-Regidor's avatar Javier Vegas-Regidor
Browse files

Added more tests

parent d18d90dd
......@@ -395,6 +395,10 @@ class DiagnosticChoiceOption(DiagnosticOption):
self.choices = choices
self.ignore_case = ignore_case
# To check if it is valid
if default_value is not None:
self.parse(default_value)
def parse(self, value):
value = self.check_default(value)
if self.ignore_case:
......
......@@ -4,7 +4,7 @@ class Publisher(object):
Base class to provide functionality to notify updates to other objects
"""
def __init__(self):
self.subscribers = dict()
self._subscribers = dict()
def subscribe(self, who, callback=None):
"""
......@@ -17,7 +17,7 @@ class Publisher(object):
"""
if callback is None:
callback = getattr(who, 'update')
self.subscribers[who] = callback
self._subscribers[who] = callback
def unsubscribe(self, who):
"""
......@@ -26,7 +26,7 @@ class Publisher(object):
:param who: suscriber to remove
:type who: object
"""
del self.subscribers[who]
del self._subscribers[who]
def dispatch(self, *args):
"""
......@@ -34,5 +34,13 @@ class Publisher(object):
:param args: arguments to pass
"""
for subscriber, callback in self.subscribers.items():
for subscriber, callback in self._subscribers.items():
callback(*args)
def suscribers(self, *args):
"""
Notify update to all the suscribers
:param args: arguments to pass
"""
return self._subscribers.keys()
......@@ -3,6 +3,7 @@ from earthdiagnostics.diagnostic import *
from unittest import TestCase
from earthdiagnostics.modelingrealm import ModelingRealms
from mock import patch, Mock
# noinspection PyTypeChecker,PyTypeChecker,PyTypeChecker
......@@ -258,9 +259,98 @@ class TestDiagnosticListIntOption(TestCase):
diag = DiagnosticListIntOption('option')
self.assertEqual([3], diag.parse('3'))
def test_too_low(self):
diag = DiagnosticListIntOption('option', min_limit=5)
with self.assertRaises(DiagnosticOptionError):
diag.parse('3')
def test_too_high(self):
diag = DiagnosticListIntOption('option', max_limit=5)
with self.assertRaises(DiagnosticOptionError):
diag.parse('8')
def test_parse_bad_value(self):
diag = DiagnosticListIntOption('option')
with self.assertRaises(ValueError):
diag.parse('3.5')
class TestDiagnosticChoiceOption(TestCase):
def test_choice_value(self):
diag = DiagnosticChoiceOption('option', ('a', 'b'))
self.assertEqual('a', diag.parse('a'))
def test_choice_default_value(self):
diag = DiagnosticChoiceOption('option', ('a', 'b'), default_value='a')
self.assertEqual('a', diag.parse(''))
def test_bad_default_value(self):
with self.assertRaises(DiagnosticOptionError):
DiagnosticChoiceOption('option', ('a', 'b'), default_value='c')
def test_ignore_case_value(self):
diag = DiagnosticChoiceOption('option', ('a', 'b'))
self.assertEqual('b', diag.parse('b'))
self.assertEqual('b', diag.parse('B'))
diag = DiagnosticChoiceOption('option', ('a', 'b'), ignore_case=False)
self.assertEqual('b', diag.parse('b'))
with self.assertRaises(DiagnosticOptionError):
self.assertEqual('b', diag.parse('B'))
class TestDiagnosticVariableOption(TestCase):
def get_var_mock(self, name):
mock = Mock()
mock.short_name = name
return mock
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse(self, get_variable_mock):
get_variable_mock.return_value = self.get_var_mock('var1')
diag = DiagnosticVariableOption()
self.assertEqual('var1', diag.parse('var1'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse(self, get_variable_mock):
get_variable_mock.return_value = self.get_var_mock('var1')
diag = DiagnosticVariableOption()
self.assertEqual('var1', diag.parse('var1'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_not_recognized(self, get_variable_mock):
get_variable_mock.return_value = None
diag = DiagnosticVariableOption()
self.assertEqual('var1', diag.parse('var1'))
class TestDiagnosticVariableListOption(TestCase):
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse_multiple(self, get_variable_mock):
get_variable_mock.side_effect = (self.get_var_mock('var1'), self.get_var_mock('var2'))
diag = DiagnosticVariableListOption('variables')
self.assertEqual(['var1', 'var2'], diag.parse('var1-var2'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_parse_one(self, get_variable_mock):
get_variable_mock.return_value = self.get_var_mock('var1')
diag = DiagnosticVariableListOption('variables')
self.assertEqual(['var1'], diag.parse('var1'))
@patch('earthdiagnostics.variable.VariableManager.get_variable')
def test_not_recognized(self, get_variable_mock):
get_variable_mock.return_value = None
diag = DiagnosticVariableListOption('variables')
self.assertEqual(['var1'], diag.parse('var1'))
def get_var_mock(self, name):
mock = Mock()
mock.short_name = name
return mock
# coding=utf-8
from unittest import TestCase
from earthdiagnostics.publisher import Publisher
from mock import Mock
class TestPublisher(TestCase):
def test_suscribe(self):
suscriber = Mock()
pub = Publisher()
pub.subscribe(suscriber, callback=suscriber.callback)
self.assertIn(suscriber, pub.suscribers())
def test_suscribe_default(self):
suscriber = Mock()
pub = Publisher()
pub.subscribe(suscriber)
self.assertTrue(hasattr(suscriber, 'update'))
self.assertIn(suscriber, pub.suscribers())
def test_unsuscribe(self):
suscriber = Mock()
pub = Publisher()
pub.subscribe(suscriber, callback=suscriber.callback)
pub.unsubscribe(suscriber)
self.assertNotIn(suscriber, pub.suscribers())
def test_dispatch(self):
suscriber = Mock()
pub = Publisher()
pub.subscribe(suscriber, callback=suscriber.callback)
pub.dispatch(1, 2, 3)
suscriber.callback.assert_called_with(1, 2, 3)
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