示例#1
0
    def test_pipes_hits(self):
        with cio.env(i18n=('sv-se', 'en-us')):
            with self.assertDB(inserts=2):
                with self.assertCache(calls=2, sets=2):
                    cio.set('i18n://sv-se@label/email.txt', u'epost')
                    cio.set('i18n://en-us@label/surname.txt', u'surname')

            # Lazy gets
            with self.assertDB(calls=0):
                with self.assertCache(calls=0):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode', default=u'postnummer')

            # with self.assertDB(calls=2), self.assertCache(calls=5, hits=1, misses=2, sets=2):
            with self.assertDB(calls=4, selects=4):
                with self.assertCache(calls=2, hits=1, misses=2, sets=2):
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            with self.assertDB(calls=0):
                with self.assertCache(calls=1, hits=3):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode', default=u'postnummer')
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            self.assertIsNotNone(repr(node1))
            self.assertIsNotNone(str(node1))
示例#2
0
    def test_environment_state(self):
        with cio.env(i18n='en-us'):
            node = cio.get('page/title')
            self.assertEqual(node.uri, 'i18n://en-us@page/title.txt')

        node = cio.get('page/title')
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt')
示例#3
0
    def test_environment_state(self):
        with cio.env(i18n='en-us'):
            node = cio.get('page/title')
            self.assertEqual(node.uri, 'i18n://en-us@page/title.txt')

        node = cio.get('page/title')
        self.assertEqual(node.uri, 'i18n://sv-se@page/title.txt')
示例#4
0
    def test_pipes_hits(self):
        with cio.env(i18n=('sv-se', 'en-us')):
            with self.assertDB(inserts=2):
                with self.assertCache(calls=2, sets=2):
                    cio.set('i18n://sv-se@label/email.txt', u'epost')
                    cio.set('i18n://en-us@label/surname.txt', u'surname')

            # Lazy gets
            with self.assertDB(calls=0):
                with self.assertCache(calls=0):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode',
                                    default=u'postnummer')

            # with self.assertDB(calls=2), self.assertCache(calls=5, hits=1, misses=2, sets=2):
            with self.assertDB(calls=4, selects=4):
                with self.assertCache(calls=2, hits=1, misses=2, sets=2):
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            with self.assertDB(calls=0):
                with self.assertCache(calls=1, hits=3):
                    node1 = cio.get('label/email')
                    node2 = cio.get('i18n://label/surname')
                    node3 = cio.get('i18n://monkey@label/zipcode',
                                    default=u'postnummer')
                    self.assertEqual(six.text_type(node1), u'epost')
                    self.assertEqual(node2.content, u'surname')
                    self.assertEqual(six.text_type(node3), u'postnummer')

            self.assertIsNotNone(repr(node1))
            self.assertIsNotNone(str(node1))
示例#5
0
    def test_context(self):
        self.assertTupleEqual(cio.env.state.i18n, ('sv-se',))

        with settings():
            settings.ENVIRONMENT['bogus'] = {
                'g11n': 'global',
                'i18n': ('sv', 'en'),
                'l10n': ('foo', 'bar')
            }

            with cio.env('bogus'):
                self.assertTupleEqual(cio.env.state.i18n, ('sv', 'en'))
                self.assertTupleEqual(cio.env.state.l10n, ('foo', 'bar'))

            with cio.env(i18n='en-us'):
                node = cio.get('i18n://label/firstname', lazy=False)
                buffered_node = cio.get('i18n://label/email')

                self.assertTupleEqual(cio.env.i18n, ('en-us',))
                self.assertEqual(len(pipeline._buffer), 1)
                self.assertEqual(len(pipeline.history), 1)

                def assert_new_thread_env():
                    self.assertTupleEqual(cio.env.i18n, ('sv-se',))
                    self.assertEqual(len(pipeline._buffer), 0)
                    self.assertEqual(len(pipeline.history), 0)
                    cio.get('i18n://label/surname', lazy=False)
                    self.assertEqual(len(pipeline.history), 1)

                thread = threading.Thread(target=assert_new_thread_env)
                thread.start()
                thread.join()

                buffered_node.flush()
                self.assertEqual(len(pipeline._buffer), 0)
                self.assertEqual(len(pipeline.history), 2)
                self.assertListEqual(pipeline.history.list('get'), [node, buffered_node._node])

        self.assertNotIn('bogus', settings.ENVIRONMENT.keys())
示例#6
0
    def test_context(self):
        self.assertTupleEqual(cio.env.state.i18n, ('sv-se',))

        with settings():
            settings.ENVIRONMENT['bogus'] = {
                'g11n': 'global',
                'i18n': ('sv', 'en'),
                'l10n': ('foo', 'bar')
            }

            with cio.env('bogus'):
                self.assertTupleEqual(cio.env.state.i18n, ('sv', 'en'))
                self.assertTupleEqual(cio.env.state.l10n, ('foo', 'bar'))

            with cio.env(i18n='en-us'):
                node = cio.get('i18n://label/firstname', lazy=False)
                buffered_node = cio.get('i18n://label/email')

                self.assertTupleEqual(cio.env.i18n, ('en-us',))
                self.assertEqual(len(pipeline._buffer), 1)
                self.assertEqual(len(pipeline.history), 1)

                def assert_new_thread_env():
                    self.assertTupleEqual(cio.env.i18n, ('sv-se',))
                    self.assertEqual(len(pipeline._buffer), 0)
                    self.assertEqual(len(pipeline.history), 0)
                    cio.get('i18n://label/surname', lazy=False)
                    self.assertEqual(len(pipeline.history), 1)

                thread = threading.Thread(target=assert_new_thread_env)
                thread.start()
                thread.join()

                buffered_node.flush()
                self.assertEqual(len(pipeline._buffer), 0)
                self.assertEqual(len(pipeline.history), 2)
                self.assertListEqual(pipeline.history.list('get'), [node, buffered_node._node])

        self.assertNotIn('bogus', settings.ENVIRONMENT.keys())
