class Messages(object): """ I provide a uniform API to deal with all SMS in the system (SIM, DB, etc.) """ def __init__(self, sconn=None): self.sconn = sconn self.smanager = SMSManager() def close(self): self.smanager.close() self.sconn = None def add_messages(self, smslist, where=None): if where: # where is only set when we store a draft or when a copy of # a sms is kept on the sent treeview return self.smanager.add_messages(smslist, where) responses = [self.add_message(sms) for sms in smslist] return defer.gatherResults(responses) def add_message(self, sms, where=None): if where: # where is only set when is a DB SMS return defer.maybeDeferred(self.smanager.add_message, sms, where) if is_sim_message(sms): raise NotImplementedError() def get_messages(self): d = self.sconn.get_sms() d.addCallback(lambda sim_sms: self.smanager.get_messages() + sim_sms) return d def get_message(self, sms): if is_sim_message(sms): return self.sconn.get_sms_by_index(sms.index) else: raise NotImplementedError() def delete_messages(self, smslist): resp = [] for sms in smslist: if is_sim_message(sms): resp.append(self.sconn.delete_sms(sms.get_index())) else: d = defer.maybeDeferred(self.smanager.delete_message_by_id, sms.get_index()) resp.append(d) return defer.gatherResults(resp) def delete_objs(self, objs): return self.delete_messages(objs)
class TestSMSManager(unittest.TestCase): """Test for L{vmc.common.persistent.SMSManager} functionality""" def setUpClass(self): self.mana = SMSManager(TMPFILE) self.now = datetime.datetime.now() def tearDownClass(self): self.mana.close() shutil.rmtree(TMPFILE) def test_add_messages(self): now = datetime.datetime.now() _sms1 = ShortMessage(u'+4554354333', u'how are ya', self.now) _sms2 = ShortMessage(u'+3546564564', u'hola!', self.now) _sms3 = ShortMessage(u'+4354534534', u'adios', self.now) _list = [_sms1, _sms2, _sms3] responses = self.mana.add_messages(_list, where=2) for _sms in [_sms1, _sms2, _sms3]: self.failUnlessIn(_sms, responses) def test_delete_messages(self): smslist = [ ShortMessage(u'+455438983', u'hey man', self.now), ShortMessage(u'+4554354333', u'how are ya', self.now), ShortMessage(u'+3546564564', u'hola!', self.now), ShortMessage(u'+4354534534', u'adios', self.now), ] responses = self.mana.add_messages(smslist, where=2) exp = responses.pop(0) for _sms in responses: self.mana.delete_message_by_id(_sms.get_index()) messages = list(self.mana.get_messages()) self.failUnlessIn(exp, messages)
def __init__(self, sconn=None): self.sconn = sconn self.smanager = SMSManager()
def setUpClass(self): self.mana = SMSManager(TMPFILE) self.now = datetime.datetime.now()