-
Notifications
You must be signed in to change notification settings - Fork 224
Cm/dictionary version #1
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,17 @@ | ||
| from grocery_store.product import Product | ||
| from grocery_store.order import Order | ||
| from grocery_store.product import create_product | ||
| from grocery_store.order import create_order, calculate_total, add_product | ||
|
|
||
|
|
||
| kamala_order = Order([], "Kamala") | ||
| kamala_order = create_order([], "Kamala") | ||
|
|
||
| product_1 = Product("Apples", 3.47) | ||
| product_2 = Product("Oranges", 4.89) | ||
| product_3 = Product("Macbook", 1379.99) | ||
| product_1 = create_product("Apples", 3.47) | ||
| product_2 = create_product("Oranges", 4.89) | ||
| product_3 = create_product("Macbook", 1379.99) | ||
|
|
||
| kamala_order.add_product(product_1) | ||
| kamala_order.add_product(product_2) | ||
| kamala_order.add_product(product_3) | ||
| add_product(kamala_order, product_1) | ||
| add_product(kamala_order, product_2) | ||
| add_product(kamala_order, product_3) | ||
|
|
||
| print(f"The order total for {kamala_order.customer_name} is " + | ||
| f"{kamala_order.calculate_total()}") | ||
| print(f"The order total for {kamala_order['customer_name']} is " + | ||
| f"{calculate_total(kamala_order)}") | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,18 @@ | ||
| class Order: | ||
| def __init__(self, products, customer_name): | ||
| self.products = products | ||
| self.customer_name = customer_name | ||
|
|
||
| def add_product(self, product): | ||
| self.products.append(product) | ||
|
|
||
| def calculate_total(self): | ||
| total = 0 | ||
| for i in range(1, len(self.products)): | ||
| total += self.products[i].price | ||
|
|
||
| return total | ||
|
|
||
|
|
||
| def create_order(products, customer_name): | ||
| return { | ||
| "customer_name": customer_name, | ||
| "products": products, | ||
| } | ||
|
|
||
| def add_product(order, product): | ||
| order["products"].append(product) | ||
|
|
||
| def calculate_total(order): | ||
| total = 0 | ||
| for i in range(1, len(order["products"])): | ||
| total += order["products"][i]["price"] | ||
|
|
||
|
|
||
| return total |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| class Product: | ||
| def __init__(self, name, price): | ||
| self.name = name | ||
| self.price = name | ||
|
|
||
| def create_product(name, price): | ||
| return { | ||
| "name": name, | ||
| "price": name, | ||
| } | ||
|
|
||
| def __str__(self): | ||
| return f"{self.name} - ${self.price}" | ||
| def print_product(product): | ||
| return f"{product['name']} - ${product['price']}" | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| attrs==20.3.0 | ||
| iniconfig==1.1.1 | ||
| packaging==20.9 | ||
| pluggy==0.13.1 | ||
| py==1.10.0 | ||
| pyparsing==2.4.7 | ||
| pytest==6.2.3 | ||
| toml==0.10.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,73 +1,73 @@ | ||
| import pytest | ||
| from grocery_store.order import Order | ||
| from grocery_store.product import Product | ||
| from grocery_store.order import create_order, calculate_total, add_product | ||
| from grocery_store.product import create_product | ||
|
|
||
| def test_order_creation(): | ||
| # Arrange | ||
| product_one = Product("Trombone", 478.95) | ||
| product_two = Product("Guitar", 327.99) | ||
| product_three = Product("Triangle", 2) | ||
| product_one = create_product("Trombone", 478.95) | ||
| product_two = create_product("Guitar", 327.99) | ||
| product_three = create_product("Triangle", 2) | ||
| products = [product_one, product_two, product_three] | ||
|
|
||
| # Act | ||
| sample_order = Order(products, "Shona Frederick") | ||
| sample_order = create_order(products, "Shona Frederick") | ||
|
|
||
| # Assert | ||
| assert len(sample_order.products) == 3 | ||
| assert sample_order.customer_name == "Shona Frederick" | ||
| assert len(sample_order["products"]) == 3 | ||
| assert sample_order["customer_name"] == "Shona Frederick" | ||
| for product in products: | ||
| assert product in sample_order.products | ||
| assert product in sample_order["products"] | ||
|
|
||
| def test_order_creation_with_no_products(): | ||
| # Arrange | ||
| products = [] | ||
|
|
||
| # Act | ||
| sample_order = Order(products, "Shona Frederick") | ||
| sample_order = create_order(products, "Shona Frederick") | ||
|
|
||
| # Assert | ||
| assert len(sample_order.products) == 0 | ||
| assert sample_order.customer_name == "Shona Frederick" | ||
| assert len(sample_order["products"]) == 0 | ||
| assert sample_order["customer_name"] == "Shona Frederick" | ||
|
|
||
| def test_add_product(): | ||
| # Arrange | ||
| product_one = Product("Trombone", 478.95) | ||
| product_two = Product("Guitar", 327.99) | ||
| product_three = Product("Triangle", 2) | ||
| product_one = create_product("Trombone", 478.95) | ||
| product_two = create_product("Guitar", 327.99) | ||
| product_three = create_product("Triangle", 2) | ||
| products = [product_one, product_two, product_three] | ||
| sample_order = Order(products, "Shona Frederick") | ||
| sample_order = create_order(products, "Shona Frederick") | ||
|
|
||
| new_product = Product("Bass", 115) | ||
| new_product = create_product("Bass", 115) | ||
|
|
||
| # Act | ||
| sample_order.add_product(new_product) | ||
| add_product(sample_order, new_product) | ||
|
|
||
| # Assert | ||
| assert len(sample_order.products) == 4 | ||
| assert new_product in sample_order.products | ||
| assert len(sample_order["products"]) == 4 | ||
| assert new_product in sample_order["products"] | ||
|
|
||
| def test_calculate_total_with_no_products(): | ||
| # Arrange | ||
| order = Order([], "Shona Frederick") | ||
| order = create_order([], "Shona Frederick") | ||
|
|
||
| # Act | ||
| total = order.calculate_total() | ||
| total = calculate_total(order) | ||
|
|
||
| # Assert | ||
| assert total == 0 | ||
|
|
||
| def test_calculate_total_with_multiple_products(): | ||
| # Arrange | ||
| product_one = Product("Trombone", 478.95) | ||
| product_two = Product("Guitar", 327.99) | ||
| product_three = Product("Triangle", 2) | ||
| product_one = create_product("Trombone", 478.95) | ||
| product_two = create_product("Guitar", 327.99) | ||
| product_three = create_product("Triangle", 2) | ||
| products = [product_one, product_two, product_three] | ||
|
|
||
| order = Order(products, "Shona Frederick") | ||
| order = create_order(products, "Shona Frederick") | ||
|
|
||
|
|
||
| # Act | ||
| total = order.calculate_total() | ||
| total = calculate_total(order) | ||
|
|
||
| # Assert | ||
| assert total == pytest.approx(478.95 + 327.99 + 2) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.