Completed
Push — develop ( c97d07...11b882 )
by Kale
01:01
created

tests.GatewayBaseTests   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 33
Duplicated Lines 0 %
Metric Value
dl 0
loc 33
rs 10
wmc 7
1
from testtools import TestCase, ExpectedException
2
3
from auxlib.configuration import Configuration
4
from auxlib.exceptions import InitializationError
5
from auxlib.factory import Factory
6
7
8
class AppContext(Configuration):
9
    pass
10
11
appcontext = AppContext('foo', __package__)
12
13
14
class SomeFactory(Factory):
15
16
    def do_something(self):
17
        raise NotImplementedError()
18
19
20
class ThisImplementation(SomeFactory):
21
22
    def __init__(self, context):
23
        self._value = context.test_value
24
25
    def do_something(self):
26
        return self._value
27
28
29
class ThatImplementation(SomeFactory):
30
    do_cache = True
31
32
    def __init__(self, context):
33
        self._value = context.test_value
34
35
    def do_something(self):
36
        return self._value
37
38
39
class AnotherFactory(Factory):
40
41
    def do_something(self):
42
        raise NotImplementedError()
43
44
45
class AnotherImplementation(AnotherFactory):
46
47
    def __init__(self, context):
48
        self._value = context.test_value
49
50
51
class GatewayBaseTests(TestCase):
52
53
    def test_get_instance_before_initialization(self):
54
        with ExpectedException(InitializationError):
55
            AnotherFactory.factory.get_instance()
56
        with ExpectedException(InitializationError):
57
            AnotherFactory.factory.get_instance()
58
        with ExpectedException(RuntimeError):
59
            AnotherFactory.factory.initialize(appcontext, ThisImplementation)
60
        AnotherFactory.factory.initialize(appcontext, AnotherImplementation)
61
        self.assertTrue(AnotherFactory.factory.get_instance())
62
63
    def test_initialize_unregistered_class(self):
64
        with ExpectedException(RuntimeError):
65
            SomeFactory.factory.initialize(appcontext, 'NotARegisteredClass')
66
67
    def test_cached_instances(self):
68
        SomeFactory.factory.initialize(appcontext, 'ThatImplementation')
69
        appcontext.set_env('test_value', 44)
70
        self.assertEqual(44, SomeFactory().do_something())
71
        self.assertEqual(44, SomeFactory(ThisImplementation).do_something())
72
        self.assertEqual(44, SomeFactory('ThatImplementation').do_something())
73
        self.assertEqual(44, SomeFactory.factory.get_instance().do_something())
74
        self.assertEqual(44, SomeFactory.factory.get_instance(ThatImplementation).do_something())
75
        self.assertEqual(44, SomeFactory.factory.get_instance('ThisImplementation').do_something())
76
77
        appcontext.set_env('test_value', 88)
78
        self.assertEqual(44, SomeFactory().do_something())
79
        self.assertEqual(88, SomeFactory(ThisImplementation).do_something())
80
        self.assertEqual(44, SomeFactory('ThatImplementation').do_something())
81
        self.assertEqual(44, SomeFactory.factory.get_instance().do_something())
82
        self.assertEqual(44, SomeFactory.factory.get_instance(ThatImplementation).do_something())
83
        self.assertEqual(88, SomeFactory.factory.get_instance('ThisImplementation').do_something())
84