Question
Please help me with this. the question is : Define a pytest test class 'TestingInventoryCreation' which tests the behavior of the '__init__' method with the
Please help me with this.
the question is :
- Define a pytest test class'TestingInventoryCreation'which tests the behavior of the'__init__'method with the following tests:
- Define a pytest test method'test_creating_empty_inventory', which creates an empty inventory and checks if its 'balance_inventory' is an empty dict.
- Define a pytest test method'test_creating_specified_inventory', which creates an inventory instance with input {'iPhone Model X':100, 'Xiaomi Model Y': 1000, 'Nokia Model Z':25}.
- Define a pytest test method'test_creating_inventory_with_list', which checks if the'__init__'method raises a TypeError with the message "Input inventory must be a dictionary" when a list "['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z']" is passed as input.
- Define a pytest test method'test_creating_inventory_with_numeric_keys', which checks if the'__init__'method raises a ValueError with the message "Mobile model name must be a string", when the dict {1:'iPhone Model X', 2:'Xiaomi Model Y', 3:'Nokia Model Z'} is passed as input.
- Define a pytest test method'test_creating_inventory_with_nonnumeric_values', which checks if the'__init__'method raises a ValueError with the message "No. of mobiles must be a positive integer" when the dict {'iPhone Model X':'100', 'Xiaomi Model Y': '1000', 'Nokia Model Z':'25'} is passed as input.
- Define a pytest test method'test_creating_inventory_with_negative_value', which checks if the'__init__'method raises a ValueError with the message "No. of mobiles must be a positive integer" when the dict {'iPhone Model X':-45, 'Xiaomi Model Y': 200, 'Nokia Model Z':25} is passed as input.
I have solved the first step:
class MobileInventory:
balance_inventory = 'None'
def __init__(self,inventory={}):
if type(inventory) is not dict:
raise TypeError("Input inventory must be a dictionary")
for key in inventory:
if type(key) is not str:
raise ValueError("Mobile model name must be a string")
if type(inventory[key]) is int:
if inventory[key] < 0:
raise ValueError("No. of mobiles must be a positive integer")
else:
raise ValueError("No. of mobiles must be a positive integer")
self.balance_inventory = inventory
def add_stock(self, new_stock):
if type(new_stock) is not dict:
raise TypeError("Input inventory must be a dictionary")
for key in new_stock:
if type(key) is not str:
raise ValueError("Mobile model name must be a string")
if type(new_stock[key]) is int:
if new_stock[key] < 0:
raise ValueError("No. of mobiles must be a positive integer")
else:
raise ValueError("No. of mobiles must be a positive integer")
self.balance_inventory.update(new_stock)
def sell_stock(self,requested_stock):
if type(requested_stock) is not dict:
raise TypeError("Input inventory must be a dictionary")
for key in requested_stock:
if type(key) is not str:
raise ValueError("Mobile model name must be a string")
if type(requested_stock[key]) is int:
if requested_stock[key] < 0:
raise ValueError("No. of mobiles must be a positive integer")
else:
raise ValueError("No. of mobiles must be a positive integer")
try:
if self.balance_inventory[key] < requested_stock[key]:
raise ValueError("Insufficient Stock")
except:
raise ValueError("No Stock. New Model Request")
self.balance_inventory[key] =- requested_stock[key]
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started