<s> from django . test import TestCase <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . contrib . auth . models import Group <EOL> from actstream . models import Action <EOL> from actstream . compat import get_user_model <EOL> from actstream . tests . base import LTE <EOL> class GFKManagerTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> User = get_user_model ( ) <EOL> self . user_ct = ContentType . objects . get_for_model ( User ) <EOL> self . group_ct = ContentType . objects . get_for_model ( Group ) <EOL> self . group , _ = Group . objects . get_or_create ( name = '<STR_LIT>' ) <EOL> self . user1 , _ = User . objects . get_or_create ( username = '<STR_LIT>' ) <EOL> self . user2 , _ = User . objects . get_or_create ( username = '<STR_LIT>' ) <EOL> self . user3 , _ = User . objects . get_or_create ( username = '<STR_LIT>' ) <EOL> self . user4 , _ = User . objects . get_or_create ( username = '<STR_LIT>' ) <EOL> Action . objects . get_or_create ( <EOL> actor_content_type = self . user_ct , <EOL> actor_object_id = self . user1 . id , <EOL> verb = '<STR_LIT>' , <EOL> target_content_type = self . user_ct , <EOL> target_object_id = self . user2 . id <EOL> ) <EOL> Action . objects . get_or_create ( <EOL> actor_content_type = self . user_ct , <EOL> actor_object_id = self . user1 . id , <EOL> verb = '<STR_LIT>' , <EOL> target_content_type = self . user_ct , <EOL> target_object_id = self . user3 . id <EOL> ) <EOL> Action . objects . get_or_create ( <EOL> actor_content_type = self . user_ct , <EOL> actor_object_id = self . user1 . id , <EOL> verb = '<STR_LIT>' , <EOL> target_content_type = self . user_ct , <EOL> target_object_id = self . user4 . id <EOL> ) <EOL> Action . objects . get_or_create ( <EOL> actor_content_type = self . user_ct , <EOL> actor_object_id = self . user1 . id , <EOL> verb = '<STR_LIT>' , <EOL> target_content_type = self . group_ct , <EOL> target_object_id = self . group . id <EOL> ) <EOL> def test_fetch_generic_relations ( self ) : <EOL> _actions = Action . objects . filter ( actor_content_type = self . user_ct , <EOL> actor_object_id = self . user1 . id ) <EOL> actions = lambda : _actions . _clone ( ) <EOL> num_content_types = len ( set ( actions ( ) . values_list ( <EOL> '<STR_LIT>' , flat = True ) ) ) <EOL> n = actions ( ) . count ( ) <EOL> full , generic = actions ( ) , actions ( ) . fetch_generic_relations ( '<STR_LIT:target>' ) <EOL> self . assertNumQueries ( LTE ( n + <NUM_LIT:1> ) , <EOL> lambda : [ a . target for a in full ] ) <EOL> self . assertNumQueries ( LTE ( num_content_types + <NUM_LIT:2> ) , <EOL> lambda : [ a . target for a in generic ] ) <EOL> action_targets = [ ( a . id , a . target ) for a in actions ( ) ] <EOL> action_targets_fetch_generic = [ <EOL> ( a . id , a . target ) <EOL> for a in actions ( ) . fetch_generic_relations ( '<STR_LIT:target>' ) ] <EOL> self . assertEqual ( action_targets , action_targets_fetch_generic ) <EOL> num_content_types = len ( set ( sum ( actions ( ) . values_list ( <EOL> '<STR_LIT>' , '<STR_LIT>' ) , ( ) ) ) ) <EOL> full , generic = actions ( ) , actions ( ) . fetch_generic_relations ( ) <EOL> self . assertNumQueries ( LTE ( <NUM_LIT:2> * n + <NUM_LIT:1> ) , <EOL> lambda : [ ( a . actor , a . target ) for a in full ] ) <EOL> self . assertNumQueries ( LTE ( num_content_types + <NUM_LIT:2> ) , <EOL> lambda : [ ( a . actor , a . target ) for a in generic ] ) <EOL> action_actor_targets = [ ( a . id , a . actor , a . target ) for a in actions ( ) ] <EOL> action_actor_targets_fetch_generic_all = [ <EOL> ( a . id , a . actor , a . target ) <EOL> for a in actions ( ) . fetch_generic_relations ( ) ] <EOL> self . assertEqual ( action_actor_targets , <EOL> action_actor_targets_fetch_generic_all ) <EOL> generic = lambda : actions ( ) . fetch_generic_relations ( '<STR_LIT:target>' ) <EOL> self . assertNumQueries ( LTE ( n + num_content_types + <NUM_LIT:2> ) , lambda : [ <EOL> ( a . actor , a . target ) for a in generic ( ) ] ) <EOL> action_actor_targets_fetch_generic_target = [ <EOL> ( a . id , a . actor , a . target ) for a in generic ( ) ] <EOL> self . assertEqual ( action_actor_targets , <EOL> action_actor_targets_fetch_generic_target ) </s>
