Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. # models.py
  2.  
  3. def post_saveCompany(sender, instance, created, **kwargs):
  4. connections.databases[str(instance.pk)] = {
  5. 'ENGINE': 'django.db.backends.postgresql_psycopg2',
  6. 'NAME': instance.database,
  7. 'USER': instance.user,
  8. 'PASSWORD': instance.password,
  9. 'HOST': instance.host,
  10. 'PORT': instance.port,
  11. }
  12.  
  13.  
  14. class Company(models.Model):
  15. description = models.CharField('descrição', max_length=255)
  16. database = models.CharField('banco de dados', max_length=120)
  17. user = models.CharField('usuário', max_length=120)
  18. password = models.CharField('senha', max_length=120)
  19. host = models.CharField('servidor', max_length=120)
  20. port = models.CharField('porta', max_length=4)
  21.  
  22. def __str__(self):
  23. return self.description
  24.  
  25.  
  26. signals.post_save.connect(post_saveCompany, sender=Company)
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36. # tests.py
  37.  
  38. class CompanyModelTest(TestCase):
  39. company_description = 'Empresa1'
  40.  
  41. def setUp(self):
  42. self.company = Company.objects.create(
  43. description=self.company_description,
  44. database = 'banco',
  45. user = 'usuario',
  46. password = 'senha',
  47. host = '0.0.0.0',
  48. port = '0000'
  49. )
  50.  
  51. def test_create(self):
  52. self.assertTrue(Company.objects.exists())
  53.  
  54. def test_str(self):
  55. self.assertEqual(self.company_description, str(self.company))
  56.  
  57. def test_signal_post_save_registry(self):
  58. registered_functions = [r[1]() for r in signals.post_save.receivers]
  59. self.assertIn(post_saveCompany, registered_functions)
  60.  
  61. def test_signal_post_save_has_effect(self):
  62. self.assertIn(str(self.company.pk), connections.databases)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement