From e200dba5291d686533e8fd53908e990068f0410b Mon Sep 17 00:00:00 2001 From: Albert Wang Date: Mon, 15 Jan 2024 21:07:00 -0800 Subject: [PATCH] Add back even more tests --- project/tests/test_execute_sql.py | 35 +++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/project/tests/test_execute_sql.py b/project/tests/test_execute_sql.py index d7f10029..e84fdd62 100644 --- a/project/tests/test_execute_sql.py +++ b/project/tests/test_execute_sql.py @@ -95,3 +95,38 @@ def test_no_effect(self): with patch('silk.sql.DataCollector', return_value=Mock()) as mock_DataCollector: execute_sql(sql) self.assertFalse(mock_DataCollector().register_query.call_count) + + +class TestCollectorInteraction(TestCase): + def tearDown(self): + DataCollector().stop_python_profiler() + + def _query(self): + try: + query = list(DataCollector().queries.values())[0] + except IndexError: + self.fail('No queries created') + return query + + def test_request(self): + DataCollector().configure(request=Request.objects.create(path='/path/to/somewhere')) + sql, _ = mock_sql() + execute_sql(sql) + query = self._query() + self.assertEqual(query['request'], DataCollector().request) + + def test_registration(self): + DataCollector().configure(request=Request.objects.create(path='/path/to/somewhere')) + sql, _ = mock_sql() + execute_sql(sql) + query = self._query() + self.assertIn(query, DataCollector().queries.values()) + + def test_explain(self): + DataCollector().configure(request=Request.objects.create(path='/path/to/somewhere')) + sql, qs = mock_sql() + prefix = "EXPLAIN" + mock_cursor = sql.connection.cursor.return_value.__enter__.return_value + sql.connection.ops.explain_query_prefix.return_value = prefix + execute_sql(sql) + mock_cursor.execute.assert_called_once_with(f"{prefix} {qs}", ())