Ejemplo n.º 1
0
    def test_render_not_cached(self, mock_from_string, mock_sha1):
        """If the template isn't in the cache, add it."""
        template = SnippetTemplateFactory(code='asdf')
        mock_cache = {}

        with patch('snippets.base.models.template_cache', mock_cache):
            result = template.render({})

        jinja_template = mock_from_string.return_value
        cache_key = mock_sha1.return_value.hexdigest.return_value
        self.assertEqual(mock_cache, {cache_key: jinja_template})

        mock_sha1.assert_called_with(b'asdf')
        mock_from_string.assert_called_with('asdf')
        jinja_template.render.assert_called_with({'snippet_id': 0})
        self.assertEqual(result, jinja_template.render.return_value)
Ejemplo n.º 2
0
    def test_render_cached(self, mock_from_string, mock_sha1):
        """
        If the template is in the cache, use the cached version instead
        of bothering to compile it.
        """
        template = SnippetTemplateFactory(code='asdf')
        cache_key = mock_sha1.return_value.hexdigest.return_value
        jinja_template = Mock()
        mock_cache = {cache_key: jinja_template}

        with patch('snippets.base.models.template_cache', mock_cache):
            result = template.render({})

        mock_sha1.assert_called_with(b'asdf')
        self.assertTrue(not mock_from_string.called)
        jinja_template.render.assert_called_with({'snippet_id': 0})
        self.assertEqual(result, jinja_template.render.return_value)
Ejemplo n.º 3
0
 def setUp(self):
     template = SnippetTemplateFactory()
     self.data = {
         'name':
         'Test Snippet',
         'template':
         template.id,
         'client_option_version_lower_bound':
         'any',
         'client_option_version_upper_bound':
         'any',
         'client_option_is_developer':
         'any',
         'client_option_is_default_browser':
         'any',
         'client_option_screen_resolutions':
         ['0-1024', '1024-1920', '1920-50000'],
         'client_option_has_fxaccount':
         'any',
         'client_option_sessionage_lower_bound':
         -1,
         'client_option_sessionage_upper_bound':
         -1,
         'client_option_profileage_lower_bound':
         -1,
         'client_option_profileage_upper_bound':
         -1,
         'client_option_bookmarks_count_lower_bound':
         -1,
         'client_option_bookmarks_count_upper_bound':
         -1,
         'client_option_addon_check_type':
         'any',
         'data':
         '{}',
         'weight':
         100,
         'on_release':
         'on',
     }
Ejemplo n.º 4
0
 def test_render_snippet_id(self):
     """If the template context doesn't have a snippet_id entry, add one set to 0."""
     template = SnippetTemplateFactory(code='<p>{{ snippet_id }}</p>')
     self.assertEqual(template.render({'myvar': 'foo'}), '<p>0</p>')
Ejemplo n.º 5
0
 def test_render(self):
     template = SnippetTemplateFactory(code='<p>{{myvar}}</p>')
     self.assertEqual(template.render({'myvar': 'foo'}), '<p>foo</p>')