Skip to content

Commit

Permalink
TEST: Make the tests run.
Browse files Browse the repository at this point in the history
  • Loading branch information
GaelVaroquaux committed Aug 31, 2009
1 parent 2071c60 commit fb73a21
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 72 deletions.
1 change: 1 addition & 0 deletions joblib/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Local imports
from .hashing import get_func_code, get_func_name, hash
from .logger import Logger
from . import numpy_pickle

################################################################################
# class `Memory`
Expand Down
5 changes: 3 additions & 2 deletions joblib/numpy_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def __init__(self, filename):
self.file = open(filename, 'w')
# Count the number of npy files that we have created:
self._npy_counter = 0
pickle.Pickler.__init__(self, self.file, protocol=2)
pickle.Pickler.__init__(self, self.file,
protocol=pickle.HIGHEST_PROTOCOL)
# delayed import of numpy, to avoid tight coupling
import numpy as np
self.np = np
Expand All @@ -58,7 +59,7 @@ def save(self, obj):
except:
self._npy_counter -= 1
# XXX: We should have a logging mechanism
traceback.print_exc()
print traceback.print_exc()
pickle.Pickler.save(self, obj)


Expand Down
1 change: 1 addition & 0 deletions joblib/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

File renamed without changes.
2 changes: 1 addition & 1 deletion joblib/test/test_hashing.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import hashlib

from ..hashing import hash
from .testing_utils import np, with_numpy
from .common import np, with_numpy

################################################################################
# Helper functions for the tests
Expand Down
17 changes: 8 additions & 9 deletions joblib/test/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,39 @@
import os
from tempfile import mkdtemp

import nose

from ..logger import PrintTime

################################################################################
# Test fixtures
env = dict()

def setup():
""" Test setup.
"""
global cachedir
cachedir = mkdtemp()
#cachedir = 'foobar'
if os.path.exists(cachedir):
shutil.rmtree(cachedir)
env['dir'] = cachedir


def teardown():
""" Test teardown.
"""
#return True
shutil.rmtree(cachedir)
shutil.rmtree(env['dir'])


################################################################################
# Tests
def smoke_test_print_time():
def test_print_time():
""" A simple smoke test for PrintTime.
"""
print_time = PrintTime(logfile=os.path.join(cachedir, 'test.log'))
print_time = PrintTime(logfile=os.path.join(env['dir'], 'test.log'))
print_time('Foo')
# Create a second time, to smoke test log rotation.
print_time = PrintTime(logfile=os.path.join(cachedir, 'test.log'))
print_time = PrintTime(logfile=os.path.join(env['dir'], 'test.log'))
print_time('Foo')
# And a third time
print_time = PrintTime(logfile=os.path.join(cachedir, 'test.log'))
print_time = PrintTime(logfile=os.path.join(env['dir'], 'test.log'))
print_time('Foo')

