forked from librosa/librosa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_cache.py
More file actions
73 lines (49 loc) · 1.52 KB
/
test_cache.py
File metadata and controls
73 lines (49 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# CREATED:2015-02-14 16:20:25 by Brian McFee <brian.mcfee@nyu.edu>
# unit tests for librosa cache
'''Tests for librosa.cache'''
import os
import sys
import tempfile
import numpy as np
import shutil
import matplotlib
matplotlib.use('Agg')
from nose.tools import with_setup, eq_
# Disable any initial cache settings
for key in ['DIR', 'MMAP', 'COMPRESS', 'VERBOSE']:
try:
os.environ.pop('LIBROSA_CACHE_{:s}'.format(key))
except KeyError:
pass
def cache_construct():
'''Make a temp directory for the librosa cache'''
cache_dir = tempfile.mkdtemp()
os.environ['LIBROSA_CACHE_DIR'] = cache_dir
def cache_teardown():
'''Blow away the temp directory'''
cache_dir = os.environ.pop('LIBROSA_CACHE_DIR')
shutil.rmtree(cache_dir)
def func(x):
return np.arange(x)
def test_cache_disabled():
os.environ.pop('LIBROSA_CACHE_DIR', None)
sys.modules.pop('librosa.cache', None)
import librosa.cache
func_cache = librosa.cache(func)
# When there's no cache directory in the environment,
# librosa.cache is a no-op.
eq_(func, func_cache)
@with_setup(cache_construct, cache_teardown)
def test_cache_enabled():
sys.modules.pop('librosa.cache', None)
import librosa.cache
librosa.cache.clear()
func_cache = librosa.cache(func)
# The cache should be active now
assert func_cache != func
# issue three calls to func
y = func(5)
for i in range(3):
assert np.allclose(func_cache(5), y)