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 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 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 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(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 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_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_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 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 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 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_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_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_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_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_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_put_post(self): """ Editing an existing post """ post = models.Post(title="Example Post A", body="Just a test") session.add(post) session.commit() data = {"title": "Different Post", "body": "Another 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.decode("ascii")) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "Different Post") self.assertEqual(data["body"], "Another test") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Different Post") self.assertEqual(post.body, "Another test")
def test_delete_post(self): """ Deleting 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() 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(data["message"], "Deleted post 2")
def test_update_post(self): """ Updating a post (PUT request) 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(postA) session.commit() data_payload = {"title": "Example Post", "body": "a new test"} response = self.client.put("/api/posts/{}".format(postA.id), data=json.dumps(data_payload), content_type="application/json", headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data_response = json.loads(response.data.decode("ascii")) # {u'body': u'Just a test', u'id': 1, u'title': u'Example Post'} self.assertEqual(len(data_response), 3) # postA = data[0] self.assertEqual(data_response["title"], "Example Post") self.assertEqual(data_response["body"], "a new 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_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 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_put_post(self): postA = models.Post(title="Test1", body="Post with bells") session.add(postA) session.commit() data = { "title": "Example Post", "body": "Just a test" } 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") 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"], "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_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 form_valid(self, form): title = form.cleaned_data['title'] text = form.cleaned_data['text'] author = self.request.user.author draft = True post = models.Post(title=title, text=text, author=author, draft=draft) post.save() return redirect("posts:drafts")
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)