PipelineFactoryTests   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 78
rs 10
c 0
b 0
f 0
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 2 1
B test_forFile_makes_pipeline_with_targets_children() 0 22 6
B test_forFile_should_call_repo_with_list_not_set() 0 14 5
A fileWithLocation() 0 5 1
B test_forFile_makes_pipeline_with_targets_parents_parents() 0 19 5
1
import os
2
from mock import Mock, patch, call, sentinel
3
from tests.ditest import DependencyInjectionTestBase
4
5
6
class PipelineFactoryTests(DependencyInjectionTestBase):
7
8
    def setUp(self):
9
        super(PipelineFactoryTests, self).setUp()
10
11
#    def test_forFile_returns_Pipeline(self):
12
#        from niprov.pipelinefactory import PipelineFactory
13
#        from niprov.pipeline import Pipeline
14
#        self.repo.byLocations.return_value = []
15
#        factory = PipelineFactory(dependencies=self.dependencies)
16
#        target = Mock()
17
#        target.provenance = {}
18
#        pipeline = factory.forFile(target)
19
#        self.assertIsInstance(pipeline, Pipeline)
20
21
    def test_forFile_makes_pipeline_with_targets_parents_parents(self):
22
        from niprov.pipelinefactory import PipelineFactory
23
        p1a = self.fileWithLocation('p1a')
24
        p1b = self.fileWithLocation('p1b')
25
        p2a = self.fileWithLocation('p2a')
26
        p2b = self.fileWithLocation('p2b')
27
        p1a.provenance['parents'] = ['p2a']
28
        p1b.provenance['parents'] = ['p2a','p2b']
29
        t = self.fileWithLocation('t')
30
        t.provenance['parents'] = ['p1a','p1b']
31
        repodict = {'t':t,'p1a':p1a, 'p1b':p1b, 'p2a':p2a, 'p2b':p2b}
32
        self.repo.byLocations.side_effect = lambda ls: [repodict[l] for l in ls]
33
        self.repo.byParents.side_effect = lambda ls: []
34
        factory = PipelineFactory(dependencies=self.dependencies)
35
        with patch('niprov.pipelinefactory.Pipeline') as PipelineCtr:
36
            pipeline = factory.forFile(t)
37
            PipelineCtr.assert_called_with(repodict.values())
38
            self.assertEqual(3, self.repo.byLocations.call_count)
39
            self.assertEqual(1, self.repo.byParents.call_count)
40
41
    def test_forFile_makes_pipeline_with_targets_children(self):
42
        from niprov.pipelinefactory import PipelineFactory
43
        c1a = self.fileWithLocation('c1a')
44
        c1b = self.fileWithLocation('c1b')
45
        c2a = self.fileWithLocation('c2a')
46
        c2b = self.fileWithLocation('c2b')
47
        c1a.provenance['parents'] = ['t']
48
        c1b.provenance['parents'] = ['t']
49
        c2a.provenance['parents'] = ['c1a']
50
        c2b.provenance['parents'] = ['c1a','c1b']
51
        t = self.fileWithLocation('t')
52
        repodict = {'t':t,'c1a':c1a, 'c1b':c1b, 'c2a':c2a, 'c2b':c2b}
53
        childrenByParent = {'t':[c1a, c1b],'c1a':[c2a],'c1b':[c2a, c2b],
54
            'c2a':[],'c2b':[]}
55
        self.repo.byLocations.side_effect = lambda ls: []
56
        self.repo.byParents.side_effect = lambda ps: [c for p in ps for c in childrenByParent[p]]
57
        factory = PipelineFactory(dependencies=self.dependencies)
58
        with patch('niprov.pipelinefactory.Pipeline') as PipelineCtr:
59
            pipeline = factory.forFile(t)
60
            PipelineCtr.assert_called_with(repodict.values())
61
            self.assertEqual(1, self.repo.byLocations.call_count)
62
            self.assertEqual(3, self.repo.byParents.call_count)
63
64
    def test_forFile_should_call_repo_with_list_not_set(self):
65
        from niprov.pipelinefactory import PipelineFactory
66
        p1a = self.fileWithLocation('p1a')
67
        p1b = self.fileWithLocation('p1b')
68
        t = self.fileWithLocation('t')
69
        t.provenance['parents'] = ['p1a','p1b']
70
        repodict = {'t':t,'p1a':p1a, 'p1b':p1b}
71
        self.repo.byLocations.side_effect = lambda ls: [repodict[l] for l in ls]
72
        self.repo.byParents.side_effect = lambda ls: []
73
        factory = PipelineFactory(dependencies=self.dependencies)
74
        with patch('niprov.pipelinefactory.Pipeline') as PipelineCtr:
75
            pipeline = factory.forFile(t)
76
            self.repo.byLocations.assert_called_with([])
77
            self.repo.byLocations.assert_any_call(['p1a','p1b'])
78
79
    def fileWithLocation(self, loc):
80
        f = Mock()
81
        f.location.toString.return_value = loc
82
        f.provenance = {}
83
        return f
84
85
86
87
88
89
90