def test_get_posts(self): """ Getting posts from a populated database """ #TODO: use self.create_multiple_posts() and adjust assertEquals postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test") postB = data[1] self.assertEqual(postB["title"], "Example Post B") self.assertEqual(postB["body"], "Still a test")
def test_delete_post(self): """ Delete a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() # import pdb; # pdb.set_trace() response = self.client.get("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) session.delete(postA) session.commit() self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # data = json.loads(response.data) # self.assertEqual(data["message"], "Successfully deleted post with id {}".format(postA.id)) posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) postB = posts[0] self.assertEqual(postB.title, "Example Post B") self.assertEqual(postB.body, "Still a test")
def testGetPosts(self): """ Getting posts from a populated database """ postA = models.Post(title="Title A", body= "Posting up over heeeere") postB = models.Post(title="Other title", body= "What a wonderful world") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA["title"], "Title A") self.assertEqual(postA["body"], "Posting up over heeeere") postB = data[1] self.assertEqual(postB["title"], "Other title") self.assertEqual(postB["body"], "What a wonderful world")
def test_get_posts(self): """ Getting posts from a populated database """ # First define test posts and commit them to database postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Another test") session.add_all([postA, postB]) session.commit() # Then retrieve the posts using the api endpoint # Define response as from the endpoint testing response = self.client.get("/api/posts", headers=[("Accept", "application/json")] ) # Test response is successful and mimetype correct self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Test two posts returned data = json.loads(response.data) self.assertEqual(len(data), 2) # Test two posts are same as exist in database postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test") postB = data[1] self.assertEqual(postB["title"], "Example Post B") self.assertEqual(postB["body"], "Another test")
def test_post_put(self): """ Posting a new post """ data = { "title": "Example Post", "body": "Just a test" } postA = models.Post(title="Just a test", body="Post with bells") session.add_all([postA]) session.commit() response = self.client.put("/api/posts/{}".format(postA.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "Example Post") self.assertEqual(data["body"], "Just a test") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Example Post") self.assertEqual(post.body, "Just a test")
def test_delete_post(self): """ Deleting a single post from a populated database """ postA = models.Post(title="Example Post A", body="Delete this") postB = models.Post(title="Example Post B", body="Don't Delete this") session.add_all([postA,postB]) session.commit() response = self.client.get("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")] ) session.delete(postA) session.commit() response = self.client.get("/api/posts/{}".format(postB.id)) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = json.loads(response.data.decode("ascii")) self.assertEqual(post["title"], "Example Post B") self.assertEqual(post["body"], "Don't Delete this")
def testGetPosts(self): """ Getting posts from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test") postB = data[1] self.assertEqual(postB["title"], "Example Post B") self.assertEqual(postB["body"], "Still a test")
def testGetPostsWithTitleAndBody(self): """ Filtering posts by title and body Test posts that include 'Just' in title and 'bells' in body """ # Add three posts - two contain whistle postA = models.Post(title="Just a test", body="Post with bells") postB = models.Post(title="Still a Test", body="Post with whistles") postC = models.Post(title="Another Test", body="Post with bells and whistles") session.add_all([postA, postB, postC]) session.commit() # Route using query string response = self.client.get("/api/posts?title_like=Just&body_like=bells", headers=[("Accept", "application/json")] ) # Test response status code and mimetype self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Test number of posts posts = json.loads(response.data) self.assertEqual(len(posts), 1) # Test first post content post = posts[0] self.assertEqual(post["title"], "Just a test") self.assertEqual(post["body"], "Post with bells")
def test_get_posts_with_body(self): """ Filtering posts by body """ postA = models.Post(title="Post with bells", body="A long and interesting text goes here") postB = models.Post(title="Post with whistles", body="Something really smart goes here") postC = models.Post(title="Post with bells and whistles", body="This is not a long text") session.add_all([postA, postB, postC]) session.commit() response = self.client.get("/api/posts?body_like=long", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data.decode("ascii")) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with bells") self.assertEqual(post["body"], "A long and interesting text goes here") post = posts[1] self.assertEqual(post["title"], "Post with bells and whistles") self.assertEqual(post["body"], "This is not a long text")
def test_delete_single_post(self): """ Deleting a single post from the database """ # Define test posts and commit to database postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Another test") session.add_all([postA, postB]) session.commit() # Test post is in database self.test_get_post() # Test connection to delete posts endpoint response = self.client.delete("/api/post/1", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Test response message is correct data = json.loads(response.data) self.assertEqual(data["message"], "Deleted post with id 1 from the database") # Confirm deletion by testing GETting the same post self.test_non_existant_post()
def test_delete_post(self): """ Setup and delete a post """ #### Add post a and post b postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() ### query for the post B ID post = session.query(models.Post).get(postB.id) session.delete(post) session.commit() response = self.client.get("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")] ) #post = json.loads(response.data) print "hello" with open("delete_post1.txt", 'w+') as testfile: #testfile.write(json.dumps(post)) testfile.write("response is equal to: {}".format(response)) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json")
def test_get_post(self): """ Getting a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") post = json.loads(response.data) with open("test_get_post.txt", 'w+') as testfile: testfile.write(json.dumps(post)) # f = open('workfile', 'w+') # # f.write("this is the {0} ".format(data) # f.write(json.dumps(data)) self.assertEqual(post["title"], "Example Post B") self.assertEqual(post["body"], "Still a test")
def testEditPost(self): """ Editing a single post """ postA = models.Post(title="Example post A", body="Just a test") postB = models.Post(title="Example post B", body="Still a test") session.add_all([postA, postB]) session.commit() data = { "title": "Edited post B", "body": "Now an edited test" } response = self.client.put("/api/posts/{}".format(postB.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/2") data = json.loads(response.data) self.assertEqual(data["id"], 2) self.assertEqual(data["title"], "Edited post B") self.assertEqual(data["body"], "Now an edited test") post = session.query(models.Post).get(2) self.assertEqual(post.title, "Edited post B") self.assertEqual(post.body, "Now an edited test")
def test_get_posts_with_title(self): """ Filtering posts by title """ postA = models.Post(title="Post with bells", body="Just a test") postB = models.Post(title="Post with whistles", body="Still a test") postC = models.Post(title="Post with bells and whistles", body="Another test") session.add_all([postA, postB, postC]) session.commit() response = self.client.get("/api/posts?title_like=whistles", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data.decode("ascii")) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with whistles") self.assertEqual(post["body"], "Still a test") post = posts[1] self.assertEqual(post["title"], "Post with bells and whistles") self.assertEqual(post["body"], "Another test")
def testGetPostsWithTitle(self): """ Filtering posts by title """ postA = models.Post(title="Post with green eggs", body="Just a test") postB = models.Post(title="Post with ham", body="Still a test") postC = models.Post(title="Post with green eggs and ham", body="Another test") session.add_all([postA, postB, postC]) session.commit() response = self.client.get("/api/posts?title_like=ham", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with ham") self.assertEqual(post["body"], "Still a test") post = posts[1] self.assertEqual(post["title"], "Post with green eggs and ham") self.assertEqual(post["body"], "Another test")
def test_get_posts_with_title_and_body(self): """ Filtering posts by title and body """ # First create number of posts postA = models.Post(title="Post with bells", body="Just a test") postB = models.Post(title="Post with whistles", body="Another test") postC = models.Post(title="Post with whistles", body="More tests") # Add posts to database session.add_all([postA, postB, postC]) session.commit() # Test for posts containing 'Whistle' in title and 'test' in body # - Header is correct response = self.client.get("/api/posts?title_like=whistles&body_like=test", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # - Number of posts is correct posts = json.loads(response.data) print posts self.assertEqual(len(posts), 2) # - Individual post's title and body are correct post = posts[0] self.assertEqual(post["title"], "Post with whistles" ) self.assertEqual(post["body"], "Another test" ) post = posts[1] self.assertEqual(post["title"], "Post with whistles" ) self.assertEqual(post["body"], "More tests" )
def test_edit_post(self): """ Edit an existing post """ postA = models.Post(title="Original Post Title", body="This is the original, unedited text") session.add_all([postA]) session.commit() data = { "title": "Edited Post Title", "body": "This is the new and improved text" } response = self.client.post("/api/posts/1", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/1") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "Edited Post Title") self.assertEqual(data["body"], "This is the new and improved text") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Edited Post Title") self.assertEqual(post.body, "This is the new and improved text")
def testPutPost(self): """ Editing a post via a PUT """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() data = { "title": "Edited Post B", "body": "Scrambled eggs" } response = self.client.put("/api/posts/1", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/1") data = json.loads(response.data) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "Edited Post B") self.assertEqual(data["body"], "Scrambled eggs")
def testGetPostsWithTitleAndBody(self): """ Filtering posts by title and body""" # Add three posts to database # One of which has "whistles" in title and "Another" in body postA = models.Post(title="Post with bells", body="Just a test") postB = models.Post(title="Post with whistles", body="Still a test") postC = models.Post(title="Post with bells and whistles", body="Another test") session.add_all([postA, postB, postC]) session.commit() # Make a GET request to the endpoint and adding query string response = self.client.get("/api/posts?title_like=whistles&body_like=Another", headers=[("Accept", "application/json")] ) # Check the response status and mimetype self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Ensure the correct post is returned posts = json.loads(response.data) self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post["title"], "Post with bells and whistles") self.assertEqual(post["body"], "Another test")
def test_delete_post(self): """ Deleting a post """ postA = database.Post(title="Example Post A", body="Just a test") postB = database.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/<id>") self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test") postB = data[1] self.assertEqual(postB["title"], "Example Post B") self.assertEqual(postB["body"], "Still a test")
def testGetPosts(self): """ Getting posts from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() # Use test client to make a GET request response = self.client.get("/api/posts", headers=[("Accept", "application/json")] ) # Check status returned by endpoint is 200 OK self.assertEqual(response.status_code, 200) # Check endpoint returned JSON by the mimetype self.assertEqual(response.mimetype, "application/json") # Decode the response using 'json.loads()' data = json.loads(response.data) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test") postB = data[1] self.assertEqual(postB["title"], "Example Post B") self.assertEqual(postB["body"], "Still a test")
def test_delete(self): """ Deleting a post """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) post = data self.assertEqual(post["title"], "Example Post A") self.assertEqual(post["body"], "Just a test") response = self.client.delete("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) response = self.client.get("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Could not find post with id 1")
def testGetPostsWithTitleAndBody(self): """ Filtering posts by title and body """ postA = models.Post(title="Green eggs and ham", body="A post by Sam I Am about my favorite foods") postB = models.Post(title="Green fish blue fish", body="A post about Sam I Am's fish bowl") postC = models.Post(title="Green is my favorite color", body="A post by James about how much I love the color green") postD = models.Post(title="The Cat in the Hat", body="A post by Sam I Am about my greatest rival for power") session.add_all([postA, postB, postC, postD]) session.commit() response = self.client.get("/api/posts?title_like=green&body_like=Sam", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Green eggs and ham") self.assertEqual(post["body"], "A post by Sam I Am about my favorite foods") post = posts[1] self.assertEqual(post["title"], "Green fish blue fish") self.assertEqual(post["body"], "A post about Sam I Am's fish bowl")
def testGetPosts(self): """ Getting posts from a populated database """ # Add new posts postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() # Routed from api.py and push accept header response = self.client.get("/api/posts", headers=[("Accept", "application/json")] ) # Test response status and type self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Set JSON data response and test number of responses data = json.loads(response.data) self.assertEqual(len(data), 2) # Test content of postA postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test") # Test content of postB postB = data[1] self.assertEqual(postB["title"], "Example Post B") self.assertEqual(postB["body"], "Still a test")
def testPostsPut(self): """ Editing an existing post """ postA = models.Post(title="Title A", body= "Posting up over heeeere") session.add_all([postA]) session.commit() data = { "title": "Example Post", "body": "Just a test" } response = self.client.put("/api/posts/1", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/1") data = json.loads(response.data) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "Example Post") self.assertEqual(data["body"], "Just a test") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Example Post") self.assertEqual(post.body, "Just a test")
def testPostEdit(self): """ Testing the edit an existing post """ # First, create a new post data_inject = { "title": "This Post has been updated", "body": "The body has been edited" } postA = models.Post(title="Example Post A", body="Just a test") session.add_all([postA]) session.commit() #Now edit the post with new data response = self.client.put("/api/posts/{}".format(postA.id), data=json.dumps(data_inject), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") data=json.loads(response.data) # Test number of posts posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) # Test that it contains the new data self.assertEqual(data["title"], "This Post has been updated") self.assertEqual(data["body"], "The body has been edited")
def testGetPostsWithTitle(self): """ Filtering posts by title """ postA = models.Post(title="Post with bells", body="Just a test") postB = models.Post(title="Post with whistles", body="Still a test") postC = models.Post(title="Post with bells and whistles", body="Another test") session.add_all([postA, postB, postC]) session.commit() response = self.client.get("/api/posts?title_like=whistles", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with whistles") self.assertEqual(post["body"], "Still a test") post = posts[1] self.assertEqual(post["title"], "Post with bells and whistles") self.assertEqual(post["body"], "Another test")
def test_edit_post(self): postA = models.Post(title="Post with bells", body="Just a test for bells") postB = models.Post(title="Post with whistles", body="Still a test but for whistles") postC = models.Post(title="Post with bells and whistles", body="Another test") session.add_all([postA, postB, postC]) session.commit() data = {"title": "New Title", "body": "New body"} response = self.client.put("/api/posts/1/edit", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 202) post_1 = session.query(models.Post).get(1) self.assertEqual(post_1.title, "New Title") self.assertEqual(post_1.body, "New body") post_2 = session.query(models.Post).get(2) self.assertEqual(post_2.title, "Post with whistles") self.assertEqual(post_2.body, "Still a test but for whistles")
def test_put(self): """ Updating a post via put """ postA = models.Post(title="Post with bells", body="Just a whistle test") session.add_all([postA]) session.commit() data = {"title": "Post with a whistle", "body": "Just a whistle test"} response = self.client.put("/api/post/{}".format(postA.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(data["title"], "Post with a whistle") self.assertEqual(data["body"], "Just a whistle test") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Post with a whistle") self.assertEqual(post.body, "Just a whistle test")
def test_post_put(self): """Putting a new post""" postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() data = { "title": "New Post", "body" : "Trying something out" } response = self.client.put("/api/posts/{}".format(postB.id), data=json.dumps(data), content_type="application/json",headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/{}".format(postB.id)) data=json.loads(response.data.decode("ascii")) self.assertEqual(data["id"],postB.id) self.assertEqual(data["title"], "New Post") self.assertEqual(data["body"], "Trying something out") posts= session.query(models.Post).all() self.assertEqual(len(posts), 2) post = posts[1] self.assertEqual(post.title, "New Post") self.assertEqual(post.body, "Trying something out")
def test_get_posts(self): ''' getting posts from a populated database ''' postA = models.Post(title = 'Example Post A', body = 'Just a test') postB = models.Post(title = 'Example Post B', body = 'Still a test') session.add_all([postA, postB]) session.commit() response = self.client.get('/api/posts', headers = [('Accept', 'application/json')] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') data = json.loads(response.data.decode('ascii')) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA['title'], 'Example Post A') self.assertEqual(postA['body'], 'Just a test') postB = data[1] self.assertEqual(postB['title'], 'Example Post B') self.assertEqual(postB['body'], 'Still a test')
def test_put(self): """ Updating a post via put """ postA = models.Post(title="Post with bells", body="Just a whistle test") session.add_all([postA]) session.commit() data = { "title": "Post with a whistle", "body": "Just a whistle test" } response = self.client.put("/api/post/{}".format(postA.id), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 201) self.assertEqual(data["title"], "Post with a whistle") self.assertEqual(data["body"], "Just a whistle test") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Post with a whistle") self.assertEqual(post.body, "Just a whistle test")
def test_get_posts(self): """ Getting posts from a populated database """ postA = models.Post(name="Example Post A", ingredients="Just a test", directions="testing") postB = models.Post(name="Example Post B", ingredients="Still a test", directions="still testing") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2) postA = data[0] self.assertEqual(postA["name"], "Example Post A") self.assertEqual(postA["ingredients"], "Just a test") postB = data[1] self.assertEqual(postB["name"], "Example Post B") self.assertEqual(postB["ingredients"], "Still a test")
def testGetPostWithBody(self): """Filtering posts by body""" postA = models.Post(title="Post with green eggs", body="We have eggs") postB = models.Post(title="Post with ham", body="We have eggs") postC = models.Post(title="Post with green eggs and ham", body="Another test") session.add_all([postA, postB, postC]) session.commit() response = self.client.get("/api/posts?body_like=eggs", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with green eggs") self.assertEqual(post["body"], "We have eggs") post = posts[1] self.assertEqual(post["title"], "Post with ham") self.assertEqual(post["body"], "We have eggs")
def test_delete_single_post(self): postA = models.Post(title="Example Post A", body="Delete this") postB = models.Post(title="Example Post B", body="Don't delete this") session.add_all([postA, postB]) session.commit() response = self.client.get( "/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) session.delete(postA) session.commit() self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # data = json.loads(response.data) posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) postB = posts[0] self.assertEqual(postB.title, "Example Post B") self.assertEqual(postB.body, "Don't delete this")
def testGetPostsWithTitleBody(self): """ Filtering posts by title and body""" postA = models.Post(title="Post with green eggs", body="Ham a test") postB = models.Post(title="Post with ham", body="Eggs a test") postC = models.Post(title="Post with green eggs and ham", body="Another eggs") postD = models.Post(title="Post ham", body="Hier sind keine Eier") session.add_all([postA, postB, postC, postD]) session.commit() response = self.client.get("/api/posts?title_like=ham&body_like=eggs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with ham") self.assertEqual(post["body"], "Eggs a test") post = posts[1] self.assertEqual(post["title"], "Post with green eggs and ham") self.assertEqual(post["body"], "Another eggs")
def test_get_posts_with_title_and_body(self): """ Filtering posts by title and body """ postA = models.Post(title="Post with bells", body="Just a test") postB = models.Post(title="Post with whistles", body="Bells are ringing") postC = models.Post(title="Post with bells and whistles", body="More bells") session.add_all([postA, postB, postC]) session.commit() response = self.client.get("/api/posts?title_like=whistles&body_like=bells", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data.decode("ascii")) self.assertEqual(len(posts), 2) post = posts[0] self.assertEqual(post["title"], "Post with whistles") self.assertEqual(post["body"], "Bells are ringing") post = posts[1] self.assertEqual(post["title"], "Post with bells and whistles") self.assertEqual(post["body"], "More bells")
def test_get_posts(self): """ Getting posts from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit()
def testGetPostsWithTitle(self): """ Filtering posts by title """ # Adding all the posts postA = models.Post(title="Post with bells", body="Just a bells test") postB = models.Post(title="Post with whistles", body="Still a whistles test") postC = models.Post(title="Post with bells and whistles", body="Another bells test") session.add_all([postA, postB, postC]) session.commit() # Getting a response for posts with title like "whistles" response = self.client.get("/api/posts?title_like=whistles", headers=[("Accept", "application/json")]) # Testing the response self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # Getting the posts back posts = json.loads(response.data) # Testing that we got 2 posts back self.assertEqual(len(posts), 2) # Testing the content of the posts returned post = posts[0] self.assertEqual(post["title"], "Post with whistles") self.assertEqual(post["body"], "Still a whistles test") post = posts[1] self.assertEqual(post["title"], "Post with bells and whistles") self.assertEqual(post["body"], "Another bells test")
def testDeletePost(self): """ Deleting a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}".format(postB.id), headers=[("Accept","application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") post = json.loads(response.data) self.assertEqual(post["title"], "Example Post B") self.assertEqual(post["body"], "Still a test") # test that deleted post does not exist anymore response = self.client.get("/api/posts/{}".format(postB.id), headers=[("Accept","application/json")]) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(data["message"], "Could not find post with id {}".format(postB.id))
def test_delete_post(self): """ Delete a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() # import pdb; # pdb.set_trace() response = self.client.get("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")] ) session.delete(postA) session.commit() self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") # data = json.loads(response.data) # self.assertEqual(data["message"], "Successfully deleted post with id {}".format(postA.id)) posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) postB = posts[0] self.assertEqual(postB.title, "Example Post B") self.assertEqual(postB.body, "Still a test")
def create_multiple_posts(self): """ Create multiple posts for tests """ #TODO: maybe make one for a single test post? Only if needed postA = models.Post(title="Post with bells", body="Nobody expects") postB = models.Post(title="Post with whistles", body="Still a test") postC = models.Post(title="Post with bells and whistles", body="Nobody expects the Spanish Inquisition!") session.add_all([postA, postB, postC]) session.commit()
def test_delete_post_wrong_method(self): postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.post("/api/posts/3/delete", headers=[("Accept", "application/json")]) posts = session.query(models.Post).all() self.assertIn(postA, posts) self.assertEqual(response.status_code, 405)
def test_get_posts(self): """ Getting posts from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get('/api/posts', headers=[("Accept", "application/json")]) data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 2)
def test_delete_post(self): """ Getting a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json")
def test_delete_post(self): postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() # How to change method to DELETE? response = self.client.delete("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json")
def test_delete_post(self): postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}/delete".format(postA.id), headers=[("Accept", "application/json")]) posts = session.query(models.Post).all() self.assertNotIn(postA, posts) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json")
def test_delete_post(self): """ Delete a single post """ postA = models.Post(title="Example Post A", body="Just a test") session.add_all([postA,]) session.commit() response = self.client.delete("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 0)
def test_get_post(self): postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts/{}".format(postB.id)) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "applications/json") post = json.loads(response.data.decode("ascii")) self.assertEqual(post["title"], "Example Post B") self.assertEqual(post["body"], "Still a test")
def test_delete_post_with_wrong_id(self): postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/3/delete", headers=[("Accept", "application/json")]) posts = session.query(models.Post).all() self.assertIn(postA, posts) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Could not find post with id 3")
def test_delete_single_post(self): """ deleting a post from a populated database """ postA = models.Post(title="test Post A", body="testing a") session.add_all([postA]) session.commit() response = self.client.post("/api/posts/{}/delete".format(postA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") entries = session.query(models.Post).all() data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Post has been deleted!") self.assertEqual(len(data), 1)
def testGetPost(self): """ Getting a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts/{}".format(postB.id)) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") post = json.loads(response.data) self.assertEqual(post["title"], "Example Post B") self.assertEqual(post["body"], "Still a test")
def test_delete_existing_post(self): postA = models.Post(title="Post A", body="Body A") postB = models.Post(title="Post B", body="Body B") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") post = json.loads(response.data.decode('ascii')) self.assertEqual(post['title'], postB.title) self.assertEqual(post['body'], postB.body)
def test_get_post(self): """ Getting a single post from a populated database """ postA = models.Post(title="Post A", body="Body A") postB = models.Post(title="Post B", body="Body B") session.add_all([postA, postB]) session.commit() response = self.client.get("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") post = json.loads(response.data.decode("ascii")) self.assertEqual(post["title"], "Post B") self.assertEqual(post["body"], "Body B")
def testGetPostsWithTitleWithBody(self): """ Filtering posts by title """ postA = models.Post(title="Post with ham", body="And with eggs") session.add_all([postA]) session.commit() response = self.client.get("/api/posts?title_like=ham&body_like=eggs", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") posts = json.loads(response.data) self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post["title"], "Post with ham")
def testDeletePost(self): """ Delete a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")] ) # Test one post deleted/one post remaining data = json.loads(response.data) self.assertEqual(len(data), 1) # Test response status, message, and mimetype self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") self.assertEqual(data["message"], "Post 2 deleted")
def test_delete_post(self): """ Deleting posts from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Deleted post with id 1") # Assert that there is only one post in the database posts = session.query(models.Post).all() self.assertEqual(len(posts), 1)
def test_edit_body(self): # editing title posts = [] for i in range(1, 11): posts.append( models.Post(title="Post Title Number {}".format(i), body="Post body no {}".format(i))) session.add_all(posts) session.commit() random_num = random.choice(range(1, 11)) random_post = session.query(models.Post).get(random_num) data = {"title": random_post.title, "body": "Edited body"} random_post = random.choice(range(1, 11)) response = self.client.put("/api/posts/{}".format(random_num), data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") self.assertEqual( urlparse(response.headers.get("Location")).path, "/api/posts/{}".format(random_num)) data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], random_num) self.assertEqual(data["title"], "Post Title Number {}".format(random_num)) self.assertEqual(data["body"], "Edited body") posts = session.query(models.Post).all() for i in range(0, 10): print(posts[i].title) self.assertEqual(len(posts), 10) random_post = session.query(models.Post).get(random_num) self.assertEqual(random_post.title, "Post Title Number {}".format(random_num)) self.assertEqual(random_post.body, "Edited body")
def test_delete_single_post(self): #6 """ Delete a single post from a populated database """ postA = models.Post(title="Example Post A", body="Just a test") postB = models.Post(title="Example Post B", body="Still a test") session.add_all([postA, postB]) session.commit() response = self.client.delete("/api/posts/{}".format(postB.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(len(data), 1) postA = data[0] self.assertEqual(postA["title"], "Example Post A") self.assertEqual(postA["body"], "Just a test")