<s> from __future__ import unicode_literals <EOL> from datetime import date <EOL> from decimal import Decimal <EOL> from django . db . models . query_utils import InvalidQuery <EOL> from django . test import TestCase , skipUnlessDBFeature <EOL> from . models import Author , Book , BookFkAsPk , Coffee , FriendlyAuthor , Reviewer <EOL> class RawQueryTests ( TestCase ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> cls . a1 = Author . objects . create ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , dob = date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:20> ) ) <EOL> cls . a2 = Author . objects . create ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , dob = date ( <NUM_LIT> , <NUM_LIT:4> , <NUM_LIT:2> ) ) <EOL> cls . a3 = Author . objects . create ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , dob = date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT> ) ) <EOL> cls . a4 = Author . objects . create ( first_name = '<STR_LIT>' , last_name = '<STR_LIT>' , dob = date ( <NUM_LIT> , <NUM_LIT:5> , <NUM_LIT:10> ) ) <EOL> cls . b1 = Book . objects . create ( <EOL> title = '<STR_LIT>' , author = cls . a1 , paperback = False , <EOL> opening_line = '<STR_LIT>' , <EOL> ) <EOL> cls . b2 = Book . objects . create ( <EOL> title = '<STR_LIT>' , author = cls . a1 , paperback = True , <EOL> opening_line = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) , <EOL> ) <EOL> cls . b3 = Book . objects . create ( <EOL> title = '<STR_LIT>' , author = cls . a1 , paperback = False , <EOL> opening_line = '<STR_LIT>' , <EOL> ) <EOL> cls . b4 = Book . objects . create ( <EOL> title = '<STR_LIT>' , author = cls . a3 , paperback = True , <EOL> opening_line = '<STR_LIT>' , <EOL> ) <EOL> cls . c1 = Coffee . objects . create ( brand = '<STR_LIT>' ) <EOL> cls . c2 = Coffee . objects . create ( brand = '<STR_LIT>' ) <EOL> cls . r1 = Reviewer . objects . create ( ) <EOL> cls . r2 = Reviewer . objects . create ( ) <EOL> cls . r1 . reviewed . add ( cls . b2 , cls . b3 , cls . b4 ) <EOL> def assertSuccessfulRawQuery ( self , model , query , expected_results , <EOL> expected_annotations = ( ) , params = [ ] , translations = None ) : <EOL> """<STR_LIT>""" <EOL> results = list ( model . objects . raw ( query , params = params , translations = translations ) ) <EOL> self . assertProcessed ( model , results , expected_results , expected_annotations ) <EOL> self . assertAnnotations ( results , expected_annotations ) <EOL> def assertProcessed ( self , model , results , orig , expected_annotations = ( ) ) : <EOL> """<STR_LIT>""" <EOL> self . assertEqual ( len ( results ) , len ( orig ) ) <EOL> for index , item in enumerate ( results ) : <EOL> orig_item = orig [ index ] <EOL> for annotation in expected_annotations : <EOL> setattr ( orig_item , * annotation ) <EOL> for field in model . _meta . fields : <EOL> self . assertEqual ( <EOL> getattr ( item , field . attname ) , <EOL> getattr ( orig_item , field . attname ) <EOL> ) <EOL> self . assertEqual ( <EOL> type ( getattr ( item , field . attname ) ) , <EOL> type ( getattr ( orig_item , field . attname ) ) <EOL> ) <EOL> def assertNoAnnotations ( self , results ) : <EOL> """<STR_LIT>""" <EOL> self . assertAnnotations ( results , ( ) ) <EOL> def assertAnnotations ( self , results , expected_annotations ) : <EOL> """<STR_LIT>""" <EOL> if expected_annotations : <EOL> for index , result in enumerate ( results ) : <EOL> annotation , value = expected_annotations [ index ] <EOL> self . assertTrue ( hasattr ( result , annotation ) ) <EOL> self . assertEqual ( getattr ( result , annotation ) , value ) <EOL> def test_simple_raw_query ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> authors = Author . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Author , query , authors ) <EOL> def test_raw_query_lazy ( self ) : <EOL> """<STR_LIT>""" <EOL> q = Author . objects . raw ( '<STR_LIT>' ) <EOL> self . assertIsNone ( q . query . cursor ) <EOL> list ( q ) <EOL> self . assertIsNotNone ( q . query . cursor ) <EOL> def test_FK_raw_query ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> books = Book . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Book , query , books ) <EOL> def test_db_column_handler ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> coffees = Coffee . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Coffee , query , coffees ) <EOL> def test_order_handler ( self ) : <EOL> """<STR_LIT>""" <EOL> selects = ( <EOL> ( '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' ) , <EOL> ) <EOL> for select in selects : <EOL> query = "<STR_LIT>" % select <EOL> authors = Author . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Author , query , authors ) <EOL> def test_translations ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> translations = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> authors = Author . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Author , query , authors , translations = translations ) <EOL> def test_params ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> author = Author . objects . all ( ) [ <NUM_LIT:2> ] <EOL> params = [ author . first_name ] <EOL> qset = Author . objects . raw ( query , params = params ) <EOL> results = list ( qset ) <EOL> self . assertProcessed ( Author , results , [ author ] ) <EOL> self . assertNoAnnotations ( results ) <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:1> ) <EOL> self . assertIsInstance ( repr ( qset ) , str ) <EOL> @ skipUnlessDBFeature ( '<STR_LIT>' ) <EOL> def test_pyformat_params ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> author = Author . objects . all ( ) [ <NUM_LIT:2> ] <EOL> params = { '<STR_LIT>' : author . first_name } <EOL> qset = Author . objects . raw ( query , params = params ) <EOL> results = list ( qset ) <EOL> self . assertProcessed ( Author , results , [ author ] ) <EOL> self . assertNoAnnotations ( results ) <EOL> self . assertEqual ( len ( results ) , <NUM_LIT:1> ) <EOL> self . assertIsInstance ( repr ( qset ) , str ) <EOL> def test_query_representation ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> qset = Author . objects . raw ( query , { '<STR_LIT>' : '<STR_LIT:foo>' } ) <EOL> self . assertEqual ( repr ( qset ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( qset . query ) , "<STR_LIT>" ) <EOL> query = "<STR_LIT>" <EOL> qset = Author . objects . raw ( query , { '<STR_LIT:foo>' } ) <EOL> self . assertEqual ( repr ( qset ) , "<STR_LIT>" ) <EOL> self . assertEqual ( repr ( qset . query ) , "<STR_LIT>" ) <EOL> def test_many_to_many ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> reviewers = Reviewer . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Reviewer , query , reviewers ) <EOL> def test_extra_conversions ( self ) : <EOL> """<STR_LIT>""" <EOL> query = "<STR_LIT>" <EOL> translations = { '<STR_LIT>' : '<STR_LIT>' } <EOL> authors = Author . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Author , query , authors , translations = translations ) <EOL> def test_missing_fields ( self ) : <EOL> query = "<STR_LIT>" <EOL> for author in Author . objects . raw ( query ) : <EOL> self . assertNotEqual ( author . first_name , None ) <EOL> self . assertNotEqual ( author . last_name , None ) <EOL> def test_missing_fields_without_PK ( self ) : <EOL> query = "<STR_LIT>" <EOL> try : <EOL> list ( Author . objects . raw ( query ) ) <EOL> self . fail ( '<STR_LIT>' ) <EOL> except InvalidQuery : <EOL> pass <EOL> def test_annotations ( self ) : <EOL> query = ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> expected_annotations = ( <EOL> ( '<STR_LIT>' , <NUM_LIT:3> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT:0> ) , <EOL> ) <EOL> authors = Author . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Author , query , authors , expected_annotations ) <EOL> def test_white_space_query ( self ) : <EOL> query = "<STR_LIT>" <EOL> authors = Author . objects . all ( ) <EOL> self . assertSuccessfulRawQuery ( Author , query , authors ) <EOL> def test_multiple_iterations ( self ) : <EOL> query = "<STR_LIT>" <EOL> normal_authors = Author . objects . all ( ) <EOL> raw_authors = Author . objects . raw ( query ) <EOL> first_iterations = <NUM_LIT:0> <EOL> for index , raw_author in enumerate ( raw_authors ) : <EOL> self . assertEqual ( normal_authors [ index ] , raw_author ) <EOL> first_iterations += <NUM_LIT:1> <EOL> second_iterations = <NUM_LIT:0> <EOL> for index , raw_author in enumerate ( raw_authors ) : <EOL> self . assertEqual ( normal_authors [ index ] , raw_author ) <EOL> second_iterations += <NUM_LIT:1> <EOL> self . assertEqual ( first_iterations , second_iterations ) <EOL> def test_get_item ( self ) : <EOL> query = "<STR_LIT>" <EOL> third_author = Author . objects . raw ( query ) [ <NUM_LIT:2> ] <EOL> self . assertEqual ( third_author . first_name , '<STR_LIT>' ) <EOL> first_two = Author . objects . raw ( query ) [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> self . assertEqual ( len ( first_two ) , <NUM_LIT:2> ) <EOL> with self . assertRaises ( TypeError ) : <EOL> Author . objects . raw ( query ) [ '<STR_LIT:test>' ] <EOL> def test_inheritance ( self ) : <EOL> f = FriendlyAuthor . objects . create ( first_name = "<STR_LIT>" , last_name = "<STR_LIT>" , dob = date ( <NUM_LIT> , <NUM_LIT:10> , <NUM_LIT> ) ) <EOL> query = "<STR_LIT>" <EOL> self . assertEqual ( <EOL> [ o . pk for o in FriendlyAuthor . objects . raw ( query ) ] , [ f . pk ] <EOL> ) <EOL> def test_query_count ( self ) : <EOL> self . assertNumQueries ( <NUM_LIT:1> , list , Author . objects . raw ( "<STR_LIT>" ) ) <EOL> def test_subquery_in_raw_sql ( self ) : <EOL> try : <EOL> list ( Book . objects . raw ( '<STR_LIT>' ) ) <EOL> except InvalidQuery : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_db_column_name_is_used_in_raw_query ( self ) : <EOL> """<STR_LIT>""" <EOL> b = BookFkAsPk . objects . create ( book = self . b1 ) <EOL> self . assertEqual ( list ( BookFkAsPk . objects . raw ( '<STR_LIT>' ) ) , [ b ] ) <EOL> def test_decimal_parameter ( self ) : <EOL> c = Coffee . objects . create ( brand = '<STR_LIT>' , price = <NUM_LIT> ) <EOL> qs = Coffee . objects . raw ( "<STR_LIT>" , params = [ Decimal ( <NUM_LIT:20> ) ] ) <EOL> self . assertEqual ( list ( qs ) , [ c ] ) </s>
