test_pickle2json.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
  2. # For details: https://bitbucket.org/ned/coveragepy/src/default/NOTICE.txt
  3. """Tests for coverage.pickle2json"""
  4. from coverage.backward import pickle, iitems
  5. from coverage.data import CoverageData
  6. from coverage.pickle2json import pickle2json
  7. from tests.coveragetest import CoverageTest
  8. from tests.test_data import DataTestHelpers, LINES_1, ARCS_3
  9. class Pickle2JsonTestInTempDir(DataTestHelpers, CoverageTest):
  10. """Tests pickle2json.py."""
  11. no_files_in_temp_dir = True
  12. def write_pickled_file(self, covdata, filename):
  13. """Write coverage data as pickled `filename`."""
  14. # Create the file data.
  15. file_data = {}
  16. if covdata._arcs:
  17. file_data['arcs'] = dict((f, list(amap)) for f, amap in iitems(covdata._arcs))
  18. else:
  19. file_data['lines'] = dict((f, list(lmap)) for f, lmap in iitems(covdata._lines))
  20. # Write the pickle to the file.
  21. with open(filename, 'wb') as file_obj:
  22. pickle.dump(file_data, file_obj, 2)
  23. def test_read_write_lines_pickle(self):
  24. # Test the old pickle format.
  25. covdata1 = CoverageData()
  26. covdata1.add_lines(LINES_1)
  27. self.write_pickled_file(covdata1, "lines.pkl")
  28. pickle2json("lines.pkl", "lines.json")
  29. covdata2 = CoverageData()
  30. covdata2.read_file("lines.json")
  31. self.assert_lines1_data(covdata2)
  32. def test_read_write_arcs_pickle(self):
  33. # Test the old pickle format.
  34. covdata1 = CoverageData()
  35. covdata1.add_arcs(ARCS_3)
  36. self.write_pickled_file(covdata1, "arcs.pkl")
  37. pickle2json("arcs.pkl", "arcs.json")
  38. covdata2 = CoverageData()
  39. covdata2.read_file("arcs.json")
  40. self.assert_arcs3_data(covdata2)