From 110b272cc87d92fce2a38c7994080305ece52647 Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Wed, 6 Apr 2022 12:09:55 -0700 Subject: [PATCH 01/13] Oooops forgot to commit updates... updated vendor and item classes, passing all tests in waves 01 and 02, finsihed wave assertions --- swap_meet/item.py | 4 +++- swap_meet/vendor.py | 24 +++++++++++++++++++++++- tests/unit_tests/test_wave_01.py | 16 ++++++++++------ tests/unit_tests/test_wave_02.py | 13 +++++++++---- 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 560d759c2..d68afb600 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,2 +1,4 @@ class Item: - pass \ No newline at end of file + + def __init__(self, category=""): + self.category = category diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 87302c056..e7cf0982a 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,2 +1,24 @@ +from unicodedata import category +from swap_meet.item import Item + class Vendor: - pass \ No newline at end of file + + def __init__(self, inventory = []): + self.inventory = inventory + + def add(self, item): + self.inventory.append(item) + return item + + def remove(self, item): + if item not in self.inventory: + return False + self.inventory.remove(item) + return item + + def get_by_category(self, category_str): + category_items = [] + for item in self.inventory: + if item.category == category_str: + category_items.append(item) + return category_items diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 58478ccf9..399bdb747 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_is_false(): item = "item to remove" vendor = Vendor( @@ -49,7 +49,11 @@ def test_removing_not_found_is_false(): result = vendor.remove(item) - raise Exception("Complete this test according to comments below.") + assert len(vendor.inventory) == 3 + 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_02.py b/tests/unit_tests/test_wave_02.py index 3d7060d7c..6b1331427 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -2,12 +2,12 @@ from swap_meet.vendor import Vendor from swap_meet.item import Item -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_blank_default_category(): item = Item() assert item.category == "" -@pytest.mark.skip +# @pytest.mark.skip def test_get_items_by_category(): item_a = Item(category="clothing") item_b = Item(category="electronics") @@ -23,7 +23,7 @@ def test_get_items_by_category(): assert item_c in items assert item_b not in items -@pytest.mark.skip +# @pytest.mark.skip def test_get_no_matching_items_by_category(): item_a = Item(category="clothing") item_b = Item(category="clothing") @@ -34,7 +34,12 @@ def test_get_no_matching_items_by_category(): items = vendor.get_by_category("electronics") - raise Exception("Complete this test according to comments below.") + assert len(items) == 0 + assert item_a not in items + assert item_b not in items + assert item_c not in items + + # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* From 9ea6bf5bc35c1278948f2908b9efb4bd47a3d482 Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Wed, 6 Apr 2022 13:13:54 -0700 Subject: [PATCH 02/13] deleted 1 accidental and 1 unnecessary import from vendor.py --- swap_meet/vendor.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index e7cf0982a..de86257af 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,6 +1,3 @@ -from unicodedata import category -from swap_meet.item import Item - class Vendor: def __init__(self, inventory = []): From af510a3f3df4bfb3ffe6e2547a9937c68e9ced52 Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Wed, 6 Apr 2022 14:05:31 -0700 Subject: [PATCH 03/13] use __str__ method to make Item return the default str 'Hello World!', create Vendor method swap_item, and passed all tests in wave 03... refactored swap_items from nested if statements for Vendor and other Vendor to one if statement using 'and' --- swap_meet/item.py | 3 +++ swap_meet/vendor.py | 31 +++++++++++++++++++++++++++++++ tests/unit_tests/test_wave_03.py | 12 ++++++------ 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index d68afb600..e188fcb76 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -2,3 +2,6 @@ class Item: def __init__(self, category=""): self.category = category + + def __str__(self): + return "Hello World!" diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index de86257af..0d39f6ade 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,3 +1,5 @@ +from swap_meet.item import Item + class Vendor: def __init__(self, inventory = []): @@ -19,3 +21,32 @@ def get_by_category(self, category_str): if item.category == category_str: category_items.append(item) return category_items + + def swap_items(self, swap_vendor, my_item, their_item): + + """ + Instances of Vendor will have this method swap_items: + * takes 3 arguments; another Vendor, my_item, their_item + * removes my_item from this Vendor's inventory + * removes their_item from the other Vendor's inventory + * returns True if both inventories have the respective items + * returns False if one of the items is not contained in respective inventories + """ + + if my_item in self.inventory and their_item in swap_vendor.inventory: + self.inventory.remove(my_item) + swap_vendor.inventory.append(my_item) + self.inventory.append(their_item) + swap_vendor.inventory.remove(their_item) + return True + + return False + + # if my_item in self.inventory: + # self.inventory.remove(my_item) + # swap_vendor.inventory.append(my_item) + # if their_item in swap_vendor.inventory: + # self.inventory.append(their_item) + # swap_vendor.inventory.remove(their_item) + # return True + # return False diff --git a/tests/unit_tests/test_wave_03.py b/tests/unit_tests/test_wave_03.py index 0300b638f..2d573fa52 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(): item = Item() @@ -10,7 +10,7 @@ def test_item_overrides_to_string(): assert stringified_item == "Hello World!" -@pytest.mark.skip +# @pytest.mark.skip def test_swap_items_returns_true(): item_a = Item(category="clothing") item_b = Item(category="clothing") @@ -38,7 +38,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(category="clothing") item_b = Item(category="clothing") @@ -65,7 +65,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(category="clothing") item_b = Item(category="clothing") @@ -92,7 +92,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=[] @@ -112,7 +112,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(category="clothing") item_b = Item(category="clothing") From 6ad5a8c969acd59a328588428a653c3350279045 Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Wed, 6 Apr 2022 14:07:01 -0700 Subject: [PATCH 04/13] unsaved Vendor file during last push --- swap_meet/vendor.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 0d39f6ade..865577910 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -39,14 +39,4 @@ def swap_items(self, swap_vendor, my_item, their_item): self.inventory.append(their_item) swap_vendor.inventory.remove(their_item) return True - return False - - # if my_item in self.inventory: - # self.inventory.remove(my_item) - # swap_vendor.inventory.append(my_item) - # if their_item in swap_vendor.inventory: - # self.inventory.append(their_item) - # swap_vendor.inventory.remove(their_item) - # return True - # return False From f7d2a11ee8facf95a615f0c20f9ea3fa03fb3b84 Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Wed, 6 Apr 2022 14:53:37 -0700 Subject: [PATCH 05/13] wrote Vendor method swap_first_item and passed all tests in wave 04 --- swap_meet/vendor.py | 32 +++++++++++++++++++++++++------- tests/unit_tests/test_wave_04.py | 6 +++--- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 865577910..67ee15a35 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -23,14 +23,13 @@ def get_by_category(self, category_str): return category_items def swap_items(self, swap_vendor, my_item, their_item): - """ - Instances of Vendor will have this method swap_items: - * takes 3 arguments; another Vendor, my_item, their_item - * removes my_item from this Vendor's inventory - * removes their_item from the other Vendor's inventory - * returns True if both inventories have the respective items - * returns False if one of the items is not contained in respective inventories + All instances of Vendor will have this method swap_items: + - takes 3 arguments; another Vendor, my_item, their_item + - removes my_item from this Vendor's inventory + - removes their_item from the other Vendor's inventory + - returns True if both inventories have the respective items + - returns False if one of the items is not contained in respective inventories """ if my_item in self.inventory and their_item in swap_vendor.inventory: @@ -40,3 +39,22 @@ def swap_items(self, swap_vendor, my_item, their_item): swap_vendor.inventory.remove(their_item) return True return False + + def swap_first_item(self, swap_vendor): + """ + All instances of Vendor will have this method swap_first_items: + - takes one argument; another Vendor + - method considers the first item in the instance's inventory, and the first item in the friend's inventory + - removes the first item from its inventory, and adds the friend's first item + - removes the first item from the friend's inventory, and adds the instances first item + - returns True + - either itself or the friend have an empty inventory, the method returns False + """ + while len(self.inventory) >= 1 and len(swap_vendor.inventory) >= 1: + self_first_item = self.inventory[0] + swap_vendor_first_item = swap_vendor.inventory[0] + + self.inventory[0] = swap_vendor_first_item + swap_vendor.inventory[0] = self_first_item + return True + return False \ No newline at end of file diff --git a/tests/unit_tests/test_wave_04.py b/tests/unit_tests/test_wave_04.py index 8190a4ebb..4ef21ff8e 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(category="clothing") item_b = Item(category="clothing") @@ -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(category="clothing") item_b = Item(category="clothing") From 38164279278a9ba69a15040e861c87a1c55f7e1e Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Wed, 6 Apr 2022 15:11:37 -0700 Subject: [PATCH 06/13] create new classes (as new instances of Item) Clothing, Decor, Electronics passing first three tests in wave 05 --- swap_meet/clothing.py | 11 +++++++++-- swap_meet/decor.py | 11 +++++++++-- swap_meet/electronics.py | 9 ++++++++- tests/unit_tests/test_wave_05.py | 6 +++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index b8afdeb1e..ee40f4541 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,2 +1,9 @@ -class Clothing: - pass \ No newline at end of file +from swap_meet.item import Item + +class Clothing(Item): + + def __init__(self): + self.category = "Clothing" + + def __str__(self): + return "The finest clothing you could wear." diff --git a/swap_meet/decor.py b/swap_meet/decor.py index eab7a9dbe..764e6e3a2 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,2 +1,9 @@ -class Decor: - pass \ No newline at end of file +from swap_meet.item import Item + +class Decor(Item): + + def __init__(self): + self.category = "Decor" + + def __str__(self): + return "Something to decorate your space." diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 2f9dff68a..3707893c5 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,2 +1,9 @@ +from swap_meet.item import Item + class Electronics: - pass + + def __init__(self): + self.category = "Electronics" + + def __str__(self): + return "A gadget full of buttons and secrets." diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 7abea06cd..414392420 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -3,19 +3,19 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip def test_clothing_has_default_category_and_to_str(): cloth = Clothing() assert cloth.category == "Clothing" assert str(cloth) == "The finest clothing you could wear." -@pytest.mark.skip +# @pytest.mark.skip def test_decor_has_default_category_and_to_str(): decor = Decor() assert decor.category == "Decor" assert str(decor) == "Something to decorate your space." -@pytest.mark.skip +# @pytest.mark.skip def test_electronics_has_default_category_and_to_str(): electronics = Electronics() assert electronics.category == "Electronics" From 5032273eebf1d5a9c42f843279fd8a530628106b Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 14:16:13 -0700 Subject: [PATCH 07/13] wrote condition_description method in Item to be inherited by all instances, was able to pass all wave 05 tests except for test_items_have_condition_as_float - program stalling when it gets to this test --- swap_meet/clothing.py | 7 ++++--- swap_meet/decor.py | 6 ++++-- swap_meet/electronics.py | 8 +++++--- swap_meet/item.py | 22 +++++++++++++++++++++- tests/unit_tests/test_wave_05.py | 3 ++- 5 files changed, 36 insertions(+), 10 deletions(-) diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index ee40f4541..af9460b9b 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,9 +1,10 @@ from swap_meet.item import Item class Clothing(Item): - - def __init__(self): - self.category = "Clothing" + """A child class of Item, representing aspects of an Item specific to Clothing""" + def __init__(self, category="Clothing", condition=0.0): + self.category = category + self.condition = condition def __str__(self): return "The finest clothing you could wear." diff --git a/swap_meet/decor.py b/swap_meet/decor.py index 764e6e3a2..a09819d70 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,9 +1,11 @@ from swap_meet.item import Item class Decor(Item): + """A child class of Item, representing aspects of an Item specific to Decor""" + def __init__(self, category="Decor", condition=0.0): + self.category = category + self.condition = condition - def __init__(self): - self.category = "Decor" def __str__(self): return "Something to decorate your space." diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 3707893c5..7410a12ca 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,9 +1,11 @@ from swap_meet.item import Item -class Electronics: +class Electronics(Item): + """A child class of Item, representing aspects of an Item specific to Electronics""" - def __init__(self): - self.category = "Electronics" + def __init__(self, category="Electronics", condition=0.0): + self.category = category + self.condition = condition def __str__(self): return "A gadget full of buttons and secrets." diff --git a/swap_meet/item.py b/swap_meet/item.py index e188fcb76..8f9a27c58 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,7 +1,27 @@ class Item: - def __init__(self, category=""): + def __init__(self, category="", condition=0.0): self.category = category + self.condition = condition def __str__(self): return "Hello World!" + + def condition_description(self): + condition = self.condition + while condition >= 0 and condition <= 5: + if condition in range(0, 1): + return "Yikes..." + if condition in range(1, 2): + return "Poor" + if condition in range(2, 3): + return "Mint" + if condition in range(3, 4): + return "Fine Enough" + if condition in range(4, 5): + return "Good" + if condition == 5: + return "Perfect!" + + def __repr__(self): + return f"condition description={self.condition_description}" \ No newline at end of file diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 414392420..9812b40b6 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -29,9 +29,10 @@ def test_items_have_condition_as_float(): Electronics(condition=3.5) ] for item in items: + print(item.condition_description()) 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), From b72885b5a4bbec69d715edbdca54f44e7247406e Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 15:05:25 -0700 Subject: [PATCH 08/13] created class method get_best_category and passed tests 1 - 3 in wave 06 --- swap_meet/vendor.py | 17 ++++++++++++++++- tests/unit_tests/test_wave_06.py | 6 +++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 67ee15a35..f70a393ae 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -57,4 +57,19 @@ def swap_first_item(self, swap_vendor): self.inventory[0] = swap_vendor_first_item swap_vendor.inventory[0] = self_first_item return True - return False \ No newline at end of file + return False + + def get_best_by_category(self, category_str): + """get the item with the best condition in a certain category""" + category_items = self.get_by_category(category_str) + if len(category_items) > 1: + best_item = category_items[0] + else: + return None + + for item in category_items: + if item.condition > best_item.condition: + best_item = item + return best_item + + # def swap_best_by_category() \ No newline at end of file diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 1f7065ab4..7ea170bb6 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -4,7 +4,7 @@ from swap_meet.decor import Decor from swap_meet.electronics import Electronics -@pytest.mark.skip +# @pytest.mark.skip def test_best_by_category(): item_a = Clothing(condition=2.0) item_b = Decor(condition=2.0) @@ -20,7 +20,7 @@ def test_best_by_category(): assert best_item.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) @@ -33,7 +33,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) From e0bde7b0d5b208e234adbcb1ec948d0923e5e7ee Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 18:20:49 -0700 Subject: [PATCH 09/13] conpleted swap_best_by_category and passed remaining tests in wave 06. Also made a temporary fix to Item class issue that was causing a stall in wave 05 tests (changed while loop). Still needs refactoring. --- swap_meet/item.py | 5 +--- swap_meet/vendor.py | 17 +++++++---- tests/unit_tests/test_wave_06.py | 50 +++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 8f9a27c58..47f78813f 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -9,7 +9,7 @@ def __str__(self): def condition_description(self): condition = self.condition - while condition >= 0 and condition <= 5: + if condition >= 0 and condition <= 5: if condition in range(0, 1): return "Yikes..." if condition in range(1, 2): @@ -22,6 +22,3 @@ def condition_description(self): return "Good" if condition == 5: return "Perfect!" - - def __repr__(self): - return f"condition description={self.condition_description}" \ No newline at end of file diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index f70a393ae..9237e896c 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -24,14 +24,11 @@ def get_by_category(self, category_str): def swap_items(self, swap_vendor, my_item, their_item): """ - All instances of Vendor will have this method swap_items: - - takes 3 arguments; another Vendor, my_item, their_item - removes my_item from this Vendor's inventory - removes their_item from the other Vendor's inventory - returns True if both inventories have the respective items - returns False if one of the items is not contained in respective inventories """ - if my_item in self.inventory and their_item in swap_vendor.inventory: self.inventory.remove(my_item) swap_vendor.inventory.append(my_item) @@ -42,8 +39,6 @@ def swap_items(self, swap_vendor, my_item, their_item): def swap_first_item(self, swap_vendor): """ - All instances of Vendor will have this method swap_first_items: - - takes one argument; another Vendor - method considers the first item in the instance's inventory, and the first item in the friend's inventory - removes the first item from its inventory, and adds the friend's first item - removes the first item from the friend's inventory, and adds the instances first item @@ -72,4 +67,14 @@ def get_best_by_category(self, category_str): best_item = item return best_item - # def swap_best_by_category() \ No newline at end of file + def swap_best_by_category(self, other, my_priority, their_priority): + """swap the best item of certain categories with another Vendor + my_priority = a category that the Vendor wants to receive + their_priority = a category that the other wants to receive""" + my_best_swap = self.get_best_by_category(their_priority) + their_best_swap = other.get_best_by_category(my_priority) + + if my_best_swap and their_best_swap: + self.swap_items(other, my_best_swap, their_best_swap) + return True + return False diff --git a/tests/unit_tests/test_wave_06.py b/tests/unit_tests/test_wave_06.py index 7ea170bb6..395d0e8e0 100644 --- a/tests/unit_tests/test_wave_06.py +++ b/tests/unit_tests/test_wave_06.py @@ -50,7 +50,7 @@ def test_best_by_category_with_duplicates(): assert best_item.category == "Clothing" assert best_item.condition == pytest.approx(4.0) -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category(): # Arrange # me @@ -76,7 +76,7 @@ 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 ********** # ********************************************************************* @@ -84,8 +84,13 @@ def test_swap_best_by_category(): # - That the results is truthy # - 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 + assert result is True + assert len(jesse.inventory) == 3 + assert len(tai.inventory) == 3 + assert item_c in jesse.inventory + assert item_f in tai.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_reordered(): # Arrange item_a = Decor(condition=2.0) @@ -109,7 +114,7 @@ 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 ********** # ********************************************************************* @@ -117,8 +122,13 @@ def test_swap_best_by_category_reordered(): # - That result is truthy # - 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 + assert result is True + assert len(jesse.inventory) == 3 + assert len(tai.inventory) == 3 + assert item_c in jesse.inventory + assert item_f in tai.inventory -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_inventory_is_false(): tai = Vendor( inventory=[] @@ -144,7 +154,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) @@ -170,7 +180,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) @@ -194,7 +204,7 @@ 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 ********** # ********************************************************************* @@ -202,8 +212,18 @@ def test_swap_best_by_category_no_match_is_false(): # - That result is falsy # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories + assert result == False + assert len(jesse.inventory) == 3 + assert len(tai.inventory) == 3 + assert item_f not in tai.inventory + assert item_f in jesse.inventory + 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 -@pytest.mark.skip +# @pytest.mark.skip def test_swap_best_by_category_no_other_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -227,7 +247,7 @@ 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 ********** # ********************************************************************* @@ -235,3 +255,13 @@ def test_swap_best_by_category_no_other_match_is_false(): # - That result is falsy # - That tai and jesse's inventories are the correct length # - That all the correct items are in tai and jesse's inventories + assert result == False + assert len(jesse.inventory) == 3 + assert len(tai.inventory) == 3 + assert item_c not in jesse.inventory + assert item_c in tai.inventory + assert item_a 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 \ No newline at end of file From f1f1eb217f95d8d56e476f8918f9e8be77ed07d3 Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 20:47:36 -0700 Subject: [PATCH 10/13] refactored condition_description in Item class to fix bug and achieve 100% code coverage --- swap_meet/item.py | 25 ++++++++----------- swap_meet/vendor.py | 1 - tests/integration_tests/test_wave_01_02_03.py | 1 + tests/unit_tests/test_wave_01.py | 6 ++--- tests/unit_tests/test_wave_02.py | 10 ++++---- tests/unit_tests/test_wave_05.py | 2 +- 6 files changed, 21 insertions(+), 24 deletions(-) diff --git a/swap_meet/item.py b/swap_meet/item.py index 47f78813f..36300e38d 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -8,17 +8,14 @@ def __str__(self): return "Hello World!" def condition_description(self): - condition = self.condition - if condition >= 0 and condition <= 5: - if condition in range(0, 1): - return "Yikes..." - if condition in range(1, 2): - return "Poor" - if condition in range(2, 3): - return "Mint" - if condition in range(3, 4): - return "Fine Enough" - if condition in range(4, 5): - return "Good" - if condition == 5: - return "Perfect!" + condition = int(self.condition) + descriptions = { + 0: 'You should probably pass on this one...', + 1: 'Poor', + 2: 'Meh', + 3: 'Good', + 4: 'Great', + 5: 'Brand spankin new' + } + return descriptions[condition] + diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 9237e896c..35327ce59 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -39,7 +39,6 @@ def swap_items(self, swap_vendor, my_item, their_item): def swap_first_item(self, swap_vendor): """ - - method considers the first item in the instance's inventory, and the first item in the friend's inventory - removes the first item from its inventory, and adds the friend's first item - removes the first item from the friend's inventory, and adds the instances first item - returns True diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index 9912414da..4325f5add 100644 --- a/tests/integration_tests/test_wave_01_02_03.py +++ b/tests/integration_tests/test_wave_01_02_03.py @@ -7,6 +7,7 @@ def test_integration_wave_01_02_03(): # make a vendor vendor = Vendor() + print(vendor.inventory) assert len(vendor.inventory) == 0 # add an item diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 399bdb747..990efa53a 100644 --- a/tests/unit_tests/test_wave_01.py +++ b/tests/unit_tests/test_wave_01.py @@ -49,11 +49,11 @@ def test_removing_not_found_is_false(): result = vendor.remove(item) - assert len(vendor.inventory) == 3 - assert result == False - # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + + assert len(vendor.inventory) == 3 + assert result == False \ No newline at end of file diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index 6b1331427..7e68c4432 100644 --- a/tests/unit_tests/test_wave_02.py +++ b/tests/unit_tests/test_wave_02.py @@ -34,12 +34,12 @@ def test_get_no_matching_items_by_category(): items = vendor.get_by_category("electronics") - assert len(items) == 0 - assert item_a not in items - assert item_b not in items - assert item_c not in items - # raise Exception("Complete this test according to comments below.") # ********************************************************************* # ****** Complete Assert Portion of this test ********** # ********************************************************************* + + assert len(items) == 0 + assert item_a not in items + assert item_b not in items + assert item_c not in items \ No newline at end of file diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 9812b40b6..3137d2671 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -21,7 +21,7 @@ def test_electronics_has_default_category_and_to_str(): assert electronics.category == "Electronics" assert str(electronics) == "A gadget full of buttons and secrets." -@pytest.mark.skip +# @pytest.mark.skip def test_items_have_condition_as_float(): items = [ Clothing(condition=3.5), From f478e257906c28dbe7ede608e3e6a66c46d57eca Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 22:20:22 -0700 Subject: [PATCH 11/13] optional enhancements: created get_newest_by_category helper function and swap_newest_by_category, created new test folder test_optional_enhancement to test additions --- swap_meet/clothing.py | 3 +- swap_meet/decor.py | 4 +- swap_meet/electronics.py | 3 +- swap_meet/item.py | 3 +- swap_meet/vendor.py | 27 +++- tests/unit_tests/test_optional_enhancement.py | 116 ++++++++++++++++++ 6 files changed, 149 insertions(+), 7 deletions(-) create mode 100644 tests/unit_tests/test_optional_enhancement.py diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index af9460b9b..abda76724 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -2,9 +2,10 @@ class Clothing(Item): """A child class of Item, representing aspects of an Item specific to Clothing""" - def __init__(self, category="Clothing", condition=0.0): + def __init__(self, category="Clothing", condition=0.0, age=None): self.category = category self.condition = condition + self.age = age def __str__(self): return "The finest clothing you could wear." diff --git a/swap_meet/decor.py b/swap_meet/decor.py index a09819d70..e35a31346 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -2,10 +2,10 @@ class Decor(Item): """A child class of Item, representing aspects of an Item specific to Decor""" - def __init__(self, category="Decor", condition=0.0): + def __init__(self, category="Decor", condition=0.0, age=None): self.category = category self.condition = condition - + self.age = age def __str__(self): return "Something to decorate your space." diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 7410a12ca..f84845f41 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -3,9 +3,10 @@ class Electronics(Item): """A child class of Item, representing aspects of an Item specific to Electronics""" - def __init__(self, category="Electronics", condition=0.0): + def __init__(self, category="Electronics", condition=0.0, age=None): self.category = category self.condition = condition + self. age = age def __str__(self): return "A gadget full of buttons and secrets." diff --git a/swap_meet/item.py b/swap_meet/item.py index 36300e38d..536371fdd 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,8 +1,9 @@ class Item: - def __init__(self, category="", condition=0.0): + def __init__(self, category="", condition=0.0, age=None): self.category = category self.condition = condition + self.age = age def __str__(self): return "Hello World!" diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 35327ce59..55dc64d50 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -24,8 +24,7 @@ def get_by_category(self, category_str): def swap_items(self, swap_vendor, my_item, their_item): """ - - removes my_item from this Vendor's inventory - - removes their_item from the other Vendor's inventory + - removes my_item from this Vendor's inventory, removes their_item from the other Vendor's inventory - returns True if both inventories have the respective items - returns False if one of the items is not contained in respective inventories """ @@ -77,3 +76,27 @@ def swap_best_by_category(self, other, my_priority, their_priority): self.swap_items(other, my_best_swap, their_best_swap) return True return False + + def get_newest_by_category(self, category_str): + """optional enhancement: helper function to get newest item by category to pass to swap_by_newest""" + category_items = self.get_by_category(category_str) + if len(category_items) > 1: + newest_item = category_items[0] + else: + return None + + for item in category_items: + if item.age < newest_item.age: + newest_item = item + + return newest_item + + def swap_newest_by_category(self, other, my_priority, their_priority): + """optional enhancement: swaps newest item of certain categories with another Vendor""" + my_newest_swap = self.get_newest_by_category(their_priority) + their_newest_swap = other.get_newest_by_category(my_priority) + + if my_newest_swap and their_newest_swap: + self.swap_items(other, my_newest_swap, their_newest_swap) + return True + return False diff --git a/tests/unit_tests/test_optional_enhancement.py b/tests/unit_tests/test_optional_enhancement.py new file mode 100644 index 000000000..62b0ede65 --- /dev/null +++ b/tests/unit_tests/test_optional_enhancement.py @@ -0,0 +1,116 @@ +import pytest +from swap_meet.vendor import Vendor +from swap_meet.clothing import Clothing +from swap_meet.decor import Decor +from swap_meet.electronics import Electronics + +def test_clothing_has_age_default_None(): + clothing = Clothing() + assert clothing.age == None + +def test_decor_has_age_default_None(): + decor = Decor() + assert decor.age == None + +def test_electronics_has_age_default_None(): + electronics = Electronics() + assert electronics.age == None + +def test_newest_by_category(): + item_a = Clothing(age=2) + item_b = Decor(age=1) + item_c = Clothing(age=5) + ryan = Vendor(inventory=[item_a, item_b, item_c]) + + newest_item = ryan.get_newest_by_category("Clothing") + + assert newest_item.age == 2 + assert newest_item.category == "Clothing" + +def test_newest_by_category_no_matches_is_none(): + item_a = Electronics(age=3) + item_b = Decor(age=7) + item_c = Decor(age=5) + tara = Vendor(inventory=[item_a, item_b, item_c]) + + newest_item = tara.get_newest_by_category("Clothing") + + assert newest_item == None + +def test_newest_by_category_with_duplicates(): + item_a = Clothing(age=2) + item_b = Clothing(age=2) + item_c = Clothing(age=4) + robin = Vendor(inventory=[item_a, item_b, item_c]) + + newest_item = robin.get_newest_by_category("Clothing") + + assert newest_item.age == 2 + assert newest_item.category == "Clothing" + +def test_swap_newest_by_category(): + item_a = Decor(age=9) + item_b = Clothing(age=2) + item_c = Decor(age=3) + thomas = Vendor(inventory=[item_a, item_b, item_c]) + + item_d = Electronics(age=3) + item_e = Decor(age=3) + item_f = Electronics(age=1) + stevie = Vendor(inventory=[item_d, item_e, item_f]) + + result = thomas.swap_newest_by_category( + other=stevie, + my_priority="Electronics", + their_priority="Decor" + ) + + assert result is True + assert len(thomas.inventory) == 3 + assert len(stevie.inventory) == 3 + assert item_c in stevie.inventory + assert item_f in thomas.inventory + +def test_swap_newest_by_category_no_inventory_is_false(): + item_a = Clothing(age=9) + item_b = Clothing(age=2) + item_c = Clothing(age=3) + charlotte = Vendor(inventory=[item_a, item_b, item_c]) + + item_d = Electronics(age=3) + item_e = Decor(age=3) + item_f = Electronics(age=1) + parker = Vendor(inventory=[item_d, item_e, item_f]) + + result = charlotte.swap_newest_by_category( + other=parker, + my_priority="Decor", + their_priority="Electronics" + ) + + assert result is False + assert len(charlotte.inventory) == 3 + assert len(parker.inventory) == 3 + for item in charlotte.inventory: + assert item.category != "Electronics" + +def test_swap_newest_by_category_no_other_inventory_is_false(): + item_a = Decor(age=3) + item_b = Electronics(age=1) + item_c = Clothing(age=5) + sage = Vendor(inventory=[item_a, item_b, item_c]) + + audrey = Vendor(inventory=[]) + + result = sage.swap_newest_by_category( + other=audrey, + my_priority="Decor", + their_priority="Clothing" + ) + + assert not result + assert len(sage.inventory) == 3 + assert len(audrey.inventory) == 0 + assert item_a in sage.inventory + assert item_b in sage.inventory + assert item_c in sage.inventory \ No newline at end of file From 3bdc13fc05cf10e06969befe4b7cbc10829853eb Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 22:35:41 -0700 Subject: [PATCH 12/13] general refactoring for final submission --- swap_meet/electronics.py | 5 ++--- swap_meet/vendor.py | 34 +++++++++------------------------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index f84845f41..fdbc262af 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,12 +1,11 @@ from swap_meet.item import Item class Electronics(Item): - """A child class of Item, representing aspects of an Item specific to Electronics""" - + """A child class of Item, representing aspects of an Item specific to Electronics""" def __init__(self, category="Electronics", condition=0.0, age=None): self.category = category self.condition = condition - self. age = age + self.age = age def __str__(self): return "A gadget full of buttons and secrets." diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 55dc64d50..57980ab60 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -23,37 +23,24 @@ def get_by_category(self, category_str): return category_items def swap_items(self, swap_vendor, my_item, their_item): - """ - - removes my_item from this Vendor's inventory, removes their_item from the other Vendor's inventory - - returns True if both inventories have the respective items - - returns False if one of the items is not contained in respective inventories - """ if my_item in self.inventory and their_item in swap_vendor.inventory: - self.inventory.remove(my_item) - swap_vendor.inventory.append(my_item) - self.inventory.append(their_item) - swap_vendor.inventory.remove(their_item) + self.remove(my_item) + swap_vendor.add(my_item) + self.add(their_item) + swap_vendor.remove(their_item) return True return False - def swap_first_item(self, swap_vendor): - """ - - removes the first item from its inventory, and adds the friend's first item - - removes the first item from the friend's inventory, and adds the instances first item - - returns True - - either itself or the friend have an empty inventory, the method returns False - """ - while len(self.inventory) >= 1 and len(swap_vendor.inventory) >= 1: + def swap_first_item(self, other): + if len(self.inventory) >= 1 and len(other.inventory) >= 1: self_first_item = self.inventory[0] - swap_vendor_first_item = swap_vendor.inventory[0] - - self.inventory[0] = swap_vendor_first_item - swap_vendor.inventory[0] = self_first_item + other_first_item = other.inventory[0] + self.inventory[0] = other_first_item + other.inventory[0] = self_first_item return True return False def get_best_by_category(self, category_str): - """get the item with the best condition in a certain category""" category_items = self.get_by_category(category_str) if len(category_items) > 1: best_item = category_items[0] @@ -66,9 +53,6 @@ def get_best_by_category(self, category_str): return best_item def swap_best_by_category(self, other, my_priority, their_priority): - """swap the best item of certain categories with another Vendor - my_priority = a category that the Vendor wants to receive - their_priority = a category that the other wants to receive""" my_best_swap = self.get_best_by_category(their_priority) their_best_swap = other.get_best_by_category(my_priority) From e3ad58e0ac5783180fd65a96d2a183832195492f Mon Sep 17 00:00:00 2001 From: Morgan Adkisson Date: Thu, 7 Apr 2022 22:56:10 -0700 Subject: [PATCH 13/13] fixed bug in Vendor class that was keeping integration tests from running - the ussue was the use of mutable list as default inventory parameter --- swap_meet/vendor.py | 4 +++- tests/integration_tests/test_wave_01_02_03.py | 2 +- tests/integration_tests/test_wave_04_05_06.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 57980ab60..1d307aa37 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -2,7 +2,9 @@ class Vendor: - def __init__(self, inventory = []): + def __init__(self, inventory = None): + if not inventory: + inventory = [] self.inventory = inventory def add(self, item): diff --git a/tests/integration_tests/test_wave_01_02_03.py b/tests/integration_tests/test_wave_01_02_03.py index 4325f5add..a45623df2 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.py b/tests/integration_tests/test_wave_04_05_06.py index 4d0be9909..91b1362b6 100644 --- a/tests/integration_tests/test_wave_04_05_06.py +++ b/tests/integration_tests/test_wave_04_05_06.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(): camila = Vendor()