From 2026b218fa7e18d7f03a393a084bc18c30522fb9 Mon Sep 17 00:00:00 2001 From: ZzSteven-Wang Date: Fri, 1 Dec 2023 01:29:05 +0000 Subject: [PATCH 1/2] create the action button --- service/routes.py | 9 +++++---- service/static/index.html | 2 +- service/static/js/rest_api.js | 31 ++++++++++++++++--------------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/service/routes.py b/service/routes.py index b97232b..b5c354b 100644 --- a/service/routes.py +++ b/service/routes.py @@ -226,12 +226,13 @@ def change_product_availability(product_id): new_availability = not product.available product.available = new_availability db.session.commit() + message = {"message": f"Product availability changed to {new_availability}"} + message = {**message, **product.serialize()} app.logger.info("Product availability changed for ID [%s].", product_id) - return ( - jsonify({"message": f"Product availability changed to {new_availability}"}), - status.HTTP_200_OK, - ) + + return jsonify(message), status.HTTP_200_OK + ###################################################################### diff --git a/service/static/index.html b/service/static/index.html index 204edd9..bdbaaa7 100644 --- a/service/static/index.html +++ b/service/static/index.html @@ -103,7 +103,7 @@

Create, Retrieve, Update, and Delete a Product:

- + diff --git a/service/static/js/rest_api.js b/service/static/js/rest_api.js index 6024744..9453add 100644 --- a/service/static/js/rest_api.js +++ b/service/static/js/rest_api.js @@ -278,25 +278,26 @@ $(function () { // **************************************** $("#availability-btn").click(function () { - // let product_id = $("#product_id").val(); + let product_id = $("#product_id").val(); - // $("#flash_message").empty(); + $("#flash_message").empty(); - // let ajax = $.ajax({ - // type: "DELETE", - // url: `/products/${product_id}`, - // contentType: "application/json", - // data: '', - // }) + let ajax = $.ajax({ + type: "PUT", + url: `/products/${product_id}/change_availability`, + contentType: "application/json", + data: '', + }) - // ajax.done(function(res){ - // clear_form_data() - // flash_message("Product has been Deleted!") - // }); + ajax.done(function(res){ + update_form_data(res) + flash_message(res.message) + }); - // ajax.fail(function(res){ - // flash_message("Server error!") - // }); + ajax.fail(function(res){ + clear_form_data() + flash_message(res.responseJSON.message) + }); }); From d7015d38678cd10411b908508f65453b909b2e2f Mon Sep 17 00:00:00 2001 From: ZzSteven-Wang Date: Fri, 1 Dec 2023 01:59:22 +0000 Subject: [PATCH 2/2] add scenario for Action --- Makefile | 2 +- features/products.feature | 24 +++++++++++++++++++++++- features/steps/web_steps.py | 2 +- service/routes.py | 1 - service/static/index.html | 2 +- service/static/js/rest_api.js | 2 +- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 53bc797..0f2b527 100644 --- a/Makefile +++ b/Makefile @@ -32,7 +32,7 @@ lint: ## Run the linter flake8 service tests --count --max-complexity=10 --max-line-length=127 --statistics pylint service tests --max-line-length=127 -.PHONY: tests +.PHONY: test test: ## Run the unit tests $(info Running tests...) green -vvv --processes=1 --run-coverage --termcolor --minimum-coverage=95 diff --git a/features/products.feature b/features/products.feature index 2aac404..d01dd6d 100644 --- a/features/products.feature +++ b/features/products.feature @@ -182,4 +182,26 @@ Scenario: Query a Product by Available And I select "False" in the "Available" dropdown And I press the "Search" button Then I should see the message "Success" - And I should see "iPhone 16" in the results \ No newline at end of file + And I should see "iPhone 16" in the results + +Scenario: Action Change the Availability of a Product + When I visit the "Home Page" + And I press the "Search" button + Then I should see the message "Success" + When I copy the "Id" field + And I press the "Clear" button + Then the "Id" field should be empty + And the "Name" field should be empty + And the "Category" field should be empty + And the "Description" field should be empty + And the "Price" field should be empty + And the "image_url" field should be empty + When I paste the "Id" field + And I press the "Change Availability" button + Then I should see the message "Product availability changed to False" + And I should see "iPhone 15" in the "name" field + And I should see "Best iphone for now" in the "description" field + And I should see "999" in the "price" field + And I should see "False" in the "available" dropdown + And I should see "sample.url" in the "image_url" field + And I should see "ELECTRONICS" in the "category" field \ No newline at end of file diff --git a/features/steps/web_steps.py b/features/steps/web_steps.py index 9c5dad8..6aa2e69 100644 --- a/features/steps/web_steps.py +++ b/features/steps/web_steps.py @@ -116,7 +116,7 @@ def step_impl(context, element_name): @when('I press the "{button}" button') def step_impl(context, button): - button_id = button.lower() + "-btn" + button_id = button.lower().replace(" ", "_") + "-btn" context.driver.find_element(By.ID, button_id).click() diff --git a/service/routes.py b/service/routes.py index b5c354b..e9b6004 100644 --- a/service/routes.py +++ b/service/routes.py @@ -234,7 +234,6 @@ def change_product_availability(product_id): return jsonify(message), status.HTTP_200_OK - ###################################################################### # get product categories ###################################################################### diff --git a/service/static/index.html b/service/static/index.html index bdbaaa7..7362e08 100644 --- a/service/static/index.html +++ b/service/static/index.html @@ -103,7 +103,7 @@

Create, Retrieve, Update, and Delete a Product:

- + diff --git a/service/static/js/rest_api.js b/service/static/js/rest_api.js index 9453add..d4d9f7a 100644 --- a/service/static/js/rest_api.js +++ b/service/static/js/rest_api.js @@ -277,7 +277,7 @@ $(function () { // Change Availability // **************************************** - $("#availability-btn").click(function () { + $("#change_availability-btn").click(function () { let product_id = $("#product_id").val(); $("#flash_message").empty();