File: test_pickle_results.py

package info (click to toggle)
astroml 1.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 932 kB
  • sloc: python: 5,731; makefile: 3
file content (34 lines) | stat: -rw-r--r-- 788 bytes parent folder | download | duplicates (3)
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
import os
from astroML.utils.decorators import pickle_results


def test_pickle_results():
    filename = 'tmp.pkl'

    @pickle_results('tmp.pkl')
    def foo(x):
        foo.called = True
        return x * x

    # cleanup if necessary
    if os.path.exists(filename):
        os.remove(filename)

    # initial calculation: function should be executed
    foo.called = False
    assert foo(4) == 16
    assert foo.called is True

    # recalculation: function should not be executed
    foo.called = False
    assert foo(4) == 16
    assert foo.called is False

    # recalculation with different input: function should be executed
    foo.called = False
    assert foo(5) == 25
    assert foo.called is True

    # cleanup
    assert os.path.exists(filename)
    os.remove(filename)