Completed
Push — master ( b66745...9821e6 )
by Jasper
02:18 queued 58s
created

AddTests.test_If_file_unknown_adds_it()   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
1
import unittest
2
from mock import Mock, patch, call
3
import os
4
from tests.ditest import DependencyInjectionTestBase
5
6
7
class AddTests(DependencyInjectionTestBase):
8
9
    def setUp(self):
10
        super(AddTests, self).setUp()
11
        self.config.dryrun = False
12
        self.repo.byLocation.return_value = None
13
        self.img = Mock()
14
        self.lastProvenance = None
15
        def locAt(loc, provenance):
16
            self.lastProvenance = provenance
17
            self.lastPath = loc
18
            return self.img
19
        self.fileFactory.locatedAt.side_effect = locAt
20
        patcher = patch('niprov.adding.datetime')
21
        self.datetime = patcher.start()
22
        self.addCleanup(patcher.stop)
23
24
    def add(self, path, **kwargs):
25
        from niprov.adding import add
26
        return add(path, dependencies=self.dependencies, **kwargs)
27
28
    def assertNotCalledWith(self, m, *args, **kwargs):
29
        c = call(*args, **kwargs)
30
        assert c not in m.call_args_list, "Unexpectedly found call: "+str(c)
31
32
    def test_Returns_provenance_and_informs_listener(self):
33
        new = '/p/f2'
34
        image = self.add(new)
35
        self.listener.fileAdded.assert_called_with(self.img)
36
        self.assertEqual(image, self.img)
37
38
    def test_Sets_transient_flag_if_provided(self):
39
        image = self.add('/p/f1', transient=True)
40
        self.assertEqual(self.lastProvenance['transient'],True)
41
42
    def test_Creates_ImageFile_object_with_factory(self):
43
        image = self.add('p/afile.f')
44
        self.assertIs(self.img, image)
45
46
    def test_Calls_inspect(self):
47
        image = self.add('p/afile.f')
48
        self.img.inspect.assert_called_with()
49
50
    def test_If_inspect_raises_exceptions_tells_listener_and_doesnt_save(self):
51
        self.img.inspect.side_effect = IOError
52
        image = self.add('p/afile.f')
53
        assert not self.repo.add.called
54
        assert not self.repo.update.called
55
        self.listener.fileError.assert_called_with(self.img.path)
56
        self.assertEqual(self.img.status, 'failed')
57
58
    def test_If_dryrun_doesnt_talk_to_repo_and_status_is_test(self):
59
        self.config.dryrun = True
60
        image = self.add('p/afile.f')
61
        assert not self.repo.add.called
62
        assert not self.repo.update.called
63
        assert not self.img.inspect.called
64
65
    def test_accepts_optional_provenance(self):
66
        image = self.add('p/afile.f', provenance={'fob':'bez'})
67
        self.assertEqual(self.lastProvenance['fob'],'bez')
68
69
    def test_If_file_doesnt_exists_raises_error(self):
70
        self.filesys.fileExists.return_value = False
71
        self.assertRaises(IOError, self.add, self.img.location.path)
72
        self.filesys.fileExists.assert_called_with(self.img.location.path)
73
74
    def test_For_nonexisting_transient_file_behaves_normal(self):
75
        self.filesys.fileExists.return_value = False
76
        self.add('p/afile.f', transient=True)
77
78
    def test_Doesnt_inspect_transient_files(self):
79
        self.add('p/afile.f', transient=True)
80
        assert not self.img.inspect.called
81
82
    def test_Adds_timestamp(self):
83
        image = self.add('p/afile.f')
84
        self.assertEqual(self.lastProvenance['added'],self.datetime.now())
85
86
    def test_Adds_uid(self):
87
        with patch('niprov.adding.shortuuid') as shortuuid:
88
            shortuuid.uuid.return_value = 'abcdefghijklmn'
89
            image = self.add('p/afile.f')
90
            self.assertEqual(self.lastProvenance['id'],'abcdef')
91
92
    def test_If_config_attach_set_calls_attach_on_file(self):
93
        self.config.attach = False
94
        self.add('p/afile.f')
95
        assert not self.img.attach.called, "Shouldnt attach if not configured."
96
        self.config.attach = True
97
        self.config.attach_format = 'abracadabra'
98
        self.add('p/afile.f', transient=True)
99
        assert not self.img.attach.called, "Shouldnt attach to transient file."
100
        self.add('p/afile.f')
101
        self.img.attach.assert_called_with('abracadabra')
102
103
    def test_If_file_unknown_adds_it(self):                             # A
104
        self.repo.byLocation.return_value = None
105
        self.repo.getSeries.return_value = None
106
        image = self.add('p/afile.f')
107
        self.repo.add.assert_any_call(self.img)
108
109
    def test_If_file_is_version_but_not_series(self):                    # B
110
        previousVersion = Mock()
111
        self.repo.byLocation.return_value = previousVersion
112
        self.repo.getSeries.return_value = None
113
        img = self.add('p/afile.f')
114
        self.img.keepVersionsFromPrevious.assert_called_with(previousVersion)
115
        self.repo.update.assert_any_call(self.img)
116
117
    def test_If_file_is_version_and_series(self):                        # C
118
        previousVersion = Mock()
119
        series = Mock()
120
        self.repo.byLocation.return_value = previousVersion
121
        self.repo.getSeries.return_value = series
122
        image = self.add('p/afile.f')
123
        self.img.keepVersionsFromPrevious.assert_called_with(previousVersion)
124
        self.repo.update.assert_any_call(self.img)
125
126 View Code Duplication
    def test_If_file_not_version_but_series_and_not_in_there_yet(self):   # D1
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
127
        series = Mock()
128
        series.hasFile.return_value = False
129
        series.mergeWith.return_value = series
130
        self.repo.byLocation.return_value = None
131
        self.repo.getSeries.return_value = series
132
        image = self.add('p/afile.f')
133
        series.mergeWith.assert_called_with(self.img)
134
        self.repo.update.assert_any_call(series)
135
136 View Code Duplication
    def test_If_file_not_version_but_series_has_file(self):               # D2
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
137
        series = Mock()
138
        series.hasFile.return_value = True
139
        self.repo.byLocation.return_value = None
140
        self.repo.getSeries.return_value = series
141
        image = self.add('p/afile.f')
142
        assert not series.mergeWith.called
143
        self.img.keepVersionsFromPrevious.assert_called_with(series)
144
        self.repo.update.assert_any_call(self.img)
145
146