-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest_cachesource.py
66 lines (48 loc) · 1.91 KB
/
test_cachesource.py
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
import pytest
from unittest import mock
import numpy as np
from numpy.testing import assert_array_equal
from PyQt5.QtCore import QObject, pyqtSignal
from volumina.pixelpipeline.datasources.cachesource import CacheSource
class DummySource(QObject):
isDirty = pyqtSignal(object)
numberOfChannelsChanged = pyqtSignal(int)
class _Req:
def __init__(self, arr):
self._result = arr
def wait(self):
return self._result
def __init__(self, data):
self._data = data
super().__init__()
def set_data(self, value):
self._data = value
self.isDirty.emit(np.s_[:])
def request(self, slicing):
return self._Req(self._data[slicing])
class TestCacheSource:
@pytest.fixture
def orig_source(self):
arr = np.arange(27).reshape(3, 3, 3)
source = DummySource(arr)
return mock.Mock(wraps=source)
@pytest.fixture
def cached_source(self, orig_source):
return CacheSource(orig_source)
def test_consecutive_requests_are_cached(self, cached_source, orig_source):
slicing = np.s_[1:2, 1:2, 1:2]
res0 = cached_source.request(slicing).wait()
res1 = cached_source.request(slicing).wait()
assert_array_equal(np.array([[[13]]]), res0)
assert_array_equal(np.array([[[13]]]), res1)
orig_source.request.assert_called_once_with(slicing)
def test_cache_invalidation(self, cached_source, orig_source):
slicing = np.s_[1:2, 1:2, 1:2]
res0 = cached_source.request(slicing).wait()
res1 = cached_source.request(slicing).wait()
assert_array_equal(np.array([[[13]]]), res0)
assert_array_equal(np.array([[[13]]]), res1)
orig_source.set_data(np.arange(27, 54).reshape(3, 3, 3))
res2 = cached_source.request(slicing).wait()
assert_array_equal(np.array([[[40]]]), res2)
assert orig_source.request.call_count == 2