<s> from django . test import TestCase <EOL> from mock import Mock , NonCallableMock , NonCallableMagicMock , patch <EOL> from silk . collector import DataCollector <EOL> from silk . models import SQLQuery , Request <EOL> from silk . sql import execute_sql <EOL> from . util import delete_all_models <EOL> def mock_sql ( ) : <EOL> mock_sql_query = Mock ( spec_set = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> mock_sql_query . _execute_sql = Mock ( ) <EOL> mock_sql_query . query = NonCallableMock ( spec_set = [ '<STR_LIT>' ] ) <EOL> mock_sql_query . query . model = Mock ( ) <EOL> query_string = '<STR_LIT>' <EOL> mock_sql_query . as_sql = Mock ( return_value = ( query_string , ( ) ) ) <EOL> return mock_sql_query , query_string <EOL> class TestCall ( TestCase ) : <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> super ( TestCall , cls ) . setUpClass ( ) <EOL> DataCollector ( ) . configure ( request = None ) <EOL> delete_all_models ( SQLQuery ) <EOL> cls . mock_sql , cls . query_string = mock_sql ( ) <EOL> kwargs = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT>' : <NUM_LIT:2> <EOL> } <EOL> cls . args = [ <NUM_LIT:1> , <NUM_LIT:2> ] <EOL> cls . kwargs = kwargs <EOL> execute_sql ( cls . mock_sql , * cls . args , ** cls . kwargs ) <EOL> def test_called ( self ) : <EOL> self . mock_sql . _execute_sql . assert_called_once_with ( * self . args , ** self . kwargs ) <EOL> def test_count ( self ) : <EOL> self . assertEqual ( <NUM_LIT:1> , len ( DataCollector ( ) . queries ) ) <EOL> def _get_query ( self ) : <EOL> query = list ( DataCollector ( ) . queries . values ( ) ) [ <NUM_LIT:0> ] <EOL> return query <EOL> def test_no_request ( self ) : <EOL> query = self . _get_query ( ) <EOL> self . assertNotIn ( '<STR_LIT>' , query ) <EOL> def test_query ( self ) : <EOL> query = self . _get_query ( ) <EOL> self . assertEqual ( query [ '<STR_LIT>' ] , self . query_string ) <EOL> class TestCallSilky ( TestCase ) : <EOL> def test_no_effect ( self ) : <EOL> DataCollector ( ) . configure ( ) <EOL> sql , _ = mock_sql ( ) <EOL> sql . query . model = NonCallableMagicMock ( spec_set = [ '<STR_LIT>' ] ) <EOL> sql . query . model . __module__ = '<STR_LIT>' <EOL> with patch ( '<STR_LIT>' , return_value = Mock ( ) ) as mock_DataCollector : <EOL> execute_sql ( sql ) <EOL> self . assertFalse ( mock_DataCollector ( ) . register_query . call_count ) <EOL> class TestCollectorInteraction ( TestCase ) : <EOL> def _query ( self ) : <EOL> try : <EOL> query = list ( DataCollector ( ) . queries . values ( ) ) [ <NUM_LIT:0> ] <EOL> except IndexError : <EOL> self . fail ( '<STR_LIT>' ) <EOL> return query <EOL> def test_request ( self ) : <EOL> DataCollector ( ) . configure ( request = Request . objects . create ( path = '<STR_LIT>' ) ) <EOL> sql , _ = mock_sql ( ) <EOL> execute_sql ( sql ) <EOL> query = self . _query ( ) <EOL> self . assertEqual ( query [ '<STR_LIT>' ] , DataCollector ( ) . request ) <EOL> def test_registration ( self ) : <EOL> DataCollector ( ) . configure ( request = Request . objects . create ( path = '<STR_LIT>' ) ) <EOL> sql , _ = mock_sql ( ) <EOL> execute_sql ( sql ) <EOL> query = self . _query ( ) <EOL> self . assertIn ( query , DataCollector ( ) . queries . values ( ) ) </s>