示例#7
0
    def test_fallback(self):
        with cio.env(i18n=('sv-se', 'en-us', 'en-uk')):
            node1 = cio.set('i18n://bogus@label/email.txt', u'epost')
            node2 = cio.set('i18n://en-uk@label/surname.txt', u'surname')

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6, selects=6):
                    node3 = cio.get('i18n://label/email')
                    node4 = cio.get('i18n://label/surname', u'efternamn')
                    self.assertIsNone(node3.content)
                    self.assertEqual(node4.content, u'surname')

            cache.clear()

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6):
                    cio.get('i18n://label/email', lazy=False)
                    cio.get('i18n://label/surname', u'lastname', lazy=False)
示例#8
0
    def test_environment(self):
        """
        'default': {
            'g11n': 'global',
            'i18n': (django_settings.LANGUAGE_CODE,),
            'l10n': (project_name(django_settings) or 'local',)
        }
        """
        env = settings.ENVIRONMENT
        self.assertDictEqual(env['default'], {'g11n': 'global', 'i18n': 'sv-se', 'l10n': 'tests'})

        with self.assertRaises(IndexError):
            cio.env.pop()

        with cio.env(i18n='sv', l10n=['loc'], g11n=('glob',)):
            self.assertTupleEqual(cio.env.i18n, ('sv',))
            self.assertTupleEqual(cio.env.l10n, ('loc',))
            self.assertTupleEqual(cio.env.g11n, ('glob',))

        with self.assertRaises(SystemError):
            cio.env.__init__()
示例#9
0
    def test_environment(self):
        """
        'default': {
            'g11n': 'global',
            'i18n': (django_settings.LANGUAGE_CODE,),
            'l10n': (project_name(django_settings) or 'local',)
        }
        """
        env = settings.ENVIRONMENT
        self.assertDictEqual(env['default'], {'g11n': 'global', 'i18n': 'sv-se', 'l10n': 'tests'})

        with self.assertRaises(IndexError):
            cio.env.pop()

        with cio.env(i18n='sv', l10n=['loc'], g11n=('glob',)):
            self.assertTupleEqual(cio.env.i18n, ('sv',))
            self.assertTupleEqual(cio.env.l10n, ('loc',))
            self.assertTupleEqual(cio.env.g11n, ('glob',))

        with self.assertRaises(SystemError):
            cio.env.__init__()
示例#10
0
    def test_blocknode_with_context(self):
        cio.set('i18n://sv-se@page/title.txt', u'Hej {name}!')

        source = u"""
            {% blocknode 'page/title' edit=False name=user.get_full_name %}
                Hello {name}!
            {% endblocknode %}
        """

        context = {'user': User(first_name=u'Jonas', last_name=u'Lundberg')}
        html = self.render(source, context)
        assert html == u'Hej Jonas Lundberg!'

        with cio.env(i18n='en-us'):
            html = self.render(source, context)
            assert html == u'Hello Jonas Lundberg!'

        html = self.render(u"""
            {% blocknode 'page/title' edit=False %}
                Hello {name}!
            {% endblocknode %}
        """)
        assert html == u'Hej {name}!'
示例#11
0
    def test_blocknode_with_context(self):
        cio.set('i18n://sv-se@page/title.txt', u'Hej {name}!')

        source = u"""
            {% blocknode 'page/title' edit=False name=user.get_full_name %}
                Hello {name}!
            {% endblocknode %}
        """

        context = {'user': User(first_name=u'Jonas', last_name=u'Lundberg')}
        html = self.render(source, context)
        assert html == u'Hej Jonas Lundberg!'

        with cio.env(i18n='en-us'):
            html = self.render(source, context)
            assert html == u'Hello Jonas Lundberg!'

        html = self.render(u"""
            {% blocknode 'page/title' edit=False %}
                Hello {name}!
            {% endblocknode %}
        """)
        assert html == u'Hej {name}!'
示例#12
0
    def test_fallback(self):
        with cio.env(i18n=('sv-se', 'en-us', 'en-uk')):
            cio.set('i18n://bogus@label/email.txt', u'epost')
            cio.set('i18n://en-uk@label/surname.txt', u'surname')

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6, selects=6):
                    node1 = cio.get('i18n://label/email')
                    node2 = cio.get('i18n://label/surname', u'efternamn')
                    self.assertEqual(
                        node1.uri.namespace, 'sv-se'
                    )  # No fallback, stuck on first namespace, sv-se
                    self.assertEqual(node1.namespace_uri.namespace, 'sv-se')
                    self.assertIsNone(node1.content)
                    self.assertEqual(node2.uri.namespace, 'en-uk')
                    self.assertEqual(node2.namespace_uri.namespace, 'sv-se')
                    self.assertEqual(node2.content, u'surname')

            cache.clear()

            with self.assertCache(misses=2, sets=2):
                with self.assertDB(calls=6):
                    cio.get('i18n://label/email', lazy=False)
                    cio.get('i18n://label/surname', u'lastname', lazy=False)