[pre-commit.ci lite] apply automatic fixes
This commit is contained in:
parent
6f54c4a1f6
commit
cef45e35b0
2 changed files with 44 additions and 35 deletions
|
|
@ -1,40 +1,44 @@
|
||||||
from flask import Flask, request, jsonify, abort
|
from flask import abort
|
||||||
|
from flask import Flask
|
||||||
|
from flask import jsonify
|
||||||
|
from flask import request
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
posts = []
|
posts = []
|
||||||
current_id = 1
|
current_id = 1
|
||||||
|
|
||||||
def find_post(post_id):
|
|
||||||
return next((post for post in posts if post['id'] == post_id), None)
|
|
||||||
|
|
||||||
@app.route('/api/posts', methods=['GET'])
|
def find_post(post_id):
|
||||||
|
return next((post for post in posts if post["id"] == post_id), None)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/posts", methods=["GET"])
|
||||||
def get_posts():
|
def get_posts():
|
||||||
return jsonify(posts), 200
|
return jsonify(posts), 200
|
||||||
|
|
||||||
@app.route('/api/posts/<int:post_id>', methods=['GET'])
|
|
||||||
|
@app.route("/api/posts/<int:post_id>", methods=["GET"])
|
||||||
def get_post(post_id):
|
def get_post(post_id):
|
||||||
post = find_post(post_id)
|
post = find_post(post_id)
|
||||||
if not post:
|
if not post:
|
||||||
abort(404, description="Post not found")
|
abort(404, description="Post not found")
|
||||||
return jsonify(post), 200
|
return jsonify(post), 200
|
||||||
|
|
||||||
@app.route('/api/posts', methods=['POST'])
|
|
||||||
|
@app.route("/api/posts", methods=["POST"])
|
||||||
def create_post():
|
def create_post():
|
||||||
global current_id
|
global current_id
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if not data or 'title' not in data or 'content' not in data:
|
if not data or "title" not in data or "content" not in data:
|
||||||
abort(400, description="Missing title or content")
|
abort(400, description="Missing title or content")
|
||||||
post = {
|
post = {"id": current_id, "title": data["title"], "content": data["content"]}
|
||||||
'id': current_id,
|
|
||||||
'title': data['title'],
|
|
||||||
'content': data['content']
|
|
||||||
}
|
|
||||||
posts.append(post)
|
posts.append(post)
|
||||||
current_id += 1
|
current_id += 1
|
||||||
return jsonify(post), 201
|
return jsonify(post), 201
|
||||||
|
|
||||||
@app.route('/api/posts/<int:post_id>', methods=['PUT'])
|
|
||||||
|
@app.route("/api/posts/<int:post_id>", methods=["PUT"])
|
||||||
def update_post(post_id):
|
def update_post(post_id):
|
||||||
post = find_post(post_id)
|
post = find_post(post_id)
|
||||||
if not post:
|
if not post:
|
||||||
|
|
@ -42,17 +46,19 @@ def update_post(post_id):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if not data:
|
if not data:
|
||||||
abort(400, description="Missing data")
|
abort(400, description="Missing data")
|
||||||
post['title'] = data.get('title', post['title'])
|
post["title"] = data.get("title", post["title"])
|
||||||
post['content'] = data.get('content', post['content'])
|
post["content"] = data.get("content", post["content"])
|
||||||
return jsonify(post), 200
|
return jsonify(post), 200
|
||||||
|
|
||||||
@app.route('/api/posts/<int:post_id>', methods=['DELETE'])
|
|
||||||
|
@app.route("/api/posts/<int:post_id>", methods=["DELETE"])
|
||||||
def delete_post(post_id):
|
def delete_post(post_id):
|
||||||
post = find_post(post_id)
|
post = find_post(post_id)
|
||||||
if not post:
|
if not post:
|
||||||
abort(404, description="Post not found")
|
abort(404, description="Post not found")
|
||||||
posts.remove(post)
|
posts.remove(post)
|
||||||
return '', 204
|
return "", 204
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
|
||||||
|
|
@ -1,53 +1,56 @@
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from app import app
|
from app import app
|
||||||
|
|
||||||
|
|
||||||
class BlogPostTestCase(unittest.TestCase):
|
class BlogPostTestCase(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.client = app.test_client()
|
self.client = app.test_client()
|
||||||
self.sample_post = {"title": "Test Post", "content": "This is a test post"}
|
self.sample_post = {"title": "Test Post", "content": "This is a test post"}
|
||||||
|
|
||||||
def test_create_post_success(self):
|
def test_create_post_success(self):
|
||||||
response = self.client.post('/api/posts', json=self.sample_post)
|
response = self.client.post("/api/posts", json=self.sample_post)
|
||||||
self.assertEqual(response.status_code, 201)
|
self.assertEqual(response.status_code, 201)
|
||||||
|
|
||||||
def test_create_post_missing_field(self):
|
def test_create_post_missing_field(self):
|
||||||
response = self.client.post('/api/posts', json={"title": "Only title"})
|
response = self.client.post("/api/posts", json={"title": "Only title"})
|
||||||
self.assertEqual(response.status_code, 400)
|
self.assertEqual(response.status_code, 400)
|
||||||
|
|
||||||
def test_get_all_posts(self):
|
def test_get_all_posts(self):
|
||||||
self.client.post('/api/posts', json=self.sample_post)
|
self.client.post("/api/posts", json=self.sample_post)
|
||||||
response = self.client.get('/api/posts')
|
response = self.client.get("/api/posts")
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def test_get_single_post_success(self):
|
def test_get_single_post_success(self):
|
||||||
post_resp = self.client.post('/api/posts', json=self.sample_post)
|
post_resp = self.client.post("/api/posts", json=self.sample_post)
|
||||||
post_id = post_resp.get_json()['id']
|
post_id = post_resp.get_json()["id"]
|
||||||
response = self.client.get(f'/api/posts/{post_id}')
|
response = self.client.get(f"/api/posts/{post_id}")
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def test_get_single_post_not_found(self):
|
def test_get_single_post_not_found(self):
|
||||||
response = self.client.get('/api/posts/999')
|
response = self.client.get("/api/posts/999")
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
def test_update_post_success(self):
|
def test_update_post_success(self):
|
||||||
post_resp = self.client.post('/api/posts', json=self.sample_post)
|
post_resp = self.client.post("/api/posts", json=self.sample_post)
|
||||||
post_id = post_resp.get_json()['id']
|
post_id = post_resp.get_json()["id"]
|
||||||
response = self.client.put(f'/api/posts/{post_id}', json={"title": "Updated"})
|
response = self.client.put(f"/api/posts/{post_id}", json={"title": "Updated"})
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
|
|
||||||
def test_update_post_not_found(self):
|
def test_update_post_not_found(self):
|
||||||
response = self.client.put('/api/posts/999', json={"title": "Nothing"})
|
response = self.client.put("/api/posts/999", json={"title": "Nothing"})
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
def test_delete_post_success(self):
|
def test_delete_post_success(self):
|
||||||
post_resp = self.client.post('/api/posts', json=self.sample_post)
|
post_resp = self.client.post("/api/posts", json=self.sample_post)
|
||||||
post_id = post_resp.get_json()['id']
|
post_id = post_resp.get_json()["id"]
|
||||||
response = self.client.delete(f'/api/posts/{post_id}')
|
response = self.client.delete(f"/api/posts/{post_id}")
|
||||||
self.assertEqual(response.status_code, 204)
|
self.assertEqual(response.status_code, 204)
|
||||||
|
|
||||||
def test_delete_post_not_found(self):
|
def test_delete_post_not_found(self):
|
||||||
response = self.client.delete('/api/posts/999')
|
response = self.client.delete("/api/posts/999")
|
||||||
self.assertEqual(response.status_code, 404)
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue