test code for Rwhois.py
[python-rwhoisd.git] / test / TestRwhois.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 Rwhois
23 import unittest, types
24
25 result_str = """contact:id:001\r
26 contact:class-name:contact\r
27 contact:class-name:foo\r
28 contact:name:Test User\r
29 contact:email:tuser@rwhois.net\r
30 contact:email:test.user@example.org\r
31 contact:org-name:YoYoDyne, Inc.\r
32 contact:first-name:Test\r
33 """
34
35 class ObjCreate(unittest.TestCase):
36
37     def testOne(self):
38         obj = Rwhois.rwhoisobject()
39         obj.add_attr('id', '001')
40         obj.add_attr("class-name", 'contact')
41         obj.add_attr("class-name", "foo")
42         obj.add_attr('name', 'Test User')
43         obj.add_attr('email', 'tuser@rwhois.net')
44         obj.add_attr('org-name', 'YoYoDyne, Inc.')
45         obj.add_attr('email', 'test.user@example.org')
46         obj.add_attr('First-Name', 'Test')
47
48         # just to make it easier on ourselves, check equality line by
49         # line first.
50         obj_lines = obj.to_wire_str().split("\r\n");
51         result_lines = result_str.split("\r\n");
52         for i in range(len(result_lines)):
53             self.assertEquals(obj_lines[i], result_lines[i])
54         self.assertEquals(obj.to_wire_str(), result_str)
55
56     def testMulti(self):
57         obj = Rwhois.rwhoisobject()
58         obj.add_attrs([ ("id", "001"),
59                         ("class-name", "contact"),
60                         ("Class-Name", "foo"),
61                         ("Name", "Test User") ])
62         obj.add_attrs([ ("email", "tuser@rwhois.net"),
63                         ("Org-Name", "YoYoDyne, Inc."),
64                         ("email", "test.user@example.org"),
65                         ("first-name", "Test") ])
66         self.assertEquals(obj.to_wire_str(), result_str)
67
68
69 class ObjGet(unittest.TestCase):
70
71     def setUp(self):
72         self.obj = Rwhois.rwhoisobject()
73         self.obj.add_attr('id', '001')
74         self.obj.add_attr("class-name", 'contact')
75         self.obj.add_attr("class-name", "foo")
76         self.obj.add_attr('name', 'Test User')
77         self.obj.add_attr('email', 'tuser@rwhois.net')
78         self.obj.add_attr('org-name', 'YoYoDyne, Inc.')
79         self.obj.add_attr('email', 'test.user@example.org')
80         self.obj.add_attr('first-name', 'Test')
81
82     def testGetAttr(self):
83         cn_list = self.obj.get_attr(" Class-Name")
84         self.assertEquals(cn_list, ["contact", "foo"])
85         name_list = self.obj.get_attr("name")
86         self.assertEquals(name_list, ['Test User'])
87         dne_list = self.obj.get_attr("foobar", "default value")
88         self.assertEquals(dne_list, ["default value"])
89
90     def testGetAttrValue(self):
91         cn = self.obj.get_attr_value("class-name")
92         self.assertEquals(cn, "contact")
93         email = self.obj.get_attr_value("email")
94         self.assertEquals(email, "tuser@rwhois.net")
95         dne = self.obj.get_attr_value("foobar", "dne value")
96         self.assertEquals(dne, "dne value")
97
98     def testGetId(self):
99         id = self.obj.getid()
100         self.assertEquals(id, "001")
101
102     def testHasAttr(self):
103         assert(self.obj.has_attr("email"))
104         assert(self.obj.has_attr("name"))
105         assert(not self.obj.has_attr("dne"))
106
107     def testItems(self):
108         items = self.obj.items()
109         self.assertEquals(len(items), 8)
110         self.assertEquals(len(items[0]), 2)
111         self.assertEquals(items[0][1], "001")
112         self.assertEquals(items[7][0], "first-name")
113
114     def testValues(self):
115         values = self.obj.values()
116         self.assertEquals(len(values), 8)
117         values.sort()
118         self.assertEquals(values[0], "001")
119         self.assertEquals(values[2], "Test User")
120         self.assertEquals(values[7], "tuser@rwhois.net")
121
122 class ErrorMsg(unittest.TestCase):
123
124     def testTuple(self):
125         err = Rwhois.error_message((336, "foo"))
126         self.assertEquals(err, "%error 336 Object Not Found: foo\r\n")
127         err = Rwhois.error_message(("501", "maintenance"))
128         self.assertEquals(err,
129                           "%error 501 Service Not Available: maintenance\r\n")
130
131     def testCodeOnly(self):
132         err = Rwhois.error_message(230)
133         self.assertEquals(err, "%error 230 No Objects Found\r\n")
134         err = Rwhois.error_message("321")
135         self.assertEquals(err, "%error 321 Invalid Attribute Syntax\r\n")
136
137     def testMsgOnly(self):
138         err = Rwhois.error_message("broken index")
139         self.assertEquals(err,
140                           "%error 402 Unidentified Error: broken index\r\n")
141
142     def testOK(self):
143         ok = Rwhois.ok()
144         self.assertEquals(ok, "%ok\r\n")
145
146
147 if __name__ == "__main__":
148     unittest.main()