from unittest import mock

from django.test import TestCase
from django.urls import reverse

from promgen import models, prometheus
from promgen.tests import TEST_RULE


_RULES = '''
# Service: Service 1
# Service URL: /service/1/
ALERT RuleName
  IF up==0
  FOR 1s
  LABELS {severity="severe"}
  ANNOTATIONS {service="http://example.com/service/1/", summary="Test case"}


'''.lstrip()


class RuleTest(TestCase):
    @mock.patch('django.db.models.signals.post_save', mock.Mock())
    def setUp(self):
        self.shard = models.Shard.objects.create(name='Shard 1')
        self.service = models.Service.objects.create(id=1, name='Service 1', shard=self.shard)
        self.rule = models.Rule.objects.create(
            name='RuleName',
            clause='up==0',
            duration='1s',
            service=self.service
        )
        models.RuleLabel.objects.create(name='severity', value='severe', rule=self.rule)
        models.RuleAnnotation.objects.create(name='summary', value='Test case', rule=self.rule)

    @mock.patch('django.db.models.signals.post_save')
    def test_write(self, mock_render):
        result = prometheus.render_rules()
        self.assertEqual(result, _RULES)

    @mock.patch('django.db.models.signals.post_save')
    def test_copy(self, mock_render):
        service = models.Service.objects.create(name='Service 2', shard=self.shard)
        copy = self.rule.copy_to(service)
        # Test that our copy has the same labels and annotations
        self.assertIn('severity', copy.labels())
        self.assertIn('summary', copy.annotations())
        # and test that we actually duplicated them and not moved them
        self.assertEqual(models.RuleLabel.objects.count(), 2)
        self.assertEqual(models.RuleAnnotation.objects.count(), 2)

    @mock.patch('django.db.models.signals.post_save')
    def test_import(self, mock_render):
        self.client.post(reverse('import'), {
            'rules': TEST_RULE
        })

        rule = models.Rule.objects.filter(name='ImportRule').get()
        self.assertEqual(models.RuleLabel.objects.filter(rule=rule).count(), 1, 'Missing labels')
        self.assertEqual(models.RuleAnnotation.objects.filter(rule=rule).count(), 1, 'Missing annotations')

@@ -1976,65 +1976,131 @@
     
-rule = models.Rule.objects.filter(name='ImportRule').get(
+# Includes count of our setUp rule + imported rules%0A        self.assertEqual(models.Rule.objects.count(), 3, 'Missing Rule'
 )%0A  
@@ -2147,34 +2147,16 @@
 objects.
-filter(rule=rule).
 count(),
@@ -2148,33 +2148,33 @@
 bjects.count(), 
-1
+4
 , 'Missing label
@@ -2236,26 +2236,8 @@
 cts.
-filter(rule=rule).
 coun
@@ -2241,17 +2241,17 @@
 ount(), 
-1
+7
 , 'Missi

Update our test case to account for missing object