from pprint import pprint from pathlib import Path import unittest import sys import io sys.path.append('../') from ChaChaSimpleINI import * class Test_ChaChaSimpleINI_base(unittest.TestCase): def setUp(self): [f.unlink() for f in Path("tmp").glob("*")] print("======================") def test_simpleread_value(self): testini = ChaChaSimpleINI("testfiles/test_simpleread.ini") self.assertEqual(testini.getKeyValue("testsection", "key1"),"test") self.assertEqual(testini.getKeyValue("testsection", "key2"), "2") self.assertEqual(testini.getKeyValue("testsection", "key3"), "43") self.assertEqual(testini.getKeyValue("testsection", "key4"), "0.54") def test_simpleread_value__bForceAlwaysOutputArrays(self): testini = ChaChaSimpleINI("testfiles/test_simpleread.ini",True) self.assertEqual(testini.getKeyValue("testsection", "key1"),["test"]) self.assertEqual(testini.getKeyValue("testsection", "key2"), ["2"]) self.assertEqual(testini.getKeyValue("testsection", "key3"), ["43"]) self.assertEqual(testini.getKeyValue("testsection", "key4"), ["0.54"]) def test_complexread1_value(self): testini = ChaChaSimpleINI("testfiles/test_complexread1.ini") self.assertEqual(testini.getKeyValue("testsection1", "key1"),"test1") self.assertEqual(testini.getKeyValue("testsection1", "key2"),"test2") self.assertEqual(testini.getKeyValue("testsection1", "key3"),"test3") self.assertEqual(testini.getKeyValue("testsection1", "key4"),"test4") self.assertEqual(testini.getKeyValue("testsection2", "key1"),"test1") self.assertEqual(testini.getKeyValue("testsection2", "key2"),["test2","test3"]) self.assertEqual(testini.getKeyValue("testsection2", "keya"),"0") self.assertEqual(testini.getKeyValue("test section 4", "test key two"),"test value two") def test_simpleread_section_comment(self): testini = ChaChaSimpleINI("testfiles/test_comment.ini") self.assertEqual(testini.getKeyValue("testsection1", "key1"),"test1") testini.setFilePath("tmp/out3.ini") testini.writeFile(False) testinitmp = ChaChaSimpleINI("tmp/out3.ini") self.assertEqual(testinitmp.getKeyValue("testsection1", "key1"), "test1") self.assertListEqual(list(io.open("testfiles/test_comment.ini")),list(io.open("tmp/out3.ini"))) def test_complexread1_value__bForceAlwaysOutputArrays(self): testini = ChaChaSimpleINI("testfiles/test_complexread1.ini",True) self.assertEqual(testini.getKeyValue("testsection1", "key1"),["test1"]) self.assertEqual(testini.getKeyValue("testsection1", "key2"),["test2"]) self.assertEqual(testini.getKeyValue("testsection1", "key3"),["test3"]) self.assertEqual(testini.getKeyValue("testsection1", "key4"),["test4"]) self.assertEqual(testini.getKeyValue("testsection2", "key1"),["test1"]) self.assertEqual(testini.getKeyValue("testsection2", "key2"),["test2","test3"]) self.assertEqual(testini.getKeyValue("testsection2", "keya"),["0"]) self.assertEqual(testini.getKeyValue("test section 4", "test key two"),["test value two"]) def test_complexreadwrite_value(self): testini = ChaChaSimpleINI("testfiles/test_complexreadwrite.ini") self.assertEqual(testini.getKeyValue("testsection", "key"), ["test1","test2","test3"]) testini.setFilePath("tmp/out.ini") testini.writeFile(False) testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection", "key"), ["test1","test2","test3"]) testini.setAddKeyValue("testsection", "key", "test4", True) testini.setFilePath("tmp/out2.ini") testini.writeFile(False) testinitmp = ChaChaSimpleINI("tmp/out2.ini") self.assertEqual(testinitmp.getKeyValue("testsection", "key"), ["test1","test2","test3","test4"]) testini.setAddKeyValue("testsection", "key", "test4", True,True) testini.setFilePath("tmp/out3.ini") testini.writeFile(True) testinitmp = ChaChaSimpleINI("tmp/out3.ini") self.assertEqual(testinitmp.getKeyValue("testsection", "key"), ["test1","test2","test3","test4","test4"]) def test_deletekey(self): #create copy of the file testini = ChaChaSimpleINI("testfiles/test_delete.ini") testini.setFilePath("tmp/out.ini") testini.writeFile(False) #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") section = testinitmp.getSection("testsection2") section.delKey("key1") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") with self.assertRaises(ChaChaINI_KeyNotFoundException): testinitmp.getKeyValue("testsection2", "key1") #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") section = testinitmp.getSection("testsection2") section.delKey("key2",2) testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection2", "key2"), ["test2","test3"]) #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") section = testinitmp.getSection("testsection2") section.delKey("key2",None,"test3") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection2", "key2"), "test2") #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") section = testinitmp.getSection("testsection1") with self.assertRaises(ChaChaINI_KeyNotFoundException): section.delKey("key2",1,"test2") #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") section = testinitmp.getSection("testsection1") section.delKey("key2",1,"test3") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection1", "key2"), "test2") def test_deletesection(self): #create copy of the file testini = ChaChaSimpleINI("testfiles/test_delete.ini") testini.setFilePath("tmp/out.ini") testini.writeFile(False) #remove a section testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delSection("testsection1") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") with self.assertRaises(ChaChaINI_SectionNotFoundException): testinitmp.getSection("testsection1") #remove a section testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delSection("testsection2",1) testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.getSection("testsection2") #remove a section testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delSection("testsection2",0) testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") with self.assertRaises(ChaChaINI_SectionNotFoundException): testinitmp.getSection("testsection2") #remove a section testinitmp = ChaChaSimpleINI("tmp/out.ini") with self.assertRaises(ChaChaINI_SectionNotFoundException): testinitmp.delSection("testsection2",0) def test_deletekey_fromfile(self): #create copy of the file testini = ChaChaSimpleINI("testfiles/test_delete.ini") testini.setFilePath("tmp/out.ini") testini.writeFile(False) #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delKey("testsection2","key1") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") with self.assertRaises(ChaChaINI_KeyNotFoundException): testinitmp.getKeyValue("testsection2", "key1") #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delKeyEx("testsection2","key2",2) testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection2", "key2"), ["test2","test3"]) #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delKeyEx("testsection2","key2",None,"test3") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection2", "key2"), "test2") #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") with self.assertRaises(ChaChaINI_KeyNotFoundException): testinitmp.delKeyEx("testsection1","key2",1,"test2") #create copy of the file testini = ChaChaSimpleINI("testfiles/test_delete.ini") testini.setFilePath("tmp/out.ini") testini.writeFile(False) #remove an item testinitmp = ChaChaSimpleINI("tmp/out.ini") testinitmp.delKeyEx("testsection1","key2",1,"test3") testinitmp.writeFile(True) #verify testinitmp = ChaChaSimpleINI("tmp/out.ini") self.assertEqual(testinitmp.getKeyValue("testsection1", "key2"), "test2") def test_strict_mode(self): with self.assertRaises(ChaChaINI_WrongFormatException): testini = ChaChaSimpleINI("testfiles/test_strict.ini",False,True) testini = ChaChaSimpleINI("testfiles/test_strict.ini",False,False) def test_samba(self): testini = ChaChaSimpleINI("testfiles/test_smb.conf") testini.setFilePath("tmp/test_smb.ini") testini.writeFile(False,False) testini.setFilePath("tmp/test_smb_wiped.ini") testini.writeFile(False,True) testini=None pass