Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

refs #220 - Raise an error when pattern/keyword is not defined

  • Loading branch information...
commit 31a31950498496f79e927acde02078e864fdbcbc 1 parent 07098c4
@rebecca-caktus rebecca-caktus authored
View
1  .gitignore
@@ -16,3 +16,4 @@ _build
*.log
*.egg
coverage.xml
+htmlcov
View
6 rapidsms/contrib/handlers/exceptions.py
@@ -0,0 +1,6 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+
+
+class HandlerError(Exception):
+ pass
View
4 rapidsms/contrib/handlers/handlers/keyword.py
@@ -6,6 +6,7 @@
from django.core.exceptions import ObjectDoesNotExist
+from ..exceptions import HandlerError
from .base import BaseHandler
@@ -45,12 +46,11 @@ def _keyword(cls):
if hasattr(cls, "keyword"):
prefix = r"^\s*(?:%s)(?:[\s,;:]+(.+))?$" % (cls.keyword)
return re.compile(prefix, re.IGNORECASE)
+ raise HandlerError('KeywordHandler must define a keyword.')
@classmethod
def dispatch(cls, router, msg):
keyword = cls._keyword()
- if keyword is None:
- return False
match = keyword.match(msg.text)
if match is None:
View
7 rapidsms/contrib/handlers/handlers/pattern.py
@@ -1,8 +1,9 @@
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
-
import re
+
+from ..exceptions import HandlerError
from .base import BaseHandler
@@ -48,13 +49,11 @@ class PatternHandler(BaseHandler):
def _pattern(cls):
if hasattr(cls, "pattern"):
return re.compile(cls.pattern, re.IGNORECASE)
+ raise HandlerError('PatternHandler must define a pattern.')
@classmethod
def dispatch(cls, router, msg):
-
pattern = cls._pattern()
- if pattern is None:
- return False
match = pattern.match(msg.text)
if match is None:
View
3  rapidsms/contrib/handlers/tests/__init__.py
@@ -1,7 +1,6 @@
#!/usr/bin/env python
# vim: ai ts=4 sts=4 et sw=4
-from .keyword import *
-from .pattern import *
from .app import *
+from .handlers import *
from .utils import *
View
140 rapidsms/contrib/handlers/tests/handlers.py
@@ -0,0 +1,140 @@
+#!/usr/bin/env python
+# vim: ai ts=4 sts=4 et sw=4
+
+from rapidsms.messages import IncomingMessage
+from rapidsms.tests.harness import RapidTest
+
+from rapidsms.contrib.handlers.exceptions import HandlerError
+from rapidsms.contrib.handlers.handlers.base import BaseHandler
+from rapidsms.contrib.handlers.tests.harness import EchoKeywordHandler
+from rapidsms.contrib.handlers.tests.harness import AdditionPatternHandler
+
+
+__all__ = ['TestBaseHandler', 'TestKeywordHandler', 'TestPatternHandler']
+
+
+class TestBaseHandler(RapidTest):
+ """Tests for rapidsms.contrib.handlers.handlers.base"""
+
+ def setUp(self):
+ self.connection = self.create_connection()
+
+ def test_dispatch(self):
+ """BaseHandler dispatch should always return False."""
+ msg = IncomingMessage(self.connection, 'hello')
+ retVal = BaseHandler.dispatch(self.router, msg)
+ self.assertFalse(retVal)
+ self.assertEqual(len(msg.responses), 0)
+
+
+class TestKeywordHandler(RapidTest):
+ """Tests for rapidsms.contrib.handlers.handlers.keyword"""
+
+ def setUp(self):
+ self.connection = self.create_connection()
+
+ def _check_dispatch(self, text, correct_response):
+ msg = IncomingMessage(self.connection, text)
+ retVal = EchoKeywordHandler.dispatch(self.router, msg)
+ if correct_response is not None:
+ self.assertTrue(retVal)
+ self.assertEqual(len(msg.responses), 1)
+ self.assertEqual(msg.responses[0].text, correct_response)
+ else:
+ self.assertFalse(retVal)
+ self.assertEqual(len(msg.responses), 0)
+
+ def test_no_keyword(self):
+ """Handler should raise an exception if there is no keyword."""
+ keyword = getattr(EchoKeywordHandler, 'keyword')
+ delattr(EchoKeywordHandler, 'keyword')
+ try:
+ with self.assertRaises(HandlerError):
+ msg = IncomingMessage(self.connection, 'hello')
+ EchoKeywordHandler.dispatch(self.router, msg)
+ finally:
+ setattr(EchoKeywordHandler, 'keyword', keyword)
+
+ def test_no_match(self):
+ """Handler should return nothing if there is no match."""
+ self._check_dispatch('no match', None)
+
+ def test_keyword_only(self):
+ """Handler should call help() if only keyword is sent."""
+ self._check_dispatch('hello', EchoKeywordHandler.HELP_TEXT)
+
+ def test_keyword_and_whitespace(self):
+ """Handler should call help() if only whitespace is after keyword."""
+ self._check_dispatch('hello ', EchoKeywordHandler.HELP_TEXT)
+
+ def test_match(self):
+ """
+ Handler should call handle() if there is non-whitespace text after
+ keyword.
+ """
+ self._check_dispatch('hello world', 'world')
+
+ def test_case_insensitive_match(self):
+ """Handler should use case-insensitive match."""
+ self._check_dispatch('HeLlO World', 'World')
+
+ def test_trailing_whitespace(self):
+ """Trailing whitespace should be passed to handler."""
+ self._check_dispatch('hello world ', 'world ')
+
+ def test_leading_whitespace(self):
+ """Prepended whitespace should not be passed to the handler."""
+ self._check_dispatch(' hello world', 'world')
+
+
+class TestPatternHandler(RapidTest):
+ """Tests for rapidsms.contrib.handlers.handlers.pattern"""
+
+ def setUp(self):
+ self.connection = self.create_connection()
+
+ def _check_dispatch(self, text, correct_response):
+ msg = IncomingMessage(self.connection, text)
+ retVal = AdditionPatternHandler.dispatch(self.router, msg)
+ if correct_response is not None:
+ self.assertTrue(retVal)
+ self.assertEqual(len(msg.responses), 1)
+ self.assertEqual(msg.responses[0].text, correct_response)
+ else:
+ self.assertFalse(retVal)
+ self.assertEqual(len(msg.responses), 0)
+
+ def test_no_pattern(self):
+ """Handler should not operate if it does not have a pattern."""
+ pattern = getattr(AdditionPatternHandler, 'pattern')
+ delattr(AdditionPatternHandler, 'pattern')
+ try:
+ with self.assertRaises(HandlerError):
+ msg = IncomingMessage(self.connection, '1 plus 2')
+ AdditionPatternHandler.dispatch(self.router, msg)
+ finally:
+ setattr(AdditionPatternHandler, 'pattern', pattern)
+
+ def test_no_match(self):
+ """Handler should return False if there is no match."""
+ self._check_dispatch('no match', None)
+
+ def test_match(self):
+ """Handler should return response if there is a match."""
+ self._check_dispatch('1 plus 2', '1 + 2 = 3')
+
+ def test_case_insensitive_match(self):
+ """Handler pattern is not case sensitive."""
+ self._check_dispatch('1 PLUS 2', '1 + 2 = 3')
+
+ def test_leading_whitespace(self):
+ """Handler pattern is sensitive to leading whitespace."""
+ self._check_dispatch(' 1 plus 2', None)
+
+ def test_trailing_whitespace(self):
+ """Handler pattern is sensitive to trailing whitespace."""
+ self._check_dispatch('1 plus 2 ', None)
+
+ def test_extra_whitespace(self):
+ """Handler pattern is sensitive to extra whitespace."""
+ self._check_dispatch('1 plus 2', None)
View
57 rapidsms/contrib/handlers/tests/keyword.py
@@ -1,57 +0,0 @@
-#!/usr/bin/env python
-# vim: ai ts=4 sts=4 et sw=4
-
-from rapidsms.contrib.handlers.tests.harness import EchoKeywordHandler
-from rapidsms.messages import IncomingMessage
-from rapidsms.tests.harness import RapidTest
-
-
-__all__ = ['TestKeywordHandler']
-
-
-class TestKeywordHandler(RapidTest):
- """Tests for rapidsms.contrib.handlers.handlers.keyword"""
-
- def setUp(self):
- self.connection = self.create_connection()
-
- def _check_dispatch(self, text, correct_response):
- msg = IncomingMessage(self.connection, text)
- retVal = EchoKeywordHandler.dispatch(self.router, msg)
- if correct_response is not None:
- self.assertEqual(retVal, True)
- self.assertEqual(len(msg.responses), 1)
- self.assertEqual(msg.responses[0].text, correct_response)
- else:
- self.assertEqual(retVal, False)
- self.assertEqual(len(msg.responses), 0)
-
- def test_no_match(self):
- """Handler should return nothing if there is no match."""
- self._check_dispatch('no match', None)
-
- def test_keyword_only(self):
- """Handler should call help() if only keyword is sent."""
- self._check_dispatch('hello', EchoKeywordHandler.HELP_TEXT)
-
- def test_keyword_and_whitespace(self):
- """Handler should call help() if only whitespace is after keyword."""
- self._check_dispatch('hello ', EchoKeywordHandler.HELP_TEXT)
-
- def test_match(self):
- """
- Handler should call handle() if there is non-whitespace text after
- keyword.
- """
- self._check_dispatch('hello world', 'world')
-
- def test_case_insensitive_match(self):
- """Handler should use case-insensitive match."""
- self._check_dispatch('HeLlO World', 'World')
-
- def test_trailing_whitespace(self):
- """
- Trailing whitespace should be passed to handler, but prepended
- whitespace should not.
- """
- self._check_dispatch('hello world ', 'world ')
View
56 rapidsms/contrib/handlers/tests/pattern.py
@@ -1,56 +0,0 @@
-#!/usr/bin/env python
-# vim: ai ts=4 sts=4 et sw=4
-
-from rapidsms.contrib.handlers.tests.harness import AdditionPatternHandler
-from rapidsms.messages import IncomingMessage
-from rapidsms.tests.harness import RapidTest
-
-
-__all__ = ['TestPatternHandler']
-
-
-class TestPatternHandler(RapidTest):
- """Tests for rapidsms.contrib.handlers.handlers.pattern"""
-
- def setUp(self):
- self.connection = self.create_connection()
-
- def _check_dispatch(self, text, correct_response):
- msg = IncomingMessage(self.connection, text)
- retVal = AdditionPatternHandler.dispatch(self.router, msg)
- if correct_response is not None:
- self.assertEqual(retVal, True)
- self.assertEqual(len(msg.responses), 1)
- self.assertEqual(msg.responses[0].text, correct_response)
- else:
- self.assertEqual(retVal, False)
- self.assertEqual(len(msg.responses), 0)
-
- def test_no_pattern(self):
- """Handler should not operate if it does not have a pattern."""
- delattr(AdditionPatternHandler, 'pattern')
- self._check_dispatch('1 plus 2', None)
-
- def test_no_match(self):
- """Handler should return False if there is no match."""
- self._check_dispatch('no match', None)
-
- def test_match(self):
- """Handler should return response if there is a match."""
- self._check_dispatch('1 plus 2', '1 + 2 = 3')
-
- def test_case_insensitive_match(self):
- """Handler pattern is not case sensitive."""
- self._check_dispatch('1 PLUS 2', '1 + 2 = 3')
-
- def test_prepended_whitespace(self):
- """Handler pattern is sensitive to prepended whitespace."""
- self._check_dispatch(' 1 plus 2', None)
-
- def test_trailing_whitespace(self):
- """Handler pattern is sensitive to trailing whitespace."""
- self._check_dispatch('1 plus 2 ', None)
-
- def test_extra_whitespace(self):
- """Handler pattern is sensitive to extra whitespace."""
- self._check_dispatch('1 plus 2', None)
Please sign in to comment.
Something went wrong with that request. Please try again.