Skip to content

Commit

Permalink
Add back even more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
albertyw committed Jan 16, 2024
1 parent 446bc8e commit e200dba
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions project/tests/test_execute_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}", ())

0 comments on commit e200dba

Please sign in to comment.