Added tag cubicweb-debian-version-3.4.6-1 for changeset 52dba800ca4d
"""provide utilies for web (live) unit testing:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""importsocketimportloggingfromos.pathimportjoin,dirname,existsfromStringIOimportStringIO#from twisted.application import service, strports# from twisted.internet import reactor, taskfromtwisted.web2importchannelfromtwisted.web2importserverfromtwisted.web2importstaticfromtwisted.internetimportreactorfromtwisted.internet.errorimportCannotListenErrorfromlogilab.common.testlibimportTestCaseimportcubicweb.webfromcubicweb.dbapiimportin_memory_cnxfromcubicweb.etwist.serverimportCubicWebRootResourcefromcubicweb.devtoolsimportLivetestConfiguration,init_test_databasedefget_starturl(port=7777,login=None,passwd=None):iflogin:return'http://%s:%s/view?login=%s&password=%s'%(socket.gethostname(),port,login,passwd)else:return'http://%s:%s/'%(socket.gethostname(),port)classLivetestResource(CubicWebRootResource):"""redefines main resource to search for data files in several directories"""deflocateChild(self,request,segments):"""Indicate which resource to use to process down the URL's path"""iflen(segments)andsegments[0]=='data':# Anything in data/ is treated as static filesdatadir=self.config.locate_resource(segments[1])ifdatadir:returnstatic.File(str(datadir),segments[1:])# Otherwise we use this single resourcereturnself,()defmake_site(cube,options=None):fromcubicweb.etwistimporttwconfig# trigger configuration registrationsourcefile=options.sourcefileconfig=LivetestConfiguration(cube,sourcefile,pyro_name=options.pyro_name,log_threshold=logging.DEBUG)source=config.sources()['system']init_test_database(driver=source['db-driver'],config=config)# if '-n' in sys.argv: # debug modecubicweb=LivetestResource(config,debug=True)toplevel=cubicwebwebsite=server.Site(toplevel)cube_dir=config.cube_dir(cube)forportinxrange(7777,7798):try:reactor.listenTCP(port,channel.HTTPFactory(website))saveconf(cube_dir,port,source['db-user'],source['db-password'])breakexceptCannotListenError,exc:print"port %s already in use, I will try another one"%portelse:raisecubicweb.base_url=get_starturl(port=port)print"you can go here : %s"%cubicweb.base_urldefrunserver():reactor.run()defsaveconf(templhome,port,user,passwd):importpickleconffile=file(join(templhome,'test','livetest.conf'),'w')pickle.dump((port,user,passwd,get_starturl(port,user,passwd)),conffile)conffile.close()defloadconf(filename='livetest.conf'):importpicklereturnpickle.load(file(filename))defexecute_scenario(filename,**kwargs):"""based on twill.parse.execute_file, but inserts cubicweb extensions"""fromtwill.parseimport_execute_scriptstream=StringIO('extend_with cubicweb.devtools.cubicwebtwill\n'+file(filename).read())kwargs['source']=filename_execute_script(stream,**kwargs)defhijack_twill_output(new_output):fromtwillimportcommandsastwcfromtwillimportbrowserastwbtwc.OUT=new_outputtwb.OUT=new_outputclassLiveTestCase(TestCase):sourcefile=Nonecube=''defsetUp(self):assertself.cube,"You must specify a cube in your testcase"# twill can be quite verbose ...self.twill_output=StringIO()hijack_twill_output(self.twill_output)# build a config, and get a connectionself.config=LivetestConfiguration(self.cube,self.sourcefile)_,user,passwd,_=loadconf()self.repo,self.cnx=in_memory_cnx(self.config,user,passwd)self.setup_db(self.cnx)deftearDown(self):self.teardown_db(self.cnx)defsetup_db(self,cnx):"""override setup_db() to setup your environment"""defteardown_db(self,cnx):"""override teardown_db() to clean up your environment"""defget_loggedurl(self):port,user,passwd,logged_url=loadconf()returnlogged_urldefget_anonurl(self):port,_,_,_=loadconf()return'http://%s:%s/view?login=anon&password=anon'%(socket.gethostname(),port)# convenienceexecute_scenario=staticmethod(execute_scenario)if__name__=='__main__':runserver()