From f0ee0a18b13c788906c973bb3b2a99594f27a07e Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Wed, 13 Nov 2013 14:20:58 +0100 Subject: [PATCH] move unittest helpers into a separate file --- specs/fixtures.py | 31 +++++++++++++++++++++++++++++++ specs/test_comments.py | 35 +++++------------------------------ specs/test_vote.py | 29 ++--------------------------- 3 files changed, 38 insertions(+), 57 deletions(-) create mode 100644 specs/fixtures.py diff --git a/specs/fixtures.py b/specs/fixtures.py new file mode 100644 index 0000000..91f8349 --- /dev/null +++ b/specs/fixtures.py @@ -0,0 +1,31 @@ +# -*- encoding: utf-8 -*- + +import json + + +class FakeIP(object): + + def __init__(self, app, ip): + self.app = app + self.ip = ip + + def __call__(self, environ, start_response): + environ['REMOTE_ADDR'] = self.ip + return self.app(environ, start_response) + + +class Dummy: + + status = 200 + + def __enter__(self): + return self + + def read(self): + return '' + + def __exit__(self, exc_type, exc_val, exc_tb): + pass + +curl = lambda method, host, path: Dummy() +loads = lambda data: json.loads(data.decode('utf-8')) diff --git a/specs/test_comments.py b/specs/test_comments.py index bed2355..81d90a8 100644 --- a/specs/test_comments.py +++ b/specs/test_comments.py @@ -1,6 +1,5 @@ # -*- encoding: utf-8 -*- - from __future__ import unicode_literals import os @@ -20,32 +19,8 @@ from isso import Isso, core from isso.utils import http from isso.views import comments -class Dummy: - - status = 200 - - def __enter__(self): - return self - - def read(self): - return '' - - def __exit__(self, exc_type, exc_val, exc_tb): - pass - -http.curl = lambda method, host, path: Dummy() - -loads = lambda data: json.loads(data.decode('utf-8')) - - -class FakeIP(object): - - def __init__(self, app): - self.app = app - - def __call__(self, environ, start_response): - environ['REMOTE_ADDR'] = '192.168.1.1' - return self.app(environ, start_response) +from fixtures import curl, loads, FakeIP +http.curl = curl class TestComments(unittest.TestCase): @@ -60,7 +35,7 @@ class TestComments(unittest.TestCase): pass self.app = App(conf) - self.app.wsgi_app = FakeIP(self.app.wsgi_app) + self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1") self.client = Client(self.app, Response) self.get = self.client.get @@ -307,7 +282,7 @@ class TestModeratedComments(unittest.TestCase): pass self.app = App(conf) - self.app.wsgi_app = FakeIP(self.app.wsgi_app) + self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1") self.client = Client(self.app, Response) def tearDown(self): @@ -338,7 +313,7 @@ class TestPurgeComments(unittest.TestCase): pass self.app = App(conf) - self.app.wsgi_app = FakeIP(self.app.wsgi_app) + self.app.wsgi_app = FakeIP(self.app.wsgi_app, "192.168.1.1") self.client = Client(self.app, Response) def testPurgeDoesNoHarm(self): diff --git a/specs/test_vote.py b/specs/test_vote.py index 8feb97b..874ed39 100644 --- a/specs/test_vote.py +++ b/specs/test_vote.py @@ -12,33 +12,8 @@ from werkzeug.wrappers import Response from isso import Isso, core from isso.utils import http -class Dummy: - - status = 200 - - def __enter__(self): - return self - - def read(self): - return '' - - def __exit__(self, exc_type, exc_val, exc_tb): - pass - -http.curl = lambda method, host, path: Dummy() - -loads = lambda data: json.loads(data.decode('utf-8')) - - -class FakeIP(object): - - def __init__(self, app, ip): - self.app = app - self.ip = ip - - def __call__(self, environ, start_response): - environ['REMOTE_ADDR'] = self.ip - return self.app(environ, start_response) +from fixtures import curl, loads, FakeIP +http.curl = curl class TestVote(unittest.TestCase):