MemDB test driver, bug fixes
[python-rwhoisd.git] / test / TestMemDB.py
1 # This file is part of python-rwhoisd
2 #
3 # Copyright (C) 2008 David E. Blacka
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 # USA
19
20
21 import path
22 import MemDB, Rwhois
23 import unittest, types
24
25 class InitSchemaTest(unittest.TestCase):
26
27     def testInitSchema(self):
28         db = MemDB.MemDB()
29         assert(db)
30
31         db.init_schema("test_schema")
32
33         assert(db.is_attribute("id")) # built-in
34         assert(db.is_attribute("auth-area")) # built-in
35         assert(db.is_attribute("class-name")) # built-in
36         assert(db.is_attribute("updated")) # built-in
37         assert(db.is_attribute("referred-auth-area")) # built-in
38         assert(db.is_attribute("name"))
39         assert(db.is_attribute("ip-network"))
40         assert(db.is_indexed_attr("id"))
41         assert(db.is_indexed_attr("ip-network"))
42         assert(db.is_indexed_attr("ref-like"))
43
44     # At the moment, init_schema doesn't really fail on malformed
45     # schema files.  Instead, it just silently fails to add an index
46     # for the mailformed attributes.
47     def testBadSchema(self):
48         db = MemDB.MemDB()
49         assert(db)
50
51         db.init_schema("test_bad_schema")
52
53         assert(db.is_attribute("name"))
54         assert(not db.is_indexed_attr("name"))
55         assert(db.is_attribute("bad"))
56         assert(not db.is_indexed_attr("bad"))
57         assert(db.is_indexed_attr("questionable"))
58
59
60 class SchemaTest(unittest.TestCase):
61
62     def setUp(self):
63         self.db = MemDB.MemDB()
64         assert(self.db)
65         self.db.init_schema("test_schema")
66         self.db.load_data("test_data")
67         self.db.index_data()
68
69     auth_areas = ["10.0.0.0/8", "a.com"]
70
71     def testAuthAreas(self):
72         a = set()
73         b = set()
74         for aa in self.auth_areas:
75             a.add(aa)
76             assert(self.db.is_autharea(aa))
77         for aa in self.db.get_authareas():
78             b.add(aa)
79
80         self.assertEquals(a, b)
81
82     classes = ['contact', 'domain', 'host', 'network',
83                'organization', 'referral']
84
85     def testClasses(self):
86         for c in self.classes:
87             assert(self.db.is_objectclass(c))
88
89     attrs = ['Admin-Contact', 'Auth-Area', 'Billing-Contact', 'City',
90              'Class-Name', 'Country-Code', 'Created', 'Domain-Name',
91              'Email', 'Fax', 'First-Name', 'Guardian', 'Host-Name',
92              'ID', 'IP-Address', 'IP-Network', 'Last-Name', 'Name',
93              'Network-Block', 'Network-Name', 'Organization', 'Org-Name',
94              'Phone', 'Postal-Code', 'Primary-Server', 'Referral',
95              'Referred-Auth-Area', 'Secondary-Server', 'See-Also',
96              'State', 'Street-Address', 'Tech-Contact', 'Type', 'Updated',
97              'Updated-By']
98
99     def testAttributes(self):
100         for a in self.attrs:
101             assert(self.db.is_attribute(a))
102         assert(self.db.is_indexed_attr("Host-Name"))
103         assert(not self.db.is_indexed_attr("Phone"))
104         assert(not self.db.is_indexed_attr("foobar"))
105
106     ids = ['local-block-1.a.com', '888.a.com', 'does-not-exist.a.com' ]
107
108     def testFetchObjs(self):
109         objs = self.db.fetch_objects(self.ids)
110         assert(len(objs) == 2)
111         for o in objs:
112             assert(isinstance(o, Rwhois.rwhoisobject))
113         for i in range(len(objs)):
114             self.assertEquals(objs[i].getid().lower(), self.ids[i])
115         
116 class SearchTest(unittest.TestCase):
117
118     def setUp(self):
119         self.db = MemDB.MemDB()
120         assert(self.db)
121         self.db.init_schema("test_schema")
122         self.db.load_data("test_data")
123
124     attr_search = [ ('domain-name', 'a.com', '333.a.com'),
125                     ('ip-network', '10.131.0.0/16', '666.10.0.0.0/8'),
126                     ('host-name', 'NS2.A.COM', '5552.a.com'),
127                     ('domain-name', 'ns2.a.com', None) ]
128
129     def testAttrSearch(self):
130         for c, k, v in self.attr_search:
131             res = self.db.search_attr(c, k)
132             assert(isinstance(res, MemDB.IndexResult))
133             length = 1
134             if v == None: length = 0
135             self.assertEquals(len(res), length)
136             if length > 0:
137                 self.assertEquals(res.list()[0], v)
138
139     nrml_search = [ ('a-net', '666.10.0.0.0/8', 1),
140                     ('ns2.a.*', '5552.a.com', 1),
141                     ('foo.a.com', None, 0), 
142                     ('a*', '666.10.0.0.0/8', 3) ]
143
144     def testNormalSearch(self):
145         for k, v, l in self.nrml_search:
146             res = self.db.search_normal(k)
147             assert(isinstance(res, MemDB.IndexResult))
148             self.assertEquals(len(res), l)
149             if l > 0:
150                 self.assertEquals(res.list()[0], v)
151             if l > 1:
152                 res = self.db.search_normal(k, 1)
153                 self.assertEquals(len(res), 1)
154
155
156     cidr_search = [ ('10.0.0.2', '666.10.0.0.0/8', 2),
157                     ('192.168.0.0/16**', 'local-block-1.a.com', 1),
158                     ('127.0.0.1/26', None, 0) ]
159
160     def testCidrSearch(self):
161         for k, v, l in self.cidr_search:
162             res = self.db.search_cidr(k)
163             assert(isinstance(res, MemDB.IndexResult))
164             self.assertEquals(len(res), l)
165             if l > 0:
166                 self.assertEquals(res.list()[0], v)
167             if l > 1:
168                 res = self.db.search_cidr(k, 1)
169                 self.assertEquals(len(res), 1)
170
171     def testReferralSearch(self):
172         res = self.db.search_referral("fddi.a.com")
173         self.assertEquals(len(res), 1)
174         self.assertEquals(res.list()[0], '888.a.com')
175
176     def testIterator(self):
177         it = self.db.object_iterator()
178         l = [ x for x in it ]
179         self.assertEquals(len(l), 9)
180
181 if __name__ == "__main__":
182     unittest.main()