Skip to content

Commit

Permalink
Merge pull request #200 from Peque/pytest
Browse files Browse the repository at this point in the history
Pytest compatibility
  • Loading branch information
mmckerns authored Jan 6, 2017
2 parents 90a588e + d914795 commit 9580cbc
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 137 deletions.
51 changes: 36 additions & 15 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

from __future__ import with_statement
from dill import check
import sys

from dill.temp import capture
from dill.dill import PY3
import sys

f = lambda x:x**2

#FIXME: this doesn't catch output... it's from the internal call
def test(func, **kwds):
def raise_check(func, **kwds):
try:
with capture('stdout') as out:
check(func, **kwds)
Expand All @@ -28,19 +28,40 @@ def test(func, **kwds):
out.close()


if __name__ == '__main__':
test(f)
test(f, recurse=True)
test(f, byref=True)
test(f, protocol=0)
#TODO: test incompatible versions
# SyntaxError: invalid syntax
f = lambda x:x**2


def test_simple():
raise_check(f)


def test_recurse():
raise_check(f, recurse=True)


def test_byref():
raise_check(f, byref=True)


def test_protocol():
raise_check(f, protocol=True)


def test_python():
if PY3:
test(f, python='python3.4')
raise_check(f, python='python3.4')
else:
test(f, python='python2.7')
#TODO: test dump failure
#TODO: test load failure
raise_check(f, python='python2.7')


#TODO: test incompatible versions
#TODO: test dump failure
#TODO: test load failure

# EOF

if __name__ == '__main__':
test_simple()
test_recurse()
test_byref()
test_protocol()
test_python()
27 changes: 17 additions & 10 deletions tests/test_extendpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,27 @@
except ImportError:
from io import BytesIO as StringIO


def my_fn(x):
return x * 17

obj = lambda : my_fn(34)
assert obj() == 578

obj_io = StringIO()
pickler = pickle.Pickler(obj_io)
pickler.dump(obj)
def test_extend():
obj = lambda : my_fn(34)
assert obj() == 578

obj_io = StringIO()
pickler = pickle.Pickler(obj_io)
pickler.dump(obj)

obj_str = obj_io.getvalue()

obj2_io = StringIO(obj_str)
unpickler = pickle.Unpickler(obj2_io)
obj2 = unpickler.load()

obj_str = obj_io.getvalue()
assert obj2() == 578

obj2_io = StringIO(obj_str)
unpickler = pickle.Unpickler(obj2_io)
obj2 = unpickler.load()

assert obj2() == 578
if __name__ == '__main__':
test_extend()
51 changes: 37 additions & 14 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
# License: 3-clause BSD. The full license text is available at:
# - http://trac.mystic.cacr.caltech.edu/project/pathos/browser/dill/LICENSE

import dill
import random
import os
import sys
import string
import random

import dill


dill.settings['recurse'] = True

fname = "_test_file.txt"
Expand All @@ -21,6 +24,7 @@
buffer_error = ValueError("invalid buffer size")
dne_error = FileNotFoundError("[Errno 2] No such file or directory: '%s'" % fname)


def write_randomness(number=200):
f = open(fname, "w")
for i in range(number):
Expand All @@ -45,7 +49,17 @@ def throws(op, args, exc):
return False


def test(strictio, fmode):
def teardown_module():
if os.path.exists(fname):
os.remove(fname)


def bench(strictio, fmode, skippypy):
import platform
if skippypy and platform.python_implementation() == 'PyPy':
# Skip for PyPy...
return

# file exists, with same contents
# read

Expand Down Expand Up @@ -462,18 +476,27 @@ def test(strictio, fmode):
f2.close()


if __name__ == '__main__':
def test_nostrictio_handlefmode():
bench(False, dill.HANDLE_FMODE, False)
teardown_module()

test(strictio=False, fmode=dill.HANDLE_FMODE)
test(strictio=False, fmode=dill.FILE_FMODE)
if not dill.dill.IS_PYPY: #FIXME: fails due to pypy/issues/1233
test(strictio=False, fmode=dill.CONTENTS_FMODE)

#test(strictio=True, fmode=dill.HANDLE_FMODE)
#test(strictio=True, fmode=dill.FILE_FMODE)
#test(strictio=True, fmode=dill.CONTENTS_FMODE)
def test_nostrictio_filefmode():
bench(False, dill.FILE_FMODE, False)
teardown_module()

if os.path.exists(fname):
os.remove(fname)

# EOF
def test_nostrictio_contentsfmode():
bench(False, dill.CONTENTS_FMODE, True)
teardown_module()


#bench(True, dill.HANDLE_FMODE, False)
#bench(True, dill.FILE_FMODE, False)
#bench(True, dill.CONTENTS_FMODE, True)


if __name__ == '__main__':
test_nostrictio_handlefmode()
test_nostrictio_filefmode()
test_nostrictio_contentsfmode()
25 changes: 17 additions & 8 deletions tests/test_functors.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@
import dill
dill.settings['recurse'] = True


def f(a, b, c): # without keywords
pass


def g(a, b, c=2): # with keywords
pass


def h(a=1, b=2, c=3): # without args
pass

fp = functools.partial(f, 1, 2)
gp = functools.partial(g, 1, c=2)
hp = functools.partial(h, 1, c=2)
bp = functools.partial(int, base=2)

assert dill.pickles(fp, safe=True)
assert dill.pickles(gp, safe=True)
assert dill.pickles(hp, safe=True)
assert dill.pickles(bp, safe=True)
def test_functools():
fp = functools.partial(f, 1, 2)
gp = functools.partial(g, 1, c=2)
hp = functools.partial(h, 1, c=2)
bp = functools.partial(int, base=2)

assert dill.pickles(fp, safe=True)
assert dill.pickles(gp, safe=True)
assert dill.pickles(hp, safe=True)
assert dill.pickles(bp, safe=True)


if __name__ == '__main__':
test_functools()
14 changes: 11 additions & 3 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dill
dill.settings['recurse'] = True


def wtf(x,y,z):
def zzz():
return x
Expand All @@ -18,6 +19,7 @@ def xxx():
return z
return zzz,yyy


def quad(a=1, b=1, c=0):
inverted = [False]
def invert():
Expand All @@ -38,8 +40,10 @@ def func(*args, **kwds):
def double_add(*args):
return sum(args)


fx = sum([1,2,3])


### to make it interesting...
def quad_factory(a=1,b=1,c=0):
def dec(f):
Expand All @@ -49,25 +53,28 @@ def func(*args,**kwds):
return func
return dec


@quad_factory(a=0,b=4,c=0)
def quadish(x):
return x+1


quadratic = quad_factory()


def doubler(f):
def inner(*args, **kwds):
fx = f(*args, **kwds)
return 2*fx
return inner


@doubler
def quadruple(x):
return 2*x


if __name__ == '__main__':

def test_mixins():
# test mixins
assert double_add(1,2,3) == 2*fx
double_add.invert()
Expand Down Expand Up @@ -110,4 +117,5 @@ def quadruple(x):
#*****


# EOF
if __name__ == '__main__':
test_mixins()
Loading

0 comments on commit 9580cbc

Please sign in to comment.