add test module for configreader
1 files changed, 145 insertions(+), 0 deletions(-)

A => test/test_configreader.py
A => test/test_configreader.py +145 -0
@@ 0,0 1,145 @@ 
+
+import unittest
+import os
+import shutil
+import tempfile
+
+from vanity import configreader
+
+
+class TestConfigReader(unittest.TestCase):
+
+    def setUp(self):
+        self.cwd = os.path.abspath('.')
+        self.tdir = tempfile.mkdtemp(prefix='test_configreader_')
+        for name, text in SAMPLES.iteritems():
+            path = os.path.join(self.tdir, name)
+            fp = open(path, 'w')
+            fp.write(text)
+            fp.close()
+        os.chdir(self.tdir)
+
+    def tearDown(self):
+        os.chdir(self.cwd)
+        shutil.rmtree(self.tdir)
+        
+    
+    def test_read_config_files(self):
+        cr = configreader.ConfigReader()
+        cr.read('sample1.conf')
+        cr.read('sample2.txt')
+        cr.read('mongo.txt')
+        self.assertRaises(IOError, cr.read, 'mongo.txt', True)
+        fp = open('sample1.conf')
+        cr.readfile(fp)
+        fp.close()
+
+
+    def test_read_sample_configuration(self):
+        cr = configreader.ConfigReader()
+        cr.read('sample1.conf')
+        v = list(cr.sections())
+        self.assertEqual(v, ['hello', 'blarg'])
+        v = list(cr.items('hello'))
+        self.assertEqual(v, [('foo', '100'), ('bar', '200'), ('baz', '225')]) 
+        v = dict(cr.items('blarg'))
+        self.assertTrue('fish.sticks' in v)
+        self.assertTrue('cheese.burgers' in v)
+        self.assertTrue('cheese.burgers' in cr.keys('blarg'))
+
+    def test_iterate_all_items(self):
+        cr = configreader.ConfigReader()
+        cr.read('sample1.conf')
+        v = list(cr.allitems())
+        self.assertEqual(v, [
+            ('hello', 'foo', '100'),
+            ('hello', 'bar', '200'),
+            ('hello', 'baz', '225'),
+            ('blarg', 'fish.sticks', 'are yummy'),
+            ('blarg', 'cheese.burgers', 'are great too'),
+            ])
+        
+    def test_raise_parsing_error(self):
+        cr = configreader.ConfigReader()
+        self.assertRaises(configreader.ParsingError,
+            cr.read, 'junk.txt')
+        self.assertRaises(configreader.ParsingError,
+            cr.read, 'broken1.txt')
+        self.assertRaises(configreader.ParsingError,
+            cr.read, 'broken2.txt')
+
+
+    def test_sample_with_continuation(self):
+        cr = configreader.ConfigReader()
+        cr.read('sample2.txt')
+        v = list(cr.items('something'))
+        self.assertEqual(v[0],
+            ('long', 'this long text\ncontinues on the next line'))
+        self.assertEqual(cr.get('something', 'long'),
+            'this long text\ncontinues on the next line')
+
+
+        
+
+
+SAMPLE1 = '''
+
+[hello]
+foo = 100
+bar = 200
+baz = 225
+
+[blarg]
+fish.sticks = are yummy
+cheese.burgers = are great too
+# this is a comment, it should not be in the config
+
+'''
+
+SAMPLE2 = '''
+
+[something]
+long = this long text
+  continues on the next line
+
+; this is a ini style comment
+[check]
+url : http://foo.example.com/Blip%20Blap/zzz.txt
+url.encoding : utf-8
+'''
+
+SAMPLE3 = '''
+Hello Mike,
+
+I hope you recived my message.
+Talk to you later.
+'''
+
+SAMPLE4 = '''
+# this has a messed up heeader
+[foobar
+no = such luck
+'''
+
+SAMPLE5 = '''
+# this has a messed up continuation
+[foobar]
+this = is a working
+\tcontinuation line
+
+[barbaz]
+  a bad little indentation
+
+'''
+
+SAMPLES = {
+    'sample1.conf': SAMPLE1,
+    'sample2.txt': SAMPLE2,
+    'junk.txt': SAMPLE3,
+    'broken1.txt': SAMPLE4,
+    'broken2.txt': SAMPLE5,
+    }
+
+
+if __name__ == '__main__':
+    unittest.main()