diff options
| author | schneefux <schneefux+commit@schneefux.xyz> | 2014-05-24 19:30:36 -0700 |
|---|---|---|
| committer | schneefux <schneefux+commit@schneefux.xyz> | 2014-05-24 19:30:36 -0700 |
| commit | 45faa4cba77b8b7740bd8a34830abddfe5d8423e (patch) | |
| tree | 74d023f2ba11117841df90d69d168e5d84ddd63c /client/test.py | |
| parent | 6b659afc82d4a08362e4e7b49af8d8c793b8fc3c (diff) | |
| download | jasper-client-45faa4cba77b8b7740bd8a34830abddfe5d8423e.tar.gz jasper-client-45faa4cba77b8b7740bd8a34830abddfe5d8423e.zip | |
Unit tests for brain using mocks
Diffstat (limited to 'client/test.py')
| -rw-r--r-- | client/test.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/client/test.py b/client/test.py index 461ceba..06c67fe 100644 --- a/client/test.py +++ b/client/test.py @@ -1,8 +1,10 @@ import unittest +from mock import Mock, patch from urllib2 import URLError, urlopen import yaml from test_mic import Mic from modules import * +import brain def activeInternet(): @@ -94,5 +96,40 @@ class TestModules(unittest.TestCase): or "Tomorrow" in outputs[0]) +class TestBrain(unittest.TestCase): + + @staticmethod + def _emptyBrain(): + mic = Mic([]) + profile = yaml.safe_load(open("profile.yml", "r")) + return brain.Brain(mic, profile) + + @patch.object(brain, 'logError') + def testLog(self, logError): + """Does Brain correctly log errors when raised by modules?""" + my_brain = TestBrain._emptyBrain() + unclear = my_brain.modules[-1] + with patch.object(unclear, 'handle') as mocked_handle: + mocked_handle.side_effect = KeyError('foo') + my_brain.query("zzz gibberish zzz") + logError.assert_called_with() + + def testSortByPriority(self): + """Does Brain sort modules by priority?""" + my_brain = TestBrain._emptyBrain() + with_priority = filter(lambda m: hasattr(m, 'PRIORITY'), my_brain.modules) + self.assertEqual(sorted(with_priority, key=lambda m: m.PRIORITY), with_priority) + + def testPriority(self): + """Does Brain correctly send query to higher-priority module?""" + my_brain = TestBrain._emptyBrain() + hn_module = 'modules.HN' + hn = filter(lambda m: m.__name__ == hn_module, my_brain.modules)[0] + + with patch.object(hn, 'handle') as mocked_handle: + my_brain.query("hacker news") + self.assertTrue(mocked_handle.called) + + if __name__ == '__main__': unittest.main() |
