From 4c96e58cef434820095b3a623b26e5c2e14b8963 Mon Sep 17 00:00:00 2001 From: Tom Hunger Date: Mon, 26 Jan 2015 15:31:15 +0000 Subject: [PATCH] Move testing base into a separate testing directory. --- sirius/testing/__init__.py | 0 sirius/testing/base.py | 39 +++++++++++++++++++++++++++++++++++++ sirius/web/test_claiming.py | 37 +++-------------------------------- 3 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 sirius/testing/__init__.py create mode 100644 sirius/testing/base.py diff --git a/sirius/testing/__init__.py b/sirius/testing/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sirius/testing/base.py b/sirius/testing/base.py new file mode 100644 index 0000000..46fa64e --- /dev/null +++ b/sirius/testing/base.py @@ -0,0 +1,39 @@ +from flask.ext import testing +from flask.ext import login + +from sirius.models import user +from sirius.models import hardware +from sirius.models.db import db +from sirius.web import webapp + + +class Base(testing.TestCase): + """Common base class for tests that require database interactions. + """ + + def create_app(self): + app = webapp.create_app('test') + return app + + def setUp(self): + testing.TestCase.setUp(self) + db.create_all() + self.testuser = user.User(username="testuser") + db.session.add(self.testuser) + db.session.commit() + + def tearDown(self): + db.session.remove() + db.drop_all() + + def autologin(self): + # Most disgusting hack but I see no other way to log in users: + # Flask session doesn't pick up the cookies if I call just + # login_user. The login_user call needs to be embedded in a + # request. + @self.app.route('/autologin') + def autologin(): + login.login_user(self.testuser) + return '' + + self.client.get('/autologin') diff --git a/sirius/web/test_claiming.py b/sirius/web/test_claiming.py index 790ffc0..5d3bae2 100644 --- a/sirius/web/test_claiming.py +++ b/sirius/web/test_claiming.py @@ -1,44 +1,13 @@ import flask -from flask.ext import testing from flask.ext import login from sirius.models import user from sirius.models import hardware from sirius.models.db import db from sirius.web import webapp +from sirius.testing import base - -class Base(testing.TestCase): - - def create_app(self): - app = webapp.create_app('test') - return app - - def setUp(self): - testing.TestCase.setUp(self) - db.create_all() - self.testuser = user.User(username="testuser") - db.session.add(self.testuser) - db.session.commit() - - def tearDown(self): - db.session.remove() - db.drop_all() - - def autologin(self): - # Most disgusting hack but I see no other way to log in users: - # Flask session doesn't pick up the cookies if I call just - # login_user. The login_user call needs to be embedded in a - # request. - @self.app.route('/autologin') - def autologin(): - login.login_user(self.testuser) - return '' - - self.client.get('/autologin') - - -class TestClaiming(Base): +class TestClaiming(base.Base): def test_claim_first(self): self.testuser.claim_printer('n5ry-p6x6-kth7-7hc4', 'my test printer') @@ -72,7 +41,7 @@ class TestClaiming(Base): self.assertEqual(printer.owner, self.testuser2) -class TestClaimingWeb(Base): +class TestClaimingWeb(base.Base): def get_claim_url(self): return flask.url_for( -- 2.47.3