Michael Merickel
2018-10-15 2b024920847481592b1a13d4006d2a9fa8881d72
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import os
import unittest
 
from pyramid.tests.test_config import dummyfactory
 
here = os.path.dirname(__file__)
locale = os.path.abspath(
    os.path.join(here, '..', 'pkgs', 'localeapp', 'locale'))
locale2 = os.path.abspath(
    os.path.join(here, '..', 'pkgs', 'localeapp', 'locale2'))
locale3 = os.path.abspath(
    os.path.join(here, '..', 'pkgs', 'localeapp', 'locale3'))
 
class TestI18NConfiguratorMixin(unittest.TestCase):
    def _makeOne(self, *arg, **kw):
        from pyramid.config import Configurator
        config = Configurator(*arg, **kw)
        return config
 
    def test_set_locale_negotiator(self):
        from pyramid.interfaces import ILocaleNegotiator
        config = self._makeOne(autocommit=True)
        def negotiator(request): pass
        config.set_locale_negotiator(negotiator)
        self.assertEqual(config.registry.getUtility(ILocaleNegotiator),
                         negotiator)
 
    def test_set_locale_negotiator_dottedname(self):
        from pyramid.interfaces import ILocaleNegotiator
        config = self._makeOne(autocommit=True)
        config.set_locale_negotiator(
            'pyramid.tests.test_config.dummyfactory')
        self.assertEqual(config.registry.getUtility(ILocaleNegotiator),
                         dummyfactory)
 
    def test_add_translation_dirs_missing_dir(self):
        from pyramid.exceptions import ConfigurationError
        config = self._makeOne()
        config.add_translation_dirs('/wont/exist/on/my/system')
        self.assertRaises(ConfigurationError, config.commit)
 
    def test_add_translation_dirs_no_specs(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne()
        config.add_translation_dirs()
        self.assertEqual(config.registry.queryUtility(ITranslationDirectories),
                         None)
 
    def test_add_translation_dirs_asset_spec(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale')
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale])
 
    def test_add_translation_dirs_asset_spec_existing_translation_dirs(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        directories = ['abc']
        config.registry.registerUtility(directories, ITranslationDirectories)
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale')
        result = config.registry.getUtility(ITranslationDirectories)
        self.assertEqual(result, [locale, 'abc'])
 
    def test_add_translation_dirs_multiple_specs(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale',
                                    'pyramid.tests.pkgs.localeapp:locale2')
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale, locale2])
 
    def test_add_translation_dirs_multiple_specs_multiple_calls(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale',
                                    'pyramid.tests.pkgs.localeapp:locale2')
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale3')
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale3, locale, locale2])
 
    def test_add_translation_dirs_override_multiple_specs_multiple_calls(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale',
                                    'pyramid.tests.pkgs.localeapp:locale2')
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale3',
                                    override=True)
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale, locale2, locale3])
 
    def test_add_translation_dirs_invalid_kwargs(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        with self.assertRaises(TypeError):
            config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale',
                                        foo=1)
 
    def test_add_translation_dirs_abspath(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.add_translation_dirs(locale)
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale])
 
    def test_add_translation_dirs_uses_override_out_of_order(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne()
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale')
        config.override_asset('pyramid.tests.pkgs.localeapp:locale/',
                              'pyramid.tests.pkgs.localeapp:locale2/')
        config.commit()
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale2])
 
    def test_add_translation_dirs_doesnt_use_override_w_autocommit(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale')
        config.override_asset('pyramid.tests.pkgs.localeapp:locale/',
                              'pyramid.tests.pkgs.localeapp:locale2/')
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale])
 
    def test_add_translation_dirs_uses_override_w_autocommit(self):
        from pyramid.interfaces import ITranslationDirectories
        config = self._makeOne(autocommit=True)
        config.override_asset('pyramid.tests.pkgs.localeapp:locale/',
                              'pyramid.tests.pkgs.localeapp:locale2/')
        config.add_translation_dirs('pyramid.tests.pkgs.localeapp:locale')
        self.assertEqual(config.registry.getUtility(ITranslationDirectories),
                         [locale2])