예제 #1
0
class TestBackend(unittest.TestCase):
    def setUp (self):
        self.router = MockRouter()
        self.backend = Backend(self.router)
        self.router.add_backend(self.backend)

    def test__properties (self):
        self.assertEquals(self.backend.title, "backend")
        self.assertEquals(self.backend.router, self.router)
        self.assertFalse(self.backend.running)

    def test_start_stop (self):
        self.router.start()
        time.sleep(0.5)
        self.assertTrue(self.backend.running, "backend starts when router starts")
        self.router.stop()
        time.sleep(2.5)
        self.assertFalse(self.backend.running, "backend stops when router stops")
    
    def test_message (self):
        msg = self.backend.message("0000", "Good morning!") 
        self.assertEquals(type(msg), Message, "message() returns a message")

    def test_route (self):
        msg = self.backend.message("0000", "Good morning!") 
        self.backend.route(msg)
        self.assertTrue(self.router.message_waiting, "backend sends to router")
예제 #2
0
class TestScript (TestCase):
    __metaclass__ = MetaTestScript

    """
    The scripted.TestScript class subclasses unittest.TestCase
    and allows you to define unit tests for your RapidSMS apps
    in the form of a 'conversational' script:
    
        from myapp.app import App as MyApp
        from rapidsms.tests.scripted import TestScript

        class TestMyApp (TestScript):
            apps = (MyApp,)
            testRegister = \"""
               8005551212 > register as someuser
               8005551212 < Registered new user 'someuser' for 8005551212!
            \"""

            testDirectMessage = \"""
               8005551212 > tell anotheruser what's up??
               8005550000 < someuser said "what's up??"
            \"""

    This TestMyApp class would then work exactly as any other
    unittest.TestCase subclass (so you could, for example, call
    unittest.main()).
    """
    apps = None

    def setUp (self):
        self.router = MockRouter()
        self.backend = Backend(self.router)
        self.router.add_backend(self.backend)
        if not self.apps:
            raise Exception(
                "You must define a list of apps in your TestScript class!")
        for app_class in self.apps:
            app = app_class(self.router)
            self.router.add_app(app)

    def tearDown (self):
        if self.router.running:
            self.router.stop() 

    @classmethod
    def parseScript (cls, script):
        cmds  = []
        for line in map(lambda(x): x.strip(), script.split("\n")):
            if not line or line.startswith("#"): continue
            tokens = re.split(r'([<>])', line, 1)
            num, dir, txt = map(lambda (x):x.strip(), tokens)
            # allow users to optionally put dates in the number
            # 19232922@200804150730
            if "@" in num:
                num, datestr = num.split("@")
                date = datetime.strptime(datestr, "%Y%m%d%H%M")
            else:
                date = datetime.now()
            cmds.append((num, date, dir, txt))
        return cmds
예제 #3
0
class TestScript (TestCase):
    __metaclass__ = MetaTestScript

    """
    The scripted.TestScript class subclasses unittest.TestCase
    and allows you to define unit tests for your RapidSMS apps
    in the form of a 'conversational' script:
    
        from myapp.app import App as MyApp
        from rapidsms.tests.scripted import TestScript

        class TestMyApp (TestScript):
            apps = (MyApp,)
            testRegister = \"""
               8005551212 > register as someuser
               8005551212 < Registered new user 'someuser' for 8005551212!
            \"""

            testDirectMessage = \"""
               8005551212 > tell anotheruser what's up??
               8005550000 < someuser said "what's up??"
            \"""

    This TestMyApp class would then work exactly as any other
    unittest.TestCase subclass (so you could, for example, call
    unittest.main()).
    """
    apps = None

    def setUp (self):
        self.router = MockRouter()
        self.backend = Backend(self.router)
        self.router.add_backend(self.backend)
        if not self.apps:
            raise Exception(
                "You must define a list of apps in your TestScript class!")
        for app_class in self.apps:
            app = app_class(self.router)
            self.router.add_app(app)

    def tearDown (self):
        if self.router.running:
            self.router.stop() 

    @classmethod
    def parseScript (cls, script):
        cmds  = []
        for line in map(lambda(x): x.strip(), script.split("\n")):
            if not line or line.startswith("#"): continue
            tokens = re.split(r'([<>])', line, 1)
            num, dir, txt = map(lambda (x):x.strip(), tokens)
            # allow users to optionally put dates in the number
            # 19232922@200804150730
            if "@" in num:
                num, datestr = num.split("@")
                date = datetime.strptime(datestr, "%Y%m%d%H%M")
            else:
                date = datetime.now()
            cmds.append((num, date, dir, txt))
        return cmds
예제 #4
0
class TestBackend(unittest.TestCase):
    def setUp(self):
        self.router = MockRouter()
        self.backend = Backend(self.router)
        self.router.add_backend(self.backend)

    def test__properties(self):
        self.assertEquals(self.backend.title, "backend")
        self.assertEquals(self.backend.router, self.router)
        self.assertFalse(self.backend.running)

    def test_start_stop(self):
        self.router.start()
        time.sleep(0.5)
        self.assertTrue(self.backend.running,
                        "backend starts when router starts")
        self.router.stop()
        time.sleep(2.5)
        self.assertFalse(self.backend.running,
                         "backend stops when router stops")

    def test_message(self):
        msg = self.backend.message("0000", "Good morning!")
        self.assertEquals(type(msg), Message, "message() returns a message")

    def test_route(self):
        msg = self.backend.message("0000", "Good morning!")
        self.backend.route(msg)
        self.assertTrue(self.router.message_waiting, "backend sends to router")
