<s> from django . db import models <EOL> from djangae . contrib import sleuth <EOL> from djangae . test import TestCase , inconsistent_db <EOL> from djangae . utils import get_next_available_port <EOL> from djangae . db . consistency import ensure_instance_consistent , ensure_instances_consistent <EOL> class AvailablePortTests ( TestCase ) : <EOL> def test_get_next_available_port ( self ) : <EOL> url = "<STR_LIT:127.0.0.1>" <EOL> port = <NUM_LIT> <EOL> self . assertEquals ( <NUM_LIT> , get_next_available_port ( url , port ) ) <EOL> with sleuth . switch ( "<STR_LIT>" , <EOL> lambda * args , ** kwargs : False if args [ <NUM_LIT:1> ] < <NUM_LIT> else True ) : <EOL> self . assertEquals ( <NUM_LIT> , get_next_available_port ( url , port ) ) <EOL> class EnsureCreatedModel ( models . Model ) : <EOL> field1 = models . IntegerField ( ) <EOL> class Meta : <EOL> app_label = "<STR_LIT>" <EOL> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" . format ( self . pk , self . field1 ) <EOL> class EnsureCreatedTests ( TestCase ) : <EOL> def test_basic_usage ( self ) : <EOL> for i in xrange ( <NUM_LIT:5> ) : <EOL> EnsureCreatedModel . objects . create ( <EOL> pk = i + <NUM_LIT:1> , <EOL> field1 = i <EOL> ) <EOL> with inconsistent_db ( ) : <EOL> new_instance = EnsureCreatedModel . objects . create ( <EOL> pk = <NUM_LIT:7> , <EOL> field1 = <NUM_LIT:3> <EOL> ) <EOL> qs = EnsureCreatedModel . objects . all ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( qs ) ) <EOL> self . assertEqual ( <NUM_LIT:6> , len ( ensure_instance_consistent ( qs , new_instance . pk ) ) ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( ensure_instance_consistent ( qs [ : <NUM_LIT:5> ] , new_instance . pk ) ) ) <EOL> self . assertEqual ( <NUM_LIT:3> , len ( ensure_instance_consistent ( qs [ <NUM_LIT:2> : <NUM_LIT:5> ] , new_instance . pk ) ) ) <EOL> evaled = [ x for x in ensure_instance_consistent ( qs . order_by ( "<STR_LIT>" ) , new_instance . pk ) ] <EOL> self . assertEqual ( new_instance , evaled [ <NUM_LIT:4> ] ) <EOL> evaled = [ x for x in ensure_instance_consistent ( qs . order_by ( "<STR_LIT>" ) , new_instance . pk ) ] <EOL> self . assertEqual ( new_instance , evaled [ <NUM_LIT:1> ] , evaled ) <EOL> evaled = [ x for x in ensure_instance_consistent ( qs . order_by ( "<STR_LIT>" ) , new_instance . pk ) [ : <NUM_LIT:2> ] ] <EOL> self . assertEqual ( new_instance , evaled [ <NUM_LIT:1> ] , evaled ) <EOL> another_instance = EnsureCreatedModel . objects . create ( <EOL> pk = <NUM_LIT:8> , <EOL> field1 = <NUM_LIT:3> <EOL> ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( qs ) ) <EOL> self . assertEqual ( <NUM_LIT:7> , len ( ensure_instances_consistent ( qs , [ new_instance . pk , another_instance . pk ] ) ) ) <EOL> instance_id = another_instance . pk <EOL> another_instance . delete ( ) <EOL> self . assertEqual ( <NUM_LIT:6> , len ( ensure_instances_consistent ( qs , [ new_instance . pk , instance_id ] ) ) ) <EOL> self . assertTrue ( EnsureCreatedModel . objects . filter ( pk = <NUM_LIT:7> ) [ : <NUM_LIT:1> ] ) <EOL> self . assertEqual ( <NUM_LIT:6> , len ( ensure_instance_consistent ( qs , new_instance . pk ) ) ) <EOL> new_instance . delete ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( ensure_instance_consistent ( qs , <NUM_LIT:7> ) ) ) <EOL> new_instance = EnsureCreatedModel . objects . create ( pk = <NUM_LIT:8> , field1 = <NUM_LIT:8> ) <EOL> self . assertEqual ( <NUM_LIT:1> , list ( ensure_instance_consistent ( qs , <NUM_LIT:8> ) ) . count ( new_instance ) ) <EOL> def test_add_many_instances ( self ) : <EOL> for i in xrange ( <NUM_LIT:5> ) : <EOL> EnsureCreatedModel . objects . create ( <EOL> pk = i + <NUM_LIT:1> , <EOL> field1 = i + <NUM_LIT:5> <EOL> ) <EOL> with inconsistent_db ( ) : <EOL> new_instances = [ ] <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> instance = EnsureCreatedModel . objects . create ( <EOL> pk = i + <NUM_LIT:7> , <EOL> field1 = i <EOL> ) <EOL> new_instances . append ( instance ) <EOL> new_instance_pks = [ i . pk for i in new_instances ] <EOL> qs = EnsureCreatedModel . objects . all ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( qs ) ) <EOL> self . assertEqual ( <NUM_LIT:8> , len ( ensure_instances_consistent ( qs , new_instance_pks ) ) ) <EOL> evaled = [ x for x in ensure_instances_consistent ( qs . order_by ( "<STR_LIT>" ) , new_instance_pks ) ] <EOL> self . assertEqual ( new_instances [ <NUM_LIT:0> ] , evaled [ <NUM_LIT:0> ] ) <EOL> self . assertEqual ( new_instances [ <NUM_LIT:1> ] , evaled [ <NUM_LIT:1> ] ) <EOL> self . assertEqual ( new_instances [ <NUM_LIT:2> ] , evaled [ <NUM_LIT:2> ] ) <EOL> self . assertTrue ( EnsureCreatedModel . objects . filter ( pk = <NUM_LIT:7> ) [ : <NUM_LIT:1> ] ) <EOL> self . assertTrue ( EnsureCreatedModel . objects . filter ( pk = <NUM_LIT:8> ) [ : <NUM_LIT:1> ] ) <EOL> self . assertEqual ( <NUM_LIT:8> , len ( ensure_instances_consistent ( qs , new_instance_pks ) ) ) <EOL> for instance in new_instances : <EOL> instance . delete ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( ensure_instances_consistent ( qs , new_instance_pks ) ) ) <EOL> def test_delete_many_instances ( self ) : <EOL> for i in xrange ( <NUM_LIT:5> ) : <EOL> EnsureCreatedModel . objects . create ( <EOL> pk = i + <NUM_LIT:1> , <EOL> field1 = i + <NUM_LIT:5> <EOL> ) <EOL> instances_to_delete = [ ] <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> instance = EnsureCreatedModel . objects . create ( <EOL> pk = i + <NUM_LIT:7> , <EOL> field1 = i + <NUM_LIT:1> <EOL> ) <EOL> instances_to_delete . append ( instance ) <EOL> instances_to_delete_pks = [ i . pk for i in instances_to_delete ] <EOL> qs = EnsureCreatedModel . objects . all ( ) <EOL> self . assertEqual ( <NUM_LIT:8> , len ( ensure_instances_consistent ( qs , instances_to_delete_pks ) ) ) <EOL> with inconsistent_db ( ) : <EOL> for instance in instances_to_delete : <EOL> instance . delete ( ) <EOL> self . assertEqual ( <NUM_LIT:5> , len ( ensure_instances_consistent ( qs , instances_to_delete_pks ) ) ) </s>
