Skip to content

Commit

Permalink
Add tests for verify filter by room attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Sep 14, 2023
1 parent 019c513 commit 59f8014
Showing 1 changed file with 127 additions and 2 deletions.
129 changes: 127 additions & 2 deletions tests/integration/models/alert_test.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from datetime import datetime

from nav.alertengine.dispatchers import InvalidAlertAddressError
from nav.models.manage import Room
from nav.models.profiles import (
Account,
AccountAlertQueue,
AlertAddress,
AlertProfile,
AlertSender,
AlertSubscription,
Expression,
Filter,
FilterGroup,
MatchField,
Operator,
TimePeriod,
)
from nav.models.event import AlertQueue, Subsystem
Expand Down Expand Up @@ -45,6 +50,113 @@ def test_sending_alert_to_alert_address_with_invalid_address_will_delete_alert_a
assert not AlertQueue.objects.filter(pk=alert.pk).exists()


def test_verify_alert_with_in_filter_with_matching_room_attribute_will_succeed(
db, alert, filtr
):
myroom = Room.objects.get(id="myroom")
myroom.data = {"size": "big"}
myroom.save()
Expression.objects.create(
filter=filtr,
match_field=MatchField.objects.get(name="Room attribute"),
operator=Operator.IN,
value="color: blue|size: big",
)

assert filtr.verify(alert)


def test_verify_alert_with_in_filter_with_non_matching_room_attribute_will_fail(
db, alert, filtr
):
myroom = Room.objects.get(id="myroom")
myroom.data = {"size": "small"}
myroom.save()
Expression.objects.create(
filter=filtr,
match_field=MatchField.objects.get(name="Room attribute"),
operator=Operator.IN,
value="color: blue|size: big",
)

assert not filtr.verify(alert)


def test_verify_alert_with_equals_filter_with_matching_room_attribute_will_succeed(
db, alert, filtr
):
myroom = Room.objects.get(id="myroom")
myroom.data = {
"size": "big",
"color": "blue",
}
myroom.save()
Expression.objects.create(
filter=filtr,
match_field=MatchField.objects.get(name="Room attribute"),
operator=Operator.EQUALS,
value="color: blue|size: big",
)

assert filtr.verify(alert)


def test_verify_alert_with_equals_filter_with_not_exactly_matching_room_attribute_will_fail(
db, alert, filtr
):
myroom = Room.objects.get(id="myroom")
myroom.data = {"size": "big"}
myroom.save()
Expression.objects.create(
filter=filtr,
match_field=MatchField.objects.get(name="Room attribute"),
operator=Operator.EQUALS,
value="color: blue|size: big",
)

assert not filtr.verify(alert)


def test_verify_alert_with_contains_filter_with_matching_room_attribute_will_succeed(
db, alert, filtr
):
myroom = Room.objects.get(id="myroom")
myroom.data = {
"size": "big",
"color": "blue",
"temperature": "warm",
}
myroom.save()
Expression.objects.create(
filter=filtr,
match_field=MatchField.objects.get(name="Room attribute"),
operator=Operator.CONTAINS,
value="color: blue|size: big",
)

assert filtr.verify(alert)


def test_verify_alert_with_contains_filter_with_not_exactly_matching_room_attribute_will_fail(
db, alert, filtr
):
myroom = Room.objects.get(id="myroom")
myroom.data = {
"size": "big",
"color": "red",
"temperature": "warm",
}
myroom.save()
Expression.objects.create(
filter=filtr,
match_field=MatchField.objects.get(name="Room attribute"),
operator=Operator.CONTAINS,
value="color: blue|size: big",
)

assert not filtr.verify(alert)


@pytest.fixture
def account(db):
return Account.objects.get(pk=Account.ADMIN_ACCOUNT)
Expand Down Expand Up @@ -94,9 +206,13 @@ def alertsub(db, alert_address, time_period):


@pytest.fixture
def alert(db):
def alert(db, localhost):
alert = AlertQueue(
source=Subsystem.objects.first(), time=datetime.now(), value=1, severity=3
source=Subsystem.objects.first(),
netbox=localhost,
time=datetime.now(),
value=1,
severity=3,
)
alert.save()
yield alert
Expand All @@ -111,3 +227,12 @@ def account_alert_queue(db, alert, alertsub):
yield account_queue
if account_queue.pk:
account_queue.delete()


@pytest.fixture
def filtr(db):
filtr = Filter(name="dummy", owner=Account.objects.get(id=Account.ADMIN_ACCOUNT))
filtr.save()
yield filtr
if filtr.pk:
filtr.delete()

0 comments on commit 59f8014

Please sign in to comment.