configurationengine/source/cone/confml/tests/unittest_persistentconfml.py
changeset 9 63964d875993
parent 5 d2c80f5cab53
equal deleted inserted replaced
8:a2e65c705db8 9:63964d875993
    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 
       
    32 
    30 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    33 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    31 
    34 
    32 ElementTree = utils.etree
    35 ElementTree = utils.etree
    33 
    36 
    34 testdata  = os.path.join(ROOT_PATH,'data')
    37 testdata  = os.path.join(ROOT_PATH,'data')
  1741         
  1744         
  1742         self.assert_file_contents_equal(
  1745         self.assert_file_contents_equal(
  1743             os.path.join(self.TEMP_DIR, FILE_NAME),
  1746             os.path.join(self.TEMP_DIR, FILE_NAME),
  1744             os.path.join(self.EXPECTED_DIR, 'complex_seq_with_nones.confml'))
  1747             os.path.join(self.EXPECTED_DIR, 'complex_seq_with_nones.confml'))
  1745 
  1748 
       
  1749 class TestExtensions(unittest.TestCase):
       
  1750     def test_get_reader_for_extensions(self):
       
  1751         reader = persistentconfml.get_reader_for_elem("extensions")
       
  1752         self.assertTrue(isinstance(reader, persistentconfml.ExtensionsReader))
       
  1753 
       
  1754     def test_parse_extensions_elem(self):
       
  1755         reader = persistentconfml.get_reader_for_elem("extensions")
       
  1756         elem = ElementTree.Element('extension')
       
  1757         owner = ElementTree.Element('owner')
       
  1758         owner.text = 'Testing owner'
       
  1759         origin = ElementTree.Element('origin')
       
  1760         origin.text = 'just origin'
       
  1761         target = ElementTree.Element('target')
       
  1762         target.text = 'target hw'
       
  1763         elem.append(owner)
       
  1764         elem.append(origin)
       
  1765         elem.append(target)
       
  1766         data = reader.loads(elem)
       
  1767         exts = data._get('_extension')
       
  1768         self.assertTrue(isinstance(exts, list))
       
  1769         self.assertEquals(exts[0].tag, 'owner')
       
  1770         self.assertEquals(exts[0].value, 'Testing owner')
       
  1771         self.assertEquals(exts[1].tag, 'origin')
       
  1772         self.assertEquals(exts[1].value, 'just origin')
       
  1773         self.assertEquals(exts[2].tag, 'target')
       
  1774         self.assertEquals(exts[2].value, 'target hw')
       
  1775 
       
  1776     def test_write_extensions_elem(self):
       
  1777         writer = persistentconfml.get_writer_for_class("ConfmlExtensions")
       
  1778         celem = model.ConfmlExtensions()
       
  1779         celem._add([model.ConfmlExtension('test', 123), 
       
  1780                     model.ConfmlExtension('owner', "some ownername"), 
       
  1781                     model.ConfmlExtension('target', "hw")])
       
  1782         etree = writer.dumps(celem)
       
  1783         self.assertEquals(etree.find('test').text,123)
       
  1784         self.assertEquals(etree.find('owner').text,'some ownername')
       
  1785         self.assertEquals(etree.find('target').text,'hw')
       
  1786         
  1746 class TestReadWriteConfml(BaseTestCase):
  1787 class TestReadWriteConfml(BaseTestCase):
  1747     """
  1788     """
  1748     Test case for ensuring that reading in a ConfML file and then writing
  1789     Test case for ensuring that reading in a ConfML file and then writing
  1749     it out again results in logically the same data (XML-wise) as the
  1790     it out again results in logically the same data (XML-wise) as the
  1750     original data was.
  1791     original data was.
  1833         self.assertEquals(view.list_all_features(), ['group1.proxy_fea1_set1']) 
  1874         self.assertEquals(view.list_all_features(), ['group1.proxy_fea1_set1']) 
  1834         
  1875         
  1835         self.assertTrue(os.path.exists("temp/testprojectviews"))
  1876         self.assertTrue(os.path.exists("temp/testprojectviews"))
  1836         shutil.rmtree("temp")
  1877         shutil.rmtree("temp")
  1837 
  1878 
       
  1879 class TestPickle(BaseTestCase):
       
  1880     """
       
  1881     Test case for ensuring that pickling in a ConfML file and then unpickling
       
  1882     it out again results in logically the same data (XML-wise) as the
       
  1883     original data was.
       
  1884     """
       
  1885     
       
  1886     def _normalize_xml_data(self, data):
       
  1887         """
       
  1888         Normalize XML data so that it can be compared using a binary
       
  1889         comparison.
       
  1890         """
       
  1891         etree = ElementTree.fromstring(data)
       
  1892         persistence.indent(etree)
       
  1893         normalized_data = ElementTree.tostring(etree)
       
  1894         return normalized_data
       
  1895     
       
  1896     def _run_pickle_and_unpickle_test(self, file_name, input_dir, output_dir):
       
  1897         file_path = os.path.join(input_dir, file_name)
       
  1898         
       
  1899         f = open(file_path, "rb")
       
  1900         try:        original_data = f.read()
       
  1901         finally:    f.close()
       
  1902         
       
  1903         model = persistentconfml.loads(original_data)
       
  1904         model.get_default_view()#verify that dump works also after get_default_view is called
       
  1905         
       
  1906         PATH_ORIGINAL = os.path.join(output_dir, 'original_pickled', file_name)
       
  1907         PATH_DUMPED   = os.path.join(output_dir, 'pickled', file_name)
       
  1908         
       
  1909         file_dir = os.path.dirname(PATH_DUMPED)
       
  1910         if not os.path.exists(file_dir):
       
  1911             os.makedirs(file_dir)
       
  1912     
       
  1913         dfile  = open(PATH_DUMPED, 'w')
       
  1914         try:
       
  1915             pickle.dump(model, dfile)
       
  1916         except Exception, e:
       
  1917             self.fail("Couldn't pickle \nfile: %s \nobject: %s \nexception: %s" % (PATH_ORIGINAL, model, e))
       
  1918         finally:
       
  1919             dfile.close()
       
  1920         
       
  1921         dfile  = open(PATH_DUMPED)
       
  1922         model_unpickled = pickle.load(dfile)
       
  1923         
       
  1924         normalized_model = self._normalize_xml_data(persistentconfml.dumps(model))
       
  1925         normalized_model_unpickled = self._normalize_xml_data(persistentconfml.dumps(model_unpickled))
       
  1926         
       
  1927         if normalized_model_unpickled != normalized_model:
       
  1928             self.fail("Pickle-unpickle output for file '%s' \noriginal: '%s' \npickled/unpickled: '%s'" % (PATH_ORIGINAL, normalized_model, normalized_model_unpickled))
       
  1929     
       
  1930     def _run_pickle_test_for_file(self, file_path):
       
  1931         self._run_pickle_and_unpickle_test(
       
  1932             file_name  = os.path.basename(file_path),
       
  1933             input_dir  = os.path.dirname(file_path),
       
  1934             output_dir = os.path.normpath(os.path.join(ROOT_PATH, 'temp/pickle_unpickle_results')))
       
  1935     
  1838 # Create a separate test method for each ConfML file in the read-write test data
  1936 # 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')
  1937 _READ_WRITE_TESTDATA_DIR = os.path.join(ROOT_PATH, 'testdata/read_write')
       
  1938 
  1840 for filename in filter(lambda fn: fn.endswith('.confml'), os.listdir(_READ_WRITE_TESTDATA_DIR)):
  1939 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)
  1940     path = os.path.join(_READ_WRITE_TESTDATA_DIR, filename)
  1842     test_method_name = 'test_read_write_file__%s' % filename.replace('.', '_')
  1941     test_method_name = 'test_read_write_file__%s' % filename.replace('.', '_')
  1843     
  1942     
  1844     # Use a separate function to create and set the lambda function on the
  1943     # Use a separate function to create and set the lambda function on the
  1847     def _register_test_method(path):
  1946     def _register_test_method(path):
  1848         method = lambda self: self._run_test_for_file(path)
  1947         method = lambda self: self._run_test_for_file(path)
  1849         method.__name__ = test_method_name
  1948         method.__name__ = test_method_name
  1850         setattr(TestReadWriteConfml, test_method_name, method)
  1949         setattr(TestReadWriteConfml, test_method_name, method)
  1851     _register_test_method(path)
  1950     _register_test_method(path)
  1852     
  1951 
       
  1952 _PICKLE_UNPICKLE_TESTDATA_DIR = os.path.join(ROOT_PATH, 'testdata/pickle_unpickle')
       
  1953 
       
  1954 for filename in filter(lambda fn: fn.endswith('.confml'), os.listdir(_PICKLE_UNPICKLE_TESTDATA_DIR)):
       
  1955     path = os.path.join(_PICKLE_UNPICKLE_TESTDATA_DIR, filename)
       
  1956     test_pickle_method_name = 'test_pickle_unpickle_file__%s' % filename.replace('.', '_')
       
  1957     
       
  1958     # Use a separate function to create and set the lambda function on the
       
  1959     # test class, because otherwise 'path' would be the last one value set to
       
  1960     # it in the for loop
       
  1961     def _register_pickle_test_method(path):
       
  1962         method = lambda self: self._run_pickle_test_for_file(path)
       
  1963         method.__name__ = test_pickle_method_name
       
  1964         setattr(TestPickle, test_pickle_method_name, method)
       
  1965     _register_pickle_test_method(path)
  1853 
  1966 
  1854 if __name__ == '__main__':
  1967 if __name__ == '__main__':
  1855     unittest.main()
  1968     unittest.main()