summaryrefslogtreecommitdiff
path: root/tests/test_brain.py
diff options
context:
space:
mode:
authorschneefux <schneefux+commit@schneefux.xyz>2014-12-30 19:09:49 +0100
committerschneefux <schneefux+commit@schneefux.xyz>2014-12-30 19:17:15 +0100
commit7d80c7d7b6c983cd631986b3c74672389c8916d9 (patch)
treee9a7e66082f2603958d8e32fb86c13a8b7653eff /tests/test_brain.py
parent74b2639b574ecaf600798d234d93e2d807f63021 (diff)
downloadjasper-client-7d80c7d7b6c983cd631986b3c74672389c8916d9.tar.gz
jasper-client-7d80c7d7b6c983cd631986b3c74672389c8916d9.zip
Split unittests into separate files and move them to tests/
Diffstat (limited to 'tests/test_brain.py')
-rw-r--r--tests/test_brain.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/tests/test_brain.py b/tests/test_brain.py
new file mode 100644
index 0000000..7b4c482
--- /dev/null
+++ b/tests/test_brain.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python2
+# -*- coding: utf-8-*-
+import unittest
+import mock
+from client import brain, test_mic
+
+
+DEFAULT_PROFILE = {
+ 'prefers_email': False,
+ 'location': 'Cape Town',
+ 'timezone': 'US/Eastern',
+ 'phone_number': '012344321'
+}
+
+
+class TestBrain(unittest.TestCase):
+
+ @staticmethod
+ def _emptyBrain():
+ mic = test_mic.Mic([])
+ profile = DEFAULT_PROFILE
+ return brain.Brain(mic, profile)
+
+ def testLog(self):
+ """Does Brain correctly log errors when raised by modules?"""
+ my_brain = TestBrain._emptyBrain()
+ unclear = my_brain.modules[-1]
+ with mock.patch.object(unclear, 'handle') as mocked_handle:
+ with mock.patch.object(my_brain._logger, 'error') as mocked_log:
+ mocked_handle.side_effect = KeyError('foo')
+ my_brain.query("zzz gibberish zzz")
+ self.assertTrue(mocked_log.called)
+
+ def testSortByPriority(self):
+ """Does Brain sort modules by priority?"""
+ my_brain = TestBrain._emptyBrain()
+ priorities = filter(lambda m: hasattr(m, 'PRIORITY'), my_brain.modules)
+ target = sorted(priorities, key=lambda m: m.PRIORITY, reverse=True)
+ self.assertEqual(target, priorities)
+
+ def testPriority(self):
+ """Does Brain correctly send query to higher-priority module?"""
+ my_brain = TestBrain._emptyBrain()
+ hn_module = 'HN'
+ hn = filter(lambda m: m.__name__ == hn_module, my_brain.modules)[0]
+
+ with mock.patch.object(hn, 'handle') as mocked_handle:
+ my_brain.query(["hacker news"])
+ self.assertTrue(mocked_handle.called)