Move message format from Feeder to Sender class
darcs-hash:20070515152352-86b55-d8cc1aeec67125efc83681fe0bfec4b12c5fbf3b.gz
This commit is contained in:
@@ -32,6 +32,8 @@ from jcl.jabber.component import JCLComponent
|
|||||||
from jcl.lang import Lang
|
from jcl.lang import Lang
|
||||||
from jcl.model.account import Account
|
from jcl.model.account import Account
|
||||||
|
|
||||||
|
from pyxmpp.message import Message
|
||||||
|
|
||||||
class FeederComponent(JCLComponent):
|
class FeederComponent(JCLComponent):
|
||||||
"""Implement a feeder sender behavior based on the
|
"""Implement a feeder sender behavior based on the
|
||||||
regular interval behavior of JCLComponent
|
regular interval behavior of JCLComponent
|
||||||
@@ -64,8 +66,8 @@ class FeederComponent(JCLComponent):
|
|||||||
self.db_connect()
|
self.db_connect()
|
||||||
for _account in Account.select(clauseTables = ["account"], \
|
for _account in Account.select(clauseTables = ["account"], \
|
||||||
orderBy = "user_jid"):
|
orderBy = "user_jid"):
|
||||||
for data in self.feeder.feed(_account):
|
for subject, body in self.feeder.feed(_account):
|
||||||
self.sender.send(_account, data)
|
self.sender.send(_account, subject, body)
|
||||||
self.db_disconnect()
|
self.db_disconnect()
|
||||||
|
|
||||||
|
|
||||||
@@ -85,7 +87,30 @@ class Sender(object):
|
|||||||
def __init__(self, component = None):
|
def __init__(self, component = None):
|
||||||
self.component = component
|
self.component = component
|
||||||
|
|
||||||
def send(self, to_account, data):
|
def send(self, to_account, subject, body):
|
||||||
"""Send data to given account"""
|
"""Send data (subject and body) to given account"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
class MessageSender(Sender):
|
||||||
|
"""Send data as Jabber Message"""
|
||||||
|
|
||||||
|
def send(self, to_account, subject, body):
|
||||||
|
"""Implement abstract method from Sender class and send data as Jabber message"""
|
||||||
|
self.component.stream.send(Message(\
|
||||||
|
from_jid = to_account.jid, \
|
||||||
|
to_jid = to_account.user_jid, \
|
||||||
|
subject = subject, \
|
||||||
|
stanza_type = "normal", \
|
||||||
|
body = body))
|
||||||
|
|
||||||
|
class HeadlineSender(Sender):
|
||||||
|
"""Send data as Jabber Headline"""
|
||||||
|
|
||||||
|
def send(self, to_account, subject, body):
|
||||||
|
"""Implement abstract method from Sender class and send data as Jabber headline"""
|
||||||
|
self.component.stream.send(Message(\
|
||||||
|
from_jid = to_account.jid, \
|
||||||
|
to_jid = to_account.user_jid, \
|
||||||
|
subject = subject, \
|
||||||
|
stanza_type = "headline", \
|
||||||
|
body = body))
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ from sqlobject.dbconnection import TheURIOpener
|
|||||||
from pyxmpp.message import Message
|
from pyxmpp.message import Message
|
||||||
|
|
||||||
from jcl.jabber.component import JCLComponent
|
from jcl.jabber.component import JCLComponent
|
||||||
from jcl.jabber.feeder import FeederComponent, Feeder, Sender
|
from jcl.jabber.feeder import FeederComponent, Feeder, Sender, MessageSender, HeadlineSender
|
||||||
from jcl.model.account import Account
|
from jcl.model.account import Account
|
||||||
from jcl.model import account
|
from jcl.model import account
|
||||||
|
|
||||||
@@ -97,16 +97,10 @@ class FeederComponent_TestCase(JCLComponent_TestCase):
|
|||||||
def test_handle_tick(self):
|
def test_handle_tick(self):
|
||||||
class AccountFeeder(Feeder):
|
class AccountFeeder(Feeder):
|
||||||
def feed(self, _account):
|
def feed(self, _account):
|
||||||
return ["user_jid: " + _account.user_jid, \
|
return [("Simple Message for account " + _account.name,
|
||||||
"jid: " + _account.jid]
|
"user_jid: " + _account.user_jid), \
|
||||||
|
("Simple Message for account " + _account.name, \
|
||||||
class MessageAccountSender(Sender):
|
"jid: " + _account.jid)]
|
||||||
def send(self, _account, data):
|
|
||||||
self.component.stream.send(Message(\
|
|
||||||
from_jid = _account.jid, \
|
|
||||||
to_jid = _account.user_jid, \
|
|
||||||
subject = "Simple Message for account " + _account.name, \
|
|
||||||
body = data))
|
|
||||||
|
|
||||||
self.comp.stream = MockStream()
|
self.comp.stream = MockStream()
|
||||||
self.comp.stream_class = MockStream
|
self.comp.stream_class = MockStream
|
||||||
@@ -121,7 +115,7 @@ class FeederComponent_TestCase(JCLComponent_TestCase):
|
|||||||
name = "account2", \
|
name = "account2", \
|
||||||
jid = "account2@jcl.test.com")
|
jid = "account2@jcl.test.com")
|
||||||
self.comp.feeder = AccountFeeder(self.comp)
|
self.comp.feeder = AccountFeeder(self.comp)
|
||||||
self.comp.sender = MessageAccountSender(self.comp)
|
self.comp.sender = MessageSender(self.comp)
|
||||||
self.comp.handle_tick()
|
self.comp.handle_tick()
|
||||||
|
|
||||||
messages_sent = self.comp.stream.sent
|
messages_sent = self.comp.stream.sent
|
||||||
@@ -173,13 +167,63 @@ class Feeder_TestCase(unittest.TestCase):
|
|||||||
class Sender_TestCase(unittest.TestCase):
|
class Sender_TestCase(unittest.TestCase):
|
||||||
def test_send_exist(self):
|
def test_send_exist(self):
|
||||||
sender = Sender()
|
sender = Sender()
|
||||||
self.assertRaises(NotImplementedError, sender.send, None, None)
|
self.assertRaises(NotImplementedError, sender.send, None, None, None)
|
||||||
|
|
||||||
|
class MessageSender_TestCase(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
if os.path.exists(DB_PATH):
|
||||||
|
os.unlink(DB_PATH)
|
||||||
|
self.comp = FeederComponent("jcl.test.com",
|
||||||
|
"password",
|
||||||
|
"localhost",
|
||||||
|
"5347",
|
||||||
|
'sqlite://' + DB_URL)
|
||||||
|
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||||
|
Account.createTable(ifNotExists = True)
|
||||||
|
del account.hub.threadConnection
|
||||||
|
self.sender = MessageSender(self.comp)
|
||||||
|
self.message_type = "normal"
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||||
|
Account.dropTable(ifExists = True)
|
||||||
|
del TheURIOpener.cachedURIs['sqlite://' + DB_URL]
|
||||||
|
account.hub.threadConnection.close()
|
||||||
|
del account.hub.threadConnection
|
||||||
|
if os.path.exists(DB_PATH):
|
||||||
|
os.unlink(DB_PATH)
|
||||||
|
|
||||||
|
def test_send(self):
|
||||||
|
self.comp.stream = MockStream()
|
||||||
|
self.comp.stream_class = MockStream
|
||||||
|
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||||
|
account11 = Account(user_jid = "user1@test.com", \
|
||||||
|
name = "account11", \
|
||||||
|
jid = "account11@jcl.test.com")
|
||||||
|
self.sender.send(account11, "subject", "Body message")
|
||||||
|
self.assertEquals(len(self.comp.stream.sent), 1)
|
||||||
|
message = self.comp.stream.sent[0]
|
||||||
|
self.assertEquals(message.get_from(), account11.jid)
|
||||||
|
self.assertEquals(message.get_to(), account11.user_jid)
|
||||||
|
self.assertEquals(message.get_subject(), "subject")
|
||||||
|
self.assertEquals(message.get_body(), "Body message")
|
||||||
|
self.assertEquals(message.get_type(), self.message_type)
|
||||||
|
del account.hub.threadConnection
|
||||||
|
|
||||||
|
class HeadlineSender_TestCase(MessageSender_TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
MessageSender_TestCase.setUp(self)
|
||||||
|
self.sender = HeadlineSender(self.comp)
|
||||||
|
self.message_type = "headline"
|
||||||
|
|
||||||
|
|
||||||
def suite():
|
def suite():
|
||||||
suite = unittest.TestSuite()
|
suite = unittest.TestSuite()
|
||||||
suite.addTest(unittest.makeSuite(FeederComponent_TestCase, 'test'))
|
suite.addTest(unittest.makeSuite(FeederComponent_TestCase, 'test'))
|
||||||
suite.addTest(unittest.makeSuite(Feeder_TestCase, 'test'))
|
suite.addTest(unittest.makeSuite(Feeder_TestCase, 'test'))
|
||||||
suite.addTest(unittest.makeSuite(Sender_TestCase, 'test'))
|
suite.addTest(unittest.makeSuite(Sender_TestCase, 'test'))
|
||||||
|
suite.addTest(unittest.makeSuite(MessageSender_TestCase, 'test'))
|
||||||
|
suite.addTest(unittest.makeSuite(HeadlineSender_TestCase, 'test'))
|
||||||
return suite
|
return suite
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user