<s> from nose . tools import * <EOL> from mock import Mock , MagicMock , patch <EOL> from git_orm . testcases import GitTestCase <EOL> from git_orm import models <EOL> from git_orm . models import Q <EOL> from git_orm . models . queryset import QuerySet <EOL> class X ( models . Model ) : <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT:x>' <EOL> class TestRepr ( GitTestCase ) : <EOL> def test_repr ( self ) : <EOL> qs = QuerySet ( X ) <EOL> qs . REPR_MAXLEN = <NUM_LIT:2> <EOL> for repr_ in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> eq_ ( repr ( qs ) , repr_ ) <EOL> X . create ( ) <EOL> qs . REPR_MAXLEN = <NUM_LIT:3> <EOL> eq_ ( repr ( qs ) , '<STR_LIT>' ) <EOL> qs . REPR_MAXLEN = <NUM_LIT:4> <EOL> eq_ ( repr ( qs ) , '<STR_LIT>' ) <EOL> class TestEq : <EOL> def test_eq ( self ) : <EOL> qs = QuerySet ( X ) <EOL> eq_ ( qs , qs ) <EOL> eq_ ( qs , QuerySet ( X ) ) <EOL> eq_ ( qs , qs . all ( ) ) <EOL> eq_ ( qs , qs . filter ( ) ) <EOL> eq_ ( qs . filter ( ) , qs . all ( ) ) <EOL> def test_eq_somthing_else ( self ) : <EOL> assert_not_equal ( QuerySet ( X ) , None ) <EOL> assert_not_equal ( QuerySet ( X ) , '<STR_LIT>' ) <EOL> assert_not_equal ( QuerySet ( X ) , X ) <EOL> def test_eq_other_model ( self ) : <EOL> class Y ( models . Model ) : pass <EOL> assert_not_equal ( QuerySet ( X ) , QuerySet ( Y ) ) <EOL> def test_eq_other_query ( self ) : <EOL> assert_not_equal ( QuerySet ( X ) , QuerySet ( X ) . filter ( title = '<STR_LIT:foo>' ) ) <EOL> class TestOperators : <EOL> def test_or ( self ) : <EOL> qs = QuerySet ( X ) <EOL> eq_ ( ( qs . filter ( a = <NUM_LIT:1> ) | qs . filter ( b = <NUM_LIT:2> ) ) . query , Q ( a = <NUM_LIT:1> ) | Q ( b = <NUM_LIT:2> ) ) <EOL> def test_and ( self ) : <EOL> qs = QuerySet ( X ) <EOL> eq_ ( ( qs . filter ( a = <NUM_LIT:1> ) & qs . filter ( b = <NUM_LIT:2> ) ) . query , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> def test_invert ( self ) : <EOL> qs = QuerySet ( X ) <EOL> eq_ ( ~ qs . filter ( a = <NUM_LIT:1> ) . query , ~ Q ( a = <NUM_LIT:1> ) ) <EOL> def test_slice ( self ) : <EOL> qs = QuerySet ( X ) <EOL> eq_ ( qs [ : <NUM_LIT:3> ] . query , Q ( ) [ : <NUM_LIT:3> ] ) <EOL> class TestAll : <EOL> def test_all ( self ) : <EOL> eq_ ( QuerySet ( X ) . all ( ) . query , Q ( ) ) <EOL> class TestFilter : <EOL> def filter ( self , * args , ** kwargs ) : <EOL> return QuerySet ( X ) . filter ( * args , ** kwargs ) . query <EOL> def test_filter ( self ) : <EOL> eq_ ( self . filter ( ) , Q ( ) ) <EOL> def test_with_args ( self ) : <EOL> eq_ ( self . filter ( Q ( a = <NUM_LIT:1> ) ) , Q ( a = <NUM_LIT:1> ) ) <EOL> eq_ ( self . filter ( Q ( a = <NUM_LIT:1> ) , Q ( b = <NUM_LIT:2> ) ) , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> def test_with_kwargs ( self ) : <EOL> eq_ ( self . filter ( a = <NUM_LIT:1> ) , Q ( a = <NUM_LIT:1> ) ) <EOL> eq_ ( self . filter ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> def test_with_args_and_kwargs ( self ) : <EOL> eq_ ( self . filter ( Q ( a = <NUM_LIT:1> ) , b = <NUM_LIT:2> ) , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> eq_ ( self . filter ( Q ( a = <NUM_LIT:1> ) , Q ( b = <NUM_LIT:2> ) , c = <NUM_LIT:3> ) , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> , c = <NUM_LIT:3> ) ) <EOL> eq_ ( self . filter ( Q ( a = <NUM_LIT:1> ) , b = <NUM_LIT:2> , c = <NUM_LIT:3> ) , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> , c = <NUM_LIT:3> ) ) <EOL> eq_ ( self . filter ( Q ( a = <NUM_LIT:1> ) , Q ( b = <NUM_LIT:2> ) , c = <NUM_LIT:3> , d = <NUM_LIT:4> ) , Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> , c = <NUM_LIT:3> , d = <NUM_LIT:4> ) ) <EOL> class TestExclude : <EOL> def exclude ( self , * args , ** kwargs ) : <EOL> return QuerySet ( X ) . exclude ( * args , ** kwargs ) . query <EOL> def test_exclude ( self ) : <EOL> eq_ ( self . exclude ( ) , ~ Q ( ) ) <EOL> def test_with_args ( self ) : <EOL> eq_ ( self . exclude ( Q ( a = <NUM_LIT:1> ) ) , ~ Q ( a = <NUM_LIT:1> ) ) <EOL> eq_ ( self . exclude ( Q ( a = <NUM_LIT:1> ) , Q ( b = <NUM_LIT:2> ) ) , ~ Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> def test_with_kwargs ( self ) : <EOL> eq_ ( self . exclude ( a = <NUM_LIT:1> ) , ~ Q ( a = <NUM_LIT:1> ) ) <EOL> eq_ ( self . exclude ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) , ~ Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> def test_with_args_and_kwargs ( self ) : <EOL> eq_ ( self . exclude ( Q ( a = <NUM_LIT:1> ) , b = <NUM_LIT:2> ) , ~ Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> eq_ ( self . exclude ( Q ( a = <NUM_LIT:1> ) , Q ( b = <NUM_LIT:2> ) , c = <NUM_LIT:3> ) , ~ Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> , c = <NUM_LIT:3> ) ) <EOL> eq_ ( self . exclude ( Q ( a = <NUM_LIT:1> ) , b = <NUM_LIT:2> , c = <NUM_LIT:3> ) , ~ Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> , c = <NUM_LIT:3> ) ) <EOL> eq_ ( self . exclude ( Q ( a = <NUM_LIT:1> ) , Q ( b = <NUM_LIT:2> ) , c = <NUM_LIT:3> , d = <NUM_LIT:4> ) , ~ Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> , c = <NUM_LIT:3> , d = <NUM_LIT:4> ) ) <EOL> class TestOrderBy : <EOL> def order_by ( self , * args ) : <EOL> return QuerySet ( X ) . order_by ( * args ) . query <EOL> def test_order_by ( self ) : <EOL> eq_ ( self . order_by ( ) , Q ( ) . order_by ( ) ) <EOL> eq_ ( self . order_by ( '<STR_LIT:a>' ) , Q ( ) . order_by ( '<STR_LIT:a>' ) ) <EOL> eq_ ( self . order_by ( '<STR_LIT:a>' , '<STR_LIT>' ) , Q ( ) . order_by ( '<STR_LIT:a>' , '<STR_LIT>' ) ) <EOL> class TestChaining : <EOL> def filter_exclude ( self ) : <EOL> eq_ ( QuerySet ( X ) . filter ( a = <NUM_LIT:1> ) . exclude ( b = <NUM_LIT:2> ) . query , Q ( a = <NUM_LIT:1> ) & ~ Q ( b = <NUM_LIT:2> ) ) <EOL> def filter_order_by ( self ) : <EOL> eq_ ( QuerySet ( X ) . filter ( a = <NUM_LIT:1> ) . order_by ( '<STR_LIT:b>' ) . query , Q ( a = <NUM_LIT:1> ) . order_by ( '<STR_LIT:b>' ) ) <EOL> def filter_slice ( self ) : <EOL> eq_ ( QuerySet ( X ) . filter ( a = <NUM_LIT:1> ) [ : <NUM_LIT:3> ] . query , Q ( a = <NUM_LIT:1> ) [ : <NUM_LIT:3> ] ) <EOL> def exclude_filter ( self ) : <EOL> eq_ ( QuerySet ( X ) . exclude ( a = <NUM_LIT:1> ) . filter ( b = <NUM_LIT:2> ) . query , ~ Q ( a = <NUM_LIT:1> ) & Q ( b = <NUM_LIT:2> ) ) <EOL> def exclude_order_by ( self ) : <EOL> eq_ ( QuerySet ( X ) . exclude ( a = <NUM_LIT:1> ) . order_by ( '<STR_LIT:b>' ) . query , ~ Q ( a = <NUM_LIT:1> ) . order_by ( '<STR_LIT:b>' ) ) <EOL> def exclude_slice ( self ) : <EOL> eq_ ( QuerySet ( X ) . exclude ( a = <NUM_LIT:1> ) [ : <NUM_LIT:3> ] . query , ~ Q ( a = <NUM_LIT:1> ) [ : <NUM_LIT:3> ] ) <EOL> def order_by_filter ( self ) : <EOL> eq_ ( QuerySet ( X ) . order_by ( '<STR_LIT:a>' ) . filter ( b = <NUM_LIT:2> ) . query , <EOL> Q ( ) . order_by ( '<STR_LIT:a>' ) & Q ( b = <NUM_LIT:2> ) ) <EOL> def order_by_exclude ( self ) : <EOL> eq_ ( QuerySet ( X ) . order_by ( '<STR_LIT:a>' ) . exclude ( b = <NUM_LIT:2> ) . query , <EOL> Q ( ) . order_by ( '<STR_LIT:a>' ) & ~ Q ( b = <NUM_LIT:2> ) ) <EOL> def order_by_slice ( self ) : <EOL> eq_ ( QuerySet ( X ) . order_by ( '<STR_LIT:a>' ) [ : <NUM_LIT:3> ] . query , Q ( ) . order_by ( '<STR_LIT:a>' ) [ : <NUM_LIT:3> ] ) <EOL> def slice_filter ( self ) : <EOL> eq_ ( QuerySet ( X ) [ : <NUM_LIT:3> ] . filter ( a = <NUM_LIT:1> ) . query , Q ( ) [ : <NUM_LIT:3> ] & Q ( a = <NUM_LIT:1> ) ) <EOL> def slice_exclude ( self ) : <EOL> eq_ ( QuerySet ( X ) [ : <NUM_LIT:3> ] . exclude ( a = <NUM_LIT:1> ) . query , Q ( ) [ : <NUM_LIT:3> ] & ~ Q ( a = <NUM_LIT:1> ) ) <EOL> def slice_order_by ( self ) : <EOL> eq_ ( QuerySet ( X ) [ : <NUM_LIT:3> ] . order_by ( '<STR_LIT:a>' ) . query , Q ( ) [ : <NUM_LIT:3> ] . order_by ( '<STR_LIT:a>' ) ) <EOL> class TestExecute : <EOL> @ patch ( '<STR_LIT>' ) <EOL> def test_execute ( self , MockCache ) : <EOL> obj_cache = Mock ( pks = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ] ) <EOL> MockCache . return_value = obj_cache <EOL> qs = QuerySet ( X ) <EOL> qs . query = MagicMock ( ) <EOL> qs . query . __and__ . return_value = qs . query <EOL> qs . query . execute . return_value = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> eq_ ( qs . _execute ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) , ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , obj_cache ) ) <EOL> qs . query . __and__ . assert_called_once_with ( Q ( a = <NUM_LIT:1> , b = <NUM_LIT:2> ) ) <EOL> qs . query . execute . assert_called_once_with ( obj_cache , obj_cache . pks ) <EOL> def test_zero ( self ) : <EOL> qs = QuerySet ( X ) <EOL> qs . _execute = lambda : ( iter ( [ ] ) , { } ) <EOL> assert_raises ( X . DoesNotExist , qs . get ) <EOL> ok_ ( not qs . exists ( ) ) <EOL> eq_ ( qs . count ( ) , <NUM_LIT:0> ) <EOL> eq_ ( list ( qs ) , [ ] ) <EOL> def test_one ( self ) : <EOL> x = X ( ) <EOL> qs = QuerySet ( X ) <EOL> qs . _execute = lambda : ( iter ( [ <NUM_LIT:0> ] ) , { <NUM_LIT:0> : x } ) <EOL> eq_ ( qs . get ( ) , x ) <EOL> ok_ ( qs . exists ( ) ) <EOL> eq_ ( qs . count ( ) , <NUM_LIT:1> ) <EOL> eq_ ( list ( qs ) , [ x ] ) <EOL> def test_two ( self ) : <EOL> x1 = X ( ) <EOL> x2 = X ( ) <EOL> qs = QuerySet ( X ) <EOL> qs . _execute = lambda : ( iter ( [ <NUM_LIT:0> , <NUM_LIT:1> ] ) , { <NUM_LIT:0> : x1 , <NUM_LIT:1> : x2 } ) <EOL> assert_raises ( X . MultipleObjectsReturned , qs . get ) <EOL> ok_ ( qs . exists ( ) ) <EOL> eq_ ( qs . count ( ) , <NUM_LIT:2> ) <EOL> eq_ ( list ( qs ) , [ x1 , x2 ] ) </s>
