0% found this document useful (0 votes)
269 views

Hands On Python Qualis Pytest

1) The document defines a MobileInventory class to manage inventory of mobile models and quantities. It allows initializing inventory, adding new stock, and selling stock while validating data types and values. 2) Test classes are defined to test the initialization, stock addition, and stock selling methods. They include tests that validate exception raising for invalid data types or values. 3) The tests utilize the Pytest framework and assertions to validate the MobileInventory class functions as expected under different conditions.

Uploaded by

TECHer YT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views

Hands On Python Qualis Pytest

1) The document defines a MobileInventory class to manage inventory of mobile models and quantities. It allows initializing inventory, adding new stock, and selling stock while validating data types and values. 2) Test classes are defined to test the initialization, stock addition, and stock selling methods. They include tests that validate exception raising for invalid data types or values. 3) The tests utilize the Pytest framework and assertions to validate the MobileInventory class functions as expected under different conditions.

Uploaded by

TECHer YT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

------------------------------------------------ HANDS ON PYTEST

class InsufficientException(Exception):

pass

class MobileInventory:

def __init__(self, inventory={}):

if type(inventory) != dict:

raise TypeError("Input inventory must be a dictionary")

elif inventory == {}:

self.balanced_inventory = inventory

elif inventory != {}:

for i in inventory.keys():

if type(i) != str:

raise ValueError("Mobile model name must be a string")

for i in inventory.values():

if type(i) != int or i < 0:

raise ValueError("No. of mobiles must be a positive integer")

self.balanced_inventory = inventory

def add_stock(self, new_stock):

if type(new_stock) != dict:

raise TypeError("Input stock must be a dictionary")

elif new_stock != {}:

for i in new_stock.keys():

if type(i) != str:

raise ValueError("Mobile model name must be a string")

for i in new_stock.values():

if type(i) != int or i < 0:

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
raise ValueError("No. of mobiles must be a positive integer")

self.balanced_inventory.update(new_stock)

def sell_stock(self, requested_stock):

if type(requested_stock) != dict:

raise TypeError("Input stock must be a dictionary")

elif requested_stock != {}:

mobileName = []

mobileQuantity = []

for i in requested_stock.keys():

if type(i) != str:

raise ValueError("Mobile model name must be a string")

for i in requested_stock.values():

if type(i) != int or i < 0:

raise ValueError("No. of mobiles must be a positive integer")

for i in requested_stock.keys():

if i not in self.balanced_inventory.keys():

raise InsufficientException("No Stock. New Model Request")

if self.balanced_inventory[i] < requested_stock[i]:

raise InsufficientException("Insufficient Stock")

else:

