def test_removing_flowers_by_event(self, dispatcher):
        warehouse = Warehouse(dispatcher)
        warehouse.add_flower(Flower('a', 1))
        warehouse.add_flower(Flower('b', 1))

        self.assertEqual({'a': 1, 'b': 1}, warehouse.get_stock())
        warehouse.remove_flowers(
            Event(AvailableEvents.FLOWER_REMOVED, Flower('a', 1)))
        self.assertEqual({'a': 0, 'b': 1}, warehouse.get_stock())

        dispatcher.dispatch_event.assert_called()
    def test_adding_flowers(self, dispatcher):
        flower = Flower('a', 1)

        warehouse = Warehouse(dispatcher)
        warehouse.add_flower(flower)

        self.assertEqual({'a': 1}, warehouse.get_stock())
        dispatcher.dispatch_event.assert_called_once()
 def update_flower_numbers(self, bouquet: Bouquet, flower_stock,
                           flower_specie: str,
                           flowers_to_remove: int) -> None:
     if flowers_to_remove == 0:
         return
     flower_stock[flower_specie] -= flowers_to_remove
     bouquet.current_total += flowers_to_remove
     self._event_dispatcher.dispatch_event(
         Event(AvailableEvents.FLOWER_REMOVED,
               Flower(flower_specie, flowers_to_remove)))
    def take_random_flowers(bouquet_size: chr, flowers_still_to_retrieve: int,
                            flower_stock: dict) -> Flower:
        if bouquet_size == 'L':
            random_flowers = dict(
                filter(lambda elem: elem[0].isupper() and elem[1] > 0,
                       flower_stock.items()))
        else:
            random_flowers = dict(
                filter(lambda elem: elem[0].islower() and elem[1] > 0,
                       flower_stock.items()))

        if len(random_flowers) == 0:
            return Flower(Flower.NO_FLOWER, 0)

        specie = list(random_flowers)[randint(0, len(random_flowers) - 1)]

        flowers_to_remove = ProductionFacility.get_flowers_to_remove_from_stock(
            flowers_still_to_retrieve, flower_stock[specie])

        return Flower(specie, flowers_to_remove)
Beispiel #5
0
def main():
    for line in fileinput.input():
        if not WAREHOUSE.needs_more_flowers:
            break
        line = line.strip()
        if not line:
            continue

        if len(line) > 2:
            PRODUCTION_FACILITY.add_bouquet(line)
        else:
            WAREHOUSE.add_flower(Flower(line[0], line[-1]))
    def check_warehouse_stock_for_bouquet(self, event: Event):
        flower_stock = event.payload.copy()
        for bouquet in self._bouquets:
            if bouquet.current_total < bouquet.flower_quantity:
                for flower_specie in bouquet.flowers:
                    self.get_species_from_stock_for_bouquet(
                        bouquet, flower_specie, flower_stock)

            if bouquet.flower_quantity <= bouquet.current_total < bouquet.total_flowers:
                flower = self.take_random_flowers(
                    bouquet.size,
                    bouquet.total_flowers - bouquet.flower_quantity,
                    flower_stock)

                if flower.specie is not Flower.NO_FLOWER and flower.amount != 0:
                    bouquet.add_specie_amount(
                        Flower(flower.specie, flower.amount))
                    self.update_flower_numbers(bouquet, flower_stock,
                                               flower.specie, flower.amount)
            self.is_bouquet_complete(bouquet)

        self.are_there_still_bouquets_to_fill()
Beispiel #7
0
 def test_can_add_specie_amount(self):
     self.assertNotIn('5x', str(self.bouquet))
     self.bouquet.add_specie_amount(Flower('x', 5))
     self.assertIn('5x', str(self.bouquet))