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)
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()
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 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
def setUp (self, default_lang='en'): """ default_lang specifies the default language in ISO 639/X in in which sms messages are expected (note this code should correspond to the contents of contrib/locale/(code) (currently, we only support testing one language at a time) """ 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) i18n_init(default_lang, [[default_lang]] )
def test_backend_spomc(self): router = MockRouter() backend = Backend(router) backend.configure(host="localhost", port=65000) self.assertEquals(type(backend), Backend, "SPOMC backend loads") self.assertEquals(type(backend.client), Client, "SPOMC backend has Spomsky client")
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 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)
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
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")
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")
def test_backend_irc (self): router = MockRouter.instance() try: import irclib from rapidsms.backends.irc import Backend backend = Backend("irc", router) backend.configure(host="localhost",nick="test",channels="#test1,#test2") self.assertEquals(type(backend), Backend, "IRC backend loads") self.assertEquals(backend.nick, "test", "IRC backend has nick set") self.assertEquals(backend.host, "localhost", "IRC backend has host set") self.assertEquals(backend.channels, ["#test1","#test2"], "IRC backend has channels correctly set") except ImportError: pass
def test_backend_irc(self): router = MockRouter() try: import irclib from rapidsms.backends.irc import Backend backend = Backend(router) backend.configure(host="localhost", nick="test", channels="#test1,#test2") self.assertEquals(type(backend), Backend, "IRC backend loads") self.assertEquals(backend.nick, "test", "IRC backend has nick set") self.assertEquals(backend.host, "localhost", "IRC backend has host set") self.assertEquals(backend.channels, ["#test1", "#test2"], "IRC backend has channels correctly set") except ImportError: pass
def setUp(self): self.router = MockRouter()
def setUp (self): self.router = MockRouter() self.backend = Backend(self.router) self.router.add_backend(self.backend)
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()
def setUp (self): self.router = MockRouter() self.backend = Backend(self.router, "testing") self.router.add_backend(self.backend)
def setUp(self): self.router = MockRouter() self.backend = Backend(self.router) self.router.add_backend(self.backend)