File: test_cache.py

package info (click to toggle)
python-librosa 0.11.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 166,732 kB
  • sloc: python: 21,731; makefile: 141; sh: 2
file content (61 lines) | stat: -rw-r--r-- 1,323 bytes parent folder | download
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
#!/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 shutil
import numpy as np

import pytest
import librosa._cache


# Disable any initial cache settings
for key in ["DIR", "MMAP", "COMPRESS", "VERBOSE", "LEVEL"]:
    try:
        os.environ.pop("LIBROSA_CACHE_{:s}".format(key))
    except KeyError:
        pass


@pytest.fixture
def local_cache():
    cache_dir = tempfile.mkdtemp()
    cache = librosa._cache.CacheManager(cache_dir, verbose=0, level=10)
    yield cache
    shutil.rmtree(cache_dir)


def func(x):

    return np.arange(x)


def test_cache_disabled():

    # When there's no cache directory in the environment,
    # librosa.cache is a no-op.
    cache = librosa._cache.CacheManager(None, verbose=0, level=10)
    func_cache = cache(level=-10)(func)

    assert func == func_cache


def test_cache_enabled(local_cache):

    local_cache.clear()

    func_cache = local_cache(level=-10)(func)

    # The cache should be active now, so func_cache should be a different object from func
    assert func_cache != func

    # issue three calls to func
    y = func(5)

    for i in range(3):
        assert np.allclose(func_cache(5), y)