Skip to content

Commit

Permalink
增加brain和diagnose模块的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
wzpan committed May 31, 2017
1 parent c2e515a commit cf06a46
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions client/plugins/Echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

WORDS = [u"ECHO", u"CHUANHUA"]

PRIORITY = 0

def handle(text, mic, profile, wxbot=None):
"""
Expand Down
37 changes: 37 additions & 0 deletions tests/test_brain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/env python2
# -*- coding: utf-8-*-
from nose.tools import *
import mock
from client import brain, test_mic


DEFAULT_PROFILE = {
'prefers_email': False,
'timezone': 'HKT'
}


class TestBrain():

@staticmethod
def _emptyBrain():
mic = test_mic.Mic([])
profile = DEFAULT_PROFILE
return brain.Brain(mic, profile)

def testSortByPriority(self):
"""Does Brain sort plugins by priority?"""
my_brain = TestBrain._emptyBrain()
priorities = filter(lambda m: hasattr(m, 'PRIORITY'), my_brain.plugins)
target = sorted(priorities, key=lambda m: m.PRIORITY, reverse=True)
assert target == priorities

def testPriority(self):
"""Does Brain correctly send query to higher-priority plugin?"""
my_brain = TestBrain._emptyBrain()
echo_plugin = 'Echo'
hn = filter(lambda m: m.__name__ == echo_plugin, my_brain.plugins)[0]

with mock.patch.object(hn, 'handle') as mocked_handle:
my_brain.query(["echo 你好吗"])
assert mocked_handle.called
11 changes: 11 additions & 0 deletions tests/test_diagnose.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python2
# -*- coding: utf-8-*-
from nose.tools import *
from client import diagnose


def testPythonImportCheck():
# This a python stdlib module that definitely exists
assert diagnose.check_python_import("os")
# I sincerly hope nobody will ever create a package with that name
assert not diagnose.check_python_import("nonexistant_package")

0 comments on commit cf06a46

Please sign in to comment.