Completed
Push — master ( 9821e6...398a65 )
by Jasper
8s
created

LocationTests.test_location_equals()   A

Complexity

Conditions 1

Size

Total Lines 7

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 7
rs 9.4285
1
from mock import Mock, patch
2
from tests.ditest import DependencyInjectionTestBase
3
4
5
class LocationTests(DependencyInjectionTestBase):
6
7
    def setUp(self):
8
        super(LocationTests, self).setUp()
9
10
    def test_LocationFactory_fromString(self):
11
        from niprov.locationfactory import LocationFactory
12
        with patch('niprov.locationfactory.Location') as Location:
13
            factory = LocationFactory()
14
            out = factory.fromString('abc')
15
            Location.assert_called_with('abc')
16
            self.assertEqual(Location(), out)
17
18
    def test_Location_toDictionary(self):
19
        from niprov.location import Location
20
        loc = Location('HAL:/p/n1.f')
21
        expected = {'path':'/p/n1.f','hostname':'HAL','location':'HAL:/p/n1.f'}
22
        self.assertEqual(expected, loc.toDictionary())
23
24
    def test_Location_by_default_fills_in_local_hostname(self):
25
        from niprov.location import Location
26
        with patch('niprov.location.socket') as socket:
27
            loc = Location('/p/n1.f')
28
            self.assertEqual(socket.gethostname(), loc.hostname)
29
30
    def test_Location_parses_locationString(self):
31
        from niprov.location import Location
32
        with patch('niprov.location.socket') as socket:
33
            loc = Location('HAL:/p/n1.f')
34
            self.assertEqual('HAL', loc.hostname)
35
            self.assertEqual('/p/n1.f', loc.path)
36
37
    def test_Location_stringifies_to_full_locationString(self):
38
        from niprov.location import Location
39
        with patch('niprov.location.socket') as socket:
40
            loc = Location('HAL:/p/n1.f')
41
            self.assertEqual('HAL:/p/n1.f', str(loc))
42
43
    def test_LocationFactory_completeString(self):
44
        from niprov.locationfactory import LocationFactory
45
        with patch('niprov.locationfactory.Location') as Location:
46
            factory = LocationFactory()
47
            factory.fromString = Mock()
48
            outstr = factory.completeString('/j/k/l')
49
            factory.fromString.assert_called_with('/j/k/l')
50
            self.assertEqual(str(factory.fromString()), outstr)
51
52
    def test_Location_toUrl(self):
53
        from niprov.location import Location
54
        loc = Location('HAL:/p/n1.f')
55
        expected = 'file://HAL/p/n1.f'
56
        self.assertEqual(expected, loc.toUrl())
57
58
    def test_Location_makes_relative_paths_absolute(self):
59
        from niprov.location import Location
60
        with patch('niprov.location.socket') as socket:
61
            socket.gethostname.return_value = 'HAL'
62
            with patch('niprov.location.os.path') as ospath:
63
                ospath.abspath.return_value = '/absolute/path'
64
                loc = Location('relative/path')
65
                self.assertEqual('/absolute/path', loc.path)
66
                self.assertEqual('HAL:/absolute/path', str(loc))
67
                loc = Location('KITT:relative/path')
68
                self.assertEqual('/absolute/path', loc.path)
69
                self.assertEqual('KITT:/absolute/path', str(loc))
70
71
    def test_location_equals(self):
72
        from niprov.location import Location
73
        loc1 = Location('relative/path')
74
        loc2 = Location('relative/path')
75
        loc3 = Location('relative/other/path')
76
        self.assertEqual(loc1, loc2)
77
        self.assertNotEqual(loc1, loc3)
78
79