1
# Copyright (c) 2012 OpenStack, LLC.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
15
import unittest2 as unittest
17
from quantum.common import utils
20
class TestParseMappings(unittest.TestCase):
21
def parse(self, mapping_list, unique_values=True):
22
return utils.parse_mappings(mapping_list, unique_values)
24
def test_parse_mappings_fails_for_missing_separator(self):
25
with self.assertRaises(ValueError):
28
def test_parse_mappings_fails_for_missing_key(self):
29
with self.assertRaises(ValueError):
32
def test_parse_mappings_fails_for_missing_value(self):
33
with self.assertRaises(ValueError):
36
def test_parse_mappings_fails_for_extra_separator(self):
37
with self.assertRaises(ValueError):
38
self.parse(['key:val:junk'])
40
def test_parse_mappings_fails_for_duplicate_key(self):
41
with self.assertRaises(ValueError):
42
self.parse(['key:val1', 'key:val2'])
44
def test_parse_mappings_fails_for_duplicate_value(self):
45
with self.assertRaises(ValueError):
46
self.parse(['key1:val', 'key2:val'])
48
def test_parse_mappings_succeeds_for_one_mapping(self):
49
self.assertEqual(self.parse(['key:val']), {'key': 'val'})
51
def test_parse_mappings_succeeds_for_n_mappings(self):
52
self.assertEqual(self.parse(['key1:val1', 'key2:val2']),
53
{'key1': 'val1', 'key2': 'val2'})
55
def test_parse_mappings_succeeds_for_duplicate_value(self):
56
self.assertEqual(self.parse(['key1:val', 'key2:val'], False),
57
{'key1': 'val', 'key2': 'val'})
59
def test_parse_mappings_succeeds_for_no_mappings(self):
60
self.assertEqual(self.parse(['']), {})