示例#1
0
 def testTestMode(self):
     tstfile = tempfile.NamedTemporaryFile(suffix=".yaml")
     tstfile = tstfile.name
     #Set testmode
     cfg= ConfigYaml()
     cfg.read(tstfile)
     cfg.testmode()
     #Make change
     cfg["new"]="Really!"
     #Release the handle
     cfg = None
     cfg1=ConfigYaml()
     cfg1.read("/tmp.mmtest.yaml")
     self.assertRaises(KeyError,lambda x:cfg1[x],"new")
示例#2
0
 def testNoFile(self):
     tstfile = tempfile.NamedTemporaryFile(suffix=".yaml")
     tstfile = tstfile.name
     try:
         os.remove(tstfile) 
     except:
         pass
     cfg= ConfigYaml()
     cfg.read(tstfile)
     cfg['test1'] ="foo"
     cfg['test2'] = {'a':'2' }
     cfg.write()
     cfg = None
     cfg1 = ConfigYaml()
     cfg1.read(tstfile)
     self.assertEquals(cfg1['test1'],"foo")
     self.assertEquals(cfg1['test2'],{'a':'2' })
      #cleanup
     try:
         os.remove(tstfile) 
     except:
         pass 
示例#3
0
class ConfigYamlTest(unittest.TestCase):
    def setUp(self):
        self.id = ExtensionSecureID.fromPathName("tests/test.yaml")
        self.cfg= ConfigYaml(False)
        self.cfg.read("tests/test.yaml")
        pass
    
    def testRead(self):
        self.assertEqual(type(self.cfg["testsection"]),dict)
        self.assertEqual(int(self.cfg["testsection"]["test"]),1)
        #TODO test __iter__    
   
    def testWrite(self):
        self.assertEqual(type(self.cfg["testsection"]),dict)
        self.cfg["testsection"]["newval"]=1
        self.assertEqual(type(self.cfg["testsection"]),dict)
        self.assertEqual(int(self.cfg["testsection"]["newval"]),1)

    def testDelete(self):
        self.assertEqual(type(self.cfg["testsection"]),dict)
        self.assertEqual(int(self.cfg["testsection"]["test"]),1)
        del self.cfg["testsection"]["test"]
        self.assertRaises(KeyError,lambda : self.cfg["testsection"]["test"])

    def testDict(self):
        self.assertEqual(type(self.cfg["extensions"]),dict)
        self.assertEqual(type(self.cfg["extensions"]["testsection"]),dict)
        self.assertEqual(int(self.cfg["extensions"]["testsection"]["test"]),1)
        self.assertEqual(int(self.cfg["extensions"]["value"]),1)

    def testList(self):
        self.assertEqual(type(self.cfg["testlist"]),list)
        self.assertEqual(int(self.cfg["testlist"][0]),1)
        self.assertEqual(type(self.cfg["testlist"][1]),dict)
    
    def testiter(self):
        for i in self.cfg:
            self.assertFalse(self.cfg is i)
        for i in self.cfg["testsection"]:
            self.assertFalse(self.cfg["testsection"] is i)
        for i in self.cfg["testlist"]:
            self.assertFalse(self.cfg["testlist"] is i)       

    def testNoFile(self):
        tstfile = tempfile.NamedTemporaryFile(suffix=".yaml")
        tstfile = tstfile.name
        try:
            os.remove(tstfile) 
        except:
            pass
        cfg= ConfigYaml()
        cfg.read(tstfile)
        cfg['test1'] ="foo"
        cfg['test2'] = {'a':'2' }
        cfg.write()
        cfg = None
        cfg1 = ConfigYaml()
        cfg1.read(tstfile)
        self.assertEquals(cfg1['test1'],"foo")
        self.assertEquals(cfg1['test2'],{'a':'2' })
         #cleanup
        try:
            os.remove(tstfile) 
        except:
            pass 

    

    def testTestMode(self):
        tstfile = tempfile.NamedTemporaryFile(suffix=".yaml")
        tstfile = tstfile.name
        #Set testmode
        cfg= ConfigYaml()
        cfg.read(tstfile)
        cfg.testmode()
        #Make change
        cfg["new"]="Really!"
        #Release the handle
        cfg = None
        cfg1=ConfigYaml()
        cfg1.read("/tmp.mmtest.yaml")
        self.assertRaises(KeyError,lambda x:cfg1[x],"new")