def test_edit_title_and_body(self): """ Editing a post's title and body """ data = { "title": "new example A", "body": "new example body" } postA= models.Post(title="Example A", body="test edit") session.add(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, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["title"], "new example A") self.assertEqual(data["body"], "new example body") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "new example A") self.assertEqual(post.body, "new example body")
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) # {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_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_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(postA) session.add(postB) session.commit() session.delete(postA) session.delete(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), 0)
def test_delete_post(self): post = models.Post(title="Example for testing delete post", body="Just a test for delete post") session.add(post) session.commit() response = self.client.delete("/api/posts/{}".format(post.id)) self.assertEqual(response.status_code, 200)
def testPostPut(self): """ Editing a post """ data = { "title": "UPDATED Example Post", "body": "Just an UPDATED test" } #first you create a post post = models.Post(title="example title", body="example body") session.add(post) session.commit() response = self.client.put("/api/edit/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") data = json.loads(response.data) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "UPDATED Example Post") self.assertEqual(data["body"], "Just an UPDATED test") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "UPDATED Example Post") self.assertEqual(post.body, "Just an UPDATED test")
def testEditPost(self): """ Editing a post """ postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) session.commit() data = { "title": "Change Post", "body": "Change 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, 200) self.assertEqual(response.mimetype, "application/json") posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Change Post") self.assertEqual(post.body, "Change test")
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_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 seed(): content = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.""" for i in range(25): post = Post( title="Test Post #{}".format(i), body=content ) session.add(post) session.commit()
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(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')
def seed(): content = """Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.""" for i in range(25): posts = Post( title="Test Entry #{}".format(i), body=content ) session.add(posts) session.commit()
def test_delete_post(self): """ Delete a single post from a populated database """ post = models.Post(title="Example Post", body="Just a test") post_id = post.id session.add(post) session.commit() # response = self.client.get("/api/posts/{}".format(postB.id)) response = self.client.delete("/api/posts/{}".format(post.id), headers=[("Accept", "application/json")]) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json")
def test_delete_post(self): # deleting a single post postC = models.Post(title="Example Post C", body="Yet another a test") session.add(postC) session.commit() response = self.client.delete("/api/posts/1", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) data = json.loads(response.data.decode("ascii")) self.assertEqual(data["message"], "Post with id 1 has been deleted")
def test_delete_post(self): postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) session.commit() session.delete(postA) session.commit() response = self.client.get("/api/posts/{}".format(postA.id), headers=[("Accept", "application/json")]) #check if post is not there (return 404 not found) self.assertEqual(response.status_code, 404) self.assertEqual(response.mimetype, "application/json")
def test_delete_post(self): post = models.Post(title='Example Post A', body='Just a test') session.add(post) session.commit() self.assertEqual(session.query(models.Post).get(1), post) response = self.client.delete('/api/posts/1', headers=[('Accept', 'application/json')] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, 'application/json') self.assertEqual(session.query(models.Post).get(1), None)
def test_delete_post(self): """Deleting a single post from the database""" postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) session.commit() response = self.client.delete("/api/posts/{}".format(postA.id)) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") postA = session.query(models.Post).get(1) #self.assertNotEqual(postA.id, 1) self.assertIsNone(postA)
def test_delete_post(self): postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) 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") posts = session.query(models.Post).all() self.assertEqual(len(posts), 0)
def test_delete_non_existent_post(self): postA = models.Post(title = 'Example Post A', body = 'Just a test') session.add(postA) session.commit() response = self.client.delete('/api/posts/2', 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'], 'Post with id 2 requested for deletion does not exist.')
def test_delete_post(self): """ Deleting a single post which exist """ delete_post = models.Post(title="Test Delete", body="Please delete me") session.add(delete_post) session.commit() response = self.client.get("/api/posts/delete/1", 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"], "Post with id 1 has been deleted")
def testDeletePost(self): postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) session.commit() # Where is proper documentation for delete() method? response = self.client.delete("/api/posts/1", headers=[("Accept", "application/json")] ) self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") data = json.loads(response.data) self.assertEqual(data["message"], "Post id 1 has been deleted.")
def test_delete_post(self): """ Deleting a post """ #TODO: see create_multiple_posts() postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) session.commit() response = self.client.delete("/api/posts/1", 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"], "Post #1 successfully deleted") self.assertEqual(session.query(models.Post).count(), 0)
def test_update_post_with_missing_data(self): """ Attempting to update a post with missing data (title or body)""" postA = models.Post(title="Example Post A", body="Just a test") session.add(postA) session.commit() data_payload = { "title": "Example Post" } 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, 422) data = json.loads(response.data) self.assertEqual(data["message"], "'body' is a required property")
def testPutPost(self): """ Editing a post """ # Adding a post to the database post = models.Post(title="Example Post", body="Just a test") session.add(post) session.commit() data = { "title": "Updated Example Post", "body": "Just an updated test" } # Getting a response for the post response = self.client.put("/api/post/{}".format(post.id), data = json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) # Testing the response self.assertEqual(response.status_code, 201) self.assertEqual(response.mimetype, "application/json") # Location of the updated post self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/1") # Testing that the json response data is our updated post data data = json.loads(response.data) self.assertEqual(data["id"], 1) self.assertEqual(data["title"], "Updated Example Post") self.assertEqual(data["body"], "Just an updated test") # Querying the database to check that our updated post is there posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) # Checking that the contents of the post in the database matches our updated post post = posts[0] self.assertEqual(post.title, "Updated Example Post") self.assertEqual(post.body, "Just an updated test")
def testPutPost(self): """ Edit an existing post """ # Add new post postNew = models.Post(title="Just a test", body="Post with bells") session.add(postNew) session.commit() data = { "title": "Change the title", "body": "Change the body" } # Create PUT request using JSON from 'data' dictionary response = self.client.put("/api/posts/1", data=json.dumps(data), content_type="application/json", headers=[("Accept", "application/json")] ) # Test response code, mimetype, and location path self.assertEqual(response.status_code, 200) self.assertEqual(response.mimetype, "application/json") self.assertEqual(urlparse(response.headers.get("Location")).path, "/api/posts/1") # Test post content from response data = json.loads(response.data) self.assertEqual(data["title"], "Change the title") self.assertEqual(data["body"], "Change the body") # Query post from database posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) # Test post content from database post = posts[0] self.assertEqual(post.title, "Change the title") self.assertEqual(post.body, "Change the body")
def test_update_post_with_invalid_json(self): """ Attempting to update an existing post with invalid JSON""" postA = models.Post( title="Example Post A", body="this will be valid JSON") session.add(postA) session.commit() data_payload = { "title": "Example Post", "body": 32 } 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, 422) data = json.loads(response.data) self.assertEqual(data["message"], "32 is not of type 'string'")
def test_post_put(self): """ Getting a single post from a populated database """ post = models.Post(title="Example Post A", body="Just a test") session.add(post) session.commit() data = { "title": "Alice edited the title", "body": "Alice edited the body" } response = self.client.put("/api/posts/{}".format(post.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") data = json.loads(response.data.decode("ascii")) self.assertEqual(data["id"], post.id) self.assertEqual(data["title"], "Alice edited the title") self.assertEqual(data["body"], "Alice edited the body")
def test_post_edit_post(self): """ Edit an existing post """ # Creat an entry in the datbase to edit. edit_post = models.Post(title="Test Edit", body="Please change the content of this post") session.add(edit_post) session.commit() data = { "title": "Example editing a post", "body": "Test edit" } response = self.client.post("/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"], "Example editing a post") self.assertEqual(data["body"], "Test edit") # Verify the post was inserted into the database posts = session.query(models.Post).all() self.assertEqual(len(posts), 1) post = posts[0] self.assertEqual(post.title, "Example editing a post") self.assertEqual(post.body, "Test edit")
def seed(): post = Post(title="ksldfj", body="test2-2") session.add(post) session.commit()