def test_module(self): # testing option 1: add_new_item(). with patch('builtins.input', side_effect='1'): self.assertEqual(main.main_menu().__name__, 'add_new_item') main.get_latest_price = MagicMock() main.get_latest_price.return_value = 24 electric_appliance_inputs = ['001', 'oven', 12, 'n', 'y', 'GE', 120] with patch('builtins.input', side_effect=electric_appliance_inputs): main.add_new_item() self.assertEqual(len(main.FULL_INVENTORY), 1) self.assertEqual(main.FULL_INVENTORY[0].product_code, '001') self.assertEqual(main.FULL_INVENTORY[0].description, 'oven') self.assertEqual(main.FULL_INVENTORY[0].market_price, 24) self.assertEqual(main.FULL_INVENTORY[0].rental_price, 12) self.assertEqual(main.FULL_INVENTORY[0].brand, 'GE') self.assertEqual(main.FULL_INVENTORY[0].voltage, 120) # testing option 2: item_info(). with patch('builtins.input', side_effect='2'): self.assertEqual(main.main_menu().__name__, 'item_info') 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]) # testing option 3: exit_program(). with patch('builtins.input', side_effect='q'): self.assertEqual(main.main_menu().__name__, 'exit_program') with self.assertRaises(SystemExit) as texit: main.exit_program() the_exception = texit.exception self.assertEqual(the_exception.code, 3)
def test_exit(self): """ Tests main.exit_program() """ with self.assertRaises(SystemExit): main.exit_program(None) main.exit_program("exit")
def test_exit_program(self): with patch('builtins.input', side_effect='q'): self.assertEqual(main.main_menu().__name__, 'exit_program') with self.assertRaises(SystemExit) as texit: main.exit_program() the_exception = texit.exception self.assertEqual(the_exception.code, 3)
def test_main_menu(self): ''' test main menu options ''' main.FULL_INVENTORY = {} # test the ability to add new items to the inventory with patch('builtins.input', side_effect=self.update_inventory[0]): main.addnew_item() with patch('builtins.input', side_effect=self.update_inventory[1]): main.addnew_item() # assert that it is the same as the setup self.assertEqual(self.FULL_INVENTORY, main.FULL_INVENTORY) # assert adding non furniture or electric appliance works with patch('builtins.input', side_effect=self.update_inventory[2]): main.addnew_item() self.assertEqual(main.FULL_INVENTORY[3]['description'], 'hops') with patch('builtins.input', side_effect=self.update_inventory[3]): main.main_menu() self.assertEqual(main.FULL_INVENTORY[3]['rental_price'], 20) # add a new item to the inventory with patch('builtins.input', side_effect=[1]): main.item_info() # Trigger else statement in item_info() with patch('sys.stdout', new=io.StringIO()) as output: with patch('builtins.input', side_effect=['1']): main.item_info() self.assertEqual('Item not found in inventory\n', output.getvalue()) # test exit_program with self.assertRaises(SystemExit) as exit_program: main.exit_program() self.assertEqual(exit_program.exception.code, 0)
def test_main(self): add_inv_item = ("1234", "inventory item", "2", "n", "n") add_furn_item = ("5678", "furniture item", "4", "y", "furniture material", "M") add_elect_item = ("9012", "electric appliance item", "6", "n", "y", "appliance brand", "120") test_inventory = {"9012": {"product_code": "9012", "description": "electric appliance item", "market_price": 24, "rental_price": "6", "brand": "appliance brand", "voltage": "120"}, "5678": {"product_code": "5678", "description": "furniture item", "market_price": 24, "rental_price": "4", "material": "furniture material", "size": "M"}, "1234": {"product_code": "1234", "description": "inventory item", "market_price": 24, "rental_price": "2"}} test_item = ["1234"] test_item_expected = {"description": "inventory item", "market_price": 24, "product_code": "1234", "rental_price": "2"} with patch("builtins.input", side_effect=add_inv_item): main.add_new_item() with patch("builtins.input", side_effect=add_furn_item): main.add_new_item() with patch("builtins.input", side_effect=add_elect_item): main.add_new_item() with patch("builtins.input", side_effect=test_item): self.assertEqual(main.item_info(), test_item_expected) self.assertEqual(test_inventory, main.FULL_INVENTORY) self.assertEqual(24, market_prices.get_latest_price(1234)) with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): """ Test to validate that the correct signal is raised when exit_program is called. """ with self.assertRaises(SystemExit): main.exit_program()
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_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_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_main(self): """Test main_menu function """ with mock.patch('builtins.input', side_effect=['1', '1', '2', '3', 'n', 'y', '4', '5']): main.main_menu() self.assertEqual(main.FULL_INVENTORY['1']['product_code'], '1') self.assertEqual(main.FULL_INVENTORY['1']['market_price'], 24) self.assertEqual(main.FULL_INVENTORY['1']['brand'], '4') self.assertEqual(main.FULL_INVENTORY['1']['voltage'], '5') with mock.patch('builtins.input', side_effect=['2', '2', '3', 'n', 'n', '4', '5']): main.main_menu('1') self.assertEqual(main.FULL_INVENTORY['2']['product_code'], '2') self.assertEqual(main.FULL_INVENTORY['2']['market_price'], 24) with mock.patch('builtins.input', side_effect=['3', '2', '3', 'y', '4', '5']): main.main_menu('1') self.assertEqual(main.FULL_INVENTORY['3']['product_code'], '3') self.assertEqual(main.FULL_INVENTORY['3']['market_price'], 24) self.assertEqual(main.FULL_INVENTORY['3']['material'], '4') self.assertEqual(main.FULL_INVENTORY['3']['size'], '5') with mock.patch('builtins.input', side_effect=['q', '2', '3', 'y', '4', '5']): with self.assertRaises(SystemExit): main.main_menu() self.assertRaises(SystemExit, main.exit_program()) with mock.patch('builtins.input', side_effect=['2', '2', '3', 'y', '4', '5']): main.main_menu() self.assertEqual(main.FULL_INVENTORY['3']['product_code'], '3') self.assertEqual(main.FULL_INVENTORY['3']['market_price'], 24) self.assertEqual(main.FULL_INVENTORY['3']['material'], '4') self.assertEqual(main.FULL_INVENTORY['3']['size'], '5')
def test_whole_system(self): #input_inventory = ('K2', 'Snowboard', 1000, 10) input_list = [('OL1', 'OLED TV', 2000, 'n', 'y', 'LG', 110), ('C1', 'Lounge Chair', 1000, 'y', 'Brass', 'L'), ('K2', 'Snowboard', 1000, 'n', 'n') ] gold_list = [{'productCode': 'OL1', 'description': 'OLED TV', 'marketPrice': 24, 'rentalPrice': 2000, 'brand': 'LG', 'voltage': 110}, {'productCode': 'C1', 'description': 'Lounge Chair', 'marketPrice': 24, 'rentalPrice': 1000, 'material': 'Brass', 'size': 'L'}, {'productCode': 'K2', 'description': 'Snowboard', 'marketPrice': 24, 'rentalPrice': 1000} ] print_out = '''productCode:OL1 description:OLED TV marketPrice:24 rentalPrice:2000 brand:LG voltage:110 ''' #add a new item with patch('builtins.input', side_effect='1'): main_menu() with patch('builtins.input', side_effect=input_list[0]): add_new_item() self.assertDictEqual(gold_list[0], FULL_INVENTORY['OL1']) #print the item in inventory with patch('builtins.input', side_effect='2'): main_menu() with patch('builtins.input', side_effect=input_list[0]): with patch('sys.stdout', new=io.StringIO()) as actual_result: item_info() self.assertEqual(actual_result.getvalue(), print_out) #then quit with patch('builtins.input', side_effect='q'): main_menu() with self.assertRaises(SystemExit): exit_program()
def test_main(self): """Test main_menu function """ with mock.patch('builtins.input', side_effect=['0', 'q']): with mock.patch('sys.stdout'): with self.assertRaises(SystemExit): main.main_menu() mock.call.write( "Please choose from the following options (1, 2, q)" ).assert_called_once() self.assertRaises(SystemExit, main.exit_program())
def test_integration(self): main.FULLINVENTORY = {} #test menu with patch('builtins.input', side_effect='1'): self.assertEqual(main.main_menu(), main.add_new_item) with patch('builtins.input', side_effect='2'): self.assertEqual(main.main_menu(), main.item_info) with patch('builtins.input', side_effect='q'): self.assertEqual(main.main_menu(), main.exit_program) #test add item with patch('builtins.input', side_effect=i_obj): main.add_new_item() with patch('builtins.input', side_effect=f_obj): main.add_new_item() with patch('builtins.input', side_effect=e_obj): main.add_new_item() self.assertEqual(main.FULLINVENTORY, expected) #reset fullinventory to just 400 product main.FULLINVENTORY = {'400': item_info} self.assertEqual(24, main.get_price(400)) # appliance item was added with mock.patch("builtins.print") as print_mock, mock.patch( "builtins.input") as input_mock: input_mock.return_value = "400" main.item_info() for k, val in item_info.items(): print_mock.assert_any_call(f"{k}:{val}") # item info for non existing product with mock.patch("builtins.print") as print_mock, mock.patch( "builtins.input") as input_mock: input_mock.return_value = "666" main.item_info() print_mock.assert_called_with("Item not found in inventory") #quit with self.assertRaises(SystemExit): main.exit_program()
def test_workflow(self): # Verify that the FULL_INVENTORY doesn't contain expected items yet. with patch('sys.stdout', new=io.StringIO()) as stdout: with patch('builtins.input', side_effect=[1321], autospec=True): M.item_info() expected_init = "Item not found in inventory\n" self.assertEqual(expected_init, stdout.getvalue()) # Add all items to the FULL_INVENTORY dictionary in main for k, v in self.items.items(): with patch('builtins.input', side_effect=v): with patch('inventory_management.main.get_latest_price', return_value=k): M.add_new_item() self.assertEqual(self.inventory, M.FULL_INVENTORY) # Verify that items are in the FULL_INVENTORY in main with patch('sys.stdout', new=io.StringIO()) as stdout1: with patch('builtins.input', side_effect=[1321], autospec=True): M.item_info() expected = "product_code:1321\n" \ "description:Chair\n" \ "market_price:689\n" \ "rental_price:13.05\n" \ "material:wood\n" \ "size:XL\n" self.assertEqual(expected, stdout1.getvalue()) # Verify that items will return expected stdout statement with patch('sys.stdout', new=io.StringIO()) as stdout2: with patch('builtins.input', side_effect=[11321], autospec=True): M.item_info() expected_nf = "Item not found in inventory\n" self.assertEqual(expected_nf, stdout2.getvalue()) # Ensure program exits gracefully with self.assertRaises(SystemExit): M.exit_program()
def test_exit_program(self): # testing that program throw the expected code with self.assertRaises(SystemExit) as exit_program: main.exit_program() self.assertEqual(exit_program.exception.code, 0)
def test_exit(self): sys.exit = MagicMock() m.exit_program() sys.exit.assert_called()
def test_exit_program(self): """Tests functionality program will close.""" with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): '''Tests the exit_program function''' with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): with self.assertRaises(SystemExit) as context: main.exit_program() self.assertEqual(context.exception.code, None)
def test_exit_program(self): with self.assertRaises(SystemExit): exit_program(full_inventory={}) with self.assertRaises(TypeError): exit_program()
def test_exit(self): """Test exit""" with self.assertRaises(SystemExit): Main.exit_program()
def test_exit(self): """Test the exit_program function.""" with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): """Test to make sure the program""" with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): """Test for raised exception if system does not exit properly""" with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): """ Test the exit program menu option """ with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): """Test exit_program function""" with self.assertRaises(SystemExit): self.assertRaises(SystemExit, main.exit_program())
def test_exit_program(self): """tests the exit program function""" with self.assertRaises(SystemExit): main.exit_program()
def test_exit(self): """Test if exit_program exits the program successfully""" with self.assertRaises(SystemExit): main.exit_program()
def test_exit_program(self): with self.assertRaises(SystemExit): main.exit_program()
def test_quit_program(self): """Test quit program function in main module.""" with self.assertRaises(SystemExit): m.exit_program()