Package rats :: Package test :: Module test_flatconfig
[hide private]
[frames] | no frames]

Source Code for Module rats.test.test_flatconfig

  1  #!/usr/bin/env python 
  2  import tempfile 
  3   
  4  from rats import flatconfig  
  5   
  6  from twisted.trial import unittest 
  7  from twisted.internet import defer 
  8  from twisted.internet import reactor 
  9  from twisted.python import failure 
 10   
 11  VERBOSE = False 
12 -def verb(txt):
13 global VERBOSE 14 if VERBOSE: 15 print(txt)
16 17
18 -class Test_Config_File(unittest.TestCase):
19 - def setUp(self):
20 self.file_name = tempfile.mktemp() 21 verb("File name : " + self.file_name) 22 f = file(self.file_name, "w") 23 f.write(""" 24 # this is a comment 25 "ham"=spam 26 egg="bacon and toast" 27 egg="cheese" 28 number =2 29 number= 3 30 bool_val = yes 31 """) 32 f.close()
33
34 - def tearDown(self):
35 pass
36
37 - def test_01_parse(self):
38 cp = flatconfig.ConfigParser() 39 cp.read(self.file_name) 40 res = cp.items() 41 verb("Result is %s" % (res)) 42 has_ham = False 43 num_eggs = 0 44 for k, v in res: 45 if k == "egg": 46 num_eggs += 1 47 elif k == "ham": 48 has_ham = True 49 if not has_ham: 50 self.fail("Expected key \"ham\" not found.") 51 if num_eggs != 2: 52 self.fail("Expected %s times the key %s, but found it %s times." % (2, "egg", num_eggs)) 53 # number of elements 54 expected_length = 6 55 if len(res) != expected_length: 56 self.fail("Expected a list of %d items, but has %s." % (expected_length, len(res))) 57 dict_len = 4 58 d = dict(res) 59 if len(d) != dict_len: 60 self.fail("Expected a dict of %d items, but has %s." % (dict_len, len(d)))
61
62 - def test_02_list(self):
63 cp = flatconfig.ConfigParser() 64 cp.read(self.file_name) 65 # many entries with same key + int type 66 numbers = cp.get_list("number", int) 67 if len(numbers) != 2: 68 self.fail("Expected two values with the \"number\" option name.") 69 for i in [2, 3]: 70 if i not in numbers: 71 self.fail("Could not find value %d" % (i))
72
73 - def test_03_types(self):
74 cp = flatconfig.ConfigParser() 75 cp.read(self.file_name) 76 # bool 77 val = cp.get("bool_val", bool) 78 if val != True: 79 self.fail("Expected value to be True.")
80 81
82 -class Test_Invalid_Config_File(unittest.TestCase):
83 - def setUp(self):
84 self.file_name = tempfile.mktemp() 85 verb("File name : " + self.file_name) 86 f = file(self.file_name, "w") 87 f.write(""" 88 # this is a comment 89 "ham" = 90 """) 91 f.close()
92
93 - def tearDown(self):
94 pass
95
96 - def test_01_no_value_for_key(self):
97 cp = flatconfig.ConfigParser() 98 try: 99 cp.read(self.file_name) 100 except flatconfig.ParsingError, e: 101 pass 102 else: 103 res = cp.items() 104 self.fail("Expected an error parsing the file but got %s" % (res))
105
106 -class Test_File_Not_Found(unittest.TestCase):
107 - def test_not_found(self):
108 cp = flatconfig.ConfigParser() 109 try: 110 cp.read(tempfile.mktemp()) 111 except flatconfig.FileNotFoundError, e: 112 pass 113 else: 114 self.fail("Expected an error trying to read non-existant file.")
115