configurationengine/source/cone/confml/tests/unittest_persistentconfml.py
changeset 4 0951727b8815
parent 3 e7e0ae78773e
child 5 d2c80f5cab53
equal deleted inserted replaced
3:e7e0ae78773e 4:0951727b8815
    25 
    25 
    26 from cone.public import api, persistence, utils
    26 from cone.public import api, persistence, utils
    27 from cone.storage import filestorage
    27 from cone.storage import filestorage
    28 from cone.confml import persistentconfml, model, confmltree
    28 from cone.confml import persistentconfml, model, confmltree
    29 from testautomation.base_testcase import BaseTestCase
    29 from testautomation.base_testcase import BaseTestCase
       
    30 import pickle
       
    31 
    30 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    32 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    31 
    33 
    32 ElementTree = utils.etree
    34 ElementTree = utils.etree
    33 
    35 
    34 testdata  = os.path.join(ROOT_PATH,'data')
    36 testdata  = os.path.join(ROOT_PATH,'data')
  1833         self.assertEquals(view.list_all_features(), ['group1.proxy_fea1_set1']) 
  1835         self.assertEquals(view.list_all_features(), ['group1.proxy_fea1_set1']) 
  1834         
  1836         
  1835         self.assertTrue(os.path.exists("temp/testprojectviews"))
  1837         self.assertTrue(os.path.exists("temp/testprojectviews"))
  1836         shutil.rmtree("temp")
  1838         shutil.rmtree("temp")
  1837 
  1839 
       
  1840 class TestPickle(BaseTestCase):
       
  1841     """
       
  1842     Test case for ensuring that pickling in a ConfML file and then unpickling
       
  1843     it out again results in logically the same data (XML-wise) as the
       
  1844     original data was.
       
  1845     """
       
  1846     
       
  1847     def _normalize_xml_data(self, data):
       
  1848         """
       
  1849         Normalize XML data so that it can be compared using a binary
       
  1850         comparison.
       
  1851         """
       
  1852         etree = ElementTree.fromstring(data)
       
  1853         persistence.indent(etree)
       
  1854         normalized_data = ElementTree.tostring(etree)
       
  1855         return normalized_data
       
  1856     
       
  1857     def _run_pickle_and_unpickle_test(self, file_name, input_dir, output_dir):
       
  1858         file_path = os.path.join(input_dir, file_name)
       
  1859         
       
  1860         f = open(file_path, "rb")
       
  1861         try:        original_data = f.read()
       
  1862         finally:    f.close()
       
  1863         
       
  1864         model = persistentconfml.loads(original_data)
       
  1865         
       
  1866         PATH_ORIGINAL = os.path.join(output_dir, 'original_pickled', file_name)
       
  1867         PATH_DUMPED   = os.path.join(output_dir, 'pickled', file_name)
       
  1868         
       
  1869         file_dir = os.path.dirname(PATH_DUMPED)
       
  1870         if not os.path.exists(file_dir):
       
  1871             os.makedirs(file_dir)
       
  1872     
       
  1873         dfile  = open(PATH_DUMPED, 'w')
       
  1874         try:
       
  1875             pickle.dump(model, dfile)
       
  1876         except Exception, e:
       
  1877             self.fail("Couldn't pickle \nfile: %s \nobject: %s \nexception: %s" % (PATH_ORIGINAL, model, e))
       
  1878         finally:
       
  1879             dfile.close()
       
  1880         
       
  1881         dfile  = open(PATH_DUMPED)
       
  1882         model_unpickled = pickle.load(dfile)
       
  1883         
       
  1884         normalized_model = self._normalize_xml_data(persistentconfml.dumps(model))
       
  1885         normalized_model_unpickled = self._normalize_xml_data(persistentconfml.dumps(model_unpickled))
       
  1886         
       
  1887         if normalized_model_unpickled != normalized_model:
       
  1888             self.fail("Pickle-unpickle output for file '%s' \noriginal: '%s' \npickled/unpickled: '%s'" % (PATH_ORIGINAL, normalized_model, normalized_model_unpickled))
       
  1889     
       
  1890     def _run_pickle_test_for_file(self, file_path):
       
  1891         self._run_pickle_and_unpickle_test(
       
  1892             file_name  = os.path.basename(file_path),
       
  1893             input_dir  = os.path.dirname(file_path),
       
  1894             output_dir = os.path.normpath(os.path.join(ROOT_PATH, 'temp/pickle_unpickle_results')))
       
  1895     
  1838 # Create a separate test method for each ConfML file in the read-write test data
  1896 # Create a separate test method for each ConfML file in the read-write test data
  1839 _READ_WRITE_TESTDATA_DIR = os.path.join(ROOT_PATH, 'testdata/read_write')
  1897 _READ_WRITE_TESTDATA_DIR = os.path.join(ROOT_PATH, 'testdata/read_write')
       
  1898 
  1840 for filename in filter(lambda fn: fn.endswith('.confml'), os.listdir(_READ_WRITE_TESTDATA_DIR)):
  1899 for filename in filter(lambda fn: fn.endswith('.confml'), os.listdir(_READ_WRITE_TESTDATA_DIR)):
  1841     path = os.path.join(_READ_WRITE_TESTDATA_DIR, filename)
  1900     path = os.path.join(_READ_WRITE_TESTDATA_DIR, filename)
  1842     test_method_name = 'test_read_write_file__%s' % filename.replace('.', '_')
  1901     test_method_name = 'test_read_write_file__%s' % filename.replace('.', '_')
  1843     
  1902     
  1844     # Use a separate function to create and set the lambda function on the
  1903     # Use a separate function to create and set the lambda function on the
  1847     def _register_test_method(path):
  1906     def _register_test_method(path):
  1848         method = lambda self: self._run_test_for_file(path)
  1907         method = lambda self: self._run_test_for_file(path)
  1849         method.__name__ = test_method_name
  1908         method.__name__ = test_method_name
  1850         setattr(TestReadWriteConfml, test_method_name, method)
  1909         setattr(TestReadWriteConfml, test_method_name, method)
  1851     _register_test_method(path)
  1910     _register_test_method(path)
  1852     
  1911 
       
  1912 _PICKLE_UNPICKLE_TESTDATA_DIR = os.path.join(ROOT_PATH, 'testdata/pickle_unpickle')
       
  1913 
       
  1914 for filename in filter(lambda fn: fn.endswith('.confml'), os.listdir(_PICKLE_UNPICKLE_TESTDATA_DIR)):
       
  1915     path = os.path.join(_PICKLE_UNPICKLE_TESTDATA_DIR, filename)
       
  1916     test_pickle_method_name = 'test_pickle_unpickle_file__%s' % filename.replace('.', '_')
       
  1917     
       
  1918     # Use a separate function to create and set the lambda function on the
       
  1919     # test class, because otherwise 'path' would be the last one value set to
       
  1920     # it in the for loop
       
  1921     def _register_pickle_test_method(path):
       
  1922         method = lambda self: self._run_pickle_test_for_file(path)
       
  1923         method.__name__ = test_pickle_method_name
       
  1924         setattr(TestPickle, test_pickle_method_name, method)
       
  1925     _register_pickle_test_method(path)
  1853 
  1926 
  1854 if __name__ == '__main__':
  1927 if __name__ == '__main__':
  1855     unittest.main()
  1928     unittest.main()