Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # models.py
- def post_saveCompany(sender, instance, created, **kwargs):
- connections.databases[str(instance.pk)] = {
- 'ENGINE': 'django.db.backends.postgresql_psycopg2',
- 'NAME': instance.database,
- 'USER': instance.user,
- 'PASSWORD': instance.password,
- 'HOST': instance.host,
- 'PORT': instance.port,
- }
- class Company(models.Model):
- description = models.CharField('descrição', max_length=255)
- database = models.CharField('banco de dados', max_length=120)
- user = models.CharField('usuário', max_length=120)
- password = models.CharField('senha', max_length=120)
- host = models.CharField('servidor', max_length=120)
- port = models.CharField('porta', max_length=4)
- def __str__(self):
- return self.description
- signals.post_save.connect(post_saveCompany, sender=Company)
- # tests.py
- class CompanyModelTest(TestCase):
- company_description = 'Empresa1'
- def setUp(self):
- self.company = Company.objects.create(
- description=self.company_description,
- database = 'banco',
- user = 'usuario',
- password = 'senha',
- host = '0.0.0.0',
- port = '0000'
- )
- def test_create(self):
- self.assertTrue(Company.objects.exists())
- def test_str(self):
- self.assertEqual(self.company_description, str(self.company))
- def test_signal_post_save_registry(self):
- registered_functions = [r[1]() for r in signals.post_save.receivers]
- self.assertIn(post_saveCompany, registered_functions)
- def test_signal_post_save_has_effect(self):
- self.assertIn(str(self.company.pk), connections.databases)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement