1. andrea_crotti
  2. cpython

Source

cpython / Lib / test / test_pstats.py

import unittest
import pstats
import tempfile
import cProfile as profile

from test import support
from io import StringIO
from os import remove, path
from functools import cmp_to_key


class AddCallersTestCase(unittest.TestCase):
    """Tests for pstats.add_callers helper."""

    def test_combine_results(self):
        # pstats.add_callers should combine the call results of both target
        # and source by adding the call time. See issue1269.
        # new format: used by the cProfile module
        target = {"a": (1, 2, 3, 4)}
        source = {"a": (1, 2, 3, 4), "b": (5, 6, 7, 8)}
        new_callers = pstats.add_callers(target, source)
        self.assertEqual(new_callers, {'a': (2, 4, 6, 8), 'b': (5, 6, 7, 8)})
        # old format: used by the profile module
        target = {"a": 1}
        source = {"a": 1, "b": 5}
        new_callers = pstats.add_callers(target, source)
        self.assertEqual(new_callers, {'a': 2, 'b': 5})


class StatsTestCase(unittest.TestCase):
    def setUp(self):
        stats_file = support.findfile('pstats.pck')
        self.stats = pstats.Stats(stats_file)
        #TODO: add more complicated tests, which might almost compile
        to_compile = 'import os'
        # temp_storage = tempfile.NamedTemporaryFile()
        self.temp_storage = tempfile.mktemp()
        profiled = compile(to_compile, '<string>', 'exec')
        profile.run(profiled, filename=self.temp_storage)

    def tearDown(self):
        remove(self.temp_storage)

    def test_add(self):
        """Add stats object to a stats created from an empty stream
        """
        stream = StringIO()
        stats = pstats.Stats(stream=stream)
        stats.add(self.stats, self.stats)

    def test_dump_and_load_works_correctly(self):
        """Dump a statistics on file and load the same file produces
        the same statistic
        """
        self.stats.dump_stats(filename=self.temp_storage)
        tmp_stats = pstats.Stats(self.temp_storage)
        self.assertEqual(self.stats.stats, tmp_stats.stats)
        # tmp_out = StringIO()
        # stats.dump_stats(tmp_out)

    def test_load_equivalent_to_init(self):
        """Check that we can create a pstats file with different
        objects in input
        """
        empty = pstats.Stats()
        empty.load_stats(self.temp_storage)
        created = pstats.Stats(self.temp_storage)
        self.assertEqual(empty.stats, created.stats)

    def test_loading_wrong_types(self):
        empty = pstats.Stats()
        with self.assertRaises(TypeError):
            empty.load_stats(42)

    def test_sorting_produce_correct_order(self):
        # can create a fake stats maybe to show the order
        self.stats.sort_stats("stdname")


class TupleCompTestCase(unittest.TestCase):

    def test_tuple_comp_compare_is_correct(self):
        """Test the internal class TupleComp, to see if the order
        generated on the tuples is correct
        """
        comp_list = [(0, 1), (1, -1), (2, 1)]
        tup = pstats.TupleComp(comp_list)
        to_sort = [(1, 3, 4), (2, 3, 1), (5, 4, 3)]
        desired = [(1, 3, 4), (2, 3, 1), (5, 4, 3)]
        to_sort.sort(key=cmp_to_key(tup.compare))
        self.assertEqual(to_sort, desired)
        #TODO: add more tests and make some fail too


class UtilsTestCase(unittest.TestCase):
    def test_count_calls(self):
        dic = {
            1: 2, 3: 5
        }
        dic_null = {
            1: 0, 2: 0
        }
        self.assertEqual(pstats.count_calls(dic), 7)
        self.assertEqual(pstats.count_calls(dic_null), 0)

    def test_f8(self):
        self.assertEqual(pstats.f8(2.3232), '   2.323')
        self.assertEqual(pstats.f8(0), '   0.000')

    def test_func_name(self):
        func = ('file', 10, 'name')
        self.assertEqual(pstats.func_get_function_name(func), 'name')

    def test_strip_path(self):
        # we should not test for the implementation but sometimes is good to do so
        func = (path.join('long', 'path'), 10, 'name')
        desired = ('path', 10, 'name')
        self.assertEqual(pstats.func_strip_path(func), desired)


def test_main():
    support.run_unittest(
        AddCallersTestCase,
        StatsTestCase,
        TupleCompTestCase,
        UtilsTestCase
    )


if __name__ == "__main__":
    test_main()