<commit_before># -*- coding: utf-8 -*-

from verta._internal_utils import _utils
from verta.common import comparison
from verta.operations.monitoring.alert import (
    FixedAlerter,
    ReferenceAlerter,
)
from verta.operations.monitoring.alert.status import (
    Alerting,
    Ok,
)
from verta.operations.monitoring.alert import _entities
from verta.operations.monitoring.summaries import SummarySampleQuery
from verta.operations.monitoring.notification_channel import (
    SlackNotificationChannel,
)


class TestIntegration:
    """Alerts + related entities/objects."""

    def test_add_notification_channels(self, client, monitored_entity):
        alerts = monitored_entity.alerts
        name = _utils.generate_default_name()
        alerter = FixedAlerter(comparison.GreaterThan(0.7))
        sample_query = SummarySampleQuery()

        channel1 = client.operations.notification_channels.create(
            _utils.generate_default_name(),
            SlackNotificationChannel(_utils.generate_default_name()),
        )
        channel2 = client.operations.notification_channels.create(
            _utils.generate_default_name(),
            SlackNotificationChannel(_utils.generate_default_name()),
        )

        alert = alerts.create(
            name,
            alerter,
            sample_query,
            notification_channels=[channel1],
        )
        assert alert._msg.notification_channels.keys() == {channel1.id}

        alert.add_notification_channels([channel2])
        alert._refresh_cache()
        assert alert._msg.notification_channels.keys() == {channel1.id, channel2.id}

    def test_set_status(self, monitored_entity, summary_sample):
        alerts = monitored_entity.alerts
        name = _utils.generate_default_name()
        alerter = FixedAlerter(comparison.GreaterThan(0.7))
        sample_query = SummarySampleQuery()

        alert = alerts.create(name, alerter, sample_query)
        assert alert.status == Ok()

        alert.set_status(Alerting([summary_sample]))
        assert alert.status == Alerting([summary_sample])

        alert.set_status(Ok())
        assert alert.status == Ok()

    def test_summary_sample_query(self, monitored_entity, summary_sample):
        alerts = monitored_entity.alerts
        name = _utils.generate_default_name()
        alerter = FixedAlerter(comparison.GreaterThan(0.7))<commit_msg>Properly compare sets (#2146)<commit_after># -*- coding: utf-8 -*-

from verta._internal_utils import _utils
from verta.common import comparison
from verta.operations.monitoring.alert import (
    FixedAlerter,
    ReferenceAlerter,
)
from verta.operations.monitoring.alert.status import (
    Alerting,
    Ok,
)
from verta.operations.monitoring.alert import _entities
from verta.operations.monitoring.summaries import SummarySampleQuery
from verta.operations.monitoring.notification_channel import (
    SlackNotificationChannel,
)


class TestIntegration:
    """Alerts + related entities/objects."""

    def test_add_notification_channels(self, client, monitored_entity):
        alerts = monitored_entity.alerts
        name = _utils.generate_default_name()
        alerter = FixedAlerter(comparison.GreaterThan(0.7))
        sample_query = SummarySampleQuery()

        channel1 = client.operations.notification_channels.create(
            _utils.generate_default_name(),
            SlackNotificationChannel(_utils.generate_default_name()),
        )
        channel2 = client.operations.notification_channels.create(
            _utils.generate_default_name(),
            SlackNotificationChannel(_utils.generate_default_name()),
        )

        alert = alerts.create(
            name,
            alerter,
            sample_query,
            notification_channels=[channel1],
        )
        retrieved_channel_ids = alert._msg.notification_channels.keys()
        assert set(retrieved_channel_ids) == {channel1.id}

        alert.add_notification_channels([channel2])
        alert._refresh_cache()
        retrieved_channel_ids = alert._msg.notification_channels.keys()
        assert set(retrieved_channel_ids) == {channel1.id, channel2.id}

    def test_set_status(self, monitored_entity, summary_sample):
        alerts = monitored_entity.alerts
        name = _utils.generate_default_name()
        alerter = FixedAlerter(comparison.GreaterThan(0.7))
        sample_query = SummarySampleQuery()

        alert = alerts.create(name, alerter, sample_query)
        assert alert.status == Ok()

        alert.set_status(Alerting([summary_sample]))
        assert alert.status == Alerting([summary_sample])

        alert.set_status(Ok())
        assert alert.status == Ok()

    def test_summary_sample_query(self, monitored_entity, summary_sample):
        alerts = monitored_entity.alerts
        name = _utils.generate_default_name()
        alerter = FixedAlerter(comparison.GreaterThan(0.7))
{'fraction_non_alphanumeric': 0.09118727050183599, 'fraction_numerical': 0.006527947776417789, 'mean_word_length': 3.2340241796200346, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3019378', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1323, 'n_words': 226}