[Zodb-checkins] CVS: Packages/ZConfig/tests - test_datatypes.py:1.1.2.4 test_zopeschema.py:1.1.2.4

Chris McDonough chrism@zope.com
Sat, 21 Dec 2002 23:19:17 -0500


Update of /cvs-repository/Packages/ZConfig/tests
In directory cvs.zope.org:/tmp/cvs-serv4104/tests

Modified Files:
      Tag: zconfig-schema-devel-branch
	test_datatypes.py test_zopeschema.py 
Log Message:
Added following "stock" datatypes and tests:

"ipaddr-or-hostname":  Validates a valid IP address or hostname.
"existing-directory":  Ensures that a directory by the given name exists.
"existing-path":       Ensures that a path by the given name exists.
"existing-file":       Ensure that a file by the given name exists.
"existing-dirpath":    Ensure that the directory implied by path exists.
"constructor":         Parse a "constructor(1,2,3)" value into klass, pos, kw.
"key-value":           Space-separated key-value pairs in values.

Changed Zope schema to make use of these datatypes.



=== Packages/ZConfig/tests/test_datatypes.py 1.1.2.3 => 1.1.2.4 ===
--- Packages/ZConfig/tests/test_datatypes.py:1.1.2.3	Fri Dec 20 13:40:33 2002
+++ Packages/ZConfig/tests/test_datatypes.py	Sat Dec 21 23:18:46 2002
@@ -14,12 +14,17 @@
 """Tests of standard ZConfig datatypes."""
 
 import sys
+import os
 import unittest
 
 from socket import AF_INET, AF_UNIX
 
 import ZConfig.datatypes
 
+try:
+    here = __file__
+except NameError:
+    here = sys.argv[0]
 
 class DatatypeTestCase(unittest.TestCase):
     types = ZConfig.datatypes.Registry()
@@ -161,6 +166,73 @@
         eq(convert("/tmp/var/@345.4"),    (AF_UNIX, "/tmp/var/@345.4"))
         eq(convert("/tmp/var/@345.4:80"), (AF_UNIX, "/tmp/var/@345.4:80"))
 
+    def test_constructor(self):
+        convert = self.types.get('constructor')
+        eq = self.assertEqual
+        raises = self.assertRaises
+        eq(convert('Foo()'),          ('Foo', [], {}))
+        eq(convert('Foo(1,a=1,b=2)'), ('Foo', [1], {'a':1, 'b':2}))
+        eq(convert('Foo(1,2,3)'),     ('Foo', [1,2,3], {}))
+        eq(convert('Foo(1,2,3)'),     ('Foo', [1,2,3], {}))
+        raises(ValueError, convert, 'Foo')
+        raises(ValueError, convert, 'Foo(')
+        raises(NameError, convert, 'Foo(a, b, c)')
+
+    def test_ipaddr_or_hostname(self):
+        convert = self.types.get('ipaddr-or-hostname')
+        eq = self.assertEqual
+        raises = self.assertRaises
+        eq(convert('hostname'),          'hostname')
+        eq(convert('hostname.com'),      'hostname.com')
+        eq(convert('www.hostname.com'),  'www.hostname.com')
+        eq(convert('127.0.0.1'),         '127.0.0.1')
+        raises(ValueError, convert,  '1hostnamewithleadingnumeric')
+        raises(ValueError, convert,  '255.255')
+        raises(ValueError, convert,  '12345678')
+        raises(ValueError, convert,  '999.999.999.999')
+        raises(ValueError, convert,  'a!badhostname')
+
+    def test_existing_directory(self):
+        convert = self.types.get('existing-directory')
+        eq = self.assertEqual
+        raises = self.assertRaises
+        eq(convert('.'), '.')
+        eq(convert(os.path.dirname(here)), os.path.dirname(here))
+        raises(ValueError, convert, '/a/hopefully/non/existent/path')
+
+    def test_existing_file(self):
+        convert = self.types.get('existing-file')
+        eq = self.assertEqual
+        raises = self.assertRaises
+        eq(convert('.'), '.')
+        eq(convert(here), here)
+        raises(ValueError, convert, '/a/hopefully/non/existent/path')
+
+    def test_existing_path(self):
+        convert = self.types.get('existing-path')
+        eq = self.assertEqual
+        raises = self.assertRaises
+        eq(convert('.'), '.')
+        eq(convert(here), here)
+        eq(convert(os.path.dirname(here)), os.path.dirname(here))
+        raises(ValueError, convert, '/a/hopefully/non/existent/path')
+
+    def test_existing_dirpath(self):
+        convert = self.types.get('existing-dirpath')
+        eq = self.assertEqual
+        raises = self.assertRaises
+        eq(convert('.'), '.')
+        eq(convert(here), here)
+        raises(ValueError, convert, '/a/hopefully/non/existent/path')
+        raises(ValueError, convert, here + '/bogus')
+
+    def test_space_sep_key_value(self):
+        convert = self.types.get('key-value')
+        eq = self.assertEqual
+        eq(convert('A B'), ['A', 'B'])
+        eq(convert('Foo Boo'), ['Foo', 'Boo'])
+        eq(convert('Foo Boo Baz'), ['Foo', 'Boo Baz'])
+        eq(convert('Foo'), ['Foo', ''])
 
 def test_suite():
     return unittest.makeSuite(DatatypeTestCase)


=== Packages/ZConfig/tests/test_zopeschema.py 1.1.2.3 => 1.1.2.4 ===
--- Packages/ZConfig/tests/test_zopeschema.py:1.1.2.3	Fri Dec 20 11:24:51 2002
+++ Packages/ZConfig/tests/test_zopeschema.py	Sat Dec 21 23:18:46 2002
@@ -30,6 +30,10 @@
 class Formatter:
     pass
 
+def _assert(expr):
+    if not expr:
+        raise AssertionError, expr
+
 def _get_arglist(s):
     pos = []
     kw = {}
@@ -97,11 +101,11 @@
         socket.gethostbyname(value)
 
 def directory(value):
-    assert(os.path.exists(value))
+    _assert(os.path.isdir(value))
     return value
 
 def dirname(value):
-    assert os.path.exists(os.path.dirname(value))
+    _assert( os.path.exists(os.path.dirname(value)) )
     return value
 
 def constructor(value):
@@ -117,10 +121,10 @@
     def test_load_populated(self):
         schema = self.load_schema('zope.xml')
         conf = self.load_config(schema, 'zope-allpopulated.conf')
-        self.assertEqual(conf.zope_home, 'foo')
-        self.assertEqual(conf.instance_home, 'bar')
-        self.assertEqual(conf.software_home, 'foo/lib/python')
-        self.assertEqual(conf.client_home, 'instance_home/var')
+        self.assertEqual(conf.zope_home, '.')
+        self.assertEqual(conf.instance_home, '.')
+        self.assertEqual(conf.software_home, '.')
+        self.assertEqual(conf.client_home, '.')
         self.assertEqual(conf.debug_mode, True)
         self.assertEqual(conf.effective_user, 'chrism')
         self.assertEqual(conf.enable_product_installation, True)
@@ -148,7 +152,8 @@
         self.assertEqual(conf.maximum_security_manager_stack_size, 100)
         self.assertEqual(conf.publisher_profile_file, 'bleah')
         self.assertEqual(conf.module, 'Zope')
-        self.assertEqual(conf.cgi_environment_variables, ['A 1', 'B 2'])
+        self.assertEqual(conf.cgi_environment_variables,
+                         [['A','1'], ['B', '2']])
         self.assertEqual(conf.dns_ip_address, '127.0.0.1')
         self.assertEqual(conf.http_realm, 'Zope')
         servers = conf.servers