blob: 676d9a009d17f96a5b634360456eae50c76cdebd [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Unit tests for image templates."""
import mock
import unittest
from django.template import loader
from django.test import client
from melange.templates import image
from soc.views.helper import request_data
_TEST_TEMPLATE_PATH = '/arbitrary/template/path'
_TEST_IMAGE_URL = 'http://test.domain.com/path/to/image'
_TEST_HEIGHT = 256
_TEST_WIDTH = 512
_MOCK_RENDERED_CONTENT = 'mock content'
class ImageTest(unittest.TestCase):
"""Unit tests for Image class."""
def setUp(self):
"""See unittest.TestCase.setUp for specification."""
request_factory = client.RequestFactory()
request = request_factory.get('http://some-unused.url.com/')
self.data = request_data.RequestData(request, [], {})
def testImageTemplateProperties(self):
"""Tests properties of a newly created template."""
# check instance with unset dimensions
instance = image.Image(
self.data, _TEST_IMAGE_URL, _TEST_TEMPLATE_PATH)
self.assertEqual(_TEST_IMAGE_URL, instance._image_url)
self.assertEqual(_TEST_TEMPLATE_PATH, instance._template_path)
self.assertIsNone(instance._height)
self.assertIsNone(instance._width)
# check instance with custom dimensions
instance = image.Image(
self.data, _TEST_IMAGE_URL, _TEST_TEMPLATE_PATH,
width=_TEST_WIDTH, height=_TEST_HEIGHT)
self.assertEqual(_TEST_IMAGE_URL, instance._image_url)
self.assertEqual(_TEST_TEMPLATE_PATH, instance._template_path)
self.assertEqual(_TEST_HEIGHT, instance._height)
self.assertEqual(_TEST_WIDTH, instance._width)
@mock.patch.object(
loader, 'render_to_string', return_value=_MOCK_RENDERED_CONTENT)
def testRenderedContent(self, mock_func):
"""Tests content of the rendered template."""
# NOTE: adding actual template file would be burdensome
# so this test only checks whether the rendering function is called
instance = image.Image(
self.data, _TEST_IMAGE_URL, _TEST_TEMPLATE_PATH)
rendered = instance.render()
self.assertEqual(_MOCK_RENDERED_CONTENT, rendered)