test_build_capakey_default_settings()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 8
loc 8
rs 10
1
# -*- coding: utf-8 -*-
2
'''
3
Testing of the capakey specific aspects.
4
.. versionadded:: 0.1.0
5
'''
6
7
from crabpy.gateway.capakey import CapakeyRestGateway
8
9
from crabpy_pyramid import (
10
    get_capakey,
11
    _parse_settings,
12
    _filter_settings,
13
    _build_capakey,
14
    ICapakey
15
)
16
17
try:
18
    import unittest2 as unittest
19
except ImportError:
20
    import unittest  # noqa
21
    
22
    
23
class TestRegistry(object):
24
25
    def __init__(self, settings=None):
26
27
        if settings is None:
28
            self.settings = {}
29
        else:
30
            self.settings = settings
31
32
        self.capakey = None
33
34
    def queryUtility(self, iface):
35
        return self.capakey
36
37
    def registerUtility(self, capakey, iface):
38
        self.capakey = capakey
39
40
41 View Code Duplication
class TestGetAndBuild(unittest.TestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
43
    def test_get_capakey(self):
44
        r = TestRegistry()
45
        G = CapakeyRestGateway()
46
        r.registerUtility(G, ICapakey)
47
        G2 = get_capakey(r)
48
        self.assertIsInstance(G, CapakeyRestGateway)
49
        self.assertIsInstance(G2, CapakeyRestGateway)
50
        self.assertEqual(G, G2)
51
52
    def test_build_capakey_already_exists(self):
53
        r = TestRegistry()
54
        G = CapakeyRestGateway()
55
        r.registerUtility(G, ICapakey)
56
        G2 = _build_capakey(r, {})
57
        self.assertIsInstance(G, CapakeyRestGateway)
58
        self.assertIsInstance(G2, CapakeyRestGateway)
59
        self.assertEqual(G, G2)
60
61
    def test_build_capakey_default_settings(self):
62
        r = TestRegistry()
63
        G = CapakeyRestGateway()
64
        r.registerUtility(G, ICapakey)
65
        G2 = _build_capakey(r, {})
66
        self.assertIsInstance(G, CapakeyRestGateway)
67
        self.assertIsInstance(G2, CapakeyRestGateway)
68
        self.assertEqual(G, G2)
69
70
    def test_build_capakey_custom_settings(self):
71
        settings = {
72
            'crabpy.cache.file.root': './dogpile_data/',
73
            'crabpy.capakey.permanent.backend': 'dogpile.cache.dbm',
74
            'crabpy.capakey.permanent.expiration_time': 604800,
75
            'crabpy.capakey.permanent.arguments.filename': 'dogpile_data/capakey_permanent.dbm',
76
            'crabpy.capakey.long.backend': 'dogpile.cache.dbm',
77
            'crabpy.capakey.long.expiration_time': 86400,
78
            'crabpy.capakey.long.arguments.filename': 'dogpile_data/capakey_long.dbm',
79
            'crabpy.capakey.short.backend': 'dogpile.cache.dbm',
80
            'crabpy.capakey.short.expiration_time': 3600,
81
            'crabpy.capakey.short.arguments.filename': 'dogpile_data/capakey_short.dbm'
82
        }
83
        r = TestRegistry(settings)
84
        capakey_settings = _filter_settings(_parse_settings(settings), 'capakey.')
85
        if 'include' in capakey_settings:
86
            del capakey_settings['include']
87
        G = _build_capakey(r, capakey_settings)
88
        self.assertIsInstance(G, CapakeyRestGateway)
89
90
91
class TestSettings(unittest.TestCase):
92
93
    def _assert_contains_all_keys(self, args):
94
        self.assertIn('proxy.http', args)
95
96
    def test_get_all_settings(self):
97
        settings = {
98
            'crabpy.proxy.http': 'test'
99
        }
100
        args = _parse_settings(settings)
101
        self._assert_contains_all_keys(args)
102
        self.assertEqual('test', args['proxy.http'])
103