<s> from __future__ import with_statement , absolute_import <EOL> from operator import attrgetter <EOL> from django . db import connection <EOL> from django . test import TestCase , skipIfDBFeature <EOL> from django . test . utils import override_settings <EOL> from . models import Country , Restaurant , Pizzeria , State , TwoFields <EOL> class BulkCreateTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . data = [ <EOL> Country ( name = "<STR_LIT>" , iso_two_letter = "<STR_LIT>" ) , <EOL> Country ( name = "<STR_LIT>" , iso_two_letter = "<STR_LIT>" ) , <EOL> Country ( name = "<STR_LIT>" , iso_two_letter = "<STR_LIT>" ) , <EOL> Country ( name = "<STR_LIT>" , iso_two_letter = "<STR_LIT>" ) <EOL> ] <EOL> def test_simple ( self ) : <EOL> created = Country . objects . bulk_create ( self . data ) <EOL> self . assertEqual ( len ( created ) , <NUM_LIT:4> ) <EOL> self . assertQuerysetEqual ( Country . objects . order_by ( "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" <EOL> ] , attrgetter ( "<STR_LIT:name>" ) ) <EOL> created = Country . objects . bulk_create ( [ ] ) <EOL> self . assertEqual ( created , [ ] ) <EOL> self . assertEqual ( Country . objects . count ( ) , <NUM_LIT:4> ) <EOL> def test_efficiency ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> Country . objects . bulk_create ( self . data ) <EOL> def test_inheritance ( self ) : <EOL> Restaurant . objects . bulk_create ( [ <EOL> Restaurant ( name = "<STR_LIT>" ) <EOL> ] ) <EOL> self . assertQuerysetEqual ( Restaurant . objects . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> ] , attrgetter ( "<STR_LIT:name>" ) ) <EOL> with self . assertRaises ( ValueError ) : <EOL> Pizzeria . objects . bulk_create ( [ <EOL> Pizzeria ( name = "<STR_LIT>" ) <EOL> ] ) <EOL> self . assertQuerysetEqual ( Pizzeria . objects . all ( ) , [ ] ) <EOL> self . assertQuerysetEqual ( Restaurant . objects . all ( ) , [ <EOL> "<STR_LIT>" , <EOL> ] , attrgetter ( "<STR_LIT:name>" ) ) <EOL> def test_non_auto_increment_pk ( self ) : <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> State . objects . bulk_create ( [ <EOL> State ( two_letter_code = s ) <EOL> for s in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> ] ) <EOL> self . assertQuerysetEqual ( State . objects . order_by ( "<STR_LIT>" ) , [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> ] , attrgetter ( "<STR_LIT>" ) ) <EOL> def test_batch_same_vals ( self ) : <EOL> Restaurant . objects . bulk_create ( [ <EOL> Restaurant ( name = '<STR_LIT:foo>' ) for i in range ( <NUM_LIT:0> , <NUM_LIT:2> ) <EOL> ] ) <EOL> self . assertEqual ( Restaurant . objects . count ( ) , <NUM_LIT:2> ) <EOL> def test_large_batch ( self ) : <EOL> with override_settings ( DEBUG = True ) : <EOL> connection . queries = [ ] <EOL> TwoFields . objects . bulk_create ( [ <EOL> TwoFields ( f1 = i , f2 = i + <NUM_LIT:1> ) for i in range ( <NUM_LIT:0> , <NUM_LIT> ) <EOL> ] ) <EOL> self . assertTrue ( len ( connection . queries ) < <NUM_LIT:10> ) <EOL> self . assertEqual ( TwoFields . objects . count ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( <EOL> TwoFields . objects . filter ( f1__gte = <NUM_LIT> , f1__lte = <NUM_LIT> ) . count ( ) , <EOL> <NUM_LIT> ) <EOL> self . assertEqual ( TwoFields . objects . filter ( f2__gte = <NUM_LIT> ) . count ( ) , <NUM_LIT> ) <EOL> def test_large_batch_mixed ( self ) : <EOL> """<STR_LIT>""" <EOL> with override_settings ( DEBUG = True ) : <EOL> connection . queries = [ ] <EOL> TwoFields . objects . bulk_create ( [ <EOL> TwoFields ( id = i if i % <NUM_LIT:2> == <NUM_LIT:0> else None , f1 = i , f2 = i + <NUM_LIT:1> ) <EOL> for i in range ( <NUM_LIT> , <NUM_LIT> ) ] ) <EOL> self . assertTrue ( len ( connection . queries ) < <NUM_LIT:10> ) <EOL> self . assertEqual ( TwoFields . objects . count ( ) , <NUM_LIT:1000> ) <EOL> id_range = range ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT:2> ) <EOL> self . assertEqual ( TwoFields . objects . filter ( id__in = id_range ) . count ( ) , <NUM_LIT> ) <EOL> self . assertEqual ( TwoFields . objects . exclude ( id__in = id_range ) . count ( ) , <NUM_LIT> ) <EOL> def test_explicit_batch_size ( self ) : <EOL> objs = [ TwoFields ( f1 = i , f2 = i ) for i in range ( <NUM_LIT:0> , <NUM_LIT:100> ) ] <EOL> with self . assertNumQueries ( <NUM_LIT:2> ) : <EOL> TwoFields . objects . bulk_create ( objs , <NUM_LIT:50> ) <EOL> TwoFields . objects . all ( ) . delete ( ) <EOL> with self . assertNumQueries ( <NUM_LIT:1> ) : <EOL> TwoFields . objects . bulk_create ( objs , len ( objs ) ) </s>