77 changes: 44 additions & 33 deletions joblib/test/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 +22,52 @@ def f(x, y=1):
"""
return x**2 + y

cachedir = None

################################################################################
# Helper function for the tests
def check_identity_lazy(func, accumulator):
""" Given a function and an accumulator (a list that grows every
time the function is called, check that the function can be
decorated by memory to be a lazy identity.
"""
# Call each function with several arguments, and check that it is
# evaluated only once per argument.
memory = Memory(cachedir=cachedir)
memory.clear()
func = memory.cache(func)
for i in range(3):
for _ in range(2):
yield nose.tools.assert_equal, func(i), i
yield nose.tools.assert_equal, len(accumulator), i + 1


################################################################################
# Test fixtures
def setup():
env = dict()

def setup_module():
""" Test setup.
"""
global cachedir
cachedir = mkdtemp()
#cachedir = 'foobar'
env['dir'] = cachedir
if os.path.exists(cachedir):
shutil.rmtree(cachedir)
# Don't make the cachedir, Memory should be able to do that on the
# fly
print 80*'_'
print 'test_memory setup'
print 80*'_'


def teardown():
def teardown_module():
""" Test teardown.
"""
#return True
shutil.rmtree(cachedir)
shutil.rmtree(env['dir'])
print 80*'_'
print 'test_memory teardown'
print 80*'_'


################################################################################
# Helper function for the tests
def check_identity_lazy(func, accumulator):
""" Given a function and an accumulator (a list that grows every
time the function is called, check that the function can be
decorated by memory to be a lazy identity.
"""
# Call each function with several arguments, and check that it is
# evaluated only once per argument.
memory = Memory(cachedir=env['dir'])
memory.clear()
func = memory.cache(func)
for i in range(3):
for _ in range(2):
yield nose.tools.assert_equal, func(i), i
yield nose.tools.assert_equal, len(accumulator), i + 1


################################################################################
Expand All @@ -80,10 +87,10 @@ def f(l):
yield test

# Now test clearing
memory = Memory(cachedir=cachedir)
memory = Memory(cachedir=env['dir'])
# First clear the cache directory, to check that our code can
# handle that:
shutil.rmtree(cachedir)
shutil.rmtree(env['dir'])
g = memory.cache(f)
g(1)
g.clear()
Expand All @@ -108,7 +115,7 @@ def g(l=None, m=1):
for test in check_identity_lazy(g, accumulator):
yield test

memory = Memory(cachedir=cachedir)
memory = Memory(cachedir=env['dir'])
g = memory.cache(g)
# Smoke test with an explicit keyword argument:
nose.tools.assert_equal(g(l=30, m=2), 30)
Expand All @@ -131,17 +138,18 @@ def helper(x):

def test_memory_eval():
" Smoke test memory with a function with a function defined in an eval."
memory = Memory(cachedir=cachedir)
memory = Memory(cachedir=env['dir'])

m = eval('lambda x: x')

yield nose.tools.assert_equal, 1, m(1)



def test_memory_exception():
""" Smoketest the exception handling of Memory.
"""
memory = Memory(cachedir=cachedir)
memory = Memory(cachedir=env['dir'])
class MyException(Exception):
pass

Expand All @@ -161,10 +169,10 @@ def h(exc=0):
def test_func_dir():
""" Test the creation of the memory cache directory for the function.
"""
memory = Memory(cachedir=cachedir)
memory = Memory(cachedir=env['dir'])
path = __name__.split('.')
path.append('f')
path = os.path.join(cachedir, *path)
path = os.path.join(env['dir'], *path)

g = memory.cache(f)
# Test that the function directory is created on demand
Expand All @@ -180,14 +188,17 @@ def test_func_dir():
g._check_previous_func_code()


def test_pickling():
def test_persistence():
""" Test the memorized functions can be pickled and restored.
"""
memory = Memory(cachedir=cachedir)
memory = Memory(cachedir=env['dir'])
g = memory.cache(f)
output = g(1)

h = pickle.loads(pickle.dumps(g))

nose.tools.assert_equal(output, h._read_output((1,), {}))
yield nose.tools.assert_equal, output, h._read_output((1,), {})




63 changes: 36 additions & 27 deletions joblib/test/test_pickle.py → joblib/test/test_numpy_pickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

import nose

from .testing_utils import np, with_numpy
from .common import np, with_numpy

# numpy_pickle is not a drop-in replacement of pickle, as it takes
# filenames instead of open files as arguments.
from .. import numpy_pickle as pickle
from .. import numpy_pickle

################################################################################
# Define a list of standard types.
Expand Down Expand Up @@ -55,52 +55,60 @@ def _function(x): yield x; typelist.append(_function)
################################################################################
# Test fixtures

cachedir = None
filename = None
env = dict()

def setup():
def setup_module():
""" Test setup.
"""
global cachedir, filename
cachedir = mkdtemp()
filename = os.path.join(cachedir, 'test.pkl')
env['dir'] = mkdtemp()
env['filename'] = os.path.join(env['dir'], 'test.pkl')
print 80*'_'
print 'setup numpy_pickle'
print 80*'_'


def teardown():
def teardown_module():
""" Test teardown.
"""
shutil.rmtree(cachedir)
shutil.rmtree(env['dir'])
#del env['dir']
#del env['filename']
print 80*'_'
print 'teardown numpy_pickle'
print 80*'_'


################################################################################
# Tests

def test_standard_types():
""" Test pickling and saving with standard types.
"""
for member in typelist:
pickle.dump(member, filename)
_member = pickle.load(filename)
# We compare the pickled instance to the reloaded one only if it
# can be compared to a copied one
if member == copy.deepcopy(member):
yield nose.tools.assert_equal, member, _member
#""" Test pickling and saving with standard types.
#"""
filename = env['filename']
for member in typelist:
numpy_pickle.dump(member, filename)
_member = numpy_pickle.load(filename)
# We compare the pickled instance to the reloaded one only if it
# can be compared to a copied one
if member == copy.deepcopy(member):
yield nose.tools.assert_equal, member, _member


@with_numpy
def test_numpy_pickling():
def test_numpy_persistence():
filename = env['filename']
a = np.random.random(10)
for obj in (a,), (a, a), [a, a, a]:
filenames = pickle.dump(obj, filename)
filenames = numpy_pickle.dump(obj, filename)
# Check that one file was created per array
yield nose.tools.assert_equal, len(filenames), len(obj) + 1
# Check that these files do exist
for file in filenames:
yield nose.tools.assert_true, \
os.path.exists(os.path.join(cachedir, file))
os.path.exists(os.path.join(env['dir'], file))

# Unpickle the object
obj_ = pickle.load(filename)
obj_ = numpy_pickle.load(filename)
# Check that the items are indeed arrays
for item in obj_:
yield nose.tools.assert_true, isinstance(item, np.ndarray)
Expand All @@ -110,10 +118,11 @@ def test_numpy_pickling():


@with_numpy
def test_memap_unpickling():
def test_memmap_persistence():
a = np.random.random(10)
pickle.dump(a, filename)
b = pickle.load(filename, mmap_mode='r')
nose.tools.assert_true(isinstance(b, np.memmap))
filename = env['filename']
numpy_pickle.dump(a, filename)
b = numpy_pickle.load(filename, mmap_mode='r')
yield nose.tools.assert_true, isinstance(b, np.memmap)


0 comments on commit fb73a21

Please sign in to comment.