Skip to content

Commit

Permalink
fixup! Custom the save method of the Rule model to handle the annotat…
Browse files Browse the repository at this point in the history
…ion "rule"

Refactor the code changes:
- Change the function's name to "test_rule_annotation" from "test_custom_save_rule" because all tests inside this function are related to rule annotation.
- Wrap comment lines at 100 characters.
- Use double quotes instead of single quotes.
  • Loading branch information
hoangpn committed Dec 3, 2024
1 parent 40958ef commit 675e6dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
5 changes: 3 additions & 2 deletions promgen/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,12 @@ def save(self, *args, **kwargs):
self.annotations["rule"] = resolve_domain("rule-detail", pk=self.pk)
super().save(*args, **kwargs)
else:
# When creating a new rule, the primary key is typically not available until the instance is saved to the database.
# When creating a new rule, the primary key is typically not available until the
# instance is saved to the database.
# Therefore, we save it first then set annotations["rule"] and save again.
super().save(*args, **kwargs)
self.annotations["rule"] = resolve_domain("rule-detail", pk=self.pk)
super().save(update_fields=['annotations'])
super().save(update_fields=["annotations"])


class AlertLabel(models.Model):
Expand Down
14 changes: 9 additions & 5 deletions promgen/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ def test_validators(self):
)

@mock.patch("django.dispatch.dispatcher.Signal.send")
def test_custom_save_rule(self, mock_post):
# Check if annotation["rule"] is automatically set to be {domain}/rule/{id} when creating a new rule
def test_rule_annotation(self, mock_post):
# Check if annotation["rule"] is automatically set to be {domain}/rule/{id} when creating a
# new rule
rule = models.Rule(
name="example-rule",
content_type=ContentType.objects.get_for_model(models.Site),
Expand All @@ -50,18 +51,21 @@ def test_custom_save_rule(self, mock_post):
rule.save()
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])

# Check if annotation["rule"] is automatically set to be {domain}/rule/{id} when updating an existed rule
# Check if annotation["rule"] is automatically set to be {domain}/rule/{id} when updating an
# existed rule
rule.name = "another-example-rule"
rule.annotations["rule"] = "another-annotation-value"
rule.save()
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])

# Check if annotation["rule"] is still set to be {domain}/rule/{id} when trying to remove annotation["rule"]
# Check if annotation["rule"] is still set to be {domain}/rule/{id} when trying to remove
# annotation["rule"]
rule.annotations["rule"] = None
rule.save()
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])

# Check if annotation["rule"] of new rule is automatically set to be {domain}/rule/{id} when cloning an existed rule
# Check if annotation["rule"] of new rule is automatically set to be {domain}/rule/{id}
# when cloning an existed rule
new_rule = rule.copy_to(content_type="service", object_id=2)
self.assertEqual(resolve_domain("rule-detail", rule.pk), rule.annotations["rule"])
self.assertEqual(resolve_domain("rule-detail", new_rule.pk), new_rule.annotations["rule"])

0 comments on commit 675e6dd

Please sign in to comment.