示例#1
0
 def test_insert_recipe( self ): 
     # setup recipe test object id
     objectId = ObjectId( '5cf4f99b6a6c520e6648eee5' )
     # insert document to recipes
     recipes_collection.insert_one( {'_id'  : objectId , 'recipeName' : 'test insert document' }  )
     # insert recipe id to user document
     users_collection.update_one( { 'email': 'chris@chris' },{ '$addToSet': { 'myRecipes': objectId } }, upsert = True )
     # find recipe document inserted
     recipe = recipes_collection.find_one( {'recipeName': 'test insert document' } )
     # find user document updated
     user_recipe = users_collection.find_one( { "myRecipes": [objectId] } )
     # ensure correct recipe content
     self.assertTrue( recipe[ 'recipeName' ] == 'test insert document' )
     # ensure correct user content
     self.assertTrue( user_recipe['myRecipes'] == [objectId] )
     # assert correct number of recipe documents
     self.assertEqual( recipes_collection.count() , 3 )
     # assert correct number of user documents
     self.assertEqual( users_collection.count() , 1 )
     with app.test_request_context( '/show_recipe/5cf4f99b6a6c520e6648eee5' ):
        response = self.client.get( '/show_recipe/5cf4f99b6a6c520e6648eee5', follow_redirects = True )
        # ensure 200 response
        self.assert200( response)
        # ensure correct recipe used
        self.assertTemplateUsed( 'show_recipe.html' )
        # ensure correct data in response
        self.assertIn( b'test insert document' , response.data )
示例#2
0
 def test_advanced_search( self ):
     response = self.client.get( "/advanced_search_results/[{'recipeCuisine': 'Italian'}]", follow_redirects = True )
     # ensure correct server response
     self.assert200( response )
     # ensure correct template used
     self.assertTemplateUsed( 'search_results.html' )
     # assert correct number of recipe documents
     self.assertEqual( recipes_collection.count() , 2 )
     # assert correct number of user documents
     self.assertEqual( users_collection.count() , 1 )
示例#3
0
 def test_delete_recipe( self ):
     recipes_collection.delete_one( { '_id' : ObjectId( '0123456789ab0123456789ab' ) } )
     # create response
     response = self.client.get( '/' , follow_redirects = True )
     # ensure correct server response
     self.assert200( response )
     # ensure correct template used
     self.assertTemplateUsed( 'index.html' )
     # ensure correct recipe count
     self.assertEqual( recipes_collection.count() , 1 )
示例#4
0
 def test_get_recipes( self ):
     # create response variable
     response = self.client.get( '/' , follow_redirects = True )
     # convert recipe documents to bson
     data_dumps = dumps( recipes_collection.find() )
     # ensure 200 repsonse given by server
     self.assert200( response )
     # ensure redirect to index page
     self.assertTemplateUsed( 'index.html' )
     # ensure recipe document text in response
     self.assertIn( b'test this document', response.data )
     self.assertIn( b'Italian', response.data )
     # ensure recipe document count is correct
     self.assertEqual( recipes_collection.count() , 2 )
     # ensure user document count is correct
     self.assertEqual( users_collection.count() , 1 )
     # ensure bson is of correct data type
     self.assertEqual( type( data_dumps ) , str )
示例#5
0
 def test_update_recipe( self ):
     # update recipe document
     recipes_collection.update_one( { '_id' : ObjectId( '0123456789ab0123456789ab' ) } , { "$set" : { 'recipeName' : 'This has been updated' } } , upsert = True )
     # find recipe updated
     recipe = recipes_collection.find_one( {'recipeName': 'This has been updated' } )
     # ensure correct recipe content
     self.assertTrue( recipe['recipeName'] == 'This has been updated' )
     # assert correct number of recipe documents
     self.assertEqual( recipes_collection.count() , 2 )
     # assert correct number of user documents
     self.assertEqual( users_collection.count() , 1 )
     with app.test_request_context( '/show_recipe/0123456789ab0123456789ab' ):
        response = self.client.get( '/show_recipe/0123456789ab0123456789ab', follow_redirects = True )
        # ensure 200 response
        self.assert200( response )
        # ensure correct template
        self.assertTemplateUsed( 'show_recipe.html' )
        # ensure correct data
        self.assertIn( b'This has been updated', response.data )