diff --git a/swap_meet/clothing.py b/swap_meet/clothing.py index b8afdeb1e..9032f334b 100644 --- a/swap_meet/clothing.py +++ b/swap_meet/clothing.py @@ -1,2 +1,15 @@ -class Clothing: - pass \ No newline at end of file +from swap_meet.item import Item + + +class Clothing(Item): + + + def __init__(self, condition = 0, age = None): + self.category = "Clothing" + self.condition = condition + self.age = age + + + #override str() for Clothing, returns "The finest clothing you could wear." + def __str__(self): + return "The finest clothing you could wear." \ No newline at end of file diff --git a/swap_meet/decor.py b/swap_meet/decor.py index eab7a9dbe..089cafcb5 100644 --- a/swap_meet/decor.py +++ b/swap_meet/decor.py @@ -1,2 +1,14 @@ -class Decor: - pass \ No newline at end of file +from swap_meet.item import Item + + +class Decor(Item): + + + def __init__(self, condition = 0, age = None): + self.category = "Decor" + self.condition = condition + self.age = age + + #override str() for Decor, returns "Something to decorate your space." + def __str__(self): + return "Something to decorate your space." \ No newline at end of file diff --git a/swap_meet/electronics.py b/swap_meet/electronics.py index 2f9dff68a..b5121b859 100644 --- a/swap_meet/electronics.py +++ b/swap_meet/electronics.py @@ -1,2 +1,13 @@ -class Electronics: - pass +from swap_meet.item import Item + + +class Electronics(Item): + + def __init__(self, condition = 0, age = None): + self.category = "Electronics" + self.condition = condition + self.age = age + + #override str() for Electronics, returns "A gadget full of buttons and secrets." + def __str__(self): + return "A gadget full of buttons and secrets." diff --git a/swap_meet/item.py b/swap_meet/item.py index 560d759c2..452236fbc 100644 --- a/swap_meet/item.py +++ b/swap_meet/item.py @@ -1,2 +1,36 @@ + + class Item: - pass \ No newline at end of file + + def __init__(self, category = "", condition = 0, age = None): + self.category = category + self.condition = condition + self.age = age + + #override str() for Item, returns "Hello World!" instead + def __str__(self): + return "Hello World!" + + + #describes condition based on the value, range from 0 to 5 + def condition_description(self): + + if self.condition == 0: + return "Terrible condition" + + if self.condition == 1: + return "Bad condition" + + if self.condition == 2: + return "Slightly bad condition" + + if self.condition == 3: + return "Acceptable condition" + + if self.condition == 4: + return "Good condition" + + if self.condition == 5: + return "Mint conditon" + + diff --git a/swap_meet/vendor.py b/swap_meet/vendor.py index 87302c056..299e9efae 100644 --- a/swap_meet/vendor.py +++ b/swap_meet/vendor.py @@ -1,2 +1,140 @@ +from swap_meet.item import Item + class Vendor: - pass \ No newline at end of file + def __init__(self, inventory = None): + #code on lines 6-7 from https://docs.python-guide.org/writing/gotchas/ + if inventory == None: + inventory = [] + self.inventory = inventory + + # Adds the item to the inventory + def add(self, item): + self.inventory.append(item) + return item + + + # Removes the matching item from the inventory + def remove(self, item): + if item not in self.inventory: + return False + else: + self.inventory.remove(item) + return item + + + # Takes one argument: a string, representing a category + # Returns a list of Items in the inventory with that category + def get_by_category(self, category): + #self.item = Item(category) + item_list = [] + for item in self.inventory: + if item.category == category: + item_list.append(item) + + + return item_list + + + # removes my_item from this Vendor's inventory + # adds my_item to the other Vendor's inventory + # removes their_item from other Vendor's inventory + # adds their_item to the this Vendor's inventory + 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 + + + #removes first item from this Vendor's inventory + # adds the other Vendor's first item + # removes first item from other Vendor's inventory + # adds this Vendor's first item + def swap_first_item(self, other_vendor): + if len(self.inventory) == 0 or len(other_vendor.inventory) == 0: + return False + + self.swap_items(other_vendor, self.inventory[0], other_vendor.inventory[0]) + + return True + + + #gets the item with the best condition in a certain category + def get_best_by_category(self, category): + + category_inventory = self.get_by_category(category) + + if len(category_inventory) == 0: + return None + + highest_rated = category_inventory[0] + + for item in category_inventory: + + if (item.condition) > (highest_rated.condition): + highest_rated = item + + return highest_rated + + + #swap the best item of certain categories with another Vendor + def swap_best_by_category(self, other, my_priority, their_priority): + + #find best item in my inventory that matches their_priority + my_best = self.get_best_by_category(their_priority) + + their_best = other.get_best_by_category(my_priority) + + if my_best == None or their_best == None: + return False + + self.swap_items(other, my_best, their_best) + + return True + + + + def swap_by_newest(self, other): + my_newest = self.inventory[0] + for item in self.inventory: + if not item.age is None: + my_newest = item + break + if my_newest.age is None: + return False + + for item in self.inventory: + if not item.age is None: + if item.age < my_newest.age: + my_newest = item + + their_newest = other.inventory[0] + for item in other.inventory: + if not item.age is None: + their_newest = item + break + if their_newest.age is None: + return False + + for item in other.inventory: + if not item.age is None: + if item.age < their_newest.age: + their_newest = item + + self.swap_items(other, my_newest, their_newest) + 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 9912414da..e766bbbf7 100644 --- a/tests/integration_tests/test_wave_01_02_03.py +++ b/tests/integration_tests/test_wave_01_02_03.py @@ -2,11 +2,13 @@ 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 vendor = Vendor() + + assert len(vendor.inventory) == 0 # add an item @@ -49,6 +51,4 @@ def test_integration_wave_01_02_03(): assert len(vendor.inventory) == 1 assert len(other_vendor.inventory) == 1 assert item2 in other_vendor.inventory - assert item3 in vendor.inventory - - + assert item3 in vendor.inventory \ No newline at end of file diff --git a/tests/integration_tests/test_wave_04_05_06.py b/tests/integration_tests/test_wave_04_05_06.py index 4d0be9909..25f268c85 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() diff --git a/tests/unit_tests/test_wave_01.py b/tests/unit_tests/test_wave_01.py index 58478ccf9..b41df178d 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( @@ -48,8 +48,10 @@ def test_removing_not_found_is_false(): ) 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 result == False diff --git a/tests/unit_tests/test_wave_02.py b/tests/unit_tests/test_wave_02.py index 3d7060d7c..f51fa588f 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,10 @@ 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 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_03.py b/tests/unit_tests/test_wave_03.py index 0300b638f..720879a7c 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") diff --git a/tests/unit_tests/test_wave_04.py b/tests/unit_tests/test_wave_04.py index 8190a4ebb..f3c91e846 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") diff --git a/tests/unit_tests/test_wave_05.py b/tests/unit_tests/test_wave_05.py index 7abea06cd..d8575f6e2 100644 --- a/tests/unit_tests/test_wave_05.py +++ b/tests/unit_tests/test_wave_05.py @@ -3,25 +3,26 @@ 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" 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), @@ -31,7 +32,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 1f7065ab4..c9d0c012c 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) @@ -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,16 +76,26 @@ 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_a in tai.inventory + assert item_b in tai.inventory + assert item_f in tai.inventory + assert item_c in jesse.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_reordered(): # Arrange item_a = Decor(condition=2.0) @@ -109,16 +119,26 @@ 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 + 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_a in tai.inventory + assert item_b in tai.inventory + assert item_f in tai.inventory + assert item_c in jesse.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_inventory_is_false(): tai = Vendor( inventory=[] @@ -144,7 +164,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 +190,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,16 +214,26 @@ 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 + assert result == False # - 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 + 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 +#@pytest.mark.skip def test_swap_best_by_category_no_other_match_is_false(): # Arrange item_a = Decor(condition=2.0) @@ -227,11 +257,119 @@ 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 + assert result == False # - 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 + 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 + + + + +# tests for swap_by_newest + +def test_swap_newest(): + # Arrange + item_a = Decor(age=8.0) + item_b = Electronics(age=None) + item_c = Decor(age = 2.0) + tai = Vendor( + inventory=[item_c, item_b, item_a] + ) + + item_d = Clothing(age=None) + item_e = Decor(age=4.0) + item_f = Clothing(age=1.0) + jesse = Vendor( + inventory=[item_e, item_d, item_f] + ) + + # Act + result = tai.swap_by_newest(other=jesse) + + assert result == True + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + + assert item_a in tai.inventory + assert item_b in tai.inventory + assert item_f in tai.inventory + assert item_c in jesse.inventory + assert item_d in jesse.inventory + assert item_e in jesse.inventory + + + +def test_swap_newest_all_ages_none(): + # Arrange + item_a = Decor(age=None) + item_b = Electronics(age=None) + item_c = Decor(age=None) + tai = Vendor( + inventory=[item_c, item_b, item_a] + ) + + item_d = Clothing(age=None) + item_e = Decor(age=None) + item_f = Clothing(age=None) + jesse = Vendor( + inventory=[item_e, item_d, item_f] + ) + + # Act + result = tai.swap_by_newest(other=jesse) + + assert result == False + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + + 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 + + + +def test_swap_newest_one_vendor_ages_none(): + # Arrange + item_a = Decor(age=None) + item_b = Electronics(age=None) + item_c = Decor(age=None) + tai = Vendor( + inventory=[item_c, item_b, item_a] + ) + + item_d = Clothing(age=4.0) + item_e = Decor(age=2.0) + item_f = Clothing(age=1.0) + jesse = Vendor( + inventory=[item_e, item_d, item_f] + ) + + # Act + result = tai.swap_by_newest(other=jesse) + + assert result == False + assert len(tai.inventory) == 3 + assert len(jesse.inventory) == 3 + + 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