예제 #5
0
class TestMessage(unittest.TestCase):
    def setUp (self):
        self.router = MockRouter()
        self.backend = Backend("testing", self.router)
        self.connection = Connection(self.backend, "12345")
        self.person = Person()
        self.person.add_connection(self.connection)
        self.router.add_backend(self.backend)

    def test__init__ (self): 
        msg = Message(self.connection, "this is a test")
        self.assertEquals(msg.connection, self.connection, "connection is right (connection)")
        msg = Message(None, "this is a test", self.person)
        self.assertEquals(msg.connection, self.connection, "connection is right (person)")
        self.assertEquals(msg.text, "this is a test", "text is right")
        self.assertEquals(msg.responses, [], "responses is empty")
        self.assertRaises(Exception, Message)

    def test__unicode__ (self):
        msg = Message(self.connection, "this is a test")
        self.assertEquals(unicode(msg), "this is a test", "unicode is right")
    
    def test_peer (self):
        msg = Message(self.connection, "this is a test")
        self.assertEquals(msg.peer, "12345", "peer identifier is right")

    def test_send (self):
        self.router.start()
        msg = Message(self.connection, "this is a test")
        self.assertTrue(msg.send(), "message was sent")
        waiting = self.backend.next_message()
        self.assertEquals(msg, waiting, "the backend got the message")
        self.router.stop()

    def test_respond (self):
        msg = Message(self.connection, "this is a test")
        msg.respond("how did it go?")
        msg.respond("okay?")
        self.assertEquals(len(msg.responses), 2, "message queues responses")
        self.assertEquals(msg.responses[0].text, "how did it go?", "it went well")
        self.assertEquals(msg.responses[1].text, "okay?", "sure enough")

    def test_flush_responses (self):
        msg = Message(self.connection, "this is a test")
        self.router.start()
        msg.respond("how did it go?")
        msg.flush_responses()
        
        waiting = self.backend.next_message()
        self.assertEquals(waiting.text, "how did it go?", "the backend got the message (1)")
        msg.respond("again?")
        msg.respond("and again?")
        msg.flush_responses()

        waiting = self.backend.next_message()
        self.assertEquals(waiting.text, "again?", "the backend got the message (2)")
        waiting = self.backend.next_message()
        self.assertEquals(waiting.text, "and again?", "the backend got the message (3)")
        self.router.stop()
예제 #6
0
 def setUp(self):
     # setup a router with the email backend and echo app enabled.
     # also mark the inboxes of both accounts read
     self._read_all(CONF)
     self._read_all(CONF2)
     self.assertEqual(0, len(self._get_mail(CONF)))
     self.assertEqual(0, len(self._get_mail(CONF2)))
     
     router = MockRouter()
     backend = Backend(router)
     backend.configure(**CONF)
     router.add_backend(backend)
     router.add_app(EchoApp(router))
     self.backend = backend
     self.router = router
예제 #7
0
 def setUp(self):
     # setup a router with the email backend and echo app enabled.
     # also mark the inboxes of both accounts read
     self._read_all(CONF)
     self._read_all(CONF2)
     self.assertEqual(0, len(self._get_mail(CONF)))
     self.assertEqual(0, len(self._get_mail(CONF2)))
     
     router = MockRouter()
     backend = Backend(router)
     backend.configure(**CONF)
     router.add_backend(backend)
     router.add_app(EchoApp(router))
     self.backend = backend
     self.router = router
class TestMessage(unittest.TestCase):
    def setUp(self):
        self.router = MockRouter()
        self.backend = Backend(self.router)
        self.connection = Connection(self.backend, "12345")
        self.person = Person()
        self.person.add_connection(self.connection)
        self.router.add_backend(self.backend)

    def test__init__(self):
        msg = Message(self.connection, "this is a test")
        self.assertEquals(msg.connection, self.connection,
                          "connection is right (connection)")
        msg = Message(None, "this is a test", self.person)
        self.assertEquals(msg.connection, self.connection,
                          "connection is right (person)")
        self.assertEquals(msg.text, "this is a test", "text is right")
        self.assertEquals(msg.responses, [], "responses is empty")
        self.assertRaises(Exception, Message)

    def test__unicode__(self):
        msg = Message(self.connection, "this is a test")
        self.assertEquals(unicode(msg), "this is a test", "unicode is right")

    def test_peer(self):
        msg = Message(self.connection, "this is a test")
        self.assertEquals(msg.peer, "12345", "peer identifier is right")

    def test_send(self):
        self.router.start()
        msg = Message(self.connection, "this is a test")
        self.assertTrue(msg.send(), "message was sent")
        waiting = self.backend.next_message()
        self.assertEquals(msg, waiting, "the backend got the message")
        self.router.stop()

    def test_respond(self):
        msg = Message(self.connection, "this is a test")
        msg.respond("how did it go?")
        msg.respond("okay?")
        self.assertEquals(len(msg.responses), 2, "message queues responses")
        self.assertEquals(msg.responses[0].text, "how did it go?",
                          "it went well")
        self.assertEquals(msg.responses[1].text, "okay?", "sure enough")

    def test_flush_responses(self):
        msg = Message(self.connection, "this is a test")
        self.router.start()
        msg.respond("how did it go?")
        msg.flush_responses()

        waiting = self.backend.next_message()
        self.assertEquals(waiting.text, "how did it go?",
                          "the backend got the message (1)")
        msg.respond("again?")
        msg.respond("and again?")
        msg.flush_responses()

        waiting = self.backend.next_message()
        self.assertEquals(waiting.text, "again?",
                          "the backend got the message (2)")
        waiting = self.backend.next_message()
        self.assertEquals(waiting.text, "and again?",
                          "the backend got the message (3)")
        self.router.stop()