Completed
Pull Request — master (#139)
by Jasper
01:17
created

AddTests.test_Makes_paths_absolute()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 4
rs 10
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.knows.return_value = False
13
        self.repo.knowsSeries.return_value = False
14
        self.img = Mock()
15
        self.lastProvenance = None
16
        def locAt(loc, provenance):
17
            self.lastProvenance = provenance
18
            self.lastPath = loc
19
            return self.img
20
        self.fileFactory.locatedAt.side_effect = locAt
21
        patcher = patch('niprov.adding.datetime')
22
        self.datetime = patcher.start()
23
        self.addCleanup(patcher.stop)
24
25
    def add(self, path, **kwargs):
26
        from niprov.adding import add
27
        return add(path, dependencies=self.dependencies, **kwargs)
28
29
    def assertNotCalledWith(self, m, *args, **kwargs):
30
        c = call(*args, **kwargs)
31
        assert c not in m.call_args_list, "Unexpectedly found call: "+str(c)
32
33
    def test_Returns_provenance_and_status(self):
34
        new = '/p/f2'
35
        (image, status) = self.add(new)
36
        self.assertEqual(image, self.img)
37
        self.assertEqual(status, 'new')
38
39
    def test_Sets_transient_flag_if_provided(self):
40
        (provenance, status) = self.add('/p/f1', transient=True)
41
        self.assertEqual(self.lastProvenance['transient'],True)
42
43
    def test_Creates_ImageFile_object_with_factory(self):
44
        (image, status) = self.add('p/afile.f')
45
        self.assertIs(self.img, image)
46
47
    def test_Calls_inspect(self):
48
        (provenance, status) = self.add('p/afile.f')
49
        self.img.inspect.assert_called_with()
50
51
    def test_Hands_provenance_to_repository(self):
52
        (provenance, status) = self.add('p/afile.f')
53
        self.repo.add.assert_any_call(self.img)
54
55
    def test_If_discovers_file_that_is_known_ignore_it(self):
56
        self.repo.knows.return_value = True
57
        (provenance, status) = self.add('p/afile.f')
58
        assert not self.repo.add.called
59
        self.listener.knownFile.assert_called_with(self.img.path)
60
        self.assertEqual(status, 'known')
61
62
    def test_If_repo_doesnt_know_file_but_knows_series_update_series(self):
63
        self.repo.knows.return_value = False
64
        self.repo.knowsSeries.return_value = True
65
        series = Mock()
66
        self.repo.getSeries.return_value = series
67
        (provenance, status) = self.add('p/afile.f')
68
        series.addFile.assert_called_with(self.img)
69
        self.repo.update.assert_called_with(series)
70
        self.listener.fileFoundInSeries.assert_called_with(self.img, series)
71
        self.assertEqual(status, 'series')
72
73
    def test_If_inspect_raises_exceptions_tells_listener_and_doesnt_save(self):
74
        self.img.inspect.side_effect = IOError
75
        (provenance, status) = self.add('p/afile.f')
76
        self.assertNotCalledWith(self.repo.add, self.img)
77
        self.listener.fileError.assert_called_with(self.img.path)
78
        self.assertEqual(status, 'failed')
79
80
    def test_If_dryrun_doesnt_talk_to_repo_and_status_is_test(self):
81
        self.config.dryrun = True
82
        (provenance, status) = self.add('p/afile.f')
83
        assert not self.repo.add.called
84
        assert not self.repo.update.called
85
        assert not self.img.inspect.called
86
        self.assertEqual(status, 'dryrun')
87
88
    def test_accepts_optional_provenance(self):
89
        (provenance, status) = self.add('p/afile.f', provenance={'fob':'bez'})
90
        self.assertEqual(self.lastProvenance['fob'],'bez')
91
92
    def test_If_file_doesnt_exists_tells_listener_and_doesnt_save_prov(self):
93
        self.filesys.fileExists.return_value = False
94
        self.assertRaises(IOError, self.add, self.img.location.path)
95
        self.filesys.fileExists.assert_called_with(self.img.location.path)
96
97
    def test_For_nonexisting_transient_file_behaves_normal(self):
98
        self.filesys.fileExists.return_value = False
99
        self.add('p/afile.f', transient=True)
100
101
    def test_Doesnt_inspect_transient_files(self):
102
        self.add('p/afile.f', transient=True)
103
        assert not self.img.inspect.called
104
105
    def test_Adds_timestamp(self):
106
        (provenance, status) = self.add('p/afile.f')
107
        self.assertEqual(self.lastProvenance['added'],self.datetime.now())
108
109
    def test_Adds_uid(self):
110
        with patch('niprov.adding.shortuuid') as shortuuid:
111
            shortuuid.uuid.return_value = 'abcdefghijklmn'
112
            (provenance, status) = self.add('p/afile.f')
113
            self.assertEqual(self.lastProvenance['id'],'abcdef')
114
115
    def test_If_file_is_known_return_stored_provenance(self):
116
        self.repo.knows.return_value = True
117
        (img, status) = self.add('p/afile.f')
118
        self.repo.byLocation.assert_called_with(self.img.location.toString())
119
        self.assertEqual(img, self.repo.byLocation())
120
121
    def test_If_config_attach_set_calls_attach_on_file(self):
122
        self.config.attach = False
123
        self.add('p/afile.f')
124
        assert not self.img.attach.called, "Shouldnt attach if not configured."
125
        self.config.attach = True
126
        self.config.attach_format = 'abracadabra'
127
        self.add('p/afile.f', transient=True)
128
        assert not self.img.attach.called, "Shouldnt attach to transient file."
129
        self.add('p/afile.f')
130
        self.img.attach.assert_called_with('abracadabra')
131
132
133