ruby
ruby copied to clipboard
[Boutique Inventory] Counts all stock that exists
This corrects a bug that would not account for all items in some inventory that would otherwise pass the tests.
Incoming fly-by e-mail patch submitted, but I can not approve.
I can't see what this is testing. Do you have an example of code that previously was incorrectly passing pls?
It is testing that we indeed get a total count for some products. Student code was passing but would not pass with this additional test.
def total_stock
stock = items.map { |item| item[:quantity_by_size] }.first
stock.nil? || stock.empty? ? OUT_OF_STOCK : stock.each_value.inject(:+)
end
Would pass the current tests, but would fail this situation as presented in the tests. The test was really testing that we ot the total in the first listed type of product, rather than the indicated "total of some products".
The additional test drove this solution for this student:
' def total_stock
items.inject(0) do |counter, item|
counter + item[:quantity_by_size].inject(0) { |sum, values| sum + values.last }
end
end