self.balanced_inventory[i] = (

self.balanced_inventory[i] - requested_stock[i]

PART 2

from proj.inventory import MobileInventory, InsufficientException

import pytest

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
class TestingInventoryCreation:

def test_creating_empty_inventory(self):

c1 = MobileInventory({})

assert c1.balanced_inventory == {}

def test_creating_specified_inventory(self):

c2 = MobileInventory(

{"iPhone Model X": 100, "Xiaomi Model Y": 1000, "Nokia Model Z": 25}

assert c2.balanced_inventory == {

"iPhone Model X": 100,

"Xiaomi Model Y": 1000,

"Nokia Model Z": 25,

def test_creating_inventory_with_list(self):

with pytest.raises(TypeError) as excinfo:

c3 = MobileInventory(["iPhone Model X", "Xiaomi Model Y", "Nokia Model Z"])

assert "Input inventory must be a dictionary" in str(excinfo.value)

def test_creating_inventory_with_numeric_keys(self):

with pytest.raises(ValueError) as excinfo:

c4 = MobileInventory(

{1: "iPhone Model X", 2: "Xiaomi Model Y", 3: "Nokia Model Z"}

assert "Mobile model name must be a string" in str(excinfo.value)

def test_creating_inventory_with_nonnumeric_values(self):

with pytest.raises(ValueError) as excinfo:

c5 = MobileInventory(

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
{

"iPhone Model X": "100",

"Xiaomi Model Y": "1000",

"Nokia Model Z": "25",

assert "No. of mobiles must be a positive integer" in str(excinfo.value)

def test_creating_inventory_with_negative_value(self):

with pytest.raises(ValueError) as excinfo:

c6 = MobileInventory(

{"iPhone Model X": -45, "Xiaomi Model Y": 200, "Nokia Model Z": 25}

assert "No. of mobiles must be a positive integer" in str(excinfo.value)

PART 3

class TestInventoryAddStock():

def setup_class(cls):

cls.inventory = MobileInventory({'iPhone Model X': 100, 'Xiaomi Model Y': 1000, 'Nokia Model
Z':25})

def test_add_new_stock_as_dict(self):

with pytest.raises(TypeError) :

MobileInventory.add_stock(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])

def test_add_new_stock_as_list(self):

with pytest.raises(TypeError) as excinfo:

MobileInventory(['iPhone Model X', 'Xiaomi Model Y', 'Nokia Model Z'])

assert "Input inventory must be a dictionary" in str(excinfo.value)

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
def test_add_new_stock_with_numeric_keys(self):

with pytest.raises(ValueError) as excinfo:

MobileInventory({1:'iPhone Model A', 2:'Xiaomi Model B', 3:'Nokia Model C'})

assert "Mobile model name must be a string" in str(excinfo.value)

def test_add_new_stock_with_nonnumeric_values(self):

with pytest.raises(ValueError) as excinfo:

MobileInventory({'iPhone Model A':'50', 'Xiaomi Model B': '2000', 'Nokia ModelC':'25'})

assert "No. of mobiles must be a positive integer" in str(excinfo.value)

def test_add_new_stock_with_float_values(self):

with pytest.raises(ValueError) as excinfo:

MobileInventory({'iPhone Model A':50.5, 'Xiaomi Model B':2000.3, 'Nokia Model C':25})

assert "No. of mobiles must be a positive integer" in str(excinfo.value)

class TestInventorySellStock:

inventory = None

@classmethod

def setup_class(cls):

cls.inventory = MobileInventory(

"iPhone Model A": 50,

"Xiaomi Model B": 2000,

"Nokia Model C": 10,

"Sony Model D": 1,

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
def test_sell_stock_as_dict(self):

self.inventory.sell_stock(

{"iPhone Model A": 2, "Xiaomi Model B": 20, "Sony Model D": 1}

def test_sell_stock_as_list(self):

with pytest.raises(TypeError):

MobileInventory.sell_stock(

self, ["iPhone Model A", "Xiaomi Model B", "Nokia Model C"]

def test_sell_stock_with_numeric_keys(self):
with pytest.raises(ValueError):

MobileInventory.sell_stock(

self, {1: "iPhone Model A", 2: "Xiaomi Model B", 3: "Nokia Model C"}

def test_sell_stock_with_nonnumeric_values(self):

with pytest.raises(ValueError):

MobileInventory.sell_stock(

self,

{"iPhone Model A": "5", "Xiaomi Model B": "3", "Nokia Model C": "4"},

def test_sell_stock_of_nonexisting_model(self):

with pytest.raises(InsufficientException, match="No Stock. New Model Request"):

self.inventory.sell_stock({"iPhone Model B": 2, "Xiaomi Model B": 5})

def test_sell_stock_of_insufficient_stock(self):

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
with pytest.raises(InsufficientException, match="Insufficient Stock"):

self.inventory.sell_stock(

{"iPhone Model A": 2, "Xiaomi Model B": 5, "Nokia Model C": 15}

This study source was downloaded by 100000832806195 from CourseHero.com on 01-10-2022 00:37:01 GMT -06:00

https://www.coursehero.com/file/72269612/HANDS-ON-PYTHON-QUALIS-PYTESTdocx/
Powered by TCPDF (www.tcpdf.org)

You might also like