<s> import random <EOL> from django . test import TestCase <EOL> from mock import Mock <EOL> from . test_lib . mock_suite import MockSuite <EOL> from silk . views . requests import RequestsView <EOL> class TestRootViewDefaults ( TestCase ) : <EOL> def test_path ( self ) : <EOL> requests = [ MockSuite ( ) . mock_request ( ) for _ in range ( <NUM_LIT:0> , <NUM_LIT:3> ) ] <EOL> paths = RequestsView ( ) . _get_paths ( ) <EOL> for r in requests : <EOL> self . assertIn ( r . path , paths ) <EOL> self . assertIn ( '<STR_LIT>' , paths ) <EOL> def test_show ( self ) : <EOL> self . assertIn ( RequestsView . default_show , RequestsView . show ) <EOL> def test_order_by ( self ) : <EOL> self . assertIn ( RequestsView . default_order_by , RequestsView . order_by ) <EOL> class TestContext ( TestCase ) : <EOL> def test_default ( self ) : <EOL> request = Mock ( spec_set = [ '<STR_LIT:GET>' , '<STR_LIT>' ] ) <EOL> request . session = { } <EOL> request . GET = { } <EOL> context = RequestsView ( ) . _create_context ( request ) <EOL> self . assertDictContainsSubset ( { <EOL> '<STR_LIT>' : RequestsView . default_show , <EOL> '<STR_LIT>' : RequestsView . default_order_by , <EOL> '<STR_LIT>' : RequestsView . show , <EOL> '<STR_LIT>' : RequestsView ( ) . options_order_by , <EOL> '<STR_LIT>' : RequestsView ( ) . options_order_dir , <EOL> '<STR_LIT>' : RequestsView ( ) . _get_paths ( ) <EOL> } , context ) <EOL> self . assertNotIn ( '<STR_LIT:path>' , context ) <EOL> self . assertIn ( '<STR_LIT>' , context ) <EOL> def test_get ( self ) : <EOL> request = Mock ( spec_set = [ '<STR_LIT:GET>' , '<STR_LIT>' ] ) <EOL> request . session = { } <EOL> show = <NUM_LIT:10> <EOL> path = '<STR_LIT>' <EOL> order_by = '<STR_LIT:path>' <EOL> request . GET = { '<STR_LIT>' : show , <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT>' : order_by } <EOL> context = RequestsView ( ) . _create_context ( request ) <EOL> self . assertDictContainsSubset ( { <EOL> '<STR_LIT>' : show , <EOL> '<STR_LIT>' : order_by , <EOL> '<STR_LIT:path>' : path , <EOL> '<STR_LIT>' : RequestsView . show , <EOL> '<STR_LIT>' : RequestsView ( ) . options_order_by , <EOL> '<STR_LIT>' : RequestsView ( ) . options_order_dir , <EOL> '<STR_LIT>' : RequestsView ( ) . _get_paths ( ) <EOL> } , context ) <EOL> self . assertIn ( '<STR_LIT>' , context ) <EOL> class TestGetObjects ( TestCase ) : <EOL> def assertSorted ( self , objects , sort_field ) : <EOL> for idx , r in enumerate ( objects ) : <EOL> try : <EOL> nxt = objects [ idx + <NUM_LIT:1> ] <EOL> self . assertGreaterEqual ( getattr ( r , sort_field ) , getattr ( nxt , sort_field ) ) <EOL> except IndexError : <EOL> pass <EOL> @ classmethod <EOL> def setUpClass ( cls ) : <EOL> super ( TestGetObjects , cls ) . setUpClass ( ) <EOL> cls . requests = [ MockSuite ( ) . mock_request ( ) for _ in range ( <NUM_LIT:0> , <NUM_LIT:50> ) ] <EOL> def test_defaults ( self ) : <EOL> objects = RequestsView ( ) . _get_objects ( ) <EOL> self . assertEqual ( len ( objects ) , <NUM_LIT> ) <EOL> self . assertSorted ( objects , '<STR_LIT>' ) <EOL> def test_show ( self ) : <EOL> objects = RequestsView ( ) . _get_objects ( show = <NUM_LIT:10> ) <EOL> self . assertEqual ( len ( objects ) , <NUM_LIT:10> ) <EOL> def test_path ( self ) : <EOL> request = random . choice ( self . requests ) <EOL> objects = RequestsView ( ) . _get_objects ( path = request . path ) <EOL> for r in objects : <EOL> self . assertEqual ( r . path , request . path ) <EOL> def test_time_spent_db_with_path ( self ) : <EOL> request = random . choice ( self . requests ) <EOL> query_set = RequestsView ( ) . _get_objects ( order_by = '<STR_LIT>' , <EOL> path = request . path ) <EOL> num_results = query_set . count ( ) <EOL> self . assertTrue ( num_results ) <EOL> for result in query_set : <EOL> self . assertEqual ( result . path , request . path ) <EOL> class TestOrderingRequestView ( TestCase ) : <EOL> def assertSorted ( self , objects , sort_field ) : <EOL> for idx , r in enumerate ( objects ) : <EOL> try : <EOL> nxt = objects [ idx + <NUM_LIT:1> ] <EOL> self . assertGreaterEqual ( getattr ( r , sort_field ) , getattr ( nxt , sort_field ) ) <EOL> except IndexError : <EOL> pass <EOL> def test_ordering ( self ) : <EOL> self . assertSorted ( objects = RequestsView ( ) . _get_objects ( order_by = '<STR_LIT>' ) , <EOL> sort_field = '<STR_LIT>' ) <EOL> self . assertSorted ( objects = RequestsView ( ) . _get_objects ( order_by = '<STR_LIT:path>' ) , <EOL> sort_field = '<STR_LIT:path>' ) <EOL> self . assertSorted ( objects = RequestsView ( ) . _get_objects ( order_by = '<STR_LIT>' ) , <EOL> sort_field = '<STR_LIT>' ) <EOL> self . assertSorted ( objects = RequestsView ( ) . _get_objects ( order_by = '<STR_LIT>' ) , <EOL> sort_field = '<STR_LIT>' ) <EOL> self . assertSorted ( objects = RequestsView ( ) . _get_objects ( order_by = '<STR_LIT>' ) , <EOL> sort_field = '<STR_LIT>' ) </s>
