def test_deletion_of_demo_drops_schema(self): user = User.objects.create_user(**CREDENTIALS) demo = DemoSchema.objects.create(user=user, from_template=self.template) self.assertTrue(_schema_exists(demo.schema)) demo.delete() self.assertFalse(_schema_exists(demo.schema))
def test_templates_can_be_created_and_destroyed(self): template = SchemaTemplate.objects.create(name='Foo') self.assertTrue(_schema_exists(template.schema)) template.activate() self.assertEqual(get_active_schema_name(), template.schema) template.deactivate() template.delete() self.assertFalse(_schema_exists(template.schema))
def test_cleanup_expired_removes_expired(self): user = User.objects.create_user(**CREDENTIALS) demo = DemoSchema.objects.create(user=user, expires_at='1970-01-01T00:00:00Z', from_template=self.template) self.assertTrue(_schema_exists(demo.schema)) call_command('cleanup_expired_demos') self.assertEqual(0, DemoSchema.objects.count()) self.assertFalse(_schema_exists(demo.schema))
def create_schema(sender, instance, created, **kwargs): """ Actually create the schema in the database. We do this in a signal handler instead of .save() so we can catch those created using raw methods. How do we indicate when we should be using a different template? """ if created: schema_name = instance.schema # How can we work out what values need to go here? # Currently, we just allow a single attribute `_clone`, that, # if set, will indicate that we should clone a schema. template_name = getattr(instance, '_clone', settings.TEMPLATE_SCHEMA) include_records = bool(getattr(instance, '_clone', False)) cursor = connection.cursor() if _schema_exists(schema_name): raise ValueError('Attempt to create an existing schema: {0}'.format(schema_name)) cursor.execute("SELECT clone_schema(%s, %s, %s)", [ template_name, schema_name, include_records ]) cursor.close() if schema_name != settings.TEMPLATE_SCHEMA: signals.schema_created.send(sender=get_schema_model(), schema=schema_name) LOGGER.info('New schema created: %s', schema_name)
def test_demo_lifecycle_views(self): CREATE_DEMO = reverse('demo:create') RESET_DEMO = reverse('demo:reset') DELETE_DEMO = reverse('demo:delete') response = self.client.post(CREATE_DEMO) self.assertEqual(302, response.status_code, 'Unauthenticated user should be redirected to login') user = User.objects.create_user(**CREDENTIALS) self.client.login(**CREDENTIALS) # reset and delete should fail with 404 when no DemoSchema for current user. response = self.client.post(DELETE_DEMO) self.assertEqual(404, response.status_code, 'DELETE when no DemoSchema should return 404') response = self.client.post(RESET_DEMO) self.assertEqual(404, response.status_code, 'RESET when no DemoSchema should return 404') # Create without from_template should fail with 409. response = self.client.post(CREATE_DEMO) self.assertEqual(409, response.status_code, 'Missing from_template should result in 409') response = self.client.post(CREATE_DEMO, data={'from_template': self.template.pk}) self.assertEqual(302, response.status_code, 'Successful creation of demo should redirect') demo_schema = DemoSchema.objects.get(user=user) self.assertTrue('/__change_schema__/{0}/'.format(demo_schema.schema)) # Create should fail: as template already exists. response = self.client.post(CREATE_DEMO, data={'from_template': self.template.pk}) self.assertEqual(409, response.status_code, 'Existing DemoSchema should result in 409') demo = DemoSchema.objects.get(user=user) demo.activate() AwareModel.objects.create(name='foo', status=True) self.assertEqual(1, AwareModel.objects.count(), 'One object created before reset') response = self.client.post(RESET_DEMO) demo = DemoSchema.objects.get(user=user) demo.activate() self.assertEqual(0, AwareModel.objects.count(), 'Reset should clear out any objects from schema') response = self.client.post(DELETE_DEMO) self.assertFalse( DemoSchema.objects.filter(user=user).exists(), 'Delete should remove schema') self.assertFalse(_schema_exists(demo.schema))
def test_demo_lifecycle_views(self): CREATE_DEMO = reverse('demo:create') RESET_DEMO = reverse('demo:reset') DELETE_DEMO = reverse('demo:delete') response = self.client.post(CREATE_DEMO) self.assertEqual(302, response.status_code, 'Unauthenticated user should be redirected to login') user = User.objects.create_user(**CREDENTIALS) self.client.login(**CREDENTIALS) # reset and delete should fail with 404 when no DemoSchema for current user. response = self.client.post(DELETE_DEMO) self.assertEqual(404, response.status_code, 'DELETE when no DemoSchema should return 404') response = self.client.post(RESET_DEMO) self.assertEqual(404, response.status_code, 'RESET when no DemoSchema should return 404') # Create without from_template should fail with 409. response = self.client.post(CREATE_DEMO) self.assertEqual(409, response.status_code, 'Missing from_template should result in 409') response = self.client.post(CREATE_DEMO, data={'from_template': self.template.pk}) self.assertEqual(302, response.status_code, 'Successful creation of demo should redirect') demo_schema = DemoSchema.objects.get(user=user) self.assertTrue('/__change_schema__/{0}/'.format(demo_schema.schema)) # Create should fail: as template already exists. response = self.client.post(CREATE_DEMO, data={'from_template': self.template.pk}) self.assertEqual(409, response.status_code, 'Existing DemoSchema should result in 409') demo = DemoSchema.objects.get(user=user) demo.activate() AwareModel.objects.create(name='foo', status=True) self.assertEqual(1, AwareModel.objects.count(), 'One object created before reset') response = self.client.post(RESET_DEMO) demo = DemoSchema.objects.get(user=user) demo.activate() self.assertEqual(0, AwareModel.objects.count(), 'Reset should clear out any objects from schema') response = self.client.post(DELETE_DEMO) self.assertFalse(DemoSchema.objects.filter(user=user).exists(), 'Delete should remove schema') self.assertFalse(_schema_exists(demo.schema))