Move dnspython to third_party.
[bbaumbach/samba-autobuild/.git] / third_party / dnspython / tests / namedict.py
1 # Copyright (C) 2003-2007, 2009-2011 Nominum, Inc.
2 #
3 # Permission to use, copy, modify, and distribute this software and its
4 # documentation for any purpose with or without fee is hereby granted,
5 # provided that the above copyright notice and this permission notice
6 # appear in all copies.
7 #
8 # THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
9 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
11 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
14 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
16 import unittest
17
18 import dns.name
19 import dns.namedict
20
21 class NameTestCase(unittest.TestCase):
22
23     def setUp(self):
24         self.ndict = dns.namedict.NameDict()
25         n1 = dns.name.from_text('foo.bar.')
26         n2 = dns.name.from_text('bar.')
27         self.ndict[n1] = 1
28         self.ndict[n2] = 2
29         self.rndict = dns.namedict.NameDict()
30         n1 = dns.name.from_text('foo.bar', None)
31         n2 = dns.name.from_text('bar', None)
32         self.rndict[n1] = 1
33         self.rndict[n2] = 2
34
35     def testDepth(self):
36         self.failUnless(self.ndict.max_depth == 3)
37
38     def testLookup1(self):
39         k = dns.name.from_text('foo.bar.')
40         self.failUnless(self.ndict[k] == 1)
41
42     def testLookup2(self):
43         k = dns.name.from_text('foo.bar.')
44         self.failUnless(self.ndict.get_deepest_match(k)[1] == 1)
45
46     def testLookup3(self):
47         k = dns.name.from_text('a.b.c.foo.bar.')
48         self.failUnless(self.ndict.get_deepest_match(k)[1] == 1)
49
50     def testLookup4(self):
51         k = dns.name.from_text('a.b.c.bar.')
52         self.failUnless(self.ndict.get_deepest_match(k)[1] == 2)
53
54     def testLookup5(self):
55         def bad():
56             n = dns.name.from_text('a.b.c.')
57             (k, v) = self.ndict.get_deepest_match(n)
58         self.failUnlessRaises(KeyError, bad)
59
60     def testLookup6(self):
61         def bad():
62             (k, v) = self.ndict.get_deepest_match(dns.name.empty)
63         self.failUnlessRaises(KeyError, bad)
64
65     def testLookup7(self):
66         self.ndict[dns.name.empty] = 100
67         n = dns.name.from_text('a.b.c.')
68         (k, v) = self.ndict.get_deepest_match(n)
69         self.failUnless(v == 100)
70
71     def testLookup8(self):
72         def bad():
73             self.ndict['foo'] = 100
74         self.failUnlessRaises(ValueError, bad)
75
76     def testRelDepth(self):
77         self.failUnless(self.rndict.max_depth == 2)
78
79     def testRelLookup1(self):
80         k = dns.name.from_text('foo.bar', None)
81         self.failUnless(self.rndict[k] == 1)
82
83     def testRelLookup2(self):
84         k = dns.name.from_text('foo.bar', None)
85         self.failUnless(self.rndict.get_deepest_match(k)[1] == 1)
86
87     def testRelLookup3(self):
88         k = dns.name.from_text('a.b.c.foo.bar', None)
89         self.failUnless(self.rndict.get_deepest_match(k)[1] == 1)
90
91     def testRelLookup4(self):
92         k = dns.name.from_text('a.b.c.bar', None)
93         self.failUnless(self.rndict.get_deepest_match(k)[1] == 2)
94
95     def testRelLookup7(self):
96         self.rndict[dns.name.empty] = 100
97         n = dns.name.from_text('a.b.c', None)
98         (k, v) = self.rndict.get_deepest_match(n)
99         self.failUnless(v == 100)
100
101 if __name__ == '__main__':
102     unittest.main()