<s> from django . test . client import RequestFactory <EOL> from raspberryio . project . tests . base import ProjectBaseTestCase <EOL> from raspberryio . project . forms import ProjectImageForm , ProjectStepForm <EOL> class ProjectFormTestCase ( ProjectBaseTestCase ) : <EOL> def setUp ( self ) : <EOL> self . request_factory = RequestFactory ( ) <EOL> self . project = self . create_project ( ) <EOL> def test_placeholder_labels ( self ) : <EOL> request = self . request_factory . get ( '<STR_LIT:/>' ) <EOL> form = ProjectStepForm ( ) <EOL> self . assertEqual ( form . fields [ '<STR_LIT:title>' ] . label , '<STR_LIT>' ) <EOL> form . Meta . remove_labels = True <EOL> form = ProjectStepForm ( request . GET , instance = self . project ) <EOL> self . assertEqual ( form . fields [ '<STR_LIT:title>' ] . label , '<STR_LIT>' ) <EOL> class ProjectStepFormTestCase ( ProjectBaseTestCase ) : <EOL> def setUp ( self ) : <EOL> self . request_factory = RequestFactory ( ) <EOL> self . project = self . create_project ( ) <EOL> self . project_step = self . create_project_step ( project = self . project ) <EOL> def test_no_content ( self ) : <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , { } ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> self . assertEqual ( form . errors [ '<STR_LIT:content>' ] , [ '<STR_LIT>' ] ) <EOL> def test_images_empty ( self ) : <EOL> post_data = { <EOL> '<STR_LIT:title>' : self . get_random_string ( ) , <EOL> '<STR_LIT:content>' : self . get_random_string ( ) , <EOL> '<STR_LIT>' : [ ] <EOL> } <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , post_data ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> if form . is_valid ( ) : <EOL> project_step = form . save ( ) <EOL> self . assertEqual ( project_step . gallery . count ( ) , <NUM_LIT:0> ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_images_bad_format ( self ) : <EOL> self . create_project_image ( ) <EOL> post_data = { <EOL> '<STR_LIT:title>' : self . get_random_string ( ) , <EOL> '<STR_LIT:content>' : self . get_random_string ( ) , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , post_data ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> if form . is_valid ( ) : <EOL> project_step = form . save ( ) <EOL> self . assertEqual ( list ( project_step . gallery . all ( ) ) , [ ] ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_images_bad_ids ( self ) : <EOL> self . create_project_image ( ) <EOL> post_data = { <EOL> '<STR_LIT:title>' : self . get_random_string ( ) , <EOL> '<STR_LIT:content>' : self . get_random_string ( ) , <EOL> '<STR_LIT>' : [ '<STR_LIT>' ] <EOL> } <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , post_data ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> if form . is_valid ( ) : <EOL> project_step = form . save ( ) <EOL> self . assertEqual ( list ( project_step . gallery . all ( ) ) , [ ] ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_images_valid ( self ) : <EOL> project_image = self . create_project_image ( ) <EOL> post_data = { <EOL> '<STR_LIT:title>' : self . get_random_string ( ) , <EOL> '<STR_LIT:content>' : self . get_random_string ( ) , <EOL> '<STR_LIT>' : [ str ( project_image . id ) ] <EOL> } <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , post_data ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> if form . is_valid ( ) : <EOL> project_step = form . save ( ) <EOL> self . assertEqual ( list ( project_step . gallery . all ( ) ) , [ project_image ] ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_multiple_images_valid ( self ) : <EOL> project_image1 = self . create_project_image ( ) <EOL> project_image2 = self . create_project_image ( ) <EOL> project_images = ( project_image1 , project_image2 ) <EOL> post_data = { <EOL> '<STR_LIT:title>' : self . get_random_string ( ) , <EOL> '<STR_LIT:content>' : self . get_random_string ( ) , <EOL> '<STR_LIT>' : '<STR_LIT:U+002C>' . join ( [ str ( image . id ) for image in project_images ] ) <EOL> } <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , post_data ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> if form . is_valid ( ) : <EOL> project_step = form . save ( ) <EOL> self . assertEqual ( tuple ( project_step . gallery . all ( ) ) , project_images ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_bad_video_url ( self ) : <EOL> post_data = { <EOL> '<STR_LIT:title>' : self . get_random_string ( ) , <EOL> '<STR_LIT:content>' : self . get_random_string ( ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , post_data ) <EOL> form = ProjectStepForm ( request . POST , instance = self . project_step ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> class ProjectImageFormTestCase ( ProjectBaseTestCase ) : <EOL> def setUp ( self ) : <EOL> self . request_factory = RequestFactory ( ) <EOL> self . project = self . create_project ( ) <EOL> def test_valid_file_upload ( self ) : <EOL> filename = self . get_random_string ( ) <EOL> f = self . create_file ( filename = filename ) <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , { '<STR_LIT:file>' : f } ) <EOL> request . is_ajax = True <EOL> form = ProjectImageForm ( request . POST , request . FILES ) <EOL> if form . is_valid ( ) : <EOL> instance = form . save ( ) <EOL> self . assertEqual ( instance . get_filename ( ) , filename ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def test_invalid_upload ( self ) : <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , { '<STR_LIT>' : self . create_file ( ) } ) <EOL> request . is_ajax = True <EOL> form = ProjectImageForm ( request . POST or None , request . FILES or None ) <EOL> self . assertFalse ( form . is_valid ( ) ) <EOL> def test_no_file ( self ) : <EOL> request = self . request_factory . post ( '<STR_LIT:/>' , { } ) <EOL> request . is_ajax = True <EOL> form = ProjectImageForm ( request . POST or None , request . FILES or None ) <EOL> self . assertFalse ( form . is_valid ( ) ) </s>
