1
0
mirror of https://github.com/webrecorder/pywb.git synced 2025-03-24 06:59:52 +01:00

tests: add FakeRedisTests class mixin for patching in FakeRedis for tests

This commit is contained in:
Ilya Kreymer 2016-03-24 10:45:48 -04:00
parent 7cc772329c
commit 61921d6c4a
3 changed files with 34 additions and 19 deletions

View File

@ -2,13 +2,13 @@
import gevent import gevent
from webagg.test.testutils import TempDirTests, LiveServerTests, BaseTestClass, to_path from webagg.test.testutils import TempDirTests, LiveServerTests, BaseTestClass, to_path
from webagg.test.testutils import FakeRedisTests
import os import os
import webtest import webtest
from fakeredis import FakeStrictRedis
from mock import patch
from pytest import raises from pytest import raises
from fakeredis import FakeStrictRedis
from recorder.recorderapp import RecorderApp from recorder.recorderapp import RecorderApp
from recorder.redisindexer import WritableRedisIndexer 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 @classmethod
def setup_class(cls): def setup_class(cls):
super(TestRecorder, cls).setup_class() super(TestRecorder, cls).setup_class()
@ -180,7 +180,7 @@ class TestRecorder(LiveServerTests, TempDirTests, BaseTestClass):
self._test_all_warcs('/warcs/', 2) self._test_all_warcs('/warcs/', 2)
@patch('redis.StrictRedis', FakeStrictRedis) #@patch('redis.StrictRedis', FakeStrictRedis)
def test_record_param_user_coll(self): def test_record_param_user_coll(self):
warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') 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')} 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): def test_record_param_user_coll_revisit(self):
warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') 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-Target-URI') == 'http://httpbin.org/get?foo=bar'
assert status_headers.get_header('WARC-Refers-To-Date') != '' 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): def test_record_param_user_coll_skip(self):
warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') 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,') res = r.zrangebylex('USER:COLL:cdxj', '[org,httpbin)/', '(org,httpbin,')
assert len(res) == 2 assert len(res) == 2
@patch('redis.StrictRedis', FakeStrictRedis) #@patch('redis.StrictRedis', FakeStrictRedis)
def test_record_param_user_coll_write_dupe_no_revisit(self): def test_record_param_user_coll_write_dupe_no_revisit(self):
warc_path = to_path(self.root_dir + '/warcs/{user}/{coll}/') 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 os.path.isfile(path)
assert len(writer.fh_cache) == 1 assert len(writer.fh_cache) == 1
@patch('redis.StrictRedis', FakeStrictRedis) #@patch('redis.StrictRedis', FakeStrictRedis)
def test_record_multiple_writes_keep_open(self): def test_record_multiple_writes_keep_open(self):
warc_path = to_path(self.root_dir + '/warcs/FOO/ABC-{hostname}-{timestamp}.warc.gz') warc_path = to_path(self.root_dir + '/warcs/FOO/ABC-{hostname}-{timestamp}.warc.gz')

View File

@ -16,11 +16,9 @@ from pywb.utils.bufferedreaders import ChunkedDataReader
from io import BytesIO from io import BytesIO
import webtest import webtest
from fakeredis import FakeStrictRedis from fakeredis import FakeStrictRedis
from mock import patch
from .testutils import to_path from .testutils import to_path, FakeRedisTests, BaseTestClass
import json import json
@ -33,9 +31,6 @@ sources = {
testapp = None testapp = None
redismock = patch('redis.StrictRedis', FakeStrictRedis)
redismock.start()
def setup_module(self): def setup_module(self):
live_source = SimpleAggregator({'live': LiveIndexSource()}) live_source = SimpleAggregator({'live': LiveIndexSource()})
live_handler = DefaultResourceHandler(live_source) live_handler = DefaultResourceHandler(live_source)
@ -69,15 +64,11 @@ def setup_module(self):
testapp = webtest.TestApp(app.application) testapp = webtest.TestApp(app.application)
def teardown_module(self):
redismock.stop()
def to_json_list(text): def to_json_list(text):
return list([json.loads(cdx) for cdx in text.rstrip().split('\n')]) return list([json.loads(cdx) for cdx in text.rstrip().split('\n')])
class TestResAgg(object): class TestResAgg(FakeRedisTests, BaseTestClass):
def setup(self): def setup(self):
self.testapp = testapp self.testapp = testapp

View File

@ -5,6 +5,9 @@ import shutil
from multiprocessing import Process from multiprocessing import Process
from fakeredis import FakeStrictRedis
from mock import patch
from wsgiref.simple_server import make_server from wsgiref.simple_server import make_server
from webagg.aggregator import SimpleAggregator from webagg.aggregator import SimpleAggregator
@ -38,6 +41,27 @@ class BaseTestClass(object):
pass 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): class TempDirTests(object):
@classmethod @classmethod