<s> import random <EOL> import string <EOL> from mongoengine import ValidationError <EOL> from st2common . models . db import stormbase <EOL> from st2tests import DbTestCase <EOL> class TaggedModel ( stormbase . StormFoundationDB , stormbase . TagsMixin ) : <EOL> pass <EOL> class TestTags ( DbTestCase ) : <EOL> def test_simple_count ( self ) : <EOL> instance = TaggedModel ( ) <EOL> instance . tags = [ stormbase . TagField ( name = '<STR_LIT>' , value = '<STR_LIT>' ) , <EOL> stormbase . TagField ( name = '<STR_LIT>' , value = '<STR_LIT>' ) ] <EOL> saved = instance . save ( ) <EOL> retrieved = TaggedModel . objects ( id = instance . id ) . first ( ) <EOL> self . assertEquals ( len ( saved . tags ) , len ( retrieved . tags ) , '<STR_LIT>' ) <EOL> def test_simple_value ( self ) : <EOL> instance = TaggedModel ( ) <EOL> instance . tags = [ stormbase . TagField ( name = '<STR_LIT>' , value = '<STR_LIT>' ) ] <EOL> saved = instance . save ( ) <EOL> retrieved = TaggedModel . objects ( id = instance . id ) . first ( ) <EOL> self . assertEquals ( len ( saved . tags ) , len ( retrieved . tags ) , '<STR_LIT>' ) <EOL> saved_tag = saved . tags [ <NUM_LIT:0> ] <EOL> retrieved_tag = retrieved . tags [ <NUM_LIT:0> ] <EOL> self . assertEquals ( saved_tag . name , retrieved_tag . name , '<STR_LIT>' ) <EOL> self . assertEquals ( saved_tag . value , retrieved_tag . value , '<STR_LIT>' ) <EOL> def test_tag_max_size_restriction ( self ) : <EOL> instance = TaggedModel ( ) <EOL> instance . tags = [ stormbase . TagField ( name = self . _gen_random_string ( ) , <EOL> value = self . _gen_random_string ( ) ) ] <EOL> saved = instance . save ( ) <EOL> retrieved = TaggedModel . objects ( id = instance . id ) . first ( ) <EOL> self . assertEquals ( len ( saved . tags ) , len ( retrieved . tags ) , '<STR_LIT>' ) <EOL> def test_name_exceeds_max_size ( self ) : <EOL> instance = TaggedModel ( ) <EOL> instance . tags = [ stormbase . TagField ( name = self . _gen_random_string ( <NUM_LIT> ) , <EOL> value = '<STR_LIT>' ) ] <EOL> try : <EOL> instance . save ( ) <EOL> self . assertTrue ( False , '<STR_LIT>' ) <EOL> except ValidationError : <EOL> pass <EOL> def test_value_exceeds_max_size ( self ) : <EOL> instance = TaggedModel ( ) <EOL> instance . tags = [ stormbase . TagField ( name = '<STR_LIT>' , <EOL> value = self . _gen_random_string ( <NUM_LIT> ) ) ] <EOL> try : <EOL> instance . save ( ) <EOL> self . assertTrue ( False , '<STR_LIT>' ) <EOL> except ValidationError : <EOL> pass <EOL> def _gen_random_string ( self , size = <NUM_LIT> , chars = string . ascii_lowercase + string . digits ) : <EOL> return '<STR_LIT>' . join ( [ random . choice ( chars ) for _ in range ( size ) ] ) </s>
