'MagicMock: How to patch the same attribute of all objects in an iterable?

I have a method that returns an iterable, and a method that processes that iterable:

class Store:

    def get_all_orders(self):
        return api_get_all_orders()


    def process_orders(self):
        dt_new_years = datetime.datetime(2022, 1, 1)


        new_orders = []
        old_orders = []

        all_orders = self.get_all_orders()

        for order in all_orders:
            
            if order.timestamp >= dt_new_years:
                new_orders.append(order)
            else: 
                old_orders.append(order)

        return new_orders, old_orders

I'd like to test this process_orders by mocking order.timestamp, how would I do that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source