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 sys |
|
23 import unittest |
|
24 import nose |
|
25 |
|
26 import configuration |
|
27 import configuration_model |
|
28 |
|
29 |
|
30 logger = logging.getLogger('test.configuration_model') |
|
31 #logging.basicConfig(level=logging.DEBUG) |
|
32 |
|
33 class GroupDefTest(unittest.TestCase): |
|
34 """ Check config model groups are correctly defined. """ |
|
35 |
|
36 def setUp(self): |
|
37 """ Create model. """ |
|
38 self.model = configuration_model.DataModel('tests/data/data_model_test.xml') |
|
39 |
|
40 def test_init_model(self): |
|
41 """ Test groups can be created. """ |
|
42 assert len(self.model.properties.keys()) == 3 |
|
43 assert len(self.model.groups.keys()) == 1 |
|
44 |
|
45 prop1 = self.model.properties['test.property.1'] |
|
46 assert prop1.description == 'Test property one.' |
|
47 |
|
48 prop2 = self.model.properties['test.property.2'] |
|
49 assert prop2.description == 'Test property two.' |
|
50 |
|
51 assert self.model.groups['testGroup'] != None |
|
52 |
|
53 def test_property_not_in_model(self): |
|
54 """ Property not in model is identified. """ |
|
55 data = {'missing.property': 'foobar', 'test.property.1': '1'} |
|
56 config = configuration.Configuration(data) |
|
57 items = self.model.validate_config(config) |
|
58 |
|
59 assert len(items) == 2 |
|
60 assert isinstance(items[0], configuration_model.MissingFromDataModelItem) |
|
61 |
|
62 def test_required_property_in_group_not_defined(self): |
|
63 """ Required property in a group missing from config is identified. """ |
|
64 config = configuration.Configuration({'test.property.1': '1', 'test.property.3': '3'}) |
|
65 items = self.model.validate_config(config) |
|
66 |
|
67 assert len(items) == 1 |
|
68 assert isinstance(items[0], configuration_model.UndefinedRequiredInGroupItem) |
|
69 |
|
70 |
|
71 class MissingFromDataModelItemTest(unittest.TestCase): |
|
72 """ Item class operations. """ |
|
73 def test_create(self): |
|
74 """ Basic validation item usage. """ |
|
75 item = configuration_model.MissingFromDataModelItem('test.property') |
|
76 assert str(item) == 'Property not in data model: test.property' |
|
77 item.log(logger) |
|
78 |
|
79 |
|
80 class DataModelTest(unittest.TestCase): |
|
81 """ Checks Data Model is properly defined """ |
|
82 |
|
83 def setUp(self): |
|
84 """ Create model. """ |
|
85 self.model = configuration_model.DataModel('tests/data/data_model_validation_test.xml') |
|
86 |
|
87 def test_init_model(self): |
|
88 """ Test groups can be created. """ |
|
89 assert len(self.model.properties.keys()) == 3 |
|
90 assert len(self.model.groups.keys()) == 1 |
|
91 |
|
92 prop1 = self.model.properties['test.property.1'] |
|
93 assert prop1.type == 'string' |
|
94 assert prop1.description == 'Test property one.' |
|
95 |
|
96 prop2 = self.model.properties['test.property.2'] |
|
97 assert prop2.type == 'string' |
|
98 assert prop2.description == 'Test property two.' |
|
99 |
|
100 assert self.model.groups['testGroup'] != None |
|
101 |
|
102 |
|
103 class DataModelPropertyTest(unittest.TestCase): |
|
104 """ Checks Data Model that an exception is thrown if properties are not properly defined """ |
|
105 |
|
106 @nose.tools.raises(Exception) |
|
107 def setUp(self): |
|
108 """ Create model. """ |
|
109 self.model = configuration_model.DataModel('tests/data/data_model_validation__property_test.xml') |
|
110 |
|
111 |
|
112 class DataModelGroupTest(unittest.TestCase): |
|
113 """ Checks Data Model that an exception is thrown if there is a required property in groups is missing""" |
|
114 |
|
115 @nose.tools.raises(Exception) |
|
116 def setUp(self): |
|
117 """ Create model. """ |
|
118 self.model = configuration_model.DataModel('tests/data/data_model_validation_group_test.xml') |
|
119 |
|
120 |
|