def test_item_info_not_found(cls): """ Test to validate that an item not found when it doesn't exist in the inventory returns the correct message """ main.FULL_INVENTORY = { 'CORN': { 'product_code': 'CORN', 'description': 'This is corn', 'market_price': 24, 'rental_price': 50 }, 'WATER': { 'product_code': 'WATER', 'description': 'This is water', 'market_price': 40, 'rental_price': 0, } } with patch('builtins.input', side_effect='WHEAT'): with patch('sys.stdout', new=io.StringIO()) as actual_result: main.item_info() expected_result_string = ["Item not found in inventory"] for expected in expected_result_string: assert not actual_result.getvalue().find(expected)
def test_item_info_found(cls): """ Test to validate that an item can be found when it exists in the inventory """ main.FULL_INVENTORY = { 'CORN': { 'product_code': 'CORN', 'description': 'This is corn', 'market_price': 24, 'rental_price': 50 }, 'WATER': { 'product_code': 'WATER', 'description': 'This is water', 'market_price': 40, 'rental_price': 0, } } with patch('builtins.input', side_effect='CORN'): with patch('sys.stdout', new=io.StringIO()) as actual_result: main.item_info() expected_result_strings = [ "productCode:CORN", "description:This is corn", "marketPrice:24", "rentalPrice:50" ] for expected in expected_result_strings: assert actual_result.getvalue().find(expected)
def test_item_info(self): """Test item info found in main module.""" m.FULL_INVENTORY = { '1': { 'item_code': 1, 'description': 'Knife Set', 'market_price': 100, 'rental_price': 10 } } # Expected output_found = 'item_code: 1\n' \ 'description: Knife Set\n' \ 'market_price: 100\n' \ 'rental_price: 10\n' output_not_found = 'Item not found in inventory\n' # Actual - Item Found with patch('builtins.input', side_effect='1'): with patch('sys.stdout', new_callable=io.StringIO) as real_out: m.item_info() self.assertEqual(output_found, real_out.getvalue()) # Actual - Item Not Found with patch('builtins.input', side_effect='2'): with patch('sys.stdout', new_callable=io.StringIO) as real_out: m.item_info() self.assertEqual(output_not_found, real_out.getvalue())
def test_item_info(self): with patch ('builtins.input', side_effect =['444']): main.FULL_INVENTORY={} self.assertEqual(main.item_info(), print("Item not found in inventory")) with patch ('builtins.input', side_effect =['444']): main.FULL_INVENTORY = {'444': {'description': 'bed', 'market_price': 180.0, 'product_code': '444', 'rental_price': 150, 'material': 'wood', 'size': 'L'}} expected = {'description': 'bed', 'market_price': 180.0, 'product_code': '444', 'rental_price': 150, 'material': 'wood', 'size': 'L'} self.assertEqual(main.item_info(), print(expected)) self.assertEqual(main.FULL_INVENTORY['444'], expected) with patch('builtins.input', side_effect='111'): self.assertEqual(main.item_info(), print("Item not found in inventory"))
def test_integration(self): """tests the various functions in main""" main.FULL_INVENTORY = { } #make sure I have a clear inventory to work with #add an appliance input_appliance = ('1', 'tv', 25, 'n', 'y', 'Samsung', '60') with patch('builtins.input', side_effect=input_appliance): main.add_new_item() #add furniture input_furniture = ('2', 'table', 50, 'y', 'wood', 'L') with patch('builtins.input', side_effect=input_furniture): main.add_new_item() #add a miscellaneous item input_inventory = ('3', 'vase', 10, 'n', 'n') with patch('builtins.input', side_effect=input_inventory): main.add_new_item() #use item_info function to look for an object with patch('builtins.input', side_effect='2'): with patch('sys.stdout', new=io.StringIO()) as actual_result: main.item_info() test_value = '''product_code:2 description:table market_price:24 rental_price:50 material:wood size:L ''' self.assertEqual(actual_result.getvalue(), test_value)
def test_item_info_print(self): """test the item info function with printed results""" main.FULL_INVENTORY = { '2': { 'product_code': '2', 'description': 'table', 'market_price': 24, 'rental_price': 50, 'material': 'wood', 'size': 'L' }, '3': { 'product_code': '3', 'description': 'vase', 'market_price': 24, 'rental_price': 10, } } with patch('builtins.input', side_effect='2'): with patch('sys.stdout', new=io.StringIO()) as actual_result: main.item_info() test_value = '''product_code:2 description:table market_price:24 rental_price:50 material:wood size:L ''' self.assertEqual(actual_result.getvalue(), test_value)
def test_item_info_not_found(self): expected_output = 'Item not found in inventory\n' user_input = ['xyz'] with patch('builtins.input', side_effect=user_input): with patch('sys.stdout', new=io.StringIO()) as actual_result: main.item_info() self.assertEqual(actual_result.getvalue(), expected_output)
def test_add_new_furniture_item(): with patch('builtins.input', side_effect=[ '62', 'Couch', '2000', 'Y', 'Leather', 'Sectional' ]): main.add_new_item() with patch('builtins.input', return_value='62'): main.item_info()
def test_item_not_found(self): # Test non existent inventory item item = ['004'] with patch('builtins.input', side_effect=item): with patch('sys.stdout', new=io.StringIO()) as output_string: item_info() self.assertEqual(output_string.getvalue(), 'Item not found in inventory\n')
def test_item_info_none(self): """Tests the program on returning None when given an invalid item code.""" target = 'Item not found in inventory\n' with patch('builtins.input', side_effect=['turds']): with patch('sys.stdout', new=io.StringIO()) as print_result: item_info() self.assertEqual(target, print_result.getvalue())
def test_item_info_none(self): """Check expected 'not found' statement.""" test_none_string = 'Item not found in inventory\n' with patch('builtins.input', side_effect='NOT_USED'): with patch('sys.stdout', new=io.StringIO()) as print_out: item_info() self.assertEqual(print_out.getvalue(), test_none_string)
def test_add_new_electric_appliance_item(): with patch('builtins.input', side_effect=[ '60', 'oven', '1800', 'N', 'Y', 'ShittyOven', '66' ]): main.add_new_item() with patch('builtins.input', return_value='60'): main.item_info()
def test_add_new_item(self): """ tests add new items to inventory inv == itemcode,descr,rentalprice,isFurn,isAppl furn == itemcode,descr,rentalprice,isFurn,isAppl,material,size appl == itemcode,descr,rentalprice,isFurn,isAppl,brand,voltage """ item_inv = ['1', 'Shirt', '50', 'n', 'n'] item_furn = ['2', 'Chair', '100', 'y', 'wood', 'L'] item_appl = ['3', 'Washer', '200', 'n', 'y', 'Maytag', '120'] with patch('builtins.input', side_effect=item_inv): main.add_new_item() with patch('builtins.input', side_effect=item_furn): main.add_new_item() with patch('builtins.input', side_effect=item_appl): main.add_new_item() test_dict = { '1': { 'product_code': '1', 'description': 'Shirt', 'market_price': 24, 'rental_price': '50' }, '2': { 'product_code': '2', 'description': 'Chair', 'market_price': 24, 'rental_price': '100', 'material': 'wood', 'size': 'L' }, '3': { 'product_code': '3', 'description': 'Washer', 'market_price': 24, 'rental_price': '200', 'brand': 'Maytag', 'voltage': '120' } } self.assertEqual(test_dict, main.return_full_inventory()) """ Test get_price """ self.assertEqual(main.get_price('test'), 24) """ test get inventory item info """ with patch('builtins.input', side_effect='2'): function = main.item_info() info = '\n'.join( ('product_code:2', 'description:Chair', 'market_price:24', 'rental_price:100', 'material:wood', 'size:L')) self.assertEqual(function, info) """ tests item info function when item code is not in the inventory """ with patch('builtins.input', side_effect='999'): function = main.item_info() self.assertEqual(function, 'Item not found in inventory') """ tests exit program """ with self.assertRaises(SystemExit): main.exit_program()
def test_main(self): ''' tests a typical sequence of a user entering two new items each mock starts with user input 1 for add new item to inventory followed by details for a piece of furniture then appliance lastly a mock looks up item info for item 001 ''' with patch('builtins.input', side_effect=('1', '001', 'bench', 63, 'y', 'wood', 'XL')): main.main_menu()() self.assertEqual( main.FULL_INVENTORY, { '001': { 'product_code': '001', 'description': 'bench', 'market_price': 24, 'rental_price': 63, 'material': 'wood', 'size': 'XL' } }) with patch('builtins.input', side_effect=('1', '024', 'tv', 299, 'n', 'y', 'tvsrus', 110)): with patch('inventory_management.market_prices.get_latest_price', return_value=859): main.main_menu()() self.assertEqual( main.FULL_INVENTORY, { '001': { 'product_code': '001', 'description': 'bench', 'market_price': 24, 'rental_price': 63, 'material': 'wood', 'size': 'XL' }, '024': { 'product_code': '024', 'description': 'tv', 'market_price': 859, 'rental_price': 299, 'brand': 'tvsrus', 'voltage': 110 } }) with patch('builtins.input', side_effect=['001']): with patch('sys.stdout', new=io.StringIO()) as input_result: main.item_info() self.assertEqual( input_result.getvalue(), 'product_code:001\n' 'description:bench\n' 'market_price:24\n' 'rental_price:63\n' 'material:wood\n' 'size:XL\n')
def test_item_info_not_found(self): myinputs = [3] with patch('builtins.input', side_effect=myinputs): captured = io.StringIO() sys.stdout = captured item_info() sys.stdout = sys.__stdout__ mystring = 'Item not found in inventory\n' self.assertEqual(captured.getvalue(), mystring)
def test_item_info(self): '''Tests the item_info function''' test_prods = { 'I123': { 'product_code': 'I123', 'description': 'Thingabob', 'market_price': 25.99, 'rental_price': 4.99 }, 'C234': { 'product_code': 'C234', 'description': 'chair', 'market_price': 599.99, 'rental_price': 39.99, 'material': 'wood', 'size': 'X-Large' }, 'E345': { 'product_code': 'E345', 'description': 'refrigerator', 'market_price': 1595.99, 'rental_price': 145.99, 'brand': 'GE', 'voltage': 110 } } main.FULL_INVENTORY = test_prods # Tests the item info function for an inventory item market_prices.get_latest_price = MagicMock(retun_value=25.99) with patch('builtins.input', side_effect=['I123']): self.assertEqual(main.item_info(), print(test_prods.get('I123'))) self.assertEqual(main.FULL_INVENTORY['I123'], test_prods.get('I123')) # Tests the item info function for a furniture item market_prices.get_latest_price = MagicMock(retun_value=599.99) with patch('builtins.input', side_effect=['C234']): self.assertEqual(main.item_info(), print(test_prods.get('C234'))) self.assertEqual(main.FULL_INVENTORY['C234'], test_prods.get('C234')) # Tests the item info function for an electrical appliance item market_prices.get_latest_price = MagicMock(retun_value=1595.99) with patch('builtins.input', side_effect=['E345']): self.assertEqual(main.item_info(), print(test_prods.get('E345'))) self.assertEqual(main.FULL_INVENTORY['E345'], test_prods.get('E345')) # Tests the item info for correct return of an item not in the inventory with patch('builtins.input', side_effect=['Z789']): self.assertEqual(main.item_info(), print('Item not found in inventory'))
def test_item_info(self): reg_item = ['10', 'reg_item', '10.00', 'n', 'n'] with mock.patch('builtins.input', side_effect=reg_item): main.add_new_item() with mock.patch('builtins.input', side_effect=['10']): main.item_info() with mock.patch('builtins.input', side_effects=['0001']): main.item_info()
def test_module(self): '''build a inventory and then test retrieval of items''' main.FULL_INVENTORY = {} first_item = ['SOFA', 'Sectional', 100, 'n', 'n'] new_e_item = ['MICWV', 'Microwave', 10, 'n', 'y', 'Whirlpool', '1000V'] new_f_item = ['DINE_TBL', 'Dining Table', 50, 'y', 'wood', 'L'] test_inventory = { 'SOFA': { 'description': 'Sectional', 'market_price': 24, 'product_code': 'SOFA', 'rental_price': 100 }, 'DINE_TBL': { 'description': 'Dining Table', 'market_price': 24, 'material': 'wood', 'product_code': 'DINE_TBL', 'rental_price': 50, 'size': 'L' }, 'MICWV': { 'brand': 'Whirlpool', 'description': 'Microwave', 'market_price': 24, 'product_code': 'MICWV', 'rental_price': 10, 'voltage': '1000V' } } get_item_sofa = 'product_code:SOFA\ndescription:Sectional\n' \ 'market_price:24\nrental_price:100\n' get_item_micwv = 'product_code:MICWV\ndescription:Microwave\n' \ 'market_price:24\nrental_price:10\n' \ 'brand:Whirlpool\nvoltage:1000V\n' with patch('builtins.input', side_effect=first_item): main.add_new_item() with patch('builtins.input', side_effect=new_e_item): main.add_new_item() with patch('builtins.input', side_effect=new_f_item): main.add_new_item() self.assertEqual(main.FULL_INVENTORY, test_inventory) with patch('builtins.input', side_effect=['SOFA']): with patch('sys.stdout', new=StringIO()) as result: main.item_info() self.assertEqual(result.getvalue(), get_item_sofa) with patch('builtins.input', side_effect=['MICWV']): with patch('sys.stdout', new=StringIO()) as result: main.item_info() self.assertEqual(result.getvalue(), get_item_micwv)
def test_multi_creation_lookup(self, mock_input): with patch('sys.stdout', new=StringIO()) as fake_output: main.get_latest_price = MagicMock(return_value='FakePrice') main.add_new_item() assert main.FULL_INVENTORY == inventory_elec main.add_new_item() assert main.FULL_INVENTORY == inventory_elec_furn main.add_new_item() assert main.FULL_INVENTORY == inventory_elec_furn_gen main.item_info() assert fake_output.getvalue().strip() == stdout_statement main.FULL_INVENTORY = {}
def test_item_info(self): myinputs = [1, "Table", 25, "y", "Wood", "M"] with patch('builtins.input', side_effect=myinputs): add_new_item() myinputs = [1] with patch('builtins.input', side_effect=myinputs): captured = io.StringIO() sys.stdout = captured item_info() sys.stdout = sys.__stdout__ mystring = 'product_code:1\ndescription:Table\nmarket_price:24\nrental_price:25\nmaterial:Wood\nsize:M\n' self.assertEqual(captured.getvalue(), mystring)
def test_inventory_system(self): """Test the main menu selections """ furniture_list = ["2", "Couch", "100", "Y", "leather", "8ft"] inventory_dict = { "2": { "product_code": "2", "description": "Couch", "market_price": 1500, "rental_price": "100", "material": "leather", "size": "8ft" } } item_found_result = "product_code:2\ndescription:Couch\nmarket_price:1500" \ "\nrental_price:100\nmaterial:leather\nsize:8ft" item_not_found_result = "Item not found in inventory" # Add an item to the full inventory with patch('builtins.input', side_effect='1'): main.main_menu() with patch('inventory_management.market_prices.get_latest_price', return_value=1500): with patch('builtins.input', side_effect=furniture_list): main.add_new_item() # Test for the furniture item in the full inventory dict self.assertEqual(inventory_dict.get("2"), main.FULL_INVENTORY.get("2")) # Find the item just added in the full inventory with patch('builtins.input', side_effect='2'): main.main_menu() with patch('sys.stdout', new=StringIO()) as test_output: with patch('builtins.input', side_effect=["2"]): main.item_info() self.assertEqual(item_found_result, test_output.getvalue().strip()) # Find an item not in the full inventory with patch('builtins.input', side_effect='2'): main.main_menu() with patch('sys.stdout', new=StringIO()) as test_output: with patch('builtins.input', side_effect=["3"]): main.item_info() self.assertEqual(item_not_found_result, test_output.getvalue().strip()) # Quit the program with patch('builtins.input', side_effect='q'): main.main_menu() with self.assertRaises(SystemExit): main.exit_program()
def test_itemInfo(self): """ Tests item info function """ with mock.patch('builtins.input', side_effect = ['300', '400']): FULL_INVENTORY = {'300':{'product_code': '300', 'description': 'shirt', 'market_price': 24, 'rental_price': '500'}} item_info() self.assertIn('300', FULL_INVENTORY.keys()) item_info() self.assertNotIn('400', FULL_INVENTORY.keys())
def test_item_info_not_found(self): """ Test getting item info when the item is not in the inventory """ # Reset the FULL_INVENTORY dict main.FULL_INVENTORY = {} expected_result = "Item not found in inventory" # Need to capture stdout to beable to compare it with the expected output with patch('sys.stdout', new=StringIO()) as test_output: with patch('builtins.input', side_effect=["15"]): main.item_info() self.assertEqual(expected_result, test_output.getvalue().strip())
def test_item_info(self): # NOTE: string '001' is an iterable, then side_effect() will return # one item from the iterable whenever the patched function is called. # In below example, one item is '0' not the '001', so we need to # make the string a list ['001']. with patch('builtins.input', side_effect=['001']): with patch('builtins.print') as fakeoutput: main.item_info()# print the information of item 001 fakeoutput.assert_called_with(main.FULL_INVENTORY[0]) with patch('builtins.input', side_effect=['008']): with patch('builtins.print') as fakeoutput: main.item_info() fakeoutput.assert_called_with("Item not found in inventory")
def test_item_info(self): """Add in new item, test item info""" print_text = '' for k, value in self.test_dict_furniture.items(): print_text += '{}:{}\n'.format(k, value) with patch('builtins.input', side_effect=self.test_inputs_furniture): add_new_item() with patch('builtins.input', side_effect=self.test_inputs_furniture): with patch('sys.stdout', new=io.StringIO()) as print_out: item_info() self.assertEqual(print_out.getvalue(), print_text)
def test_user_run(self): """This emulates a hypothetical users behavior.""" #Inputs and expected values user_in_1 = ['qws', '1'] user_in_2 = ['2222', 'new generic product', '60.00', 'n', 'n'] user_in_3 = ['3333', 'new electric product', '100.00', 'n', 'y', 'Atari', '120 volts'] user_in_4 = ['4444', 'new furniture product', '40.00', 'y', 'cedar', 'L'] expected_dict_2 = {'product_code': '2222', 'description': 'new generic product', 'market_price': 24, 'rental_price': '60.00'} expected_dict_3 = {'product_code': '3333', 'description': 'new electric product', 'market_price':24, 'rental_price': '100.00', 'brand': 'Atari', 'voltage': '120 volts'} expected_dict_4 = {'product_code': '4444', 'description': 'new furniture product', 'market_price': 24, 'rental_price': '40.00', 'material': 'cedar', 'size': 'L'} target_1 = 'product_code:3333\ndescription:new electric product\n' target_2 = 'market_price:24\nrental_price:100.00\nbrand:Atari\n' target_3 = 'voltage:120 volts\n' info_target = target_1+target_2+target_3 #try out the program with patch('builtins.input', side_effect=user_in_1): main_menu() #adding items with patch('builtins.input', side_effect=user_in_2): add_new_item() self.assertEqual(expected_dict_2, FULL_INVENTORY['2222']) with patch('builtins.input', side_effect=user_in_3): add_new_item() self.assertEqual(expected_dict_3, FULL_INVENTORY['3333']) with patch('builtins.input', side_effect=user_in_4): add_new_item() self.assertEqual(expected_dict_4, FULL_INVENTORY['4444']) #print(FULL_INVENTORY) #getting an item's info with patch('builtins.input', side_effect='2'): main_menu() with patch('builtins.input', side_effect=user_in_3): with patch('sys.stdout', new=io.StringIO()) as print_result: item_info() self.assertEqual(info_target, print_result.getvalue()) #quitting the program with patch('builtins.input', side_effect='q'): main_menu() with self.assertRaises(SystemExit): exit_program()
def test_item_info_missing(self, m_input): """Test the item_info function with a nonexistent item code.""" print("ITEM INFO") # populate mocked variables main.FULL_INVENTORY = {} m_input.side_effect = ["A1"] # run the function and grab stdout with patch('sys.stdout', new=io.StringIO()) as result: main.item_info() # compare to expected output expected = "Item not found in inventory\n" self.assertEqual(result.getvalue(), expected)
def validate_item_info_print(self, input_val, expected): """ Performs the call and assertion for Main.item_info. :self: The Class :input_val: The user's input value to mock. :expected: The expected output in the console. """ with patch("builtins.input") as handle_input: handle_input.return_value = input_val Main.item_info() std_out = self.stdout_intercept.getvalue() self.assertTrue(std_out.count(expected) == 1)
def test_item_info_gen(self): """Tests getting info on item in database.""" target_1 = 'product_code:2222\ndescription:new generic product\n' target_2 = 'market_price:24\nrental_price:60.00\n' target = target_1 + target_2 general_item = ['2222', 'new generic product', '60.00', 'n', 'n'] with patch('builtins.input', side_effect=general_item): add_new_item() with patch('builtins.input', side_effect=general_item): with patch('sys.stdout', new=io.StringIO()) as print_result: item_info() self.assertEqual(target, print_result.getvalue())
def test_item_info(self, mock_input): with patch('sys.stdout', new=StringIO()) as fake_output: main.FULL_INVENTORY = { 'S99': { 'product_code': 'S99', 'description': 'Screwdriver', 'market_price': 'FakePrice', 'rental_price': 2 } } main.item_info() assert fake_output.getvalue().strip( ) == "product_code:S99\ndescription:Screwdriver\nmarket_price:FakePrice\nrental_price:2" main.FULL_INVENTORY = {}