TestInventoryService.test_get_inventory_by_condition and TestInventoryService.test_get_inventory_by_stock_level.
Description of the failing test:
The test logic is flawed. It wrongly assumes that creating multiple random inventory items will not produce a duplicate condition or stock level and then checks that only 1 was returned from the query. This is incorrect whenever there is a duplicate condition or stock level in the data.
To fix this, we must calculate the actual number and test for it after the query. For example:
inventory_items = self._create_inventory(3)
test_condition = inventory_items[0].condition.value
condition_count = len(
[item for item in inventory_items if item.condition.value == test_condition]
)
This counts the number of conditions that match the first condition which will be one or more and saves it in a variable called condition_count. Then the assertion should test that the number returned from the query matches the actual number in the dataset:
Two test cases are failing:
TestInventoryService.test_get_inventory_by_condition
andTestInventoryService.test_get_inventory_by_stock_level
.Description of the failing test:
The test logic is flawed. It wrongly assumes that creating multiple random inventory items will not produce a duplicate condition or stock level and then checks that only 1 was returned from the query. This is incorrect whenever there is a duplicate condition or stock level in the data. To fix this, we must calculate the actual number and test for it after the query. For example:
This counts the number of conditions that match the first condition which will be one or more and saves it in a variable called
condition_count
. Then the assertion should test that the number returned from the query matches the actual number in the dataset:This will always test for the correct number.