You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The provided solution.py wrongly implements order validation. Currently, the outcome depends on how items are arranged in an order.
How to reproduce
Just add the following test case to the tests.py file and change line number 2 to import solution instead of code. Order 1 will be properly detected as exceeding the allowed maximum amount, but Order 2 will be wrongly accepted.
# Example 4 - order validation shouldn't depend on ordering of items
def test_4(self):
num_items = 12
items = [c.Item(type='product', description='tv', amount=99999, quantity=num_items)]
for i in range(num_items):
items.append(c.Item(type='payment', description='invoice_' + str(i), amount=99999, quantity=1))
order_1 = c.Order(id='1', items=items)
self.assertEqual(c.validorder(order_1), 'Total amount exceeded')
# Put payments before products
items = items[1:] + [items[0]]
order_2 = c.Order(id='2', items=items)
self.assertEqual(c.validorder(order_2), 'Total amount exceeded')
How to Fix this Bug
There are couple of possibilities as enrolled below:
Use separate variables for total payments and expenses; use the latter for checking whether the total maximum amount is exceeded or not.
Handle products before payments.
The text was updated successfully, but these errors were encountered:
Thanks for pointing out through this amazing explanation! The goal was to show a bug that many people might not be aware about, so the proposed fixes are good to haves too, but they were not the initial priority. Please feel free to submit a PR that closes this issue so learners can learn even more :-)
Summary
The provided
solution.py
wrongly implements order validation. Currently, the outcome depends on how items are arranged in an order.How to reproduce
Just add the following test case to the
tests.py
file and change line number 2 to importsolution
instead ofcode
. Order 1 will be properly detected as exceeding the allowed maximum amount, but Order 2 will be wrongly accepted.How to Fix this Bug
There are couple of possibilities as enrolled below:
The text was updated successfully, but these errors were encountered: