From 28243706694a991f01a35fbef1eeb579b1671212 Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Mon, 5 Dec 2022 09:27:40 -0800 Subject: [PATCH 1/8] Running with unit tests --- swap_meet/clothing.py | 13 +- swap_meet/decor.py | 14 ++- swap_meet/electronics.py | 14 ++- swap_meet/item.py | 23 +++- swap_meet/vendor.py | 115 +++++++++++++++++- tests/integration_tests/test_wave_01_02_03.py | 2 +- .../test_wave_04_05_06_07.py | 2 +- tests/unit_tests/test_wave_01.py | 17 +-- tests/unit_tests/test_wave_02.py | 12 +- tests/unit_tests/test_wave_03.py | 17 +-- tests/unit_tests/test_wave_04.py | 6 +- tests/unit_tests/test_wave_05.py | 28 ++--- tests/unit_tests/test_wave_06.py | 55 +++++---- tests/unit_tests/test_wave_07.py | 30 ++--- 14 files changed, 264 insertions(+), 84 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index b8afdeb1e..db24c7e98 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,2 +1,11 @@ -class Clothing: - pass \ No newline at end of file +# Wave 5 +import uuid +from swap_meet.item import Item +class Clothing(Item): + def __init__(self,id= None,category = "" ,condition = 0 ,fabric = "Unknown" ): + super().__init__(id,category,condition) + self.fabric = fabric + + def __str__(self): + txt = super().__str__() + return f"{txt}. It is made from {self.fabric} fabric." \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index eab7a9dbe..e4fb46dc5 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,2 +1,12 @@ -class Decor: - pass \ No newline at end of file +# Wave 5 +import uuid +from swap_meet.item import Item +class Decor(Item): + def __init__(self,id = None,category = "" ,condition = 0,width = 0,length = 0 ): + super().__init__(id,category,condition) + self.width = width + self.length = length + + def __str__(self): + txt = super().__str__() + return f"{txt}. It takes up a {self.width} by {self.length} sized space." \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 2f9dff68a..7905d3536 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,2 +1,12 @@ -class Electronics: - pass +# Wave 5 +import uuid +from swap_meet.item import Item +class Electronics(Item): + def __init__(self,id = None,category = "" ,condition = 0 ,type = "Unknown"): + super().__init__(id,category,condition) + self.type = type + + def __str__(self): + txt = super().__str__() + return f"{txt}. This is a {self.type} device." + \ No newline at end of file diff --git a/swap_meet/item.py b/swap_meet/item.py index 560d759c2..31b335e8d 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,2 +1,23 @@ +#Wave 2 , 5 +import uuid +#from swap_meet.vendor import Vendor class Item: - pass \ No newline at end of file + def __init__(self,id = None,category = "", condition = 0 ): + if id is None : + self.id = uuid.uuid4().int + else: + self.id = id + self.condition = condition + self.category = category + + def get_category(self): + return self.__class__.__name__#type(self).__name__ +# Wave 3 + def __str__(self): + return f"An object of type {self.get_category()} with id {self.id}" +# Wave 5 + def condition_description(self): + description_of_condition = ["Mint","Heavily Used","Like New","Best Used","Gently Used","New"] + return description_of_condition[self.condition] + + diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 87302c056..8508a4686 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,2 +1,115 @@ +#Wave 1 +#from swap_meet.item import Item class Vendor: - pass \ No newline at end of file + def __init__(self, inventory = None): + if inventory is not None: + self.inventory = inventory + else: + self.inventory = [] + + def add(self,new_item): + self.inventory.append(new_item) + return new_item + + def remove(self, item_to_remove): + if item_to_remove in self.inventory: + self.inventory.remove(item_to_remove) + return item_to_remove + return None + + def get_by_id(self,id): + if id is None: + return None + for item in self.inventory: + if item.id == id: + return item + return None +# Wave 3 + def swap_items(self,other_vendor,my_item,their_item): + if my_item not in self.inventory or their_item not in other_vendor.inventory: + return False + self.remove(my_item) + other_vendor.add(my_item) + other_vendor.remove(their_item) + self.add(their_item) + return True + +# Wave 4 + def swap_first_item(self,other_vendor): + if not self.inventory or not other_vendor.inventory: + return False + self.swap_items(other_vendor,self.inventory[0],other_vendor.inventory[0]) + return True +# Wave 6 + def get_by_category(self,category): + if self.inventory is None: + return [] + list_items_by_category = [] + + for item in self.inventory: + if item.get_category() == category: + list_items_by_category.append(item) + return list_items_by_category + + def get_best_by_category(self,category): + list_of_items_in_category = self.get_by_category(category) + if list_of_items_in_category is None: + return None + best_condition = 0 + best_item = None + for item in list_of_items_in_category: + if item.condition >= best_condition: + best_condition = item.condition + best_item = item + return best_item + def swap_best_by_category(self,other_vendor,my_priority,their_priority): + best_item_from_my_inventory = self.get_best_by_category(their_priority) + best_item_from_their_inventory = other_vendor.get_best_by_category(my_priority) + if not best_item_from_my_inventory or not best_item_from_their_inventory: + return False + self.swap_items(other_vendor,best_item_from_my_inventory,best_item_from_their_inventory) + return True + #Wave 7 + # (Needs a lot of work) + + def display_inventory(self,category = ""): + if self.inventory == []: + print("No inventory to display.") + return None + + if category: + inventory_items_by_category = self.get_by_category(category) + if inventory_items_by_category: + for index in range(len(inventory_items_by_category)): + print(f"{index +1}. {str(inventory_items_by_category[index])}") + else: + print("No inventory to display.") + else: + for index in range(len(self.inventory)): + print(f"{index +1}. {str(self.inventory[index])}") + + + def swap_by_id(self,other_vendor,my_item_id = None,their_item_id = None): + my_item = self.get_by_id(my_item_id) + vendor_item = other_vendor.get_by_id(their_item_id) + if my_item is None or vendor_item is None: + return False + result = self.swap_items(other_vendor,my_item,vendor_item) + return result + def choose_and_swap_items(self,other_vendor,category = ""): + if category is None : + self.display_inventory() + other_vendor.display_inventory() + else: + self.display_inventory(category) + other_vendor.display_inventory(category) + my_item_id = int(input("Enter Id of the item you want to swap")) + vendor_item_id = int(input("Enter Id of your friend vendor's item you want to swap with")) + if not self.get_by_id(my_item_id) or not other_vendor.get_by_id(vendor_item_id): + return False + self.swap_by_id(other_vendor,my_item_id,vendor_item_id) + return True + + + + diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index 404641a86..b510d4f21 100644 --- a/tests/integration_tests/test_wave_01_02_03.py +++ b/tests/integration_tests/test_wave_01_02_03.py @@ -3,7 +3,7 @@ from swap_meet.item import Item @pytest.mark.skip -@pytest.mark.integration_test +#@pytest.mark.integration_test def test_integration_wave_01_02_03(): # make a vendor vendor = Vendor() diff --git a/tests/integration_tests/test_wave_04_05_06_07.py b/tests/integration_tests/test_wave_04_05_06_07.py index cdbf79eaf..b8273661e 100644 --- a/tests/integration_tests/test_wave_04_05_06_07.py +++ b/tests/integration_tests/test_wave_04_05_06_07.py @@ -5,7 +5,7 @@ from swap_meet.electronics import Electronics @pytest.mark.skip -@pytest.mark.integration_test +#@pytest.mark.integration_test def test_integration_wave_04_05_06(capfd): camila = Vendor() valentina = Vendor() diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 019ff1d58..8a9c72976 100644 --- a/tests/unit_tests/test_wave_01.py +++ b/tests/unit_tests/test_wave_01.py @@ -2,12 +2,12 @@ import pytest from swap_meet.vendor import Vendor -@pytest.mark.skip +#@pytest.mark.skip def test_vendor_has_inventory(): vendor = Vendor() assert len(vendor.inventory) == 0 -@pytest.mark.skip +#@pytest.mark.skip def test_vendor_takes_optional_inventory(): inventory = ["a", "b", "c"] vendor = Vendor(inventory=inventory) @@ -16,7 +16,7 @@ def test_vendor_takes_optional_inventory(): assert "b" in vendor.inventory assert "c" in vendor.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_adding_to_inventory(): vendor = Vendor() item = "new item" @@ -27,7 +27,7 @@ def test_adding_to_inventory(): assert item in vendor.inventory assert result == item -@pytest.mark.skip +#@pytest.mark.skip def test_removing_from_inventory_returns_item(): item = "item to remove" vendor = Vendor( @@ -40,7 +40,7 @@ def test_removing_from_inventory_returns_item(): assert item not in vendor.inventory assert result == item -@pytest.mark.skip +#@pytest.mark.skip def test_removing_not_found_returns_none(): item = "item to remove" vendor = Vendor( @@ -48,8 +48,11 @@ def test_removing_not_found_returns_none(): ) result = vendor.remove(item) - - raise Exception("Complete this test according to comments below.") + #assert + assert item not in vendor.inventory + assert len(vendor.inventory) == 3 + assert result == None + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index f4b512222..880e6feec 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -2,30 +2,30 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +#@pytest.mark.skip def test_items_have_default_uuid_length_id(): item = Item() assert isinstance(item.id, int) assert len(str(item.id)) >= 32 -@pytest.mark.skip +#@pytest.mark.skip def test_item_instances_have_different_default_ids(): item_a = Item() item_b = Item() assert item_a.id != item_b.id -@pytest.mark.skip +#@pytest.mark.skip def test_items_use_custom_id_if_passed(): item = Item(id=12345) assert isinstance(item.id, int) assert item.id == 12345 -@pytest.mark.skip +#@pytest.mark.skip def test_item_obj_returns_text_item_for_category(): item = Item() assert item.get_category() == "Item" -@pytest.mark.skip +#@pytest.mark.skip def test_get_item_by_id(): test_id = 12345 item_custom_id = Item(id=test_id) @@ -36,7 +36,7 @@ def test_get_item_by_id(): result_item = vendor.get_by_id(test_id) assert result_item is item_custom_id -@pytest.mark.skip +#@pytest.mark.skip def test_get_item_by_id_no_matching(): test_id = 12345 item_a = Item() diff --git a/tests/unit_tests/test_wave_03.py b/tests/unit_tests/test_wave_03.py index d4fd96017..25927d29a 100644 --- a/tests/unit_tests/test_wave_03.py +++ b/tests/unit_tests/test_wave_03.py @@ -2,7 +2,7 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +#@pytest.mark.skip def test_item_overrides_to_string(): test_id = 12345 item = Item(id=test_id) @@ -12,7 +12,7 @@ def test_item_overrides_to_string(): expected_result = f"An object of type Item with id {test_id}" assert item_as_string == expected_result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_items_returns_true(): item_a = Item() item_b = Item() @@ -40,7 +40,7 @@ def test_swap_items_returns_true(): assert item_b in jolie.inventory assert result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_items_when_my_item_is_missing_returns_false(): item_a = Item() item_b = Item() @@ -67,7 +67,7 @@ def test_swap_items_when_my_item_is_missing_returns_false(): assert item_e in jolie.inventory assert not result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_items_when_their_item_is_missing_returns_false(): item_a = Item() item_b = Item() @@ -94,7 +94,7 @@ def test_swap_items_when_their_item_is_missing_returns_false(): assert item_e in jolie.inventory assert not result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_items_from_my_empty_returns_false(): fatimah = Vendor( inventory=[] @@ -114,7 +114,7 @@ def test_swap_items_from_my_empty_returns_false(): assert len(jolie.inventory) == 2 assert not result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_items_from_their_empty_returns_false(): item_a = Item() item_b = Item() @@ -130,8 +130,9 @@ def test_swap_items_from_their_empty_returns_false(): nobodys_item = Item() result = fatimah.swap_items(jolie, item_b, nobodys_item) - - raise Exception("Complete this test according to comments below.") + # Assert + assert result == False + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* diff --git a/tests/unit_tests/test_wave_04.py b/tests/unit_tests/test_wave_04.py index 87addbbf6..3d1433d1d 100644 --- a/tests/unit_tests/test_wave_04.py +++ b/tests/unit_tests/test_wave_04.py @@ -2,7 +2,7 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +#@pytest.mark.skip def test_swap_first_item_returns_true(): item_a = Item() item_b = Item() @@ -30,7 +30,7 @@ def test_swap_first_item_returns_true(): assert item_a in jolie.inventory assert result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_first_item_from_my_empty_returns_false(): fatimah = Vendor( inventory=[] @@ -48,7 +48,7 @@ def test_swap_first_item_from_my_empty_returns_false(): assert len(jolie.inventory) == 2 assert not result -@pytest.mark.skip +#@pytest.mark.skip def test_swap_first_item_from_their_empty_returns_false(): item_a = Item() item_b = Item() diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 7770aee07..d13a34efd 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -7,17 +7,17 @@ # ~~~~~ Clothing Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_clothing_has_default_uuid_length_id(): clothing = Clothing() check_for_default_uuid_length_id(clothing) -@pytest.mark.skip +#@pytest.mark.skip def test_clothing_has_expected_category_and_custom_id(): clothing = Clothing(id=TEST_CUSTOM_ID) check_category_and_custom_id(clothing, TEST_CUSTOM_ID, "Clothing") -@pytest.mark.skip +#@pytest.mark.skip def test_clothing_has_expected_default_to_str(): clothing = Clothing(id=TEST_CUSTOM_ID) expected_str = ( @@ -26,7 +26,7 @@ def test_clothing_has_expected_default_to_str(): ) assert str(clothing) == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_clothing_has_expected_to_str_with_custom_fabric(): clothing = Clothing(id=TEST_CUSTOM_ID, fabric="Pinstriped") expected_str = ( @@ -37,17 +37,17 @@ def test_clothing_has_expected_to_str_with_custom_fabric(): # ~~~~~ Decor Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_decor_has_default_uuid_length_id(): decor = Decor() check_for_default_uuid_length_id(decor) -@pytest.mark.skip +#@pytest.mark.skip def test_decor_has_expected_category_and_custom_id(): decor = Decor(id=TEST_CUSTOM_ID) check_category_and_custom_id(decor, TEST_CUSTOM_ID, "Decor") -@pytest.mark.skip +#@pytest.mark.skip def test_decor_has_expected_default_to_str(): decor = Decor(id=TEST_CUSTOM_ID) expected_str = ( @@ -56,7 +56,7 @@ def test_decor_has_expected_default_to_str(): ) assert str(decor) == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_decor_has_expected_to_str_with_custom_size(): decor = Decor(id=TEST_CUSTOM_ID, width=3, length=12) expected_str = ( @@ -67,17 +67,17 @@ def test_decor_has_expected_to_str_with_custom_size(): # ~~~~~ Electronics Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_electronics_has_default_uuid_length_id(): electronics = Electronics() check_for_default_uuid_length_id(electronics) -@pytest.mark.skip +#@pytest.mark.skip def test_electronics_has_expected_category_and_custom_id(): electronics = Electronics(id=TEST_CUSTOM_ID) check_category_and_custom_id(electronics, TEST_CUSTOM_ID, "Electronics") -@pytest.mark.skip +#@pytest.mark.skip def test_electronics_has_expected_default_to_str(): electronics = Electronics(id=TEST_CUSTOM_ID) expected_str = ( @@ -86,7 +86,7 @@ def test_electronics_has_expected_default_to_str(): ) assert str(electronics) == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_electronics_has_expected_to_str_with_custom_type(): electronics = Electronics(id=TEST_CUSTOM_ID, type="Mobile Phone") expected_str = ( @@ -97,7 +97,7 @@ def test_electronics_has_expected_to_str_with_custom_type(): # ~~~~~ Item Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_items_have_condition_as_float(): items = [ Clothing(condition=3.5), @@ -107,7 +107,7 @@ def test_items_have_condition_as_float(): for item in items: assert item.condition == pytest.approx(3.5) -@pytest.mark.skip +#@pytest.mark.skip def test_items_have_condition_descriptions_that_are_the_same_regardless_of_type(): items = [ Clothing(condition=5), diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index ad51bf42d..32a91ed06 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -5,7 +5,7 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +#@pytest.mark.skip def test_get_items_by_category(): item_a = Clothing() item_b = Electronics() @@ -22,7 +22,7 @@ def test_get_items_by_category(): assert item_a in items assert item_c in items -@pytest.mark.skip +#@pytest.mark.skip def test_get_no_matching_items_by_category(): item_a = Clothing() item_b = Item() @@ -32,13 +32,14 @@ def test_get_no_matching_items_by_category(): ) items = vendor.get_by_category("Electronics") - - raise Exception("Complete this test according to comments below.") + # Assert + assert items == [] + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* -@pytest.mark.skip +#@pytest.mark.skip def test_best_by_category(): item_a = Clothing(condition=2.0) item_b = Decor(condition=2.0) @@ -54,7 +55,7 @@ def test_best_by_category(): assert best_item.get_category() == "Clothing" assert best_item.condition == pytest.approx(4.0) -@pytest.mark.skip +#@pytest.mark.skip def test_best_by_category_no_matches_is_none(): item_a = Decor(condition=2.0) item_b = Decor(condition=2.0) @@ -67,7 +68,7 @@ def test_best_by_category_no_matches_is_none(): assert best_item is None -@pytest.mark.skip +#@pytest.mark.skip def test_best_by_category_with_duplicates(): # Arrange item_a = Clothing(condition=2.0) @@ -84,7 +85,7 @@ def test_best_by_category_with_duplicates(): assert best_item.get_category() == "Clothing" assert best_item.condition == pytest.approx(4.0) -@pytest.mark.skip +#@pytest.mark.skip def test_swap_best_by_category(): # Arrange # me @@ -109,8 +110,13 @@ def test_swap_best_by_category(): my_priority="Clothing", their_priority="Decor" ) - - raise Exception("Complete this test according to comments below.") + #Assert + assert item_c in jesse.inventory + assert item_f in tai.inventory + assert result == True + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -119,7 +125,7 @@ def test_swap_best_by_category(): # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories, including the items which were swapped from one vendor to the other -@pytest.mark.skip +#@pytest.mark.skip def test_swap_best_by_category_reordered(): # Arrange item_a = Decor(condition=2.0) @@ -142,8 +148,13 @@ def test_swap_best_by_category_reordered(): my_priority="Clothing", their_priority="Decor" ) - - raise Exception("Complete this test according to comments below.") + #Assert + assert item_c in jesse.inventory + assert item_f in tai.inventory + assert result == True + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -152,7 +163,7 @@ def test_swap_best_by_category_reordered(): # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories, and that the items that were swapped are not there -@pytest.mark.skip +#@pytest.mark.skip def test_swap_best_by_category_no_inventory_is_false(): tai = Vendor( inventory=[] @@ -178,7 +189,7 @@ def test_swap_best_by_category_no_inventory_is_false(): assert item_b in jesse.inventory assert item_c in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_swap_best_by_category_no_other_inventory_is_false(): item_a = Clothing(condition=2.0) item_b = Decor(condition=4.0) @@ -204,7 +215,7 @@ def test_swap_best_by_category_no_other_inventory_is_false(): assert item_b in tai.inventory assert item_c in tai.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_swap_best_by_category_no_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -227,8 +238,9 @@ def test_swap_best_by_category_no_match_is_false(): my_priority="Clothing", their_priority="Clothing" ) - - raise Exception("Complete this test according to comments below.") + #Assert + result == False + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* @@ -237,7 +249,7 @@ def test_swap_best_by_category_no_match_is_false(): # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories -@pytest.mark.skip +#@pytest.mark.skip def test_swap_best_by_category_no_other_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -260,8 +272,9 @@ def test_swap_best_by_category_no_other_match_is_false(): my_priority="Electronics", their_priority="Decor" ) - - raise Exception("Complete this test according to comments below.") + #Assert + assert result == False + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* diff --git a/tests/unit_tests/test_wave_07.py b/tests/unit_tests/test_wave_07.py index 005b82ecc..df919a64c 100644 --- a/tests/unit_tests/test_wave_07.py +++ b/tests/unit_tests/test_wave_07.py @@ -7,7 +7,7 @@ # ~~~~~ display_inventory Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_display_inventory_with_items_no_category(capfd): # Arrange item_a = Clothing(id=123, fabric="Striped") @@ -31,7 +31,7 @@ def test_display_inventory_with_items_no_category(capfd): ) assert captured.out == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_display_inventory_with_items_and_category(capfd): # Arrange item_a = Decor(id=123, width=2, length=4) @@ -52,7 +52,7 @@ def test_display_inventory_with_items_and_category(capfd): ) assert captured.out == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_display_inventory_with_category_and_no_matching_items(capfd): # Arrange item_a = Decor(id=123, width=2, length=4) @@ -72,7 +72,7 @@ def test_display_inventory_with_category_and_no_matching_items(capfd): ) assert captured.out == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_display_inventory_no_items_no_category(capfd): # Arrange vendor = Vendor(inventory=[]) @@ -87,7 +87,7 @@ def test_display_inventory_no_items_no_category(capfd): ) assert captured.out == expected_str -@pytest.mark.skip +#@pytest.mark.skip def test_display_inventory_no_items_with_category(capfd): # Arrange vendor = Vendor(inventory=[]) @@ -104,7 +104,7 @@ def test_display_inventory_no_items_with_category(capfd): # ~~~~~ swap_by_id Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_swap_by_id_success_returns_true(): # Arrange item_a = Decor(id=123) @@ -141,7 +141,7 @@ def test_swap_by_id_success_returns_true(): assert item_e in jesse.inventory assert item_f in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_swap_by_id_with_caller_empty_inventory_returns_false(): # Arrange tai = Vendor(inventory=[]) @@ -169,7 +169,7 @@ def test_swap_by_id_with_caller_empty_inventory_returns_false(): assert item_e in jesse.inventory assert item_f in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_swap_by_id_with_other_empty_inventory_returns_false(): # Arrange item_a = Decor(id=123) @@ -198,7 +198,7 @@ def test_swap_by_id_with_other_empty_inventory_returns_false(): assert len(jesse.inventory) == 0 -@pytest.mark.skip +#@pytest.mark.skip def test_swap_by_id_fails_if_caller_missing_item(): # Arrange item_a = Decor(id=123) @@ -235,7 +235,7 @@ def test_swap_by_id_fails_if_caller_missing_item(): assert item_e in jesse.inventory assert item_f in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_swap_by_id_fails_if_other_missing_item(): # Arrange item_a = Decor(id=123) @@ -274,7 +274,7 @@ def test_swap_by_id_fails_if_other_missing_item(): # ~~~~~ choose_and_swap_items Tests ~~~~~ -@pytest.mark.skip +#@pytest.mark.skip def test_choose_and_swap_items_success(monkeypatch): # Arrange item_a = Decor(id=123) @@ -311,7 +311,7 @@ def test_choose_and_swap_items_success(monkeypatch): assert item_d in jesse.inventory assert item_e in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_choose_and_swap_items_with_calling_inventory_empty(monkeypatch): # Arrange tai = Vendor(inventory=[]) @@ -339,7 +339,7 @@ def test_choose_and_swap_items_with_calling_inventory_empty(monkeypatch): assert item_e in jesse.inventory assert item_f in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_choose_and_swap_items_with_other_inventory_empty(monkeypatch): # Arrange item_a = Decor(id=123) @@ -368,7 +368,7 @@ def test_choose_and_swap_items_with_other_inventory_empty(monkeypatch): assert len(jesse.inventory) == 0 -@pytest.mark.skip +#@pytest.mark.skip def test_choose_and_swap_items_with_caller_missing_item(monkeypatch): # Arrange item_a = Decor(id=123) @@ -405,7 +405,7 @@ def test_choose_and_swap_items_with_caller_missing_item(monkeypatch): assert item_e in jesse.inventory assert item_f in jesse.inventory -@pytest.mark.skip +#@pytest.mark.skip def test_choose_and_swap_items_with_other_vendor_missing_item(monkeypatch): # Arrange item_a = Decor(id=123) From cea99d8f8765c5c485de592f51bbb470ecaefb85 Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Mon, 5 Dec 2022 09:44:10 -0800 Subject: [PATCH 2/8] Running with unit and integration test --- tests/integration_tests/test_wave_01_02_03.py | 4 ++-- tests/integration_tests/test_wave_04_05_06_07.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index b510d4f21..e3a892ea2 100644 --- a/tests/integration_tests/test_wave_01_02_03.py +++ b/tests/integration_tests/test_wave_01_02_03.py @@ -2,8 +2,8 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip -#@pytest.mark.integration_test +#@pytest.mark.skip +@pytest.mark.integration_test def test_integration_wave_01_02_03(): # make a vendor vendor = Vendor() diff --git a/tests/integration_tests/test_wave_04_05_06_07.py b/tests/integration_tests/test_wave_04_05_06_07.py index b8273661e..3d5e628b5 100644 --- a/tests/integration_tests/test_wave_04_05_06_07.py +++ b/tests/integration_tests/test_wave_04_05_06_07.py @@ -4,8 +4,8 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip -#@pytest.mark.integration_test +#@pytest.mark.skip +@pytest.mark.integration_test def test_integration_wave_04_05_06(capfd): camila = Vendor() valentina = Vendor() From 7536b12951bfabd4eb152a5d39a0f032849bae37 Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Mon, 5 Dec 2022 09:50:57 -0800 Subject: [PATCH 3/8] Running with unit and integration test --- swap_meet/vendor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 8508a4686..41106a613 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,5 +1,5 @@ #Wave 1 -#from swap_meet.item import Item + class Vendor: def __init__(self, inventory = None): if inventory is not None: @@ -70,7 +70,7 @@ def swap_best_by_category(self,other_vendor,my_priority,their_priority): self.swap_items(other_vendor,best_item_from_my_inventory,best_item_from_their_inventory) return True #Wave 7 - # (Needs a lot of work) + def display_inventory(self,category = ""): if self.inventory == []: From 98916db9285c43e9f73b374078b1ea0bdbc0f21e Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Tue, 6 Dec 2022 20:36:02 -0800 Subject: [PATCH 4/8] Refactored a little bit --- swap_meet/clothing.py | 4 +-- swap_meet/decor.py | 4 +-- swap_meet/electronics.py | 4 +-- swap_meet/item.py | 4 ++- swap_meet/vendor.py | 42 +++++++++++++++++--------------- tests/unit_tests/test_wave_03.py | 2 ++ tests/unit_tests/test_wave_06.py | 6 ++++- 7 files changed, 39 insertions(+), 27 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index db24c7e98..f68f6ddf7 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -7,5 +7,5 @@ def __init__(self,id= None,category = "" ,condition = 0 ,fabric = "Unknown" ): self.fabric = fabric def __str__(self): - txt = super().__str__() - return f"{txt}. It is made from {self.fabric} fabric." \ No newline at end of file + item_message = super().__str__() + return f"{item_message}. It is made from {self.fabric} fabric." \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index e4fb46dc5..01afd9b87 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -8,5 +8,5 @@ def __init__(self,id = None,category = "" ,condition = 0,width = 0,length = 0 ): self.length = length def __str__(self): - txt = super().__str__() - return f"{txt}. It takes up a {self.width} by {self.length} sized space." \ No newline at end of file + item_message = super().__str__() + return f"{item_message}. It takes up a {self.width} by {self.length} sized space." \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 7905d3536..07cd7047e 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -7,6 +7,6 @@ def __init__(self,id = None,category = "" ,condition = 0 ,type = "Unknown"): self.type = type def __str__(self): - txt = super().__str__() - return f"{txt}. This is a {self.type} device." + item_message = super().__str__() + return f"{item_message}. This is a {self.type} device." \ No newline at end of file diff --git a/swap_meet/item.py b/swap_meet/item.py index 31b335e8d..52b6dbfbf 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -12,12 +12,14 @@ def __init__(self,id = None,category = "", condition = 0 ): def get_category(self): return self.__class__.__name__#type(self).__name__ + # Wave 3 def __str__(self): return f"An object of type {self.get_category()} with id {self.id}" + # Wave 5 def condition_description(self): - description_of_condition = ["Mint","Heavily Used","Like New","Best Used","Gently Used","New"] + description_of_condition = ["Mint","Heavily Used","Like New","Gently Used","Best Used","New"] return description_of_condition[self.condition] diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 41106a613..e92a0bce8 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -2,11 +2,17 @@ class Vendor: def __init__(self, inventory = None): + if inventory: + self.inventory = inventory + else: + self.inventory = [] + ''' if inventory is not None: self.inventory = inventory else: self.inventory = [] - + ''' + def add(self,new_item): self.inventory.append(new_item) return new_item @@ -24,6 +30,7 @@ def get_by_id(self,id): if item.id == id: return item return None + # Wave 3 def swap_items(self,other_vendor,my_item,their_item): if my_item not in self.inventory or their_item not in other_vendor.inventory: @@ -40,9 +47,10 @@ def swap_first_item(self,other_vendor): return False self.swap_items(other_vendor,self.inventory[0],other_vendor.inventory[0]) return True + # Wave 6 def get_by_category(self,category): - if self.inventory is None: + if not self.inventory: return [] list_items_by_category = [] @@ -50,18 +58,14 @@ def get_by_category(self,category): if item.get_category() == category: list_items_by_category.append(item) return list_items_by_category - + def get_best_by_category(self,category): list_of_items_in_category = self.get_by_category(category) - if list_of_items_in_category is None: + if not list_of_items_in_category: return None - best_condition = 0 - best_item = None - for item in list_of_items_in_category: - if item.condition >= best_condition: - best_condition = item.condition - best_item = item + best_item = max(list_of_items_in_category,key = lambda item: item.condition) return best_item + def swap_best_by_category(self,other_vendor,my_priority,their_priority): best_item_from_my_inventory = self.get_best_by_category(their_priority) best_item_from_their_inventory = other_vendor.get_best_by_category(my_priority) @@ -69,9 +73,8 @@ def swap_best_by_category(self,other_vendor,my_priority,their_priority): return False self.swap_items(other_vendor,best_item_from_my_inventory,best_item_from_their_inventory) return True - #Wave 7 - - + +#Wave 7 def display_inventory(self,category = ""): if self.inventory == []: print("No inventory to display.") @@ -81,23 +84,23 @@ def display_inventory(self,category = ""): inventory_items_by_category = self.get_by_category(category) if inventory_items_by_category: for index in range(len(inventory_items_by_category)): - print(f"{index +1}. {str(inventory_items_by_category[index])}") + print(f"{index +1}. {inventory_items_by_category[index]}") else: print("No inventory to display.") else: for index in range(len(self.inventory)): - print(f"{index +1}. {str(self.inventory[index])}") - - + print(f"{index +1}. {self.inventory[index]}") + def swap_by_id(self,other_vendor,my_item_id = None,their_item_id = None): my_item = self.get_by_id(my_item_id) vendor_item = other_vendor.get_by_id(their_item_id) - if my_item is None or vendor_item is None: + if not my_item or not vendor_item: return False result = self.swap_items(other_vendor,my_item,vendor_item) return result + def choose_and_swap_items(self,other_vendor,category = ""): - if category is None : + if not category: self.display_inventory() other_vendor.display_inventory() else: @@ -112,4 +115,5 @@ def choose_and_swap_items(self,other_vendor,category = ""): + diff --git a/tests/unit_tests/test_wave_03.py b/tests/unit_tests/test_wave_03.py index 25927d29a..dbbdec553 100644 --- a/tests/unit_tests/test_wave_03.py +++ b/tests/unit_tests/test_wave_03.py @@ -132,6 +132,8 @@ def test_swap_items_from_their_empty_returns_false(): result = fatimah.swap_items(jolie, item_b, nobodys_item) # Assert assert result == False + assert nobodys_item not in fatimah.inventory + assert nobodys_item not in jolie.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 32a91ed06..60ed787ae 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -239,7 +239,9 @@ def test_swap_best_by_category_no_match_is_false(): their_priority="Clothing" ) #Assert - result == False + assert result == False + assert item_d in jesse.inventory + assert item_f in jesse.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** @@ -274,6 +276,8 @@ def test_swap_best_by_category_no_other_match_is_false(): ) #Assert assert result == False + assert item_a in tai.inventory + assert item_c in tai.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** From 88d8009925acec9e780cec5f0990fa755070bfaa Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Thu, 8 Dec 2022 15:11:26 -0800 Subject: [PATCH 5/8] Added tests for Code Coverage --- tests/unit_tests/test_wave_02.py | 15 ++++++++++++ tests/unit_tests/test_wave_07.py | 41 +++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index 880e6feec..9e7379cf6 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -8,6 +8,7 @@ def test_items_have_default_uuid_length_id(): assert isinstance(item.id, int) assert len(str(item.id)) >= 32 + #@pytest.mark.skip def test_item_instances_have_different_default_ids(): item_a = Item() @@ -35,6 +36,19 @@ def test_get_item_by_id(): result_item = vendor.get_by_id(test_id) assert result_item is item_custom_id +#########New Test +#@pytest.mark.skip +def test_get_by_id_for_none_id(): + #Arrange + + item_with_no_id = Item() + vendor = Vendor( + inventory=[Item(),Item(),item_with_no_id] + ) + result_item = vendor.get_by_id(None) + #Assert + assert result_item == None + #@pytest.mark.skip def test_get_item_by_id_no_matching(): @@ -47,6 +61,7 @@ def test_get_item_by_id_no_matching(): ) result_item = vendor.get_by_id(test_id) + assert result_item is None items = vendor.inventory diff --git a/tests/unit_tests/test_wave_07.py b/tests/unit_tests/test_wave_07.py index df919a64c..b3072d673 100644 --- a/tests/unit_tests/test_wave_07.py +++ b/tests/unit_tests/test_wave_07.py @@ -440,4 +440,43 @@ def test_choose_and_swap_items_with_other_vendor_missing_item(monkeypatch): assert len(jesse.inventory) == 3 assert item_d in jesse.inventory assert item_e in jesse.inventory - assert item_f in jesse.inventory \ No newline at end of file + assert item_f in jesse.inventory + + ###########New Test############################# +#@pytest.mark.skip +def test_choose_and_swap_items_with_other_inventory_category(monkeypatch): + # Arrange + item_a = Decor(id=123) + item_b = Electronics(id=456) + item_c = Decor(id=789) + tai = Vendor( + inventory=[item_a, item_b, item_c] + ) + + item_d = Clothing(id=321) + item_e = Decor(id=654) + item_f = Clothing(id=987) + jesse = Vendor( + inventory=[item_d, item_e, item_f] + ) + # Mock user input for picking item ids + input_responses = iter(["654","123"]) + monkeypatch.setattr('builtins.input', lambda msg: next(input_responses)) + + # Act + result = jesse.choose_and_swap_items(other_vendor=tai,category = Decor) + + # Assert + + assert result == True + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + assert item_a in jesse.inventory + assert item_b in tai.inventory + assert item_c in tai.inventory + assert item_f in jesse.inventory + assert item_d in jesse.inventory + assert item_e in tai.inventory + + + From b4e7080394fa12d3f3c69ffecf962f7aefaba1ca Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Thu, 8 Dec 2022 15:13:10 -0800 Subject: [PATCH 6/8] Added tests for Code Coverage1 --- tests/unit_tests/test_wave_02.py | 2 +- tests/unit_tests/test_wave_07.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index 9e7379cf6..58a9d9e70 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -36,7 +36,7 @@ def test_get_item_by_id(): result_item = vendor.get_by_id(test_id) assert result_item is item_custom_id -#########New Test +######### New Test for Code coverage #@pytest.mark.skip def test_get_by_id_for_none_id(): #Arrange diff --git a/tests/unit_tests/test_wave_07.py b/tests/unit_tests/test_wave_07.py index b3072d673..4dab61aed 100644 --- a/tests/unit_tests/test_wave_07.py +++ b/tests/unit_tests/test_wave_07.py @@ -442,7 +442,7 @@ def test_choose_and_swap_items_with_other_vendor_missing_item(monkeypatch): assert item_e in jesse.inventory assert item_f in jesse.inventory - ###########New Test############################# + ########### New Test for code covergae ############################# #@pytest.mark.skip def test_choose_and_swap_items_with_other_inventory_category(monkeypatch): # Arrange From 56c4469c9d29b0bb1c5cb45d685854a7f060d290 Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Thu, 8 Dec 2022 19:30:13 -0800 Subject: [PATCH 7/8] Added more assertions for test and comments --- swap_meet/item.py | 3 ++- swap_meet/vendor.py | 2 +- tests/unit_tests/test_wave_01.py | 3 ++- tests/unit_tests/test_wave_06.py | 21 +++++++++++++++++++-- tests/unit_tests/test_wave_07.py | 2 +- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 52b6dbfbf..53035abfd 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -11,7 +11,8 @@ def __init__(self,id = None,category = "", condition = 0 ): self.category = category def get_category(self): - return self.__class__.__name__#type(self).__name__ + return self.__class__.__name__#type(self).__name__ I would like to know which is best practice in these two + # Wave 3 def __str__(self): diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index e92a0bce8..5ed4fd305 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -10,7 +10,7 @@ def __init__(self, inventory = None): if inventory is not None: self.inventory = inventory else: - self.inventory = [] + self.inventory = []//I would like to know which is best practice in these two ''' def add(self,new_item): diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 8a9c72976..c8d3ffdfa 100644 --- a/tests/unit_tests/test_wave_01.py +++ b/tests/unit_tests/test_wave_01.py @@ -49,9 +49,10 @@ def test_removing_not_found_returns_none(): result = vendor.remove(item) #assert + assert result == None assert item not in vendor.inventory assert len(vendor.inventory) == 3 - assert result == None + #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 60ed787ae..8ae512109 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -34,6 +34,7 @@ def test_get_no_matching_items_by_category(): items = vendor.get_by_category("Electronics") # Assert assert items == [] + assert len(items) == 0 #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** @@ -111,9 +112,13 @@ def test_swap_best_by_category(): their_priority="Decor" ) #Assert + assert result == True assert item_c in jesse.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory assert item_f in tai.inventory - assert result == True + assert item_a in tai.inventory + assert item_b in tai.inventory assert len(tai.inventory) == 3 assert len(jesse.inventory) == 3 # raise Exception("Complete this test according to comments below.") @@ -149,9 +154,13 @@ def test_swap_best_by_category_reordered(): their_priority="Decor" ) #Assert + assert result == True assert item_c in jesse.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_b in tai.inventory + assert item_a in tai.inventory assert item_f in tai.inventory - assert result == True assert len(tai.inventory) == 3 assert len(jesse.inventory) == 3 #raise Exception("Complete this test according to comments below.") @@ -242,6 +251,10 @@ def test_swap_best_by_category_no_match_is_false(): assert result == False assert item_d in jesse.inventory assert item_f in jesse.inventory + assert item_e in jesse.inventory + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c in tai.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** @@ -278,6 +291,10 @@ def test_swap_best_by_category_no_other_match_is_false(): assert result == False assert item_a in tai.inventory assert item_c in tai.inventory + assert item_b in tai.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f in jesse.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** diff --git a/tests/unit_tests/test_wave_07.py b/tests/unit_tests/test_wave_07.py index 4dab61aed..8e1836886 100644 --- a/tests/unit_tests/test_wave_07.py +++ b/tests/unit_tests/test_wave_07.py @@ -444,7 +444,7 @@ def test_choose_and_swap_items_with_other_vendor_missing_item(monkeypatch): ########### New Test for code covergae ############################# #@pytest.mark.skip -def test_choose_and_swap_items_with_other_inventory_category(monkeypatch): +def test_choose_and_swap_items_with_other_inventory_category_given(monkeypatch): # Arrange item_a = Decor(id=123) item_b = Electronics(id=456) From 587a9c12f6efb31cb35dac66a3f6e3b453f7c250 Mon Sep 17 00:00:00 2001 From: Supriya Chavan Date: Sat, 17 Dec 2022 20:59:22 -0800 Subject: [PATCH 8/8] Worked on all the comments after grading --- swap_meet/clothing.py | 4 +- swap_meet/decor.py | 4 +- swap_meet/electronics.py | 4 +- swap_meet/item.py | 16 ++++---- swap_meet/vendor.py | 70 ++++++++++++++++---------------- tests/unit_tests/test_wave_01.py | 3 ++ tests/unit_tests/test_wave_03.py | 5 +++ tests/unit_tests/test_wave_06.py | 2 +- 8 files changed, 60 insertions(+), 48 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index f68f6ddf7..b00f6482c 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -2,8 +2,8 @@ import uuid from swap_meet.item import Item class Clothing(Item): - def __init__(self,id= None,category = "" ,condition = 0 ,fabric = "Unknown" ): - super().__init__(id,category,condition) + def __init__(self, id = None, category = "", condition = 0, fabric = "Unknown"): + super().__init__(id, category, condition) self.fabric = fabric def __str__(self): diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 01afd9b87..2908cd44c 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -2,8 +2,8 @@ import uuid from swap_meet.item import Item class Decor(Item): - def __init__(self,id = None,category = "" ,condition = 0,width = 0,length = 0 ): - super().__init__(id,category,condition) + def __init__(self, id = None, category = "", condition = 0, width = 0, length = 0): + super().__init__(id, category, condition) self.width = width self.length = length diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 07cd7047e..beeb9f105 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -2,8 +2,8 @@ import uuid from swap_meet.item import Item class Electronics(Item): - def __init__(self,id = None,category = "" ,condition = 0 ,type = "Unknown"): - super().__init__(id,category,condition) + def __init__(self, id = None, category = "", condition = 0, type = "Unknown"): + super().__init__(id, category, condition) self.type = type def __str__(self): diff --git a/swap_meet/item.py b/swap_meet/item.py index 53035abfd..ae6b14662 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,13 +1,14 @@ #Wave 2 , 5 +from math import floor import uuid #from swap_meet.vendor import Vendor class Item: - def __init__(self,id = None,category = "", condition = 0 ): - if id is None : - self.id = uuid.uuid4().int - else: - self.id = id - self.condition = condition + def __init__(self, id = None, category = "", condition = 0): + + if id is not None and not isinstance(id,int): + raise ValueError("ID must be an integer") + self.id = id if id else uuid.uuid4().int + self.condition = condition self.category = category def get_category(self): @@ -21,6 +22,7 @@ def __str__(self): # Wave 5 def condition_description(self): description_of_condition = ["Mint","Heavily Used","Like New","Gently Used","Best Used","New"] - return description_of_condition[self.condition] + + return description_of_condition[round(self.condition)] diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 5ed4fd305..25738df61 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -2,18 +2,12 @@ class Vendor: def __init__(self, inventory = None): - if inventory: - self.inventory = inventory - else: - self.inventory = [] - ''' if inventory is not None: self.inventory = inventory else: - self.inventory = []//I would like to know which is best practice in these two - ''' - - def add(self,new_item): + self.inventory = [] + + def add(self, new_item): self.inventory.append(new_item) return new_item @@ -23,7 +17,7 @@ def remove(self, item_to_remove): return item_to_remove return None - def get_by_id(self,id): + def get_by_id(self, id): if id is None: return None for item in self.inventory: @@ -32,7 +26,7 @@ def get_by_id(self,id): return None # Wave 3 - def swap_items(self,other_vendor,my_item,their_item): + def swap_items(self, other_vendor, my_item, their_item): if my_item not in self.inventory or their_item not in other_vendor.inventory: return False self.remove(my_item) @@ -42,14 +36,14 @@ def swap_items(self,other_vendor,my_item,their_item): return True # Wave 4 - def swap_first_item(self,other_vendor): + def swap_first_item(self, other_vendor): if not self.inventory or not other_vendor.inventory: return False self.swap_items(other_vendor,self.inventory[0],other_vendor.inventory[0]) return True # Wave 6 - def get_by_category(self,category): + def get_by_category(self, category): if not self.inventory: return [] list_items_by_category = [] @@ -59,14 +53,14 @@ def get_by_category(self,category): list_items_by_category.append(item) return list_items_by_category - def get_best_by_category(self,category): + def get_best_by_category(self, category): list_of_items_in_category = self.get_by_category(category) if not list_of_items_in_category: return None best_item = max(list_of_items_in_category,key = lambda item: item.condition) return best_item - def swap_best_by_category(self,other_vendor,my_priority,their_priority): + def swap_best_by_category(self, other_vendor, my_priority, their_priority): best_item_from_my_inventory = self.get_best_by_category(their_priority) best_item_from_their_inventory = other_vendor.get_best_by_category(my_priority) if not best_item_from_my_inventory or not best_item_from_their_inventory: @@ -75,31 +69,29 @@ def swap_best_by_category(self,other_vendor,my_priority,their_priority): return True #Wave 7 - def display_inventory(self,category = ""): + def display_inventory(self, category = ""): if self.inventory == []: print("No inventory to display.") - return None - - if category: - inventory_items_by_category = self.get_by_category(category) - if inventory_items_by_category: - for index in range(len(inventory_items_by_category)): - print(f"{index +1}. {inventory_items_by_category[index]}") - else: - print("No inventory to display.") + return + display_items = self.get_by_category(category) if category else self.inventory + + if not display_items: + print("No inventory to display.") + return else: - for index in range(len(self.inventory)): - print(f"{index +1}. {self.inventory[index]}") + for index, item in enumerate(display_items, start=1): + print(f"{index}. {item}") + return - def swap_by_id(self,other_vendor,my_item_id = None,their_item_id = None): + def swap_by_id(self, other_vendor, my_item_id = None, their_item_id = None): my_item = self.get_by_id(my_item_id) vendor_item = other_vendor.get_by_id(their_item_id) if not my_item or not vendor_item: return False - result = self.swap_items(other_vendor,my_item,vendor_item) + result = self.swap_items(other_vendor, my_item, vendor_item) return result - def choose_and_swap_items(self,other_vendor,category = ""): + def choose_and_swap_items(self, other_vendor, category = ""): if not category: self.display_inventory() other_vendor.display_inventory() @@ -112,8 +104,18 @@ def choose_and_swap_items(self,other_vendor,category = ""): return False self.swap_by_id(other_vendor,my_item_id,vendor_item_id) return True - - +''' + for index in range(len(display_items)): + print(f"{index +1}. {display_items[index]}") + return - - + if category: + inventory_items_by_category = self.get_by_category(category) + if inventory_items_by_category: + for index in range(len(inventory_items_by_category)): + print(f"{index +1}. {inventory_items_by_category[index]}") + else: + print("No inventory to display.") + else: + for index in range(len(self.inventory)): + print(f"{index +1}. {self.inventory[index]}")''' \ No newline at end of file diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index c8d3ffdfa..b6f57c0c9 100644 --- a/tests/unit_tests/test_wave_01.py +++ b/tests/unit_tests/test_wave_01.py @@ -52,6 +52,9 @@ def test_removing_not_found_returns_none(): assert result == None assert item not in vendor.inventory assert len(vendor.inventory) == 3 + assert "a" in vendor.inventory + assert "b" in vendor.inventory + assert "c" in vendor.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* diff --git a/tests/unit_tests/test_wave_03.py b/tests/unit_tests/test_wave_03.py index dbbdec553..6299044fa 100644 --- a/tests/unit_tests/test_wave_03.py +++ b/tests/unit_tests/test_wave_03.py @@ -134,6 +134,11 @@ def test_swap_items_from_their_empty_returns_false(): assert result == False assert nobodys_item not in fatimah.inventory assert nobodys_item not in jolie.inventory + assert fatimah.inventory == [item_a, item_b, item_c] + assert jolie.inventory == [] + assert item_a in fatimah.inventory + assert item_b in fatimah.inventory + assert item_c in fatimah.inventory #raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 8ae512109..c405e567d 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -142,7 +142,7 @@ def test_swap_best_by_category_reordered(): item_d = Clothing(condition=2.0) item_e = Decor(condition=4.0) - item_f = Clothing(condition=4.0) + item_f = Clothing(condition=3.3) jesse = Vendor( inventory=[item_f, item_e, item_d] )