|
5 | 5 | from pstats import SortKey
|
6 | 6 | from enum import StrEnum, _test_simple_enum
|
7 | 7 |
|
| 8 | +import os |
8 | 9 | import pstats
|
| 10 | +import tempfile |
9 | 11 | import cProfile
|
10 | 12 |
|
11 | 13 | class AddCallersTestCase(unittest.TestCase):
|
@@ -36,6 +38,33 @@ def test_add(self):
|
36 | 38 | stats = pstats.Stats(stream=stream)
|
37 | 39 | stats.add(self.stats, self.stats)
|
38 | 40 |
|
| 41 | + def test_dump_and_load_works_correctly(self): |
| 42 | + temp_storage_new = tempfile.NamedTemporaryFile(delete=False) |
| 43 | + try: |
| 44 | + self.stats.dump_stats(filename=temp_storage_new.name) |
| 45 | + tmp_stats = pstats.Stats(temp_storage_new.name) |
| 46 | + self.assertEqual(self.stats.stats, tmp_stats.stats) |
| 47 | + finally: |
| 48 | + temp_storage_new.close() |
| 49 | + os.remove(temp_storage_new.name) |
| 50 | + |
| 51 | + def test_load_equivalent_to_init(self): |
| 52 | + stats = pstats.Stats() |
| 53 | + self.temp_storage = tempfile.NamedTemporaryFile(delete=False) |
| 54 | + try: |
| 55 | + cProfile.run('import os', filename=self.temp_storage.name) |
| 56 | + stats.load_stats(self.temp_storage.name) |
| 57 | + created = pstats.Stats(self.temp_storage.name) |
| 58 | + self.assertEqual(stats.stats, created.stats) |
| 59 | + finally: |
| 60 | + self.temp_storage.close() |
| 61 | + os.remove(self.temp_storage.name) |
| 62 | + |
| 63 | + def test_loading_wrong_types(self): |
| 64 | + stats = pstats.Stats() |
| 65 | + with self.assertRaises(TypeError): |
| 66 | + stats.load_stats(42) |
| 67 | + |
39 | 68 | def test_sort_stats_int(self):
|
40 | 69 | valid_args = {-1: 'stdname',
|
41 | 70 | 0: 'calls',
|
|
0 commit comments