def test_inv_drug_add() -> None: """ Tests InventoryDrug.__add__ method """ soma = InventoryDrug("Soma", 22) more = InventoryDrug("Soma", 55) not_soma = InventoryDrug("Not Soma", 10) soma += not_soma assert soma.quantity == 22 assert not_soma.quantity == 10 soma += more assert soma.quantity == 77 assert more.quantity == 0
def test_player_steal_drugs() -> None: """ Tests stealing drug method. :return: """ p = Player("Bob", 5000) nothing = p.steal_drugs() assert nothing == "Nothing to take!" p.inv = {"Soma": InventoryDrug("Soma", 10)} p.steal_drugs() assert p.inv["Soma"].quantity < 10 p.inv = {"Soma": InventoryDrug("Soma", 3)} msg = p.steal_drugs() assert msg == f"1 of Soma were confiscated!"
def test_inv_drug_sell() -> None: """ Test sell method """ soma = InventoryDrug("Soma", 10) with raises(RuntimeError): too_many = soma.sell(12, 55) assert too_many == 0 assert soma.quantity == 10 sell_all = soma.sell(10, 10) assert sell_all == 100 assert soma.quantity == 0 soma = InventoryDrug("Soma", 10) with raises(RuntimeError): soma.sell(-5, 5) soma.sell(5, -12) soma + 12 soma + "Hello there!"
def test_player_buying_drug() -> None: """ Tests player buying a drug """ p = Player("Bob", 5000) soma = Drug("Soma", 100, 12) orig_quantity = soma.quantity p.buy_drugs(soma, 5) assert soma.quantity == orig_quantity - 5 p.inv = {"Soma": InventoryDrug("Soma", 5)} p.buy_drugs(soma, 1) assert soma.quantity == orig_quantity - 6 p.inv = {"Soma": InventoryDrug("Soma", 6)} with raises(RuntimeError): p.buy_drugs(soma, 50000) assert soma.quantity == orig_quantity - 6 p._money = 0 p.buy_drugs(soma, 1) assert soma.quantity == orig_quantity - 6
def test_player_selling_drug() -> None: """ Tests that money increases and inv decreases when player sells drug """ p = Player("Bob", 5000) with raises(RuntimeError): p.sell("Soma", 5, 5) p.sell("Soma", -5, 5) p.sell("Coffee", 2, 5) p.inv = {"Soma": InventoryDrug("Soma", 10)} with raises(RuntimeError): p.sell("Soma", 11, 5) p.sell("Soma", 5, 5) assert p.money == 5025 assert p.inv["Soma"].quantity == 5 p.sell("Soma", 5, 5) assert p.money == 5050 assert p.inv.get("Soma") is None
def buy_drugs(self, drug: Drug, quantity: int) -> None: """Implement drug buying interface. :param quantity: :param drug: :return: """ if quantity > drug.quantity or quantity <= 0: raise RuntimeError("Insufficient quantity") price = quantity * drug.price if self._money < price: raise RuntimeError("Insufficient funds") purchase = InventoryDrug(drug.name, quantity) drug.quantity -= quantity if self.inv.get(drug.name) is None: self.inv[drug.name] = purchase else: self.inv[drug.name] += purchase self._money -= price
def test_inv_drug() -> None: """ Tests creation of inv drug """ soma = InventoryDrug("Soma", 22) assert "Soma: 22" == str(soma)