blob: e554c86fe170aaef0f017b75ec57776df17d83ca [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 raedonly templates."""
import unittest
from melange.templates import readonly
_FIRST_TEST_GROUP_ID = 'first_test_group_id'
_FIRST_TEST_GROUP_TITLE = 'First test group title'
_FIRST_TEST_FIELD_ID_ONE = 'first_test_id_one'
_FIRST_TEST_FIELD_LABEL_ONE = 'First test label one'
_FIRST_TEST_FIELD_VALUE_ONE = 'First test value one'
_FIRST_TEST_FIELD_DESCRIPTOR_ONE = readonly.FieldDescriptor(
_FIRST_TEST_FIELD_ID_ONE, _FIRST_TEST_FIELD_LABEL_ONE)
_FIRST_TEST_FIELD_ID_TWO = 'first_test_id_two'
_FIRST_TEST_FIELD_LABEL_TWO = 'First test label two'
_FIRST_TEST_FIELD_VALUE_TWO = 'First test value two'
_FIRST_TEST_FIELD_DESCRIPTOR_TWO = readonly.FieldDescriptor(
_FIRST_TEST_FIELD_ID_TWO, _FIRST_TEST_FIELD_LABEL_TWO)
_FIRST_TEST_GROUP_DESCRIPTOR = readonly.GroupDescriptor(
_FIRST_TEST_GROUP_TITLE,
[_FIRST_TEST_FIELD_DESCRIPTOR_ONE, _FIRST_TEST_FIELD_DESCRIPTOR_TWO],
group_id=_FIRST_TEST_GROUP_ID)
_SECOND_TEST_GROUP_TITLE = 'Second test group title'
_SECOND_TEST_FIELD_ID_ONE = 'second_test_id_one'
_SECOND_TEST_FIELD_LABEL_ONE = 'Second test label one'
_SECOND_TEST_FIELD_VALUE_ONE = 'Second test value one'
_SECOND_TEST_FIELD_DESCRIPTOR_ONE = readonly.FieldDescriptor(
_SECOND_TEST_FIELD_ID_ONE, _SECOND_TEST_FIELD_LABEL_ONE)
_SECOND_TEST_FIELD_ID_TWO = 'second_test_id_two'
_SECOND_TEST_FIELD_LABEL_TWO = 'Second test label two'
_SECOND_TEST_FIELD_VALUE_TWO = 'Second test value two'
_SECOND_TEST_FIELD_DESCRIPTOR_TWO = readonly.FieldDescriptor(
_SECOND_TEST_FIELD_ID_TWO, _SECOND_TEST_FIELD_LABEL_TWO)
_SECOND_TEST_GROUP_DESCRIPTOR = readonly.GroupDescriptor(
_SECOND_TEST_GROUP_TITLE,
[_SECOND_TEST_FIELD_DESCRIPTOR_ONE, _SECOND_TEST_FIELD_DESCRIPTOR_TWO])
_TEST_READONLY_TEMPLATE_PATH = 'test/readonly/template/path'
_TEST_GROUP_TEMPLATE_PATH = 'test/group/template/path'
class GroupDescriptorTest(unittest.TestCase):
"""Unit tests for GroupDescriptor class."""
def testGroupDescriptorWithIdentifier(self):
"""Tests a group descriptor with an identifier."""
group_descriptor = readonly.GroupDescriptor(
_FIRST_TEST_GROUP_TITLE,
[_FIRST_TEST_FIELD_DESCRIPTOR_ONE, _FIRST_TEST_FIELD_DESCRIPTOR_TWO],
group_id=_FIRST_TEST_GROUP_ID)
self.assertEqual(_FIRST_TEST_GROUP_TITLE, group_descriptor.title)
self.assertEqual(2, len(group_descriptor.field_descriptors))
self.assertEqual(
_FIRST_TEST_FIELD_DESCRIPTOR_ONE, group_descriptor.field_descriptors[0])
self.assertEqual(
_FIRST_TEST_FIELD_DESCRIPTOR_TWO, group_descriptor.field_descriptors[1])
self.assertEqual(_FIRST_TEST_GROUP_ID, group_descriptor.group_id)
def testGroupDescriptorWithoutIdentifier(self):
"""Tests a group descriptor without an identifier."""
group_descriptor = readonly.GroupDescriptor(
_FIRST_TEST_GROUP_TITLE,
[_FIRST_TEST_FIELD_DESCRIPTOR_ONE, _FIRST_TEST_FIELD_DESCRIPTOR_TWO])
self.assertEqual(_FIRST_TEST_GROUP_TITLE, group_descriptor.title)
self.assertEqual(2, len(group_descriptor.field_descriptors))
self.assertEqual(
_FIRST_TEST_FIELD_DESCRIPTOR_ONE, group_descriptor.field_descriptors[0])
self.assertEqual(
_FIRST_TEST_FIELD_DESCRIPTOR_TWO, group_descriptor.field_descriptors[1])
self.assertIsNone(group_descriptor.group_id)
class ReadonlyBuilderTest(unittest.TestCase):
"""Unit tests for ReadonlyBuilder class."""
def testReadonlyWithNoValues(self):
"""Tests readonly template if no values are supplied."""
builder = readonly.ReadonlyBuilder()
builder.addGroupDescriptor(_FIRST_TEST_GROUP_DESCRIPTOR)
builder.addGroupDescriptor(_SECOND_TEST_GROUP_DESCRIPTOR)
builder.setTemplatePath(_TEST_READONLY_TEMPLATE_PATH)
builder.setGroupTemplatePath(_TEST_GROUP_TEMPLATE_PATH)
template = builder.build(None)
# check that groups are created but they have no fields
self.assertEqual(2, len(template._groups))
self.assertEqual(_FIRST_TEST_GROUP_TITLE, template._groups[0].title)
self.assertListEqual([], template._groups[0].fields)
self.assertEqual(_SECOND_TEST_GROUP_TITLE, template._groups[1].title)
self.assertListEqual([], template._groups[0].fields)
def testReadonlyWithSetValue(self):
"""Tests readonly template if values are supplied via setValue."""
builder = readonly.ReadonlyBuilder()
builder.addGroupDescriptor(_FIRST_TEST_GROUP_DESCRIPTOR)
builder.addGroupDescriptor(_SECOND_TEST_GROUP_DESCRIPTOR)
builder.setTemplatePath(_TEST_READONLY_TEMPLATE_PATH)
builder.setGroupTemplatePath(_TEST_GROUP_TEMPLATE_PATH)
# set some values
builder.setValue(_FIRST_TEST_FIELD_ID_ONE, _FIRST_TEST_FIELD_VALUE_ONE)
builder.setValue(_SECOND_TEST_FIELD_ID_TWO, _SECOND_TEST_FIELD_VALUE_TWO)
template = builder.build(None)
# check that values are set properly in the groups
self.assertEqual(1, len(template._groups[0].fields))
self.assertEqual(
_FIRST_TEST_FIELD_VALUE_ONE, template._groups[0].fields[0].value)
self.assertEqual(1, len(template._groups[1].fields))
self.assertEqual(
_SECOND_TEST_FIELD_VALUE_TWO, template._groups[1].fields[0].value)
# update one of the values that have been set
builder.setValue(_FIRST_TEST_FIELD_ID_ONE, _FIRST_TEST_FIELD_VALUE_TWO)
template = builder.build(None)
# check that the value is updated
self.assertEqual(
_FIRST_TEST_FIELD_VALUE_TWO, template._groups[0].fields[0].value)
def testReadonlyWithSetValues(self):
"""Tests readonly template if values are set via setValues."""
builder = readonly.ReadonlyBuilder()
builder.addGroupDescriptor(_FIRST_TEST_GROUP_DESCRIPTOR)
builder.addGroupDescriptor(_SECOND_TEST_GROUP_DESCRIPTOR)
builder.setTemplatePath(_TEST_READONLY_TEMPLATE_PATH)
builder.setGroupTemplatePath(_TEST_GROUP_TEMPLATE_PATH)
builder.setValues({
_FIRST_TEST_FIELD_ID_ONE: _FIRST_TEST_FIELD_VALUE_ONE,
_FIRST_TEST_FIELD_ID_TWO: _FIRST_TEST_FIELD_VALUE_TWO,
_SECOND_TEST_FIELD_ID_ONE: _SECOND_TEST_FIELD_VALUE_ONE,
_SECOND_TEST_FIELD_ID_TWO: _SECOND_TEST_FIELD_VALUE_TWO
})
template = builder.build(None)
# check that values are set properly in the groups
self.assertEqual(2, len(template._groups[0].fields))
self.assertEqual(
_FIRST_TEST_FIELD_VALUE_ONE, template._groups[0].fields[0].value)
self.assertEqual(
_FIRST_TEST_FIELD_VALUE_TWO, template._groups[0].fields[1].value)
self.assertEqual(2, len(template._groups[1].fields))
self.assertEqual(
_SECOND_TEST_FIELD_VALUE_ONE, template._groups[1].fields[0].value)
self.assertEqual(
_SECOND_TEST_FIELD_VALUE_TWO, template._groups[1].fields[1].value)
# update one of the values that have been set
builder.setValues({_FIRST_TEST_FIELD_ID_TWO: _SECOND_TEST_FIELD_VALUE_TWO})
template = builder.build(None)
# check that one value is updated and other values are untouched
self.assertEqual(2, len(template._groups[0].fields))
self.assertEqual(
_FIRST_TEST_FIELD_VALUE_ONE, template._groups[0].fields[0].value)
self.assertEqual(
_SECOND_TEST_FIELD_VALUE_TWO, template._groups[0].fields[1].value)
self.assertEqual(2, len(template._groups[1].fields))
self.assertEqual(
_SECOND_TEST_FIELD_VALUE_ONE, template._groups[1].fields[0].value)
self.assertEqual(
_SECOND_TEST_FIELD_VALUE_TWO, template._groups[1].fields[1].value)
def testRemoveFieldAfterSetValue(self):
"""Tests readonly template when a field is removed after value is set."""
builder = readonly.ReadonlyBuilder()
builder.addGroupDescriptor(_FIRST_TEST_GROUP_DESCRIPTOR)
builder.addGroupDescriptor(_SECOND_TEST_GROUP_DESCRIPTOR)
builder.setTemplatePath(_TEST_READONLY_TEMPLATE_PATH)
builder.setGroupTemplatePath(_TEST_GROUP_TEMPLATE_PATH)
# set values for all fields
builder.setValues({
_FIRST_TEST_FIELD_ID_ONE: _FIRST_TEST_FIELD_VALUE_ONE,
_FIRST_TEST_FIELD_ID_TWO: _FIRST_TEST_FIELD_VALUE_TWO,
_SECOND_TEST_FIELD_ID_ONE: _SECOND_TEST_FIELD_VALUE_ONE,
_SECOND_TEST_FIELD_ID_TWO: _SECOND_TEST_FIELD_VALUE_TWO
})
# remove one field
builder.removeField(_FIRST_TEST_FIELD_ID_TWO)
template = builder.build(None)
# check that the removed field is not in the group
self.assertNotIn(
_FIRST_TEST_FIELD_LABEL_TWO,
[field.label for field in template._groups[0].fields])
def testRemoveFieldBeforeSetValue(self):
"""Tests readonly template when a field is removed before value is set."""
builder = readonly.ReadonlyBuilder()
builder.addGroupDescriptor(_FIRST_TEST_GROUP_DESCRIPTOR)
builder.addGroupDescriptor(_SECOND_TEST_GROUP_DESCRIPTOR)
builder.setTemplatePath(_TEST_READONLY_TEMPLATE_PATH)
builder.setGroupTemplatePath(_TEST_GROUP_TEMPLATE_PATH)
# remove one field
builder.removeField(_FIRST_TEST_FIELD_ID_TWO)
# set values for all fields
builder.setValues({
_FIRST_TEST_FIELD_ID_ONE: _FIRST_TEST_FIELD_VALUE_ONE,
_FIRST_TEST_FIELD_ID_TWO: _FIRST_TEST_FIELD_VALUE_TWO,
_SECOND_TEST_FIELD_ID_ONE: _SECOND_TEST_FIELD_VALUE_ONE,
_SECOND_TEST_FIELD_ID_TWO: _SECOND_TEST_FIELD_VALUE_TWO
})
template = builder.build(None)
# check that the removed field is not in the group
self.assertNotIn(
_FIRST_TEST_FIELD_LABEL_TWO,
[field.label for field in template._groups[0].fields])