oncall-engine/engine/apps/alerts/tests/factories.py
Matias Bordese fa815b7ecd
Reworked declare incident escalation step (#5130)
Reworked https://github.com/grafana/oncall/pull/5047. Main update is the
switch from FK to a [M2M
relation](https://docs.google.com/document/d/1HeulqxoFShSHtInQrZNJLL5MDlHPNT50rVGaK3zZWvw/edit?disco=AAABVLjV4W8)
(which doesn't really change the original/intended behavior, besides not
needing to alter the alert group table, and it is a bit more flexible;
the extra table shouldn't introduce issues because this is used only for
tracking purposes and the information needed in the log record is
already there).

Avoid a db migration involving alert group table:

```
--
-- Create model RelatedIncident
--
CREATE TABLE `alerts_relatedincident` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `incident_id` varchar(50) NOT NULL, `created_at` datetime(6) NOT NULL, `is_active` bool NOT NULL, `channel_filter_id` bigint NULL, `organization_id` bigint NOT NULL);
CREATE TABLE `alerts_relatedincident_attached_alert_groups` (`id` bigint AUTO_INCREMENT NOT NULL PRIMARY KEY, `relatedincident_id` bigint NOT NULL, `alertgroup_id` bigint NOT NULL);
ALTER TABLE `alerts_relatedincident` ADD CONSTRAINT `alerts_relatedincident_organization_id_incident_id_d7fc9a4f_uniq` UNIQUE (`organization_id`, `incident_id`);
ALTER TABLE `alerts_relatedincident` ADD CONSTRAINT `alerts_relatedincide_channel_filter_id_9556c836_fk_alerts_ch` FOREIGN KEY (`channel_filter_id`) REFERENCES `alerts_channelfilter` (`id`);
ALTER TABLE `alerts_relatedincident` ADD CONSTRAINT `alerts_relatedincide_organization_id_74ed6bed_fk_user_mana` FOREIGN KEY (`organization_id`) REFERENCES `user_management_organization` (`id`);
CREATE INDEX `alerts_relatedincident_incident_id_8356a799` ON `alerts_relatedincident` (`incident_id`);
ALTER TABLE `alerts_relatedincident_attached_alert_groups` ADD CONSTRAINT `alerts_relatedincident_a_relatedincident_id_alert_3d683baa_uniq` UNIQUE (`relatedincident_id`, `alertgroup_id`);
ALTER TABLE `alerts_relatedincident_attached_alert_groups` ADD CONSTRAINT `alerts_relatedincide_relatedincident_id_3e5e7a23_fk_alerts_re` FOREIGN KEY (`relatedincident_id`) REFERENCES `alerts_relatedincident` (`id`);
ALTER TABLE `alerts_relatedincident_attached_alert_groups` ADD CONSTRAINT `alerts_relatedincide_alertgroup_id_0125deca_fk_alerts_al` FOREIGN KEY (`alertgroup_id`) REFERENCES `alerts_alertgroup` (`id`);
```
2024-10-07 19:26:10 +00:00

99 lines
2.2 KiB
Python

import factory
from apps.alerts.models import (
Alert,
AlertGroup,
AlertGroupLogRecord,
AlertReceiveChannel,
AlertReceiveChannelConnection,
ChannelFilter,
CustomButton,
EscalationChain,
EscalationPolicy,
Invitation,
RelatedIncident,
ResolutionNote,
ResolutionNoteSlackMessage,
UserNotificationBundle,
)
from common.utils import UniqueFaker
class AlertReceiveChannelFactory(factory.DjangoModelFactory):
# integration = AlertReceiveChannel.INTEGRATION_GRAFANA
verbal_name = factory.Faker("sentence", nb_words=2)
class Meta:
model = AlertReceiveChannel
class AlertReceiveChannelConnectionFactory(factory.DjangoModelFactory):
class Meta:
model = AlertReceiveChannelConnection
class ChannelFilterFactory(factory.DjangoModelFactory):
class Meta:
model = ChannelFilter
class EscalationChainFactory(factory.DjangoModelFactory):
name = UniqueFaker("sentence", nb_words=3)
class Meta:
model = EscalationChain
class EscalationPolicyFactory(factory.DjangoModelFactory):
class Meta:
model = EscalationPolicy
class AlertFactory(factory.DjangoModelFactory):
class Meta:
model = Alert
class AlertGroupFactory(factory.DjangoModelFactory):
class Meta:
model = AlertGroup
class AlertGroupLogRecordFactory(factory.DjangoModelFactory):
class Meta:
model = AlertGroupLogRecord
class ResolutionNoteFactory(factory.DjangoModelFactory):
message_text = factory.Faker("sentence", nb_words=5)
class Meta:
model = ResolutionNote
class ResolutionNoteSlackMessageFactory(factory.DjangoModelFactory):
class Meta:
model = ResolutionNoteSlackMessage
class CustomActionFactory(factory.DjangoModelFactory):
webhook = factory.Faker("url")
name = UniqueFaker("sentence", nb_words=3)
class Meta:
model = CustomButton
class InvitationFactory(factory.DjangoModelFactory):
class Meta:
model = Invitation
class UserNotificationBundleFactory(factory.DjangoModelFactory):
class Meta:
model = UserNotificationBundle
class RelatedIncidentFactory(factory.DjangoModelFactory):
class Meta:
model = RelatedIncident