<s> from django . http import Http404 <EOL> from django . test import TestCase <EOL> from source . code . models import Code <EOL> from source . tags . models import TechnologyTag , ConceptTag <EOL> from source . tags . utils import ( get_validated_tag_list , <EOL> get_tag_filtered_queryset , filter_queryset_by_tags ) <EOL> class BaseTestCase ( TestCase ) : <EOL> def assertQuerysetEqual ( self , qs1 , qs2 ) : <EOL> pk = lambda o : o . pk <EOL> return self . assertEqual ( <EOL> list ( sorted ( qs1 , key = pk ) ) , <EOL> list ( sorted ( qs2 , key = pk ) ) <EOL> ) <EOL> class TestCodeTagAdd ( BaseTestCase ) : <EOL> code_model = Code <EOL> tech_tag_model = TechnologyTag <EOL> concept_tag_model = ConceptTag <EOL> def setUp ( self ) : <EOL> self . tech_tag = self . tech_tag_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . concept_tag = self . concept_tag_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_one = self . code_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_two = self . code_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_three = self . code_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> def test_code_entries ( self ) : <EOL> self . assertEqual ( self . code_one . title , "<STR_LIT>" ) <EOL> self . assertEqual ( self . code_two . title , "<STR_LIT>" ) <EOL> self . assertEqual ( self . code_three . title , "<STR_LIT>" ) <EOL> def test_add_tags ( self ) : <EOL> self . assertEqual ( list ( self . code_one . technology_tags . all ( ) ) , [ ] ) <EOL> self . assertEqual ( list ( self . code_one . concept_tags . all ( ) ) , [ ] ) <EOL> self . code_one . technology_tags . add ( "<STR_LIT>" ) <EOL> self . code_one . concept_tags . add ( "<STR_LIT>" ) <EOL> self . assertEqual ( list ( self . code_one . technology_tags . all ( ) ) , [ self . tech_tag ] ) <EOL> self . assertEqual ( list ( self . code_one . concept_tags . all ( ) ) , [ self . concept_tag ] ) <EOL> self . assertEqual ( list ( self . code_two . technology_tags . all ( ) ) , [ ] ) <EOL> self . assertEqual ( list ( self . code_two . concept_tags . all ( ) ) , [ ] ) <EOL> self . code_two . concept_tags . add ( "<STR_LIT>" ) <EOL> self . assertEqual ( list ( self . code_two . technology_tags . all ( ) ) , [ ] ) <EOL> self . assertEqual ( list ( self . code_two . concept_tags . all ( ) ) , [ self . concept_tag ] ) <EOL> self . assertEqual ( list ( self . code_three . technology_tags . all ( ) ) , [ ] ) <EOL> self . assertEqual ( list ( self . code_three . concept_tags . all ( ) ) , [ ] ) <EOL> self . code_three . technology_tags . add ( "<STR_LIT>" ) <EOL> self . assertEqual ( list ( self . code_three . technology_tags . all ( ) ) , [ self . tech_tag ] ) <EOL> self . assertEqual ( list ( self . code_three . concept_tags . all ( ) ) , [ ] ) <EOL> class TestCodeTagQueries ( BaseTestCase ) : <EOL> code_model = Code <EOL> tech_tag_model = TechnologyTag <EOL> concept_tag_model = ConceptTag <EOL> def setUp ( self ) : <EOL> self . tech_tag = self . tech_tag_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . concept_tag = self . concept_tag_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_one = self . code_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_one . technology_tags . add ( "<STR_LIT>" ) <EOL> self . code_one . concept_tags . add ( "<STR_LIT>" ) <EOL> self . code_two = self . code_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_two . concept_tags . add ( "<STR_LIT>" ) <EOL> self . code_three = self . code_model . objects . create ( name = "<STR_LIT>" , slug = "<STR_LIT>" ) <EOL> self . code_three . technology_tags . add ( "<STR_LIT>" ) <EOL> self . code_one . tags . add ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . code_two . tags . add ( "<STR_LIT>" ) <EOL> self . code_three . tags . add ( "<STR_LIT>" ) <EOL> def test_tags_added_properly ( self ) : <EOL> self . assertEqual ( list ( self . code_one . technology_tags . all ( ) ) , [ self . tech_tag ] ) <EOL> self . assertEqual ( list ( self . code_one . concept_tags . all ( ) ) , [ self . concept_tag ] ) <EOL> self . assertEqual ( list ( self . code_two . concept_tags . all ( ) ) , [ self . concept_tag ] ) <EOL> self . assertEqual ( list ( self . code_three . technology_tags . all ( ) ) , [ self . tech_tag ] ) <EOL> def test_get_validated_tag_list ( self ) : <EOL> tag_slug_list_one = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> tags_one = get_validated_tag_list ( tag_slug_list_one ) <EOL> self . assertEqual ( tags_one , [ self . tech_tag , self . concept_tag ] ) <EOL> tag_slug_list_two = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> self . assertRaises ( Http404 , lambda : get_validated_tag_list ( tag_slug_list_two ) ) <EOL> def test_get_tag_filtered_queryset ( self ) : <EOL> code_objects = self . code_model . objects . all ( ) <EOL> tag_slug_list_one = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> queryset_one = get_tag_filtered_queryset ( code_objects , tag_slug_list_one ) <EOL> self . assertQuerysetEqual ( queryset_one , [ self . code_one ] ) <EOL> tag_slug_list_two = [ "<STR_LIT>" ] <EOL> queryset_two = get_tag_filtered_queryset ( code_objects , tag_slug_list_two ) <EOL> self . assertQuerysetEqual ( queryset_two , [ self . code_one , self . code_two ] ) <EOL> tag_slug_list_three = [ "<STR_LIT>" ] <EOL> queryset_three = get_tag_filtered_queryset ( code_objects , tag_slug_list_three ) <EOL> self . assertQuerysetEqual ( queryset_three , [ self . code_one , self . code_three ] ) <EOL> def test_filter_queryset_by_tags ( self ) : <EOL> code_objects = self . code_model . objects . all ( ) <EOL> tag_slugs_one = "<STR_LIT>" <EOL> queryset_one , tags_one = filter_queryset_by_tags ( code_objects , tag_slugs_one , tags = [ ] ) <EOL> self . assertQuerysetEqual ( queryset_one , [ self . code_one ] ) <EOL> self . assertEqual ( tags_one , [ self . tech_tag , self . concept_tag ] ) <EOL> tag_slugs_two = "<STR_LIT>" <EOL> queryset_two , tags_two = filter_queryset_by_tags ( code_objects , tag_slugs_two , tags = [ ] ) <EOL> self . assertQuerysetEqual ( queryset_two , [ self . code_one , self . code_two ] ) <EOL> self . assertEqual ( tags_two , [ self . concept_tag ] ) <EOL> tag_slugs_three = "<STR_LIT>" <EOL> queryset_three , tags_three = filter_queryset_by_tags ( code_objects , tag_slugs_three , tags = [ ] ) <EOL> self . assertQuerysetEqual ( queryset_three , [ self . code_one , self . code_three ] ) <EOL> self . assertEqual ( tags_three , [ self . tech_tag ] ) <EOL> tag_slugs_four = "<STR_LIT>" <EOL> self . assertRaises ( Http404 , lambda : filter_queryset_by_tags ( <EOL> code_objects , tag_slugs_four , tags = [ ] ) <EOL> ) </s>
