def test_num_of_queries_on_related_with_dependency(self): """ integration test test cache functionality in more complex environment """ schema = DynamicSchema.get_for_model(TestModel) schema.add_field("field", "IntegerField") with self.assertNumQueries(0): self.assertEqual( DynamicSchema.get_for_model(TestModel).fields.all()[0].name, "field") schema = DynamicSchema.get_for_model(TestModel) schema.remove_field("field") with self.assertNumQueries(0): self.assertEqual( DynamicSchema.get_for_model(TestModel).fields.count(), 0) m2m = M2MModel.objects.create() DynamicSchemaField.objects.create( schema=DynamicSchema.get_for_model(TestModel), name='email', field_type='EmailField') m2m.testmodels.add( TestModel.objects.create(about='one'), TestModel.objects.create(about='two'), TestModel.objects.create(about='three'), TestModel.objects.create(about='four'), TestModel.objects.create(about='five'), TestModel.objects.create(about='six'), ) with self.assertNumQueries(1): [(el.about, el.email) for el in m2m.testmodels.all()]
def test_cache_updated_after_removing_schema_field(self): schema = DynamicSchema.get_for_model(TestModel) schema.add_field("field", "IntegerField") schema.remove_field("field") with self.assertNumQueries(0): self.assertEqual( DynamicSchema.get_for_model(TestModel).fields.count(), 0)
def test_delete_schema_qs_clears_cache(self): DynamicSchema.get_for_model(TestModel) self.assertIsNotNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, ''))) DynamicSchema.objects.all().delete() self.assertIsNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, '')))
def test_cache_updated_after_adding_schema_field(self): schema = DynamicSchema.get_for_model(TestModel) schema.add_field("field", "IntegerField") with self.assertNumQueries(0): self.assertEqual( DynamicSchema.get_for_model(TestModel).fields.all()[0].name, "field")
def test_delete_schema_clears_cache(self): schema = DynamicSchema.get_for_model(TestModel) self.assertIsNotNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, ''))) schema.delete() self.assertIsNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, '')))
def test_manually_create_schema_typeless(self): schema = DynamicSchema.get_for_model(TypelessModel) schema.add_field(name='field', type='CharField') m1 = TypelessModel() m1.field = 'foo' m1.save() m2 = TypelessModel.objects.get(id=m1.id) self.assertEqual(m2.field, 'foo')
def test_schema_add_remove_field(self): schema = DynamicSchema.get_for_model(TypelessModel) schema.add_field(name='field', type='CharField') m1 = TypelessModel.objects.create() self.assertTrue(hasattr(m1, 'field')) schema.remove_field(name='field') m2 = TypelessModel.objects.get(id=m1.id) m2 = TypelessModel.objects.create() self.assertFalse(hasattr(m2, 'field'))
def test_num_of_queries_on_related(self): m2m = M2MModel.objects.create() DynamicSchemaField.objects.create( schema=DynamicSchema.get_for_model(TestModel), name='email', field_type='EmailField') m2m.testmodels.add( TestModel.objects.create(about='one'), TestModel.objects.create(about='two'), TestModel.objects.create(about='three'), TestModel.objects.create(about='four'), TestModel.objects.create(about='five'), TestModel.objects.create(about='six'), ) with self.assertNumQueries(1): [(el.about, el.email) for el in m2m.testmodels.all()]
def test_manually_create_schema_typed(self): schema = DynamicSchema.get_for_model(TestModel, type_value='foo') schema.add_field(name='field', type='CharField') m1 = TestModel.objects.create(type='foo') m1.field = 'foo' m1.save() m2 = TestModel.objects.get(id=m1.id) self.assertEqual(m2.field, 'foo') m3 = TestModel.objects.create(type='bar') m3.field = 'foo' m3.save() m4 = TestModel.objects.get(id=m3.id) self.assertFalse(hasattr(m4, 'field'))
def test_delete_schema_qs_with_diff_types_clears_cache(self): DynamicSchema.get_for_model(TestModel) DynamicSchema.get_for_model(TestModel, 'some_value') # there is cached value self.assertIsNotNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, ''))) self.assertIsNotNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, 'some_value'))) DynamicSchema.objects.all().delete() # there is no cached value self.assertIsNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, ''))) self.assertIsNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, 'some_value')))
def test_delete_schema_qs_with_diff_types_clears_cache(self): DynamicSchema.get_for_model(TestModel) DynamicSchema.get_for_model(TestModel, 'some_value') # there is cached value self.assertIsNotNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, ''))) self.assertIsNotNone( cache.get( DynamicSchema.get_cache_key_static(TestModel, 'some_value'))) DynamicSchema.objects.all().delete() # there is no cached value self.assertIsNone( cache.get(DynamicSchema.get_cache_key_static(TestModel, ''))) self.assertIsNone( cache.get( DynamicSchema.get_cache_key_static(TestModel, 'some_value')))
def test_second_schema_fetching_doesnt_hit_db(self): DynamicSchema.get_for_model(TestModel) with self.assertNumQueries(0): DynamicSchema.get_for_model(TestModel)