devtools/testlib.py
changeset 9402 2c48c091b6a2
parent 9124 d1d4b3669e41
parent 9363 d773589b6d46
child 9404 3e3e9b37e177
equal deleted inserted replaced
9127:aff75b69db92 9402:2c48c091b6a2
    88 
    88 
    89 # email handling, to test emails sent by an application ########################
    89 # email handling, to test emails sent by an application ########################
    90 
    90 
    91 MAILBOX = []
    91 MAILBOX = []
    92 
    92 
    93 class Email:
    93 class Email(object):
    94     """you'll get instances of Email into MAILBOX during tests that trigger
    94     """you'll get instances of Email into MAILBOX during tests that trigger
    95     some notification.
    95     some notification.
    96 
    96 
    97     * `msg` is the original message object
    97     * `msg` is the original message object
    98 
    98 
    99     * `recipients` is a list of email address which are the recipients of this
    99     * `recipients` is a list of email address which are the recipients of this
   100       message
   100       message
   101     """
   101     """
   102     def __init__(self, recipients, msg):
   102     def __init__(self, fromaddr, recipients, msg):
       
   103         self.fromaddr = fromaddr
   103         self.recipients = recipients
   104         self.recipients = recipients
   104         self.msg = msg
   105         self.msg = msg
   105 
   106 
   106     @property
   107     @property
   107     def message(self):
   108     def message(self):
   124 class MockSMTP:
   125 class MockSMTP:
   125     def __init__(self, server, port):
   126     def __init__(self, server, port):
   126         pass
   127         pass
   127     def close(self):
   128     def close(self):
   128         pass
   129         pass
   129     def sendmail(self, helo_addr, recipients, msg):
   130     def sendmail(self, fromaddr, recipients, msg):
   130         MAILBOX.append(Email(recipients, msg))
   131         MAILBOX.append(Email(fromaddr, recipients, msg))
   131 
   132 
   132 cwconfig.SMTP = MockSMTP
   133 cwconfig.SMTP = MockSMTP
   133 
   134 
   134 
   135 
   135 class TestCaseConnectionProxy(object):
   136 class TestCaseConnectionProxy(object):
   389         else:
   390         else:
   390             return self.cnx
   391             return self.cnx
   391 
   392 
   392     @nocoverage
   393     @nocoverage
   393     @deprecated('[4.0] explicitly use RepoAccess object in test instead')
   394     @deprecated('[4.0] explicitly use RepoAccess object in test instead')
   394     def execute(self, rql, args=None, eidkey=None, req=None):
   395     def execute(self, rql, args=None, req=None):
   395         """executes <rql>, builds a resultset, and returns a couple (rset, req)
   396         """executes <rql>, builds a resultset, and returns a couple (rset, req)
   396         where req is a FakeRequest
   397         where req is a FakeRequest
   397         """
   398         """
   398         if eidkey is not None:
       
   399             warn('[3.8] eidkey is deprecated, you can safely remove this argument',
       
   400                  DeprecationWarning, stacklevel=2)
       
   401         req = req or self.request(rql=rql)
   399         req = req or self.request(rql=rql)
   402         return req.execute(unicode(rql), args)
   400         return req.execute(unicode(rql), args)
   403 
   401 
   404     @nocoverage
   402     @nocoverage
   405     @deprecated('[4.0] explicitly use RepoAccess object in test instead')
   403     @deprecated('[4.0] explicitly use RepoAccess object in test instead')
   430         return req
   428         return req
   431 
   429 
   432     # server side db api #######################################################
   430     # server side db api #######################################################
   433 
   431 
   434     @deprecated('[4.0] explicitly use RepoAccess object in test instead')
   432     @deprecated('[4.0] explicitly use RepoAccess object in test instead')
   435     def sexecute(self, rql, args=None, eid_key=None):
   433     def sexecute(self, rql, args=None):
   436         if eid_key is not None:
       
   437             warn('[3.8] eid_key is deprecated, you can safely remove this argument',
       
   438                  DeprecationWarning, stacklevel=2)
       
   439         self.session.set_cnxset()
   434         self.session.set_cnxset()
   440         return self.session.execute(rql, args)
   435         return self.session.execute(rql, args)
   441 
   436 
   442 
   437 
   443     # config management ########################################################
   438     # config management ########################################################
   618     @contextmanager
   613     @contextmanager
   619     def temporary_appobjects(self, *appobjects):
   614     def temporary_appobjects(self, *appobjects):
   620         self.vreg._loadedmods.setdefault(self.__module__, {})
   615         self.vreg._loadedmods.setdefault(self.__module__, {})
   621         for obj in appobjects:
   616         for obj in appobjects:
   622             self.vreg.register(obj)
   617             self.vreg.register(obj)
       
   618             registered = getattr(obj, '__registered__', None)
       
   619             if registered:
       
   620                 for registry in obj.__registries__:
       
   621                     registered(self.vreg[registry])
   623         try:
   622         try:
   624             yield
   623             yield
   625         finally:
   624         finally:
   626             for obj in appobjects:
   625             for obj in appobjects:
   627                 self.vreg.unregister(obj)
   626                 self.vreg.unregister(obj)
  1125                 sent_to.update(msg.recipients)
  1124                 sent_to.update(msg.recipients)
  1126             self.assertSetEqual(set(recipients), sent_to)
  1125             self.assertSetEqual(set(recipients), sent_to)
  1127         if nb_msgs is not None:
  1126         if nb_msgs is not None:
  1128             self.assertEqual(len(MAILBOX), nb_msgs)
  1127             self.assertEqual(len(MAILBOX), nb_msgs)
  1129         return messages
  1128         return messages
  1130 
       
  1131     # deprecated ###############################################################
       
  1132 
       
  1133     @deprecated('[3.8] use self.execute(...).get_entity(0, 0)')
       
  1134     def entity(self, rql, args=None, eidkey=None, req=None):
       
  1135         if eidkey is not None:
       
  1136             warn('[3.8] eidkey is deprecated, you can safely remove this argument',
       
  1137                  DeprecationWarning, stacklevel=2)
       
  1138         return self.execute(rql, args, req=req).get_entity(0, 0)
       
  1139 
  1129 
  1140 
  1130 
  1141 # auto-populating test classes and utilities ###################################
  1131 # auto-populating test classes and utilities ###################################
  1142 
  1132 
  1143 from cubicweb.devtools.fill import insert_entity_queries, make_relations_queries
  1133 from cubicweb.devtools.fill import insert_entity_queries, make_relations_queries