1
|
|
|
from unittest import TestCase |
2
|
|
|
from tests.ditest import DependencyInjectionTestBase |
3
|
|
|
from mock import Mock, patch, sentinel |
4
|
|
|
import numpy |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class CameraTests(DependencyInjectionTestBase): |
8
|
|
|
|
9
|
|
|
def test_saveSnapshot(self): |
10
|
|
|
from niprov.camera import Camera |
11
|
|
|
camera = Camera(self.dependencies) |
12
|
|
|
camera.takeSnapshot = Mock() |
13
|
|
|
target = Mock() |
14
|
|
|
camera.saveSnapshot(target, for_=sentinel.object) |
15
|
|
|
newPicture = self.pictureCache.new() |
16
|
|
|
camera.takeSnapshot.assert_called_with(target, on=newPicture) |
17
|
|
|
self.pictureCache.keep.assert_called_with(newPicture, sentinel.object) |
18
|
|
|
|
19
|
|
|
def test_SaveSnapshot_doesnt_save_if_takeSnapshot_failed(self): |
20
|
|
|
from niprov.camera import Camera |
21
|
|
|
camera = Camera(self.dependencies) |
22
|
|
|
camera.takeSnapshot = Mock() |
23
|
|
|
camera.takeSnapshot.return_value = False |
24
|
|
|
target = Mock() |
25
|
|
|
camera.saveSnapshot(target, for_=sentinel.object) |
26
|
|
|
self.assertFalse(self.pictureCache.keep.called) |
27
|
|
|
|
28
|
|
|
def test_If_no_matplotlib_installed_takeSnapshot_returns_false(self): |
29
|
|
|
from niprov.camera import Camera |
30
|
|
|
camera = Camera(self.dependencies) |
31
|
|
|
self.libs.hasDependency.return_value = False |
32
|
|
|
self.libs.pyplot = None |
33
|
|
|
self.assertFalse(camera.takeSnapshot(numpy.zeros([3,3,3]), Mock())) |
34
|
|
|
|
35
|
|
|
def test_If_exception_during_plotting_returns_False(self): |
36
|
|
|
from niprov.camera import Camera |
37
|
|
|
camera = Camera(self.dependencies) |
38
|
|
|
self.libs.pyplot.subplots.side_effect = ValueError |
39
|
|
|
self.assertFalse(camera.takeSnapshot(numpy.zeros([3,3,3]), Mock())) |
40
|
|
|
|
41
|
|
|
def test_If_no_exception_during_plotting_returns_True(self): |
42
|
|
|
from niprov.camera import Camera |
43
|
|
|
camera = Camera(self.dependencies) |
44
|
|
|
self.libs.pyplot.subplots.return_value = [Mock(), [Mock()]*3] |
45
|
|
|
self.assertTrue(camera.takeSnapshot(numpy.zeros([3,3,3]), Mock())) |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
|
|
|