Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Action Button and Add scenario for Action #69

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 23 additions & 1 deletion features/products.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
2 changes: 1 addition & 1 deletion features/steps/web_steps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
8 changes: 4 additions & 4 deletions service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ 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


######################################################################
Expand Down
2 changes: 1 addition & 1 deletion service/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h3>Create, Retrieve, Update, and Delete a Product:</h3>
<button type="submit" class="btn btn-primary" id="clear-btn">Clear</button>
<button type="submit" class="btn btn-success" id="create-btn">Create</button>
<button type="submit" class="btn btn-warning" id="update-btn">Update</button>
<button type="submit" class="btn btn-danger" id="availability-btn">Change Availability</button>
<button type="submit" class="btn btn-warning" id="change_availability-btn">Change Availability</button>
</div>
</div>
</div> <!-- form horizontal -->
Expand Down
41 changes: 21 additions & 20 deletions service/static/js/rest_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,26 +277,27 @@ $(function () {
// Change Availability
// ****************************************

$("#availability-btn").click(function () {
// let product_id = $("#product_id").val();

// $("#flash_message").empty();

// let ajax = $.ajax({
// type: "DELETE",
// url: `/products/${product_id}`,
// contentType: "application/json",
// data: '',
// })

// ajax.done(function(res){
// clear_form_data()
// flash_message("Product has been Deleted!")
// });

// ajax.fail(function(res){
// flash_message("Server error!")
// });
$("#change_availability-btn").click(function () {
let product_id = $("#product_id").val();

$("#flash_message").empty();

let ajax = $.ajax({
type: "PUT",
url: `/products/${product_id}/change_availability`,
contentType: "application/json",
data: '',
})

ajax.done(function(res){
update_form_data(res)
flash_message(res.message)
});

ajax.fail(function(res){
clear_form_data()
flash_message(res.responseJSON.message)
});
});


Expand Down
Loading