Completed
Push — master ( 6d0d49...6d6a7d )
by Jasper
02:25 queued 57s
created

CameraTests   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 39
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A test_If_no_matplotlib_installed_takeSnapshot_returns_false() 0 6 1
A test_SaveSnapshot_doesnt_save_if_takeSnapshot_failed() 0 8 1
A test_If_no_exception_during_plotting_returns_True() 0 5 1
A test_saveSnapshot() 0 9 1
A test_If_exception_during_plotting_returns_False() 0 5 1
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