|
| 1 | +require "./helper" |
| 2 | + |
| 3 | +module PlaceOS::Model |
| 4 | + describe Alert do |
| 5 | + test_round_trip(Alert) |
| 6 | + |
| 7 | + it "saves an alert" do |
| 8 | + authority = Generator.authority.save! |
| 9 | + dashboard = Generator.alert_dashboard(authority_id: authority.id).save! |
| 10 | + inst = Generator.alert(alert_dashboard_id: dashboard.id).save! |
| 11 | + Alert.find!(inst.id.as(String)).id.should eq inst.id |
| 12 | + end |
| 13 | + |
| 14 | + it "validates required fields" do |
| 15 | + invalid_model = Generator.alert |
| 16 | + invalid_model.name = "" |
| 17 | + invalid_model.alert_dashboard_id = nil |
| 18 | + |
| 19 | + invalid_model.valid?.should be_false |
| 20 | + invalid_model.errors.size.should eq 2 |
| 21 | + invalid_model.errors.map(&.field).should contain(:name) |
| 22 | + invalid_model.errors.map(&.field).should contain(:alert_dashboard_id) |
| 23 | + end |
| 24 | + |
| 25 | + it "belongs to alert dashboard" do |
| 26 | + authority = Generator.authority.save! |
| 27 | + dashboard = Generator.alert_dashboard(authority_id: authority.id).save! |
| 28 | + alert = Generator.alert(alert_dashboard_id: dashboard.id).save! |
| 29 | + |
| 30 | + alert.alert_dashboard.should_not be_nil |
| 31 | + alert.alert_dashboard.try(&.id).should eq dashboard.id |
| 32 | + end |
| 33 | + |
| 34 | + it "has default values" do |
| 35 | + alert = Generator.alert |
| 36 | + alert.enabled.should be_true |
| 37 | + alert.severity.should eq Alert::Severity::MEDIUM |
| 38 | + alert.alert_type.should eq Alert::AlertType::THRESHOLD |
| 39 | + alert.debounce_period.should eq 60000 |
| 40 | + end |
| 41 | + |
| 42 | + it "validates conditions" do |
| 43 | + authority = Generator.authority.save! |
| 44 | + dashboard = Generator.alert_dashboard(authority_id: authority.id).save! |
| 45 | + model = Generator.alert(alert_dashboard_id: dashboard.id) |
| 46 | + |
| 47 | + valid = Trigger::Conditions::TimeDependent.new( |
| 48 | + type: Trigger::Conditions::TimeDependent::Type::At, |
| 49 | + time: Time.utc, |
| 50 | + ) |
| 51 | + |
| 52 | + invalid = Trigger::Conditions::TimeDependent.new( |
| 53 | + cron: "5 * * * *", |
| 54 | + ) |
| 55 | + model.conditions.try &.time_dependents = [valid, invalid] |
| 56 | + |
| 57 | + model.valid?.should be_false |
| 58 | + model.errors.size.should eq 1 |
| 59 | + model.errors.first.to_s.should end_with "type should not be nil" |
| 60 | + end |
| 61 | + |
| 62 | + describe "severity helpers" do |
| 63 | + it "identifies critical alerts" do |
| 64 | + alert = Generator.alert |
| 65 | + alert.severity = Alert::Severity::CRITICAL |
| 66 | + alert.critical?.should be_true |
| 67 | + |
| 68 | + alert.severity = Alert::Severity::HIGH |
| 69 | + alert.critical?.should be_false |
| 70 | + end |
| 71 | + |
| 72 | + it "identifies high priority alerts" do |
| 73 | + alert = Generator.alert |
| 74 | + |
| 75 | + alert.severity = Alert::Severity::CRITICAL |
| 76 | + alert.high_priority?.should be_true |
| 77 | + |
| 78 | + alert.severity = Alert::Severity::HIGH |
| 79 | + alert.high_priority?.should be_true |
| 80 | + |
| 81 | + alert.severity = Alert::Severity::MEDIUM |
| 82 | + alert.high_priority?.should be_false |
| 83 | + |
| 84 | + alert.severity = Alert::Severity::LOW |
| 85 | + alert.high_priority?.should be_false |
| 86 | + end |
| 87 | + end |
| 88 | + |
| 89 | + describe "enum validation" do |
| 90 | + it "works with valid severity values" do |
| 91 | + authority = Generator.authority.save! |
| 92 | + dashboard = Generator.alert_dashboard(authority_id: authority.id).save! |
| 93 | + |
| 94 | + Alert::Severity.values.each do |severity| |
| 95 | + alert = Generator.alert(alert_dashboard_id: dashboard.id) |
| 96 | + alert.severity = severity |
| 97 | + alert.valid?.should be_true |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + it "works with valid alert type values" do |
| 102 | + authority = Generator.authority.save! |
| 103 | + dashboard = Generator.alert_dashboard(authority_id: authority.id).save! |
| 104 | + |
| 105 | + Alert::AlertType.values.each do |alert_type| |
| 106 | + alert = Generator.alert(alert_dashboard_id: dashboard.id) |
| 107 | + alert.alert_type = alert_type |
| 108 | + alert.valid?.should be_true |
| 109 | + end |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments