587
|
1 |
#============================================================================
|
|
2 |
#Name : test_configuration_model.py
|
|
3 |
#Part of : Helium
|
|
4 |
|
|
5 |
#Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
6 |
#All rights reserved.
|
|
7 |
#This component and the accompanying materials are made available
|
|
8 |
#under the terms of the License "Eclipse Public License v1.0"
|
|
9 |
#which accompanies this distribution, and is available
|
|
10 |
#at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
11 |
#
|
|
12 |
#Initial Contributors:
|
|
13 |
#Nokia Corporation - initial contribution.
|
|
14 |
#
|
|
15 |
#Contributors:
|
|
16 |
#
|
|
17 |
#Description:
|
|
18 |
#===============================================================================
|
|
19 |
|
|
20 |
import logging
|
|
21 |
import os
|
|
22 |
import unittest
|
|
23 |
import nose
|
|
24 |
|
|
25 |
import configuration
|
|
26 |
import configuration_model
|
|
27 |
|
|
28 |
|
|
29 |
logger = logging.getLogger('test.configuration_model')
|
|
30 |
#logging.basicConfig(level=logging.DEBUG)
|
|
31 |
|
|
32 |
class GroupDefTest(unittest.TestCase):
|
|
33 |
""" Check config model groups are correctly defined. """
|
|
34 |
|
|
35 |
def setUp(self):
|
|
36 |
""" Create model. """
|
|
37 |
self.model = configuration_model.DataModel(os.environ['TEST_DATA'] + '/data/data_model_test.xml')
|
|
38 |
|
|
39 |
def test_init_model(self):
|
|
40 |
""" Test groups can be created. """
|
|
41 |
assert len(self.model.properties.keys()) == 3
|
|
42 |
assert len(self.model.groups.keys()) == 1
|
|
43 |
|
|
44 |
prop1 = self.model.properties['test.property.1']
|
|
45 |
assert prop1.description == 'Test property one.'
|
|
46 |
|
|
47 |
prop2 = self.model.properties['test.property.2']
|
|
48 |
assert prop2.description == 'Test property two.'
|
|
49 |
|
|
50 |
assert self.model.groups['testGroup'] != None
|
|
51 |
|
|
52 |
def test_property_not_in_model(self):
|
|
53 |
""" Property not in model is identified. """
|
|
54 |
data = {'missing.property': 'foobar', 'test.property.1': '1'}
|
|
55 |
config = configuration.Configuration(data)
|
|
56 |
items = self.model.validate_config(config)
|
|
57 |
|
|
58 |
assert len(items) == 2
|
|
59 |
assert isinstance(items[0], configuration_model.MissingFromDataModelItem)
|
|
60 |
|
|
61 |
def test_required_property_in_group_not_defined(self):
|
|
62 |
""" Required property in a group missing from config is identified. """
|
|
63 |
config = configuration.Configuration({'test.property.1': '1', 'test.property.3': '3'})
|
|
64 |
items = self.model.validate_config(config)
|
|
65 |
|
|
66 |
assert len(items) == 1
|
|
67 |
assert isinstance(items[0], configuration_model.UndefinedRequiredInGroupItem)
|
|
68 |
|
|
69 |
|
|
70 |
class MissingFromDataModelItemTest(unittest.TestCase):
|
|
71 |
""" Item class operations. """
|
|
72 |
def test_create(self):
|
|
73 |
""" Basic validation item usage. """
|
|
74 |
item = configuration_model.MissingFromDataModelItem('test.property')
|
|
75 |
self.assert_(str(item) == 'Property not in data model: test.property')
|
|
76 |
item.log(logger)
|
|
77 |
|
|
78 |
|
|
79 |
class DataModelTest(unittest.TestCase):
|
|
80 |
""" Checks Data Model is properly defined """
|
|
81 |
|
|
82 |
def setUp(self):
|
|
83 |
""" Create model. """
|
|
84 |
self.model = configuration_model.DataModel(os.environ['TEST_DATA'] + '/data/data_model_validation_test.xml')
|
|
85 |
|
|
86 |
def test_init_model(self):
|
|
87 |
""" Test groups can be created. """
|
|
88 |
assert len(self.model.properties.keys()) == 3
|
|
89 |
assert len(self.model.groups.keys()) == 1
|
|
90 |
|
|
91 |
prop1 = self.model.properties['test.property.1']
|
|
92 |
assert prop1.type == 'string'
|
|
93 |
assert prop1.description == 'Test property one.'
|
|
94 |
|
|
95 |
prop2 = self.model.properties['test.property.2']
|
|
96 |
assert prop2.type == 'string'
|
|
97 |
assert prop2.description == 'Test property two.'
|
|
98 |
|
|
99 |
assert self.model.groups['testGroup'] != None
|
|
100 |
|
|
101 |
|
|
102 |
class DataModelPropertyTest(unittest.TestCase):
|
|
103 |
""" Checks Data Model that an exception is thrown if properties are not properly defined """
|
|
104 |
|
|
105 |
@nose.tools.raises(Exception)
|
|
106 |
def setUp(self):
|
|
107 |
""" Create model. """
|
|
108 |
self.model = configuration_model.DataModel(os.environ['TEST_DATA'] + '/data/data_model_validation__property_test.xml')
|
|
109 |
|
|
110 |
|
|
111 |
class DataModelGroupTest(unittest.TestCase):
|
|
112 |
""" Checks Data Model that an exception is thrown if there is a required property in groups is missing"""
|
|
113 |
|
|
114 |
@nose.tools.raises(Exception)
|
|
115 |
def setUp(self):
|
|
116 |
""" Create model. """
|
|
117 |
self.model = configuration_model.DataModel(os.environ['TEST_DATA'] + '/data/data_model_validation_group_test.xml')
|
|
118 |
|
|
119 |
|