Use existing models from fixtures
# These sources are released under the terms of the MIT license: see LICENSE
from unittest import mock
-from django.test import TestCase
-from promgen import models
+from promgen import models, tests
-class SignalTest(TestCase):
+class SignalTest(tests.PromgenTest):
@mock.patch('promgen.models.Audit.log')
@mock.patch('promgen.signals.trigger_write_config.send')
def test_write_signal(self, write_mock, log_mock):
- # Build service trigger
- shard = models.Shard.objects.create(name='Shard')
- service = models.Service.objects.create(name='Service')
- farm = models.Farm.objects.create(name='farm')
- models.Host.objects.create(name='Host', farm=farm)
+ # Create a test farm
+ farm = models.Farm.objects.create(name="farm")
+ models.Host.objects.create(name="Host", farm=farm)
+ # Create a new project or testing
project = models.Project.objects.create(
- name='Project', service=service, farm=farm, shard=shard
+ name="Project", service_id=1, farm=farm, shard_id=1
)
e1 = models.Exporter.objects.create(
job='Exporter 1', port=1234, project=project,
@@ -28,21 +26,18 @@ class SignalTest(TestCase):
)
# Should be called once for each created exporter
- self.assertTrue(write_mock.call_count == 2)
- write_mock.assert_has_calls([
- mock.call(e1), mock.call(e2)
- ])
+ self.assertEquals(write_mock.call_count, 2, "Two write calls")
+ write_mock.assert_has_calls([mock.call(e1), mock.call(e2)])
@mock.patch('promgen.models.Audit.log')
@mock.patch('promgen.signals.trigger_write_config.send')
def test_write_and_delete(self, write_mock, log_mock):
- # Build service trigger
- shard = models.Shard.objects.create(name='Shard')
- service = models.Service.objects.create(name='Service')
- farm = models.Farm.objects.create(name='farm')
- models.Host.objects.create(name='Host', farm=farm)
+ # Create a test farm
+ farm = models.Farm.objects.create(name="farm")
+ models.Host.objects.create(name="Host", farm=farm)
+
project = models.Project.objects.create(
- name='Project', service=service, farm=farm, shard=shard
+ name="Project", service_id=1, farm=farm, shard_id=1
)
# Farm but no exporters so no call
self.assertEqual(write_mock.call_count, 0, 'Should not be called without exporters')
