def test_many_to_one_mapping_cache_with_save(self):
        """
        Cache should be invalidated when calling 'save' on related objects
        """
        car2 = CarFactory.create(make=self.manufacturer)
        len(Manufacturer.objects.get(id=self.manufacturer.id).cars.all())
        car2.name = 'gti'
        car2.save()
        reset_queries()

        # Only 1 cache (the one for car selection query) will be invalidated
        # as we only update data on Car table
        len(Manufacturer.objects.get(id=self.manufacturer.id).cars.all())
        self.assertEqual(len(connection.queries), 1)
    def test_many_to_many_mapping_cache_with_delete(self):
        """
        Cache should be invalidated when calling 'delete' on related objects
        """
        car2 = CarFactory.create()
        self.driver.cars.add(car2)
        initial_count = len(Driver.objects.get(id=self.driver.id).cars.all())
        car2.delete()
        reset_queries()

        # Only 1 cache (the one for car selection query) will be invalidated
        # as we only delete data on Car table
        new_count = len(Driver.objects.get(id=self.driver.id).cars.all())
        self.assertEqual(len(connection.queries), 1)
        self.assertEqual(initial_count - 1, new_count)
    def test_many_to_many_mapping_cache_with_save(self):
        """
        Cache should be invalidated when calling 'save' on related objects
        """
        car2 = CarFactory.create()
        self.driver.cars.add(car2)
        Driver.objects.get(id=self.driver.id).cars.all()[0].year
        car2.name = 'gti'
        car2.save()
        reset_queries()

        # Only 1 cache (the one for car selection query) will be invalidated
        # as we only update data on Car table
        Driver.objects.get(id=self.driver.id).cars.all()[0].year
        self.assertEqual(len(connection.queries), 1)
    def test_many_to_many_mapping_cache_with_delete(self):
        """
        Cache should be invalidated when calling 'delete' on related objects
        """
        car2 = CarFactory.create()
        self.driver.cars.add(car2)
        initial_count = len(Driver.objects.get(id=self.driver.id).cars.all())
        car2.delete()
        reset_queries()

        # Only 1 cache (the one for car selection query) will be invalidated
        # as we only delete data on Car table
        new_count = len(Driver.objects.get(id=self.driver.id).cars.all())
        self.assertEqual(len(connection.queries), 1)
        self.assertEqual(initial_count - 1, new_count)
    def test_many_to_one_mapping_cache_with_save(self):
        """
        Cache should be invalidated when calling 'save' on related objects
        """
        car2 = CarFactory.create(make=self.manufacturer)
        len(Manufacturer.objects.get(id=self.manufacturer.id).cars.all())
        car2.name = 'gti'
        car2.save()
        reset_queries()

        # Only 1 cache (the one for car selection query) will be invalidated
        # as we only update data on Car table
        len(Manufacturer.objects.get(id=self.manufacturer.id).cars.all())
        # Because of m2m fix the no. of queries will be 2 instead of 1
        self.assertEqual(len(connection.queries), 2)
    def test_many_to_many_mapping_cache(self):
        """
        Queries should be cached when making the same select queries
        on related objects repeatedly
        """
        car2 = CarFactory.create()
        self.driver.cars.add(car2)
        reset_queries()

        for i in range(5):
            Driver.objects.get(id=self.driver.id).cars.all()[0].year
        # The above query is actually composed of 2 sql queries:
        # one for fetching drivers and the other cars.
        self.assertEqual(len(connection.queries), 2)
        # no. of cars for the driver increased by 1
        self.assertEqual(len(self.driver.cars.all()), 2)
    def test_many_to_many_mapping_cache(self):
        """
        Queries should be cached when making the same select queries
        on related objects repeatedly
        """
        car2 = CarFactory.create()
        self.driver.cars.add(car2)
        reset_queries()

        for i in range(5):
            Driver.objects.get(id=self.driver.id).cars.all()[0].year
        # The above query is actually composed of 2 sql queries:
        # one for fetching drivers and the other cars.
        self.assertEqual(len(connection.queries), 2)
        # no. of cars for the driver increased by 1
        self.assertEqual(len(self.driver.cars.all()), 2)
    def test_many_to_many_mapping_cache_with_remove(self):
        """
        Cache for both models in this many-to-many relationship should
        be updated when calling 'remove' on related objects
        """
        new_car = CarFactory.create()
        self.driver.cars.add(new_car)
        Driver.objects.get(id=self.driver.id).cars.all()[0].year
        number_of_cars = len(self.driver.cars.all())
        self.driver.cars.remove(self.car)
        reset_queries()

        # Cache for both models should be invalidated as remove is an m2m change
        Driver.objects.get(id=self.driver.id).cars.all()[0].year
        self.assertEqual(len(connection.queries), 2)
        # number of cars decreases by 1
        self.assertEqual(len(self.driver.cars.all()), number_of_cars - 1)
    def test_many_to_one_mapping_cache_with_delete(self):
        """
        Cache should be invalidated when calling 'delete' on related objects
        """
        car2 = CarFactory.create(make=self.manufacturer)
        initial_count = len(
            Manufacturer.objects.get(id=self.manufacturer.id).cars.all())
        car2.delete()
        reset_queries()

        # Only 1 cache (the one for car selection query) will be invalidated
        # as we only delete data on Car table
        new_count = len(
            Manufacturer.objects.get(id=self.manufacturer.id).cars.all())
        # Because of m2m fix the no. of queries will be 2 instead of 1
        self.assertEqual(len(connection.queries), 2)
        self.assertEqual(initial_count - 1, new_count)
    def test_many_to_many_mapping_cache_with_remove(self):
        """
        Cache for both models in this many-to-many relationship should
        be updated when calling 'remove' on related objects
        """
        new_car = CarFactory.create()
        self.driver.cars.add(new_car)
        Driver.objects.get(id=self.driver.id).cars.all()[0].year
        number_of_cars = len(self.driver.cars.all())
        self.driver.cars.remove(self.car)
        reset_queries()

        # Cache for both models should be invalidated as remove is an m2m change
        Driver.objects.get(id=self.driver.id).cars.all()[0].year
        self.assertEqual(len(connection.queries), 2)
        # number of cars decreases by 1
        self.assertEqual(len(self.driver.cars.all()), number_of_cars - 1)
 def setUp(self):
     self.manufacturer = ManufacturerFactory.create()
     self.car = CarFactory.create(make=self.manufacturer, year=2015)
     reset_queries()
 def setUp(self):
     self.engine = EngineFactory.create(name='test_engine')
     self.car = CarFactory.create(engine=self.engine, year=2015)
     reset_queries()
 def setUp(self):
     self.engine = EngineFactory.create(name='test_engine')
     self.car = CarFactory.create(engine=self.engine, year=2015)
     reset_queries()
 def setUp(self):
     self.car = CarFactory.create(year=2015)
     self.driver = DriverFactory.create(cars=[self.car])
     reset_queries()
 def setUp(self):
     self.manufacturer = ManufacturerFactory.create()
     self.car = CarFactory.create(make=self.manufacturer, year=2015)
     reset_queries()
 def setUp(self):
     self.car = CarFactory.create(year=2015)
     self.driver = DriverFactory.create(cars=[self.car])
     reset_queries()
 def test_cached_model_updated(self, update_model_cache):
     self.assertEqual(update_model_cache.call_count, 0)
     CarFactory.create()
     self.assertGreater(update_model_cache.call_count, 0)
 def setUp(self):
     self.manufacturer = ManufacturerFactory.create(name='Honda')
     self.car = CarFactory.create(make=self.manufacturer, year=2015, model='Civic')
     reset_queries()
 def setUp(self):
     self.manufacturer = ManufacturerFactory.create(name='Honda')
     self.car = CarFactory.create(make=self.manufacturer,
                                  year=2015,
                                  model='Civic')
     reset_queries()