test code for Rwhois.py
authorDavid Blacka <david@blacka.com>
Tue, 1 Jul 2008 22:22:42 +0000 (18:22 -0400)
committerDavid Blacka <david@blacka.com>
Tue, 1 Jul 2008 22:22:42 +0000 (18:22 -0400)
rwhoisd/Rwhois.py
test/TestRwhois.py [new file with mode: 0644]

index 265d520..882db54 100644 (file)
@@ -167,20 +167,3 @@ class rwhoisobject:
 
         return self.attrs_to_wire_str(self.attr_order, prefix)
     
-
-
-## A basic test driver
-if __name__ == '__main__':
-
-    obj = rwhoisobject()
-    obj.add_attr('id', '001')
-    obj.add_attr("class-name", 'contact')
-    obj.add_attr("class-name", "foo")
-    obj.add_attr('name', 'Aiden Quinn')
-    obj.add_attr('email', 'aquin@yahoo.com')
-    obj.add_attr('org-name', 'YoYoDyne Inc.')
-    obj.add_attr('email', 'aq@aol.net')
-    obj.add_attr('First-Name', 'Aiden ')
-
-    print "obj:\n", obj
-    print "wire:\n", obj.to_wire_str()
diff --git a/test/TestRwhois.py b/test/TestRwhois.py
new file mode 100644 (file)
index 0000000..01da275
--- /dev/null
@@ -0,0 +1,148 @@
+# This file is part of python-rwhoisd
+#
+# Copyright (C) 2008 David E. Blacka
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+# USA
+
+
+import path
+import Rwhois
+import unittest, types
+
+result_str = """contact:id:001\r
+contact:class-name:contact\r
+contact:class-name:foo\r
+contact:name:Test User\r
+contact:email:tuser@rwhois.net\r
+contact:email:test.user@example.org\r
+contact:org-name:YoYoDyne, Inc.\r
+contact:first-name:Test\r
+"""
+
+class ObjCreate(unittest.TestCase):
+
+    def testOne(self):
+        obj = Rwhois.rwhoisobject()
+        obj.add_attr('id', '001')
+        obj.add_attr("class-name", 'contact')
+        obj.add_attr("class-name", "foo")
+        obj.add_attr('name', 'Test User')
+        obj.add_attr('email', 'tuser@rwhois.net')
+        obj.add_attr('org-name', 'YoYoDyne, Inc.')
+        obj.add_attr('email', 'test.user@example.org')
+        obj.add_attr('First-Name', 'Test')
+
+        # just to make it easier on ourselves, check equality line by
+        # line first.
+        obj_lines = obj.to_wire_str().split("\r\n");
+        result_lines = result_str.split("\r\n");
+        for i in range(len(result_lines)):
+            self.assertEquals(obj_lines[i], result_lines[i])
+        self.assertEquals(obj.to_wire_str(), result_str)
+
+    def testMulti(self):
+        obj = Rwhois.rwhoisobject()
+        obj.add_attrs([ ("id", "001"),
+                        ("class-name", "contact"),
+                        ("Class-Name", "foo"),
+                        ("Name", "Test User") ])
+        obj.add_attrs([ ("email", "tuser@rwhois.net"),
+                        ("Org-Name", "YoYoDyne, Inc."),
+                        ("email", "test.user@example.org"),
+                        ("first-name", "Test") ])
+        self.assertEquals(obj.to_wire_str(), result_str)
+
+
+class ObjGet(unittest.TestCase):
+
+    def setUp(self):
+        self.obj = Rwhois.rwhoisobject()
+        self.obj.add_attr('id', '001')
+        self.obj.add_attr("class-name", 'contact')
+        self.obj.add_attr("class-name", "foo")
+        self.obj.add_attr('name', 'Test User')
+        self.obj.add_attr('email', 'tuser@rwhois.net')
+        self.obj.add_attr('org-name', 'YoYoDyne, Inc.')
+        self.obj.add_attr('email', 'test.user@example.org')
+        self.obj.add_attr('first-name', 'Test')
+
+    def testGetAttr(self):
+        cn_list = self.obj.get_attr(" Class-Name")
+        self.assertEquals(cn_list, ["contact", "foo"])
+        name_list = self.obj.get_attr("name")
+        self.assertEquals(name_list, ['Test User'])
+        dne_list = self.obj.get_attr("foobar", "default value")
+        self.assertEquals(dne_list, ["default value"])
+
+    def testGetAttrValue(self):
+        cn = self.obj.get_attr_value("class-name")
+        self.assertEquals(cn, "contact")
+        email = self.obj.get_attr_value("email")
+        self.assertEquals(email, "tuser@rwhois.net")
+        dne = self.obj.get_attr_value("foobar", "dne value")
+        self.assertEquals(dne, "dne value")
+
+    def testGetId(self):
+        id = self.obj.getid()
+        self.assertEquals(id, "001")
+
+    def testHasAttr(self):
+        assert(self.obj.has_attr("email"))
+        assert(self.obj.has_attr("name"))
+        assert(not self.obj.has_attr("dne"))
+
+    def testItems(self):
+        items = self.obj.items()
+        self.assertEquals(len(items), 8)
+        self.assertEquals(len(items[0]), 2)
+        self.assertEquals(items[0][1], "001")
+        self.assertEquals(items[7][0], "first-name")
+
+    def testValues(self):
+        values = self.obj.values()
+        self.assertEquals(len(values), 8)
+        values.sort()
+        self.assertEquals(values[0], "001")
+        self.assertEquals(values[2], "Test User")
+        self.assertEquals(values[7], "tuser@rwhois.net")
+
+class ErrorMsg(unittest.TestCase):
+
+    def testTuple(self):
+        err = Rwhois.error_message((336, "foo"))
+        self.assertEquals(err, "%error 336 Object Not Found: foo\r\n")
+        err = Rwhois.error_message(("501", "maintenance"))
+        self.assertEquals(err,
+                          "%error 501 Service Not Available: maintenance\r\n")
+
+    def testCodeOnly(self):
+        err = Rwhois.error_message(230)
+        self.assertEquals(err, "%error 230 No Objects Found\r\n")
+        err = Rwhois.error_message("321")
+        self.assertEquals(err, "%error 321 Invalid Attribute Syntax\r\n")
+
+    def testMsgOnly(self):
+        err = Rwhois.error_message("broken index")
+        self.assertEquals(err,
+                          "%error 402 Unidentified Error: broken index\r\n")
+
+    def testOK(self):
+        ok = Rwhois.ok()
+        self.assertEquals(ok, "%ok\r\n")
+
+
+if __name__ == "__main__":
+    unittest.main()