<s> from django . contrib . contenttypes . models import ContentType <EOL> from django . test import TestCase <EOL> from tagging . models import Tag , TaggedItem , Synonym <EOL> from tagging . utils import merge , get_tag <EOL> from tagging . tests . synonym_tests import TestItem , TestItemWithCallback <EOL> def with_tag ( tag , cls = TestItem ) : <EOL> return TaggedItem . objects . get_by_model ( cls , [ tag ] ) <EOL> def model_to_ctype ( model ) : <EOL> return ContentType . _default_manager . get_for_model ( model ) <EOL> class Merge ( TestCase ) : <EOL> def testMergeTagsDeletesSecondTagIfNoMoreTaggedItems ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , Tag . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , TaggedItem . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , Synonym . objects . count ( ) ) <EOL> first = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> first . save ( ) <EOL> second = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> second . save ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> ctype = model_to_ctype ( TestItem ) <EOL> merge ( '<STR_LIT>' , '<STR_LIT>' , ctype ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( <NUM_LIT:2> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> self . assertRaises ( Tag . DoesNotExist , Tag . objects . get , name = '<STR_LIT>' ) <EOL> def testMergeTagsNotDeletesSecondTag ( self ) : <EOL> self . assertEqual ( <NUM_LIT:0> , Tag . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , TaggedItem . objects . count ( ) ) <EOL> self . assertEqual ( <NUM_LIT:0> , Synonym . objects . count ( ) ) <EOL> first = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> first . save ( ) <EOL> second = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> second . save ( ) <EOL> third = TestItemWithCallback ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> third . save ( ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' , TestItemWithCallback ) ) ) <EOL> ctype = model_to_ctype ( TestItem ) <EOL> merge ( '<STR_LIT>' , '<STR_LIT>' , ctype ) <EOL> self . assertEqual ( <NUM_LIT:0> , len ( with_tag ( '<STR_LIT>' ) ) ) <EOL> self . assertEqual ( <NUM_LIT:1> , len ( with_tag ( '<STR_LIT>' , TestItemWithCallback ) ) ) <EOL> self . assert_ ( Tag . objects . get ( name = '<STR_LIT>' ) ) <EOL> def testMergeTagsCreatesSynonyms ( self ) : <EOL> first = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> first . save ( ) <EOL> second = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> second . save ( ) <EOL> self . assertEqual ( [ ] , [ s . name for s in get_tag ( '<STR_LIT>' ) . synonyms . all ( ) ] ) <EOL> ctype = model_to_ctype ( TestItem ) <EOL> merge ( '<STR_LIT>' , '<STR_LIT>' , ctype ) <EOL> self . assertEqual ( [ '<STR_LIT>' ] , [ s . name for s in get_tag ( '<STR_LIT>' ) . synonyms . all ( ) ] ) <EOL> def testMergeTagsWhenSynonymAlreadyExists ( self ) : <EOL> first = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> first . save ( ) <EOL> second = TestItem ( title = '<STR_LIT>' , tags = '<STR_LIT>' ) <EOL> second . save ( ) <EOL> blah = get_tag ( '<STR_LIT>' ) <EOL> blah . synonyms . create ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( [ ] , [ s . name for s in get_tag ( '<STR_LIT>' ) . synonyms . all ( ) ] ) <EOL> ctype = model_to_ctype ( TestItem ) <EOL> merge ( '<STR_LIT>' , '<STR_LIT>' , ctype ) <EOL> self . assertEqual ( [ ] , [ s . name for s in get_tag ( '<STR_LIT>' ) . synonyms . all ( ) ] ) <EOL> self . assertEqual ( [ '<STR_LIT>' ] , [ s . name for s in get_tag ( '<STR_LIT>' ) . synonyms . all ( ) ] ) </s>
