diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index b8afdeb1e..9b124ebd4 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,2 +1,18 @@ -class Clothing: - pass \ No newline at end of file +from swap_meet.item import Item + +class Clothing(Item): + """Clothing class inherits id and condition from Item class and adds the + fabric attribute. + """ + + def __init__(self, fabric = "Unknown", id = None, condition = 0): + super().__init__(id, condition) + self.fabric = fabric + + def get_attribute(self): + return self.fabric + + def __str__(self): + summary = super().__str__() + class_summary = f"It is made from {self.fabric} fabric." + return ". ".join((summary, class_summary)) \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index eab7a9dbe..47c7f7ce1 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,2 +1,20 @@ -class Decor: - pass \ No newline at end of file +from swap_meet.item import Item + +class Decor(Item): + """Decor class inherits id and condition from Item class and adds the + width and length attributes. + """ + + def __init__(self, id = None, width = 0, length = 0, condition = 0): + super().__init__(id, condition) + self.width = width + self.length = length + + def get_attribute(self): + item_size = self.width * self.length + return item_size + + def __str__(self): + summary = super().__str__() + class_summary = f"It takes up a {self.width} by {self.length} sized space." + return ". ".join((summary, class_summary)) \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 2f9dff68a..61bcced7c 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,2 +1,18 @@ -class Electronics: - pass +from swap_meet.item import Item + +class Electronics(Item): + """Electronics class inherits id and condition from Item class and adds the + type attribute. + """ + + def __init__(self, id = None, type = "Unknown", condition = 0): + super().__init__(id, condition) + self.type = type + + def get_attribute(self): + return self.type + + def __str__(self): + summary = super().__str__() + class_summary = f"This is a {self.type} device." + return ". ".join((summary, class_summary)) diff --git a/swap_meet/item.py b/swap_meet/item.py index 560d759c2..050d46b64 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,2 +1,31 @@ +import uuid + class Item: - pass \ No newline at end of file + """Item class has a unique id and a condition.""" + + def __init__(self, id = None, condition = 0): + if id is not None: + if type(id) is not int: + raise ValueError("invalid id input") + else: + self.id = id + else: + self.id = uuid.uuid1().int + self.condition = condition + + def get_category(self): + return type(self).__name__ + + def condition_description(self): + switch = { + 0 : "gross", + 1 : "not good", + 2 : "okay", + 3 : "good", + 4 : "not bad", + 5 : "excellent" + } + return switch.get(self.condition) + + def __str__(self): + return f"An object of type {self.get_category()} with id {self.id}" \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 87302c056..170b94529 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,2 +1,160 @@ class Vendor: - pass \ No newline at end of file + """Vendor class contains an inventory list of Item objects and methods to + add, remove, retrieve, and swap items with other vendor instances. + """ + + def __init__(self, inventory = None): + """Creates Vendor instance""" + + self.inventory = inventory if inventory is not None else [] + + def add(self, item): + """Adds items to inventory. Returns item.""" + + self.inventory.append(item) + return item + + def remove(self, item): + """Removes item from inventory. Returns removed item or None if item + not found. + """ + + if item in self.inventory: + self.inventory.remove(item) + return item + return None + + def get_by_id(self, id): + """Gets item by item id from list of inventory. Returns Item object.""" + + item = next((item for item in self.inventory if item.id == id), None) + return item + + def get_by_category(self, category): + """Gets items in inventory that match category. Returns a list.""" + + inventory = self.inventory + items = [item for item in inventory if item.get_category() == category] + return items + + def get_best_by_category(self, category): + """Gets item with the best condition in category from inventory. Returns + Item object. + """ + + items = self.get_by_category(category) + + if not items: + return None + + max_value = max([item.condition for item in items]) + + item = next( + (item for item in items if item.condition == max_value), None) + + return item + + def swap_best_by_category(self, other_vendor, my_priority, their_priority): + """Swaps best items in category from two vendors. Returns boolean.""" + + my_item = self.get_best_by_category(their_priority) + their_item = other_vendor.get_best_by_category(my_priority) + + if not their_item or not my_item: + return False + + swapped = self.swap_items(other_vendor, my_item, their_item) + return swapped + + def get_by_category_attribute(self, category, attribute): + """Gets items from inventory that match category and item attribute. + Returns a list. + """ + + category_items = self.get_by_category(category) + items = [item for item in category_items + if attribute == item.get_attribute()] + + return items + + def swap_by_attribute(self, other_vendor, category, attribute): + """Swaps items that match category and item attribute from 2 vendors. + Returns boolean. + """ + + my_items = self.get_by_category_attribute(category, attribute) + their_items = other_vendor.get_by_category_attribute( + category, attribute) + + if not their_items or not my_items: + return False + + my_item = my_items[0] + their_item = their_items[0] + swapped = self.swap_items(other_vendor, my_item, their_item) + return swapped + + def swap_items(self, other_vendor, my_item, their_item): + """Swaps items from 2 vendors. Returns boolean.""" + + inventory = self.inventory + if my_item not in inventory or their_item not in other_vendor.inventory: + return False + + self.inventory.remove(my_item) + other_vendor.inventory.append(my_item) + + other_vendor.inventory.remove(their_item) + self.inventory.append(their_item) + + return True + + def swap_first_item(self, other_vendor): + """Swaps first item in inventory from 2 vendors. Returns boolean.""" + + if not self.inventory or not other_vendor.inventory: + return False + + my_item = self.inventory[0] + their_item = other_vendor.inventory[0] + swapped = self.swap_items(other_vendor, my_item, their_item) + return swapped + + def swap_by_id(self, other_vendor, my_item_id, their_item_id): + """Swaps items by id from 2 vendors. Returns boolean.""" + + my_item = self.get_by_id(my_item_id) + their_item = other_vendor.get_by_id(their_item_id) + + if not their_item or not my_item: + return False + + swapped = self.swap_items(other_vendor, my_item, their_item) + return swapped + + def choose_and_swap_items(self, other_vendor, category = ""): + """Displays inventory from 2 vendors and allows user to select items to + swap by id. Returns boolean. + """ + + self.display_inventory(category) + other_vendor.display_inventory(category) + + my_item_id = int(input("Enter item from your inventory by id: ")) + their_item_id = int(input("Enter item from their inventory by id: ")) + swapped = self.swap_by_id(other_vendor, my_item_id, their_item_id) + return swapped + + def display_inventory(self, category = ""): + """Displays Vendor inventory with item descriptions.""" + + if not self.inventory: + print("No inventory to display.") + return + + items = self.get_by_category(category) if category else self.inventory + if items: + for i, item in enumerate(items, 1): + print(f"{i}. {str(item)}") + else: + print("No inventory to display.") \ No newline at end of file diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index 404641a86..f69b98916 100644 --- a/tests/integration_tests/test_wave_01_02_03.py +++ b/tests/integration_tests/test_wave_01_02_03.py @@ -2,7 +2,7 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip @pytest.mark.integration_test def test_integration_wave_01_02_03(): # make a 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..d1a2a72ee 100644 --- a/tests/integration_tests/test_wave_04_05_06_07.py +++ b/tests/integration_tests/test_wave_04_05_06_07.py @@ -4,7 +4,7 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip @pytest.mark.integration_test def test_integration_wave_04_05_06(capfd): camila = Vendor() diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 019ff1d58..7652446e3 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( @@ -49,7 +49,10 @@ def test_removing_not_found_returns_none(): result = vendor.remove(item) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + assert len(vendor.inventory) == 3 + assert item not in vendor.inventory + assert result is None diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index f4b512222..22d02624f 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -2,30 +2,35 @@ 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_items_use_string_as_custom_id_raises_value_error(): + with pytest.raises(ValueError): + item = Item(id="142") + +# @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 +41,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..8fe889af1 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() @@ -131,7 +131,10 @@ def test_swap_items_from_their_empty_returns_false(): result = fatimah.swap_items(jolie, item_b, nobodys_item) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + assert len(fatimah.inventory) == 3 + assert len(jolie.inventory) == 0 + assert not result diff --git a/tests/unit_tests/test_wave_04.py b/tests/unit_tests/test_wave_04.py index 87addbbf6..135388335 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..e03c5243c 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..dfbed1904 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() @@ -33,12 +33,14 @@ def test_get_no_matching_items_by_category(): items = vendor.get_by_category("Electronics") - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + assert items == [] + assert len(items) == 0 -@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 +56,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 +69,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 +86,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 @@ -110,16 +112,30 @@ def test_swap_best_by_category(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* # Assertions should check: # - That the results is truthy + assert result == True + # - That tai and jesse's inventories are the correct length + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + # - That all the correct items are in tai and jesse's inventories, including the items which were swapped from one vendor to the other + assert item_f in tai.inventory + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c not in tai.inventory -@pytest.mark.skip + assert item_c in jesse.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f not in jesse.inventory + +# @pytest.mark.skip def test_swap_best_by_category_reordered(): # Arrange item_a = Decor(condition=2.0) @@ -143,16 +159,30 @@ def test_swap_best_by_category_reordered(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* # Assertions should check: - # - That result is truthy + # - That the results is truthy + assert result == True + # - That tai and jesse's inventories are the correct length + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + # - That all the correct items are in tai and jesse's inventories, and that the items that were swapped are not there + assert item_f in tai.inventory + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c not in tai.inventory -@pytest.mark.skip + assert item_c in jesse.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f not in jesse.inventory + +# @pytest.mark.skip def test_swap_best_by_category_no_inventory_is_false(): tai = Vendor( inventory=[] @@ -178,7 +208,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 +234,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) @@ -228,16 +258,30 @@ def test_swap_best_by_category_no_match_is_false(): their_priority="Clothing" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* # Assertions should check: - # - That result is falsy + + # - That the results is falsey + assert result == False + # - That tai and jesse's inventories are the correct length - # - That all the correct items are in tai and jesse's inventories + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 -@pytest.mark.skip + # - That all the correct items are in tai and jesse's inventories + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c in tai.inventory + + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f in jesse.inventory + + +# @pytest.mark.skip def test_swap_best_by_category_no_other_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -261,11 +305,23 @@ def test_swap_best_by_category_no_other_match_is_false(): their_priority="Decor" ) - raise Exception("Complete this test according to comments below.") + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* # Assertions should check: - # - That result is falsy + # - That the results is falsey + assert result == False + # - That tai and jesse's inventories are the correct length - # - That all the correct items are in tai and jesse's inventories + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + + # - That all the correct items are in tai and jesse's inventories + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_c in tai.inventory + + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f in jesse.inventory \ No newline at end of file diff --git a/tests/unit_tests/test_wave_07.py b/tests/unit_tests/test_wave_07.py index 005b82ecc..49fadff9e 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) @@ -440,4 +440,200 @@ 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 + + +# ~~~~~ swap_by_attribute Tests ~~~~~ +def test_swap_by_attribute_success_returns_true(): + # Arrange + item_a = Decor(width=2, length=4) + item_b = Electronics(type="radio") + item_c = Decor(width=1, length=3) + jesse = Vendor( + inventory=[item_a, item_b, item_c] + ) + + item_d = Electronics(type="radio") + item_e = Decor(width=4, length=2) + item_f = Clothing(fabric="denim") + tai = Vendor( + inventory=[item_d, item_e, item_f] + ) + + # Act + result = jesse.swap_by_attribute( + other_vendor=tai, + category="Decor", + attribute=8 + ) + + # Assert + assert result == True + + assert len(jesse.inventory) == 3 + assert item_e in jesse.inventory + assert item_b in jesse.inventory + assert item_c in jesse.inventory + + assert len(tai.inventory) == 3 + assert item_d in tai.inventory + assert item_a in tai.inventory + assert item_f in tai.inventory + +def test_swap_by_attribute_with_caller_empty_inventory_returns_false(): + # Arrange + tai = Vendor(inventory=[]) + + item_d = Electronics(type="radio") + item_e = Decor(width=4, length=3) + item_f = Clothing(fabric="denim") + jesse = Vendor( + inventory=[item_d, item_e, item_f] + ) + + # Act + result = tai.swap_by_attribute( + other_vendor=jesse, + category="Electronics", + attribute="radio" + ) + + # Assert + assert result == False + assert len(tai.inventory) == 0 + + assert len(jesse.inventory) == 3 + assert item_d in jesse.inventory + assert item_e in jesse.inventory + assert item_f in jesse.inventory + +def test_swap_by_attribute_with_other_empty_inventory_returns_false(): + # Arrange + item_a = Decor(width=2, length=4) + item_b = Electronics(type="radio") + item_c = Decor(width=1, length=3) + jesse = Vendor( + inventory=[item_a, item_b, item_c] + ) + + tai = Vendor(inventory=[]) + + # Act + result = jesse.swap_by_attribute( + other_vendor=tai, + category="Electronics", + attribute="radio" + ) + + # Assert + assert result == False + + assert len(jesse.inventory) == 3 + assert item_a in jesse.inventory + assert item_b in jesse.inventory + assert item_c in jesse.inventory + + assert len(tai.inventory) == 0 + +def test_swap_by_attribute_fails_if_caller_missing_item(): + # Arrange + item_a = Decor(width=2, length=4) + item_b = Electronics(type="laptop") + item_c = Decor(width=1, length=3) + jesse = Vendor( + inventory=[item_a, item_b, item_c] + ) + + item_d = Electronics(type="radio") + item_e = Decor(width=4, length=3) + item_f = Clothing(fabric="denim") + tai = Vendor( + inventory=[item_d, item_e, item_f] + ) + + # Act + result = jesse.swap_by_attribute( + other_vendor=tai, + category="Electronics", + attribute="radio" + ) + + # Assert + assert result == False + + assert len(jesse.inventory) == 3 + assert item_a in jesse.inventory + assert item_b in jesse.inventory + assert item_c in jesse.inventory + + assert len(tai.inventory) == 3 + assert item_d in tai.inventory + assert item_e in tai.inventory + assert item_f in tai.inventory + +def test_swap_by_attribute_fails_if_other_missing_item(): + # Arrange + item_a = Decor(width=2, length=4) + item_b = Electronics(type="laptop") + item_c = Decor(width=1, length=3) + jesse = Vendor( + inventory=[item_a, item_b, item_c] + ) + + item_d = Electronics(type="radio") + item_e = Decor(width=4, length=3) + item_f = Clothing(fabric="denim") + tai = Vendor( + inventory=[item_d, item_e, item_f] + ) + + # Act + result = jesse.swap_by_attribute( + other_vendor=tai, + category="Electronics", + attribute="laptop" + ) + + # Assert + assert result == False + + assert len(jesse.inventory) == 3 + assert item_a in jesse.inventory + assert item_b in jesse.inventory + assert item_c in jesse.inventory + + assert len(tai.inventory) == 3 + assert item_d in tai.inventory + assert item_e in tai.inventory + assert item_f in tai.inventory + +# ~~~~~ get_by_category_attribute Tests ~~~~~ + +def test_get_items_by_category_attribute(): + item_a = Clothing(fabric="silk") + item_b = Electronics(type="radio") + item_c = Clothing(fabric="silk") + item_d = Decor(width=1, length=2) + item_e = Item(condition=0) + vendor = Vendor( + inventory=[item_a, item_b, item_c, item_d, item_e] + ) + + items = vendor.get_by_category_attribute("Clothing", "silk") + + assert len(items) == 2 + assert item_a in items + assert item_c in items + +def test_get_no_matching_items_by_category_attribute(): + item_a = Decor(width=2, length=4) + item_b = Electronics(type="laptop") + item_c = Decor(width=1, length=3) + vendor = Vendor( + inventory=[item_a, item_b, item_c] + ) + + items = vendor.get_by_category_attribute("Electronics", "radio") + + assert items == [] + assert len(items) == 0 \ No newline at end of file