Skip to content

Instantly share code, notes, and snippets.

@judeaugustinej
Last active December 15, 2016 06:29
Show Gist options
  • Save judeaugustinej/a8ada93aefabf39ed85c4c38d4431f83 to your computer and use it in GitHub Desktop.
Save judeaugustinej/a8ada93aefabf39ed85c4c38d4431f83 to your computer and use it in GitHub Desktop.
Testing tornado app, using momoko
from tornado.ioloop import IOLoop
from tornado.httpserver import HTTPServer
from tornado.options import parse_command_line
from tornado import web
import momoko
class BaseHandler(web.RequestHandler):
@property
def db(self):
return self.application.db
class TutorialHandler(BaseHandler):
def get(self):
self.write('Some text here!')
self.finish()
def create_app():
application = web.Application([
(r'/', TutorialHandler)
], debug=True)
ioloop = IOLoop.instance()
application.db = momoko.Pool(
dsn='dbname=postgres user=devstack password=root',
size=1,
ioloop=ioloop,
)
# this is a one way to run ioloop in sync
future = application.db.connect()
ioloop.add_future(future, lambda f: ioloop.stop())
ioloop.start()
future.result() # raises exception on connection error
return application, ioloop
if __name__ == "__main__":
parse_command_line()
application, ioloop = create_app()
http_server = HTTPServer(application)
http_server.listen(8888, 'localhost')
ioloop.start()
coverage==4.2
Momoko==2.2.4
tornado==4.4.2
import unittest
from tornado import testing
from hello import create_app
class SimpleTest(testing.AsyncHTTPTestCase):
def get_app(self):
application, ioloop = create_app()
return application
def get_new_ioloop(self):
application, ioloop = create_app()
return ioloop
@testing.gen_test
def test_endpoint_status_code(self):
response = yield self.http_client.fetch(self.get_url("/"), method='GET')
self.assertEqual(response.code, 200)
if __name__ == "__main__":
unittest.main(verbosity=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment