making tests work better and pass

but not in python 2.7. mock.MagicMock is not an iterator there
apparently :(
This commit is contained in:
Noah Levitt 2018-09-17 13:24:59 -07:00
parent 968513cdb5
commit f347407c5b
3 changed files with 31 additions and 14 deletions

View File

@ -16,6 +16,7 @@ matrix:
allow_failures: allow_failures:
- python: nightly - python: nightly
- python: 3.7-dev - python: 3.7-dev
- python: 2.7
services: services:
- docker - docker
@ -25,7 +26,7 @@ before_install:
- docker run -d --publish=28015:28015 rethinkdb - docker run -d --publish=28015:28015 rethinkdb
install: install:
- pip install . pytest - pip install .[test]
script: script:
- py.test -v tests - py.test -v tests

View File

@ -1,6 +1,13 @@
import setuptools import setuptools
import codecs import codecs
test_deps = ['pytest']
try:
import unittest.mock
except:
test_deps.append('mock')
setuptools.setup( setuptools.setup(
name='doublethink', name='doublethink',
version='0.2.0.dev88', version='0.2.0.dev88',
@ -10,8 +17,10 @@ setuptools.setup(
'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
], ],
install_requires=['rethinkdb'], install_requires=['rethinkdb'],
extras_require={'test': test_deps},
url='https://github.com/internetarchive/doublethink', url='https://github.com/internetarchive/doublethink',
author='Noah Levitt', author='Noah Levitt',
author_email='nlevitt@archive.org', author_email='nlevitt@archive.org',

View File

@ -24,7 +24,10 @@ import gc
import pytest import pytest
import rethinkdb as r import rethinkdb as r
import datetime import datetime
from unittest import mock try:
from unittest import mock
except:
import mock
logging.basicConfig(stream=sys.stderr, level=logging.INFO, logging.basicConfig(stream=sys.stderr, level=logging.INFO,
format="%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s") format="%(asctime)s %(process)d %(levelname)s %(threadName)s %(name)s.%(funcName)s(%(filename)s:%(lineno)d) %(message)s")
@ -129,6 +132,9 @@ def test_utcnow():
## XXX what else can we test without jumping through hoops? ## XXX what else can we test without jumping through hoops?
class SpecificException(Exception):
pass
class MockRethinker(doublethink.Rethinker): class MockRethinker(doublethink.Rethinker):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.m = mock.MagicMock() self.m = mock.MagicMock()
@ -141,13 +147,13 @@ class MockRethinker(doublethink.Rethinker):
'Cannot perform read: The primary replica ' 'Cannot perform read: The primary replica '
"isn't connected... THIS IS A TEST!") "isn't connected... THIS IS A TEST!")
else: else:
e = Exception e = SpecificException
count = 0 # dict because: https://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x
count = {'value': 0}
def run(*args, **kwargs): def run(*args, **kwargs):
nonlocal count count['value'] += 1
count += 1 if count['value'] <= 2:
if count <= 2:
raise e raise e
else: else:
return mock.MagicMock() return mock.MagicMock()
@ -160,20 +166,21 @@ class MockRethinker(doublethink.Rethinker):
'Cannot perform read: The primary replica ' 'Cannot perform read: The primary replica '
"isn't connected... THIS IS A TEST!") "isn't connected... THIS IS A TEST!")
else: else:
e = Exception e = SpecificException
def run(*args, **kwargs): def run(*args, **kwargs):
mmm = mock.MagicMock() mmm = mock.MagicMock()
count = 0 # dict because: https://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x
count = {'value': 0}
def next_(*args, **kwargs): def next_(*args, **kwargs):
nonlocal count count['value'] += 1
count += 1 if count['value'] <= 2:
if count <= 2:
raise e raise e
else: else:
return mock.MagicMock() return mock.MagicMock()
mmm.__iter__ = lambda *args, **kwargs: mmm mmm.__iter__ = lambda *args, **kwargs: mmm
mmm.__next__ = next_ mmm.__next__ = next_
mmm.next = next_
return mmm return mmm
mm.run = run mm.run = run
@ -191,14 +198,14 @@ class MockRethinker(doublethink.Rethinker):
def test_error_handling(): def test_error_handling():
rr = MockRethinker(db='my_db') rr = MockRethinker(db='my_db')
with pytest.raises(Exception): with pytest.raises(SpecificException):
rr.table('err_running_query').run() rr.table('err_running_query').run()
# should not raise exception # should not raise exception
rr.table('recoverable_err_running_query').run() rr.table('recoverable_err_running_query').run()
it = rr.table('err_in_iterator').run() # no exception yet it = rr.table('err_in_iterator').run() # no exception yet
with pytest.raises(Exception): with pytest.raises(SpecificException):
next(it) # exception here next(it) # exception here
it = rr.table('recoverable_err_in_iterator').run() # no exception yet it = rr.table('recoverable_err_in_iterator').run() # no exception yet