<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django import forms <EOL> from django . test import TestCase <EOL> from tastypie . bundle import Bundle <EOL> from tastypie . validation import Validation , FormValidation , CleanedDataFormValidation <EOL> class NoteForm ( forms . Form ) : <EOL> title = forms . CharField ( max_length = <NUM_LIT:100> ) <EOL> slug = forms . CharField ( max_length = <NUM_LIT:50> ) <EOL> content = forms . CharField ( required = False , widget = forms . Textarea ) <EOL> is_active = forms . BooleanField ( ) <EOL> def clean_title ( self ) : <EOL> return self . cleaned_data . get ( '<STR_LIT:title>' , '<STR_LIT>' ) . upper ( ) <EOL> def clean ( self ) : <EOL> if not self . cleaned_data . get ( '<STR_LIT:content>' , '<STR_LIT>' ) : <EOL> raise forms . ValidationError ( '<STR_LIT>' ) <EOL> return self . cleaned_data <EOL> class ValidationTestCase ( TestCase ) : <EOL> def test_init_no_args ( self ) : <EOL> try : <EOL> Validation ( ) <EOL> except Exception : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_init_form_class_provided ( self ) : <EOL> try : <EOL> Validation ( form_class = '<STR_LIT:foo>' ) <EOL> except Exception : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_is_valid ( self ) : <EOL> valid = Validation ( ) <EOL> bundle = Bundle ( ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { } ) <EOL> class FormValidationTestCase ( TestCase ) : <EOL> def test_init_no_args ( self ) : <EOL> self . assertRaises ( ImproperlyConfigured , FormValidation ) <EOL> def test_init_form_class_provided ( self ) : <EOL> try : <EOL> FormValidation ( form_class = NoteForm ) <EOL> except Exception : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_is_valid ( self ) : <EOL> valid = FormValidation ( form_class = NoteForm ) <EOL> bundle = Bundle ( ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT:title>' : [ u'<STR_LIT>' ] , <EOL> } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { } ) <EOL> self . assertEqual ( bundle . data [ '<STR_LIT:title>' ] , '<STR_LIT>' ) <EOL> class CleanedDataFormValidationTestCase ( TestCase ) : <EOL> def test_init_no_args ( self ) : <EOL> self . assertRaises ( ImproperlyConfigured , CleanedDataFormValidation ) <EOL> def test_init_form_class_provided ( self ) : <EOL> try : <EOL> CleanedDataFormValidation ( form_class = NoteForm ) <EOL> except Exception : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_is_valid ( self ) : <EOL> valid = CleanedDataFormValidation ( form_class = NoteForm ) <EOL> bundle = Bundle ( ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT:title>' : [ u'<STR_LIT>' ] , <EOL> } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { <EOL> '<STR_LIT>' : [ u'<STR_LIT>' ] , <EOL> } ) <EOL> bundle = Bundle ( data = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:bar>' , <EOL> '<STR_LIT:content>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : True , <EOL> } ) <EOL> self . assertEqual ( valid . is_valid ( bundle ) , { } ) <EOL> self . assertEqual ( bundle . data [ '<STR_LIT:title>' ] , u'<STR_LIT>' ) </s>
