Пример #1
0
    def test_begin_alive(self):
        profiler = SessionProfiler()
        profiler.alive = True
        with self.assertRaises(AssertionError) as exec_info:
            profiler.begin()

        error = exec_info.exception
        self.assertEqual(str(error), "Profiling session has already begun")
Пример #2
0
    def test_commit_alive(self):
        profiler = SessionProfiler()
        profiler.alive = False
        with self.assertRaises(AssertionError) as exec_info:
            profiler.commit()

        error = exec_info.exception
        self.assertEqual(str(error), "Profiling session is already committed")
Пример #3
0
 def test__reset_stats(self):
     profiler = SessionProfiler()
     profiler._reset_stats()
     self.assertEqual(profiler._stats["total"], 0)
     self.assertEqual(profiler._stats["duration"], 0)
     self.assertEqual(profiler._stats["select"], 0)
     self.assertEqual(profiler._stats["insert"], 0)
     self.assertEqual(profiler._stats["update"], 0)
     self.assertEqual(profiler._stats["call_stack"], [])
     self.assertEqual(profiler._stats["duplicates"], Counter())
     self.assertEqual(profiler._stats["db"], profiler.db_name)
Пример #4
0
 def test__before_cursor_execute(self, mocked):
     expected_time = time.time()
     mocked.return_value = expected_time
     profiler = SessionProfiler()
     context = mock.Mock()
     profiler._before_cursor_execute(conn=None,
                                     cursor=None,
                                     statement=None,
                                     parameters={},
                                     context=context,
                                     executemany=None)
     self.assertEqual(context._query_start_time, expected_time)
Пример #5
0
 def test_begin(self):
     profiler = SessionProfiler()
     with mock.patch.object(profiler, "_reset_stats") as mocked:
         profiler.begin()
         mocked.assert_called()
         self.assertTrue(profiler.alive)
         self.assertIsInstance(profiler.queries, Queue)
         self.assertTrue(profiler.queries.empty())
         self.assertTrue(
             event.contains(profiler.engine, profiler._before,
                            profiler._before_cursor_execute))
         self.assertTrue(
             event.contains(profiler.engine, profiler._after,
                            profiler._after_cursor_execute))
Пример #6
0
 def test__after_cursor_execute(self, mocked):
     expected_query = debug_queries[0]
     mocked.return_value = expected_query.end_time
     profiler = SessionProfiler()
     context = mock.Mock()
     context._query_start_time = expected_query.start_time
     with profiler:
         profiler._after_cursor_execute(
             conn=None,
             cursor=None,
             statement=expected_query.statement,
             parameters=expected_query.parameters,
             context=context,
             executemany=None)
         actual_query = profiler.queries.get()
         self.assertEqual(actual_query, expected_query)
Пример #7
0
 def test_decorator_path(self):
     expected_path = "test_path"
     engine = self._create_engine()
     profiler = SessionProfiler(engine)
     reporter = mock.Mock(spec=Reporter)
     # Get profiler decorator with specified path
     wrapper = profiler(path=expected_path, reporter=reporter)
     wrapper(self._decorated_func)(engine)
     # Test that reporter method report was called with expected path
     reporter.report.assert_called_with(expected_path, profiler.stats)
Пример #8
0
 def test_decorator(self):
     engine = self._create_engine()
     profiler = SessionProfiler(engine)
     wrapper = profiler()
     wrapper(self._decorated_func)(engine)
     # Test profile statistics
     self.assertEqual(profiler.stats["db"], "undefined")
     self.assertEqual(profiler.stats["total"], 4)
     self.assertEqual(profiler.stats["select"], 3)
     self.assertEqual(profiler.stats["delete"], 1)
     self.assertEqual(profiler.stats["duplicates_count"], 1)
Пример #9
0
 async def dispatch_func(self, request: Request, call_next):
     profiler = SessionProfiler(self.engine)
     path = request.url.path
     if not self._ignore_request(path):
         if request.method:
             path = "{0} {1}".format(request.method, path)
         try:
             with profiler:
                 return await call_next(request)
         finally:
             self._report_stats(path, profiler.stats)
     return await call_next(request)
Пример #10
0
    def test_decorator_path_callback(self):
        expected_path = "path_callback"

        def _callback(func, *args, **kwargs):
            return expected_path

        engine = self._create_engine()
        profiler = SessionProfiler(engine)
        reporter = mock.Mock(spec=Reporter)
        # Get profiler decorator with specified path_callback
        wrapper = profiler(path_callback=_callback, reporter=reporter)
        wrapper(self._decorated_func)(engine)
        # Test that reporter method report was called with expected path
        reporter.report.assert_called_with(expected_path, profiler.stats)
Пример #11
0
 def test_commit(self):
     profiler = SessionProfiler()
     profiler.begin()
     with mock.patch.object(profiler, "_get_stats") as mocked:
         profiler.commit()
         mocked.assert_called()
         self.assertFalse(profiler.alive)
         self.assertFalse(
             event.contains(profiler.engine, profiler._before,
                            profiler._before_cursor_execute))
         self.assertFalse(
             event.contains(profiler.engine, profiler._after,
                            profiler._after_cursor_execute))
Пример #12
0
    def test__get_stats(self):
        profiler = SessionProfiler()
        profiler.queries = Queue()
        profiler._reset_stats()
        duplicates = Counter()
        for query in debug_queries:
            profiler.queries.put(query)
            duplicates_count = duplicates.get(query.statement, -1)
            duplicates[query.statement] = duplicates_count + 1

        stats = profiler._get_stats()

        for op in SQL_OPERATORS:
            res = filter(lambda x: op.upper() in x.statement, debug_queries)
            self.assertEqual(stats[op], len(list(res)))

        self.assertEqual(stats["db"], profiler.db_name)
        self.assertEqual(stats["total"], len(debug_queries))
        self.assertListEqual(debug_queries, stats["call_stack"])
        self.assertDictEqual(stats["duplicates"], duplicates)
Пример #13
0
 def test_initialization_custom(self):
     engine = create_engine("sqlite:///test")
     profiler = SessionProfiler(engine)
     self.assertIs(profiler.engine, engine)
     self.assertEqual(profiler.db_name, "test")
Пример #14
0
 def test_initialization_default(self):
     profiler = SessionProfiler()
     self.assertIs(profiler.engine, Engine)
     self.assertEqual(profiler.db_name, "default")
     self.assertFalse(profiler.alive)
     self.assertIsNone(profiler.queries)
Пример #15
0
 def test_contextmanager_interface(self, mocked_commit, mocked_begin):
     profiler = SessionProfiler()
     with profiler:
         mocked_begin.assert_called()
     mocked_commit.assert_called()
Пример #16
0
 def test_stats(self):
     profiler = SessionProfiler()
     self.assertIsNotNone(profiler.stats)