diff --git a/recorder/test/test_recorder.py b/recorder/test/test_recorder.py index 08d400f8..275ef6e5 100644 --- a/recorder/test/test_recorder.py +++ b/recorder/test/test_recorder.py @@ -2,13 +2,13 @@ import gevent from webagg.test.testutils import TempDirTests, LiveServerTests, BaseTestClass, to_path +from webagg.test.testutils import FakeRedisTests import os import webtest -from fakeredis import FakeStrictRedis -from mock import patch from pytest import raises +from fakeredis import FakeStrictRedis from recorder.recorderapp import RecorderApp from recorder.redisindexer import WritableRedisIndexer @@ -37,7 +37,7 @@ Host: {host}\r\n\ -class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass): +class TestRecorder(LiveServerTests, FakeRedisTests, TempDirTests, BaseTestClass): @classmethod def setup_class(cls): super(TestRecorder, cls).setup_class() @@ -180,7 +180,7 @@ class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass): self._test_all_warcs('/warcs/', 2) - @patch('redis.StrictRedis', FakeStrictRedis) + #@patch('redis.StrictRedis', FakeStrictRedis) def test_record_param_user_coll(self): warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') @@ -216,7 +216,7 @@ class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass): assert warcs == {cdx['filename'].encode('utf-8'): full_path.encode('utf-8')} - @patch('redis.StrictRedis', FakeStrictRedis) + #@patch('redis.StrictRedis', FakeStrictRedis) def test_record_param_user_coll_revisit(self): warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') @@ -263,7 +263,7 @@ class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass): assert status_headers.get_header('WARC-Refers-To-Target-URI') == 'http://httpbin.org/get?foo=bar' assert status_headers.get_header('WARC-Refers-To-Date') != '' - @patch('redis.StrictRedis', FakeStrictRedis) + #@patch('redis.StrictRedis', FakeStrictRedis) def test_record_param_user_coll_skip(self): warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') @@ -288,7 +288,7 @@ class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass): res = r.zrangebylex('USER:COLL:cdxj', '[org,httpbin)/', '(org,httpbin,') assert len(res) == 2 - @patch('redis.StrictRedis', FakeStrictRedis) + #@patch('redis.StrictRedis', FakeStrictRedis) def test_record_param_user_coll_write_dupe_no_revisit(self): warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') @@ -329,7 +329,7 @@ class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass): assert os.path.isfile(path) assert len(writer.fh_cache) == 1 - @patch('redis.StrictRedis', FakeStrictRedis) + #@patch('redis.StrictRedis', FakeStrictRedis) def test_record_multiple_writes_keep_open(self): warc_path = to_path(self.root_dir + '/warcs/FOO/ABC-{hostname}-{timestamp}.warc.gz') diff --git a/webagg/test/test_handlers.py b/webagg/test/test_handlers.py index 9ffa26b2..b28b31f9 100644 --- a/webagg/test/test_handlers.py +++ b/webagg/test/test_handlers.py @@ -16,11 +16,9 @@ from pywb.utils.bufferedreaders import ChunkedDataReader from io import BytesIO import webtest - from fakeredis import FakeStrictRedis -from mock import patch -from .testutils import to_path +from .testutils import to_path, FakeRedisTests, BaseTestClass import json @@ -33,9 +31,6 @@ sources = { testapp = None -redismock = patch('redis.StrictRedis', FakeStrictRedis) -redismock.start() - def setup_module(self): live_source = SimpleAggregator({'live': LiveIndexSource()}) live_handler = DefaultResourceHandler(live_source) @@ -69,15 +64,11 @@ def setup_module(self): testapp = webtest.TestApp(app.application) -def teardown_module(self): - redismock.stop() - - def to_json_list(text): return list([json.loads(cdx) for cdx in text.rstrip().split('\n')]) -class TestResAgg(object): +class TestResAgg(FakeRedisTests, BaseTestClass): def setup(self): self.testapp = testapp diff --git a/webagg/test/testutils.py b/webagg/test/testutils.py index 4c5c42b6..a5d8677d 100644 --- a/webagg/test/testutils.py +++ b/webagg/test/testutils.py @@ -5,6 +5,9 @@ import shutil from multiprocessing import Process +from fakeredis import FakeStrictRedis +from mock import patch + from wsgiref.simple_server import make_server from webagg.aggregator import SimpleAggregator @@ -38,6 +41,27 @@ class BaseTestClass(object): pass +# ============================================================================ +class FakeRedisTests(object): + @classmethod + def setup_class(cls): + super(FakeRedisTests, cls).setup_class() + cls.redismock = patch('redis.StrictRedis', FakeStrictRedis) + cls.redismock.start() + + @staticmethod + def add_cdx_to_redis(filename, key, redis_url='redis://localhost:6379/2'): + r = FakeStrictRedis.from_url(redis_url) + with open(filename, 'rb') as fh: + for line in fh: + r.zadd(key, 0, line.rstrip()) + + @classmethod + def teardown_class(cls): + super(FakeRedisTests, cls).teardown_class() + cls.redismock.stop() + + # ============================================================================ class TempDirTests(object): @classmethod