From 309f5252cca19be36bdb6d59259c491dc2a9d745 Mon Sep 17 00:00:00 2001
From: Stefano Borini <sborini@enthought.com>
Date: Wed, 25 Apr 2018 11:40:06 +0100
Subject: [PATCH] Added tests

---
 .../tests/test_base_data_source_factory.py    | 34 ++++++++
 force_bdss/kpi/base_kpi_calculator_factory.py |  2 +-
 .../tests/test_base_kpi_calculator_factory.py | 34 +++++---
 force_bdss/mco/tests/test_base_mco_factory.py | 48 ++++++++++++
 .../notification_listeners/tests/__init__.py  |  0
 ...test_base_notification_listener_factory.py | 77 +++++++++++++++++++
 .../tests/test_base_ui_hooks_factory.py       | 32 +++++++-
 .../tests/test_base_ui_hooks_manager.py       |  6 +-
 8 files changed, 219 insertions(+), 14 deletions(-)
 create mode 100644 force_bdss/notification_listeners/tests/__init__.py
 create mode 100644 force_bdss/notification_listeners/tests/test_base_notification_listener_factory.py

diff --git a/force_bdss/data_sources/tests/test_base_data_source_factory.py b/force_bdss/data_sources/tests/test_base_data_source_factory.py
index 9a6b562..3215cf8 100644
--- a/force_bdss/data_sources/tests/test_base_data_source_factory.py
+++ b/force_bdss/data_sources/tests/test_base_data_source_factory.py
@@ -1,9 +1,16 @@
 import unittest
+
+from force_bdss.data_sources.tests.test_base_data_source import DummyDataSource
+from force_bdss.data_sources.tests.test_base_data_source_model import \
+    DummyDataSourceModel
+
 try:
     import mock
 except ImportError:
     from unittest import mock
 
+import testfixtures
+
 from envisage.plugin import Plugin
 from force_bdss.data_sources.base_data_source_factory import \
     BaseDataSourceFactory
@@ -21,8 +28,35 @@ class DummyDataSourceFactory(BaseDataSourceFactory):
         pass
 
 
+class DummyDataSourceFactoryFast(BaseDataSourceFactory):
+    id = "foo"
+
+    name = "bar"
+
+    model_class = DummyDataSourceModel
+
+    data_source_class = DummyDataSource
+
+
 class TestBaseDataSourceFactory(unittest.TestCase):
     def test_initialization(self):
         factory = DummyDataSourceFactory(mock.Mock(spec=Plugin))
         self.assertEqual(factory.id, 'foo')
         self.assertEqual(factory.name, 'bar')
+
+    def test_fast_specification(self):
+        factory = DummyDataSourceFactoryFast(mock.Mock(spec=Plugin))
+        self.assertIsInstance(factory.create_data_source(), DummyDataSource)
+        self.assertIsInstance(factory.create_model(), DummyDataSourceModel)
+
+    def test_fast_specification_errors(self):
+        factory = DummyDataSourceFactoryFast(mock.Mock(spec=Plugin))
+        factory.model_class = None
+        factory.data_source_class = None
+
+        with testfixtures.LogCapture():
+            with self.assertRaises(RuntimeError):
+                factory.create_data_source()
+
+            with self.assertRaises(RuntimeError):
+                factory.create_model()
diff --git a/force_bdss/kpi/base_kpi_calculator_factory.py b/force_bdss/kpi/base_kpi_calculator_factory.py
index a2b7e8b..de210b5 100644
--- a/force_bdss/kpi/base_kpi_calculator_factory.py
+++ b/force_bdss/kpi/base_kpi_calculator_factory.py
@@ -63,7 +63,7 @@ class BaseKPICalculatorFactory(ABCHasStrictTraits):
             log.error(msg)
             raise RuntimeError(msg)
 
-        return self.data_source_class(self)
+        return self.kpi_calculator_class(self)
 
     def create_model(self, model_data=None):
         """Factory method.
diff --git a/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py b/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py
index 188bc4c..91a81a3 100644
--- a/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py
+++ b/force_bdss/kpi/tests/test_base_kpi_calculator_factory.py
@@ -1,8 +1,10 @@
 import unittest
+import testfixtures
 from envisage.plugin import Plugin
 
-from force_bdss.kpi.base_kpi_calculator import BaseKPICalculator
-from force_bdss.kpi.base_kpi_calculator_model import BaseKPICalculatorModel
+from force_bdss.kpi.tests.test_base_kpi_calculator import DummyKPICalculator
+from force_bdss.kpi.tests.test_base_kpi_calculator_model import \
+    DummyKPICalculatorModel
 
 try:
     import mock
@@ -12,9 +14,6 @@ except ImportError:
 from force_bdss.kpi.base_kpi_calculator_factory import \
     BaseKPICalculatorFactory
 
-kpi_calculator = mock.Mock(spec=BaseKPICalculator)
-kpi_calculator_model = mock.Mock(spec=BaseKPICalculatorModel)
-
 
 class DummyKPICalculatorFactory(BaseKPICalculatorFactory):
     id = "foo"
@@ -28,14 +27,14 @@ class DummyKPICalculatorFactory(BaseKPICalculatorFactory):
         pass
 
 
-class DummyKPICalculatorFactory2(BaseKPICalculatorFactory):
+class DummyKPICalculatorFactoryFast(BaseKPICalculatorFactory):
     id = "foo"
 
     name = "bar"
 
-    kpi_calculator_class = kpi_calculator
+    kpi_calculator_class = DummyKPICalculator
 
-    model_class = kpi_calculator_model
+    model_class = DummyKPICalculatorModel
 
 
 class TestBaseKPICalculatorFactory(unittest.TestCase):
@@ -45,4 +44,21 @@ class TestBaseKPICalculatorFactory(unittest.TestCase):
         self.assertEqual(factory.name, 'bar')
 
     def test_fast_definition(self):
-        factory = DummyKPICalculatorFactory2(mock.Mock(spec=Plugin))
+        factory = DummyKPICalculatorFactoryFast(mock.Mock(spec=Plugin))
+        self.assertIsInstance(factory.create_kpi_calculator(),
+                              DummyKPICalculator)
+
+        self.assertIsInstance(factory.create_model(),
+                              DummyKPICalculatorModel)
+
+    def test_fast_definition_errors(self):
+        factory = DummyKPICalculatorFactoryFast(mock.Mock(spec=Plugin))
+        factory.kpi_calculator_class = None
+        factory.model_class = None
+
+        with testfixtures.LogCapture():
+            with self.assertRaises(RuntimeError):
+                factory.create_kpi_calculator()
+
+            with self.assertRaises(RuntimeError):
+                factory.create_model()
diff --git a/force_bdss/mco/tests/test_base_mco_factory.py b/force_bdss/mco/tests/test_base_mco_factory.py
index 0c96a3b..2a68dcc 100644
--- a/force_bdss/mco/tests/test_base_mco_factory.py
+++ b/force_bdss/mco/tests/test_base_mco_factory.py
@@ -1,5 +1,12 @@
 import unittest
 
+import testfixtures
+
+from force_bdss.mco.base_mco_model import BaseMCOModel
+from force_bdss.mco.tests.test_base_mco import DummyMCO
+from force_bdss.mco.tests.test_base_mco_communicator import \
+    DummyMCOCommunicator
+
 try:
     import mock
 except ImportError:
@@ -28,8 +35,49 @@ class DummyMCOFactory(BaseMCOFactory):
         return []
 
 
+class DummyMCOModel(BaseMCOModel):
+    pass
+
+
+class DummyMCOFactoryFast(BaseMCOFactory):
+    id = "foo"
+
+    name = "bar"
+
+    optimizer_class = DummyMCO
+
+    model_class = DummyMCOModel
+
+    communicator_class = DummyMCOCommunicator
+
+
 class TestBaseMCOFactory(unittest.TestCase):
     def test_initialization(self):
         factory = DummyMCOFactory(mock.Mock(spec=Plugin))
         self.assertEqual(factory.id, 'foo')
         self.assertEqual(factory.name, 'bar')
+
+    def test_fast_definition(self):
+        factory = DummyMCOFactoryFast(mock.Mock(spec=Plugin))
+        self.assertIsInstance(factory.create_optimizer(),
+                              DummyMCO)
+        self.assertIsInstance(factory.create_communicator(),
+                              DummyMCOCommunicator)
+        self.assertIsInstance(factory.create_model(),
+                              DummyMCOModel)
+
+    def test_fast_definition_errors(self):
+        factory = DummyMCOFactoryFast(mock.Mock(spec=Plugin))
+        factory.optimizer_class = None
+        factory.model_class = None
+        factory.communicator_class = None
+
+        with testfixtures.LogCapture():
+            with self.assertRaises(RuntimeError):
+                factory.create_optimizer()
+
+            with self.assertRaises(RuntimeError):
+                factory.create_communicator()
+
+            with self.assertRaises(RuntimeError):
+                factory.create_model()
diff --git a/force_bdss/notification_listeners/tests/__init__.py b/force_bdss/notification_listeners/tests/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/force_bdss/notification_listeners/tests/test_base_notification_listener_factory.py b/force_bdss/notification_listeners/tests/test_base_notification_listener_factory.py
new file mode 100644
index 0000000..0298280
--- /dev/null
+++ b/force_bdss/notification_listeners/tests/test_base_notification_listener_factory.py
@@ -0,0 +1,77 @@
+import unittest
+
+import testfixtures
+from envisage.plugin import Plugin
+
+try:
+    import mock
+except ImportError:
+    from unittest import mock
+
+from force_bdss.notification_listeners.base_notification_listener import \
+    BaseNotificationListener
+from force_bdss.notification_listeners.base_notification_listener_factory \
+    import \
+    BaseNotificationListenerFactory
+from force_bdss.notification_listeners.base_notification_listener_model \
+    import \
+    BaseNotificationListenerModel
+
+
+class DummyNotificationListener(BaseNotificationListener):
+    def deliver(self, event):
+        pass
+
+
+class DummyNotificationListenerModel(BaseNotificationListenerModel):
+    pass
+
+
+class DummyNotificationListenerFactory(BaseNotificationListenerFactory):
+    id = "foo"
+
+    name = "bar"
+
+    def create_listener(self):
+        return DummyNotificationListener(self)
+
+    def create_model(self, model_data=None):
+        return DummyNotificationListenerModel(self)
+
+
+class DummyNotificationListenerFactoryFast(BaseNotificationListenerFactory):
+    id = "foo"
+
+    name = "bar"
+
+    listener_class = DummyNotificationListener
+
+    model_class = DummyNotificationListenerModel
+
+
+class TestBaseNotificationListenerFactory(unittest.TestCase):
+    def test_initialization(self):
+        factory = DummyNotificationListenerFactory(mock.Mock(spec=Plugin))
+        self.assertEqual(factory.id, 'foo')
+        self.assertEqual(factory.name, 'bar')
+
+    def test_fast_definition(self):
+        factory = DummyNotificationListenerFactoryFast(mock.Mock(spec=Plugin))
+
+        self.assertIsInstance(factory.create_listener(),
+                              DummyNotificationListener)
+
+        self.assertIsInstance(factory.create_model(),
+                              DummyNotificationListenerModel)
+
+    def test_fast_definition_errors(self):
+        factory = DummyNotificationListenerFactoryFast(mock.Mock(spec=Plugin))
+        factory.listener_class = None
+        factory.model_class = None
+
+        with testfixtures.LogCapture():
+            with self.assertRaises(RuntimeError):
+                factory.create_model()
+
+            with self.assertRaises(RuntimeError):
+                factory.create_listener()
diff --git a/force_bdss/ui_hooks/tests/test_base_ui_hooks_factory.py b/force_bdss/ui_hooks/tests/test_base_ui_hooks_factory.py
index 449fc91..dd65ce8 100644
--- a/force_bdss/ui_hooks/tests/test_base_ui_hooks_factory.py
+++ b/force_bdss/ui_hooks/tests/test_base_ui_hooks_factory.py
@@ -1,5 +1,10 @@
 import unittest
 
+import testfixtures
+
+from force_bdss.ui_hooks.tests.test_base_ui_hooks_manager import \
+    DummyUIHooksManager
+
 try:
     import mock
 except ImportError:
@@ -9,13 +14,34 @@ from envisage.api import Plugin
 from ..base_ui_hooks_factory import BaseUIHooksFactory
 
 
-class NullUIHooksFactory(BaseUIHooksFactory):
+class DummyUIHooksFactory(BaseUIHooksFactory):
     def create_ui_hooks_manager(self):
-        return None
+        return DummyUIHooksManager(self)
+
+
+class DummyUIHooksFactoryFast(BaseUIHooksFactory):
+    ui_hooks_manager_class = DummyUIHooksManager
 
 
 class TestBaseUIHooksFactory(unittest.TestCase):
     def test_initialize(self):
         mock_plugin = mock.Mock(spec=Plugin)
-        factory = NullUIHooksFactory(plugin=mock_plugin)
+        factory = DummyUIHooksFactory(plugin=mock_plugin)
         self.assertEqual(factory.plugin, mock_plugin)
+
+    def test_fast_definition(self):
+        mock_plugin = mock.Mock(spec=Plugin)
+        factory = DummyUIHooksFactoryFast(plugin=mock_plugin)
+
+        self.assertIsInstance(
+            factory.create_ui_hooks_manager(),
+            DummyUIHooksManager)
+
+    def test_fast_definition_errors(self):
+        mock_plugin = mock.Mock(spec=Plugin)
+        factory = DummyUIHooksFactoryFast(plugin=mock_plugin)
+        factory.ui_hooks_manager_class = None
+
+        with testfixtures.LogCapture():
+            with self.assertRaises(RuntimeError):
+                factory.create_ui_hooks_manager()
diff --git a/force_bdss/ui_hooks/tests/test_base_ui_hooks_manager.py b/force_bdss/ui_hooks/tests/test_base_ui_hooks_manager.py
index 8724a19..5e3afd4 100644
--- a/force_bdss/ui_hooks/tests/test_base_ui_hooks_manager.py
+++ b/force_bdss/ui_hooks/tests/test_base_ui_hooks_manager.py
@@ -8,9 +8,13 @@ except ImportError:
     from unittest import mock
 
 
+class DummyUIHooksManager(BaseUIHooksManager):
+    pass
+
+
 class TestBaseUIHooksManager(unittest.TestCase):
     def test_initialization(self):
         mock_factory = mock.Mock(spec=BaseUIHooksFactory)
-        mgr = BaseUIHooksManager(mock_factory)
+        mgr = DummyUIHooksManager(mock_factory)
 
         self.assertEqual(mgr.factory, mock_factory)
-- 
GitLab