diff --git a/src/app.py b/src/app.py index c1224fe..b686a93 100644 --- a/src/app.py +++ b/src/app.py @@ -1,4 +1,4 @@ -from flask import Flask, jsonify, request, abort +from flask import Flask, jsonify, request, abort, render_template from uuid import uuid4 app = Flask(__name__) @@ -6,6 +6,10 @@ # In-memory product store data = {} +@app.route('/') +def index(): + return render_template('index.html') + @app.route('/products', methods=['GET']) def get_products(): return jsonify(list(data.values())), 200 @@ -23,7 +27,7 @@ def create_product(): if not body or 'name' not in body: abort(400) product_id = str(uuid4()) - product = {'id': product_id, 'name': body['name'], 'description': body.get('description', '')} + product = {'id': product_id, 'name': body['name'], 'description': body.get('description', ''), 'category': body.get('category', '')} data[product_id] = product return jsonify(product), 201 @@ -34,7 +38,7 @@ def update_product(product_id): body = request.get_json() if not body or 'name' not in body: abort(400) - data[product_id].update({'name': body['name'], 'description': body.get('description', '')}) + data[product_id].update({'name': body['name'], 'description': body.get('description', ''), 'category': body.get('category', '')}) return jsonify(data[product_id]), 200 @app.route('/products/', methods=['DELETE']) diff --git a/src/templates/index.html b/src/templates/index.html new file mode 100644 index 0000000..3c7ee87 --- /dev/null +++ b/src/templates/index.html @@ -0,0 +1,499 @@ + + + + + + Product Store Demo + + + +
+
+

Product Store Demo

+
+ + + +
+

Add New Product

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+
+ +
+

Products

+
Loading products...
+ +
+
+ + + + \ No newline at end of file diff --git a/src/test_app.py b/src/test_app.py index 7f7ed38..12ed55a 100644 --- a/src/test_app.py +++ b/src/test_app.py @@ -1 +1,25 @@ -# Unit test file \ No newline at end of file +import pytest +from app import app + +@pytest.fixture +def client(): + app.config['TESTING'] = True + with app.test_client() as client: + yield client + +def test_create_and_get_product(client): + # Create + rv = client.post('/products', json={'name': 'Test', 'description': 'Desc'}) + assert rv.status_code == 201 + product = rv.get_json() + # Get + rv = client.get(f"/products/{product['id']}") + assert rv.status_code == 200 + assert rv.get_json()['name'] == 'Test' + +def test_index_route(client): + """Test that the main route serves the HTML UI""" + rv = client.get('/') + assert rv.status_code == 200 + assert b'Product Store Demo' in rv.data + assert b'Add New Product' in rv.data \ No newline at end of file