# Copyright 2012 Hewlett-Packard Development Company, L.P. # # 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. from collections import OrderedDict import testtools from testtools.tests.matchers import helpers from nova.tests.unit import matchers class TestDictMatches(testtools.TestCase, helpers.TestMatchersInterface): matches_dict = OrderedDict(sorted({'foo': 'bar', 'baz': 'DONTCARE', 'cat': {'tabby': True, 'fluffy': False}}.items())) matches_matcher = matchers.DictMatches( matches_dict ) matches_matches = [ {'foo': 'bar', 'baz': 'noox', 'cat': {'tabby': True, 'fluffy': False}}, {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': False}}, ] matches_mismatches = [ {}, {'foo': 'bar', 'baz': 'qux'}, {'foo': 'bop', 'baz': 'qux', 'cat': {'tabby': True, 'fluffy': False}}, {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': True}}, {'foo': 'bar', 'cat': {'tabby': True, 'fluffy': False}}, ] str_examples = [ ("DictMatches({0})".format(matches_dict), matches_matcher), ] describe_examples = [ ("Keys in d1 and not d2: {0}. Keys in d2 and not d1: []" .format(str(sorted(matches_dict.keys()))), {}, matches_matcher), ("Dictionaries do not match at fluffy. d1: False d2: True", {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': True}}, matches_matcher), ("Dictionaries do not match at foo. d1: bar d2: bop", {'foo': 'bop', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': False}}, matches_matcher), ] class TestDictListMatches(testtools.TestCase, helpers.TestMatchersInterface): matches_matcher = matchers.DictListMatches( [{'foo': 'bar', 'baz': 'DONTCARE', 'cat': {'tabby': True, 'fluffy': False}}, {'dog': 'yorkie'}, ]) matches_matches = [ [{'foo': 'bar', 'baz': 'qoox', 'cat': {'tabby': True, 'fluffy': False}}, {'dog': 'yorkie'}], [{'foo': 'bar', 'baz': False, 'cat': {'tabby': True, 'fluffy': False}}, {'dog': 'yorkie'}], ] matches_mismatches = [ [], {}, [{'foo': 'bar', 'baz': 'qoox', 'cat': {'tabby': True, 'fluffy': True}}, {'dog': 'yorkie'}], [{'foo': 'bar', 'baz': False, 'cat': {'tabby': True, 'fluffy': False}}, {'cat': 'yorkie'}], [{'foo': 'bop', 'baz': False, 'cat': {'tabby': True, 'fluffy': False}}, {'dog': 'yorkie'}], ] str_examples = [ ("DictListMatches([{'baz': 'DONTCARE', 'cat':" " {'fluffy': False, 'tabby': True}, 'foo': 'bar'},\n" " {'dog': 'yorkie'}])", matches_matcher), ] describe_examples = [ ("Length mismatch: len(L1)=2 != len(L2)=0", {}, matches_matcher), ("Dictionaries do not match at fluffy. d1: True d2: False", [{'foo': 'bar', 'baz': 'qoox', 'cat': {'tabby': True, 'fluffy': True}}, {'dog': 'yorkie'}], matches_matcher), ] class TestIsSubDictOf(testtools.TestCase, helpers.TestMatchersInterface): matches_matcher = matchers.IsSubDictOf( OrderedDict(sorted({'foo': 'bar', 'baz': 'DONTCARE', 'cat': {'tabby': True, 'fluffy': False}}.items())) ) matches_matches = [ {'foo': 'bar', 'baz': 'noox', 'cat': {'tabby': True, 'fluffy': False}}, {'foo': 'bar', 'baz': 'quux'} ] matches_mismatches = [ {'foo': 'bop', 'baz': 'qux', 'cat': {'tabby': True, 'fluffy': False}}, {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': True}}, {'foo': 'bar', 'cat': {'tabby': True, 'fluffy': False}, 'dog': None}, ] str_examples = [ ("IsSubDictOf({0})".format( str(OrderedDict(sorted({'foo': 'bar', 'baz': 'DONTCARE', 'cat': {'tabby': True, 'fluffy': False}}.items())))), matches_matcher), ] describe_examples = [ ("Dictionaries do not match at fluffy. d1: False d2: True", {'foo': 'bar', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': True}}, matches_matcher), ("Dictionaries do not match at foo. d1: bar d2: bop", {'foo': 'bop', 'baz': 'quux', 'cat': {'tabby': True, 'fluffy': False}}, matches_matcher), ] class TestXMLMatches(testtools.TestCase, helpers.TestMatchersInterface): matches_matcher = matchers.XMLMatches(""" some text here some other text here child 1 child 2 DONTCARE """, allow_mixed_nodes=False) matches_matches = [""" some text here some other text here child 1 child 2 child 3 """, """ some text here some other text here child 1 child 2 blah """, ] matches_mismatches = [""" some text here mismatch text child 1 child 2 child 3 """, """ some text here some other text here child 1 child 2 child 3 """, """ some text here some other text here child 1 child 2 child 3 """, """ some text here some other text here child 1 child 4 child 2 child 3 """, """ some text here some other text here child 1 child 2 """, """ some text here some other text here child 1 child 2 child 3 child 4 """, """ some text here some other text here child 2 child 1 DONTCARE """, """ some text here some other text here child 1 child 2 DONTCARE """, ] str_examples = [ ("XMLMatches('\\n" "\\n" " some text here\\n" " some other text here\\n" " \\n" " \\n" " \\n" " child 1\\n" " child 2\\n" " DONTCARE\\n" " \\n" " \\n" "')", matches_matcher), ] describe_examples = [ ("/root/text[1]: XML text value mismatch: expected text value: " "['some other text here']; actual value: ['mismatch text']", """ some text here mismatch text child 1 child 2 child 3 """, matches_matcher), ("/root/attrs[2]: XML attributes mismatch: keys only in expected: " "key2; keys only in actual: key3", """ some text here some other text here child 1 child 2 child 3 """, matches_matcher), ("/root/attrs[2]: XML attribute value mismatch: expected value of " "attribute key1: 'spam'; actual value: 'quux'", """ some text here some other text here child 1 child 2 child 3 """, matches_matcher), ("/root/children[3]: XML tag mismatch at index 1: expected tag " "; actual tag ", """ some text here some other text here child 1 child 4 child 2 child 3 """, matches_matcher), ("/root/children[3]: XML expected child element not " "present at index 2", """ some text here some other text here child 1 child 2 """, matches_matcher), ("/root/children[3]: XML unexpected child element " "present at index 3", """ some text here some other text here child 1 child 2 child 3 child 4 """, matches_matcher), ("/root/children[3]: XML tag mismatch at index 0: " "expected tag ; actual tag ", """ some text here some other text here child 2 child 1 child 3 """, matches_matcher), ("/: XML information mismatch(version, encoding) " "expected version 1.0, expected encoding UTF-8; " "actual version 1.1, actual encoding UTF-8", """ some text here some other text here child 1 child 2 DONTCARE """, matches_matcher), ] class TestXMLMatchesUnorderedNodes(testtools.TestCase, helpers.TestMatchersInterface): matches_matcher = matchers.XMLMatches(""" some text here some other text here DONTCARE child 2 child 1 """, allow_mixed_nodes=True) matches_matches = [""" some text here child 1 child 2 child 3 some other text here """, ] matches_mismatches = [""" some text here mismatch text child 1 child 2 child 3 """, ] describe_examples = [ ("/root: XML expected child element not present at index 4", """ some text here mismatch text child 1 child 2 child 3 """, matches_matcher), ] str_examples = []