Skip to content

Commit

Permalink
Add Unit Tests for "manage_host_events" SpyQWidget function
Browse files Browse the repository at this point in the history
Ref #282
  • Loading branch information
algorys committed Mar 23, 2018
1 parent 8b571e3 commit 03b500a
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 3 deletions.
4 changes: 2 additions & 2 deletions alignak_app/qobjects/events/spy.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_hint_item():

def send_spy_events(self):
"""
Send event for one host spied
Send event messages for all hosts who are spied
"""

Expand Down Expand Up @@ -203,7 +203,7 @@ def remove_event(self):

self.update_parent_spytab()

def update_parent_spytab(self):
def update_parent_spytab(self): # pragma: no cover - not testable
"""
Update the parent spy tab text
Expand Down
129 changes: 128 additions & 1 deletion test/test_spy_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@

import unittest2

from PyQt5.Qt import QApplication
from PyQt5.Qt import QApplication, Qt

from alignak_app.backend.datamanager import data_manager
from alignak_app.items.host import Host
from alignak_app.items.service import Service

from alignak_app.qobjects.events.item import EventItem
from alignak_app.qobjects.events.events import init_event_widget
Expand Down Expand Up @@ -162,3 +163,129 @@ def test_send_spy_events(self):

# Sending events does not modify spy list widget count
self.assertEqual(2, under_test.spy_list_widget.count())

def test_manage_host_events_with_wrong_row(self):
"""Manage Host Events With Wrong Row"""

under_test = SpyQWidget()
under_test.initialize()

# Hint item in spy list widget, 0 item in host list widget
self.assertEqual(1, under_test.spy_list_widget.count())
self.assertEqual(0, under_test.host_list_widget.count())

# Filling database
host_test = Host()
host_test.create(
'spy1',
{
'_id': 'spy1',
'ls_downtimed': False,
'ls_acknowledged': False,
'active_checks_enabled': True,
'passive_checks_enabled': True,
'_overall_state_id': 4,
'ls_state': 'DOWN'
},
'hostname'
)
data_manager.update_database('host', [host_test])

# Spy this host
under_test.spy_list_widget.add_spy_host(host_test.item_id)

# Host have been added in list widget, 0 item in host list widget
self.assertEqual(1, under_test.spy_list_widget.count())
self.assertEqual(host_test.item_id, under_test.spy_list_widget.item(0).host)
self.assertEqual(0, under_test.host_list_widget.count())

# Manage problems
under_test.manage_host_events(-1)

# Host list equal to 0 with row equal to -1
self.assertEqual(0, under_test.host_list_widget.count())

def test_manage_host_events_with_valid_rows(self):
"""Manage Host Events With Valid Rows"""

under_test = SpyQWidget()
under_test.initialize()

# Filling "host" database
host_test = Host()
host_test.create(
'spy1',
{
'_id': 'spy1',
'ls_downtimed': False,
'ls_acknowledged': False,
'active_checks_enabled': True,
'passive_checks_enabled': True,
'_overall_state_id': 4,
'ls_state': 'DOWN'
},
'hostname'
)
data_manager.update_database('host', [host_test])

# Spy this host and set current row
under_test.spy_list_widget.add_spy_host(host_test.item_id)
under_test.spy_list_widget.setCurrentRow(0)

# Manage problems with a valid row
under_test.manage_host_events(under_test.spy_list_widget.currentRow())

# Host list equal to 1, No services are attached to host
self.assertEqual(1, under_test.host_list_widget.count())
self.assertEqual(
'No services for this host',
under_test.host_list_widget.item(0).data(Qt.DisplayRole)
)

# Fill "services" database attached to host
service = Service()
service.create(
'_id1',
{
'host': 'spy1',
'ls_state': 'CRITICAL',
'ls_acknowledged': False,
'ls_downtimed': False,
},
'service_name'
)
service_2 = Service()
service_2.create(
'_id2',
{
'host': 'spy1',
'ls_state': 'OK',
'ls_acknowledged': False,
'ls_downtimed': False,
},
'service2_name'
)
data_manager.update_database('service', [service, service_2])

# Manage problems again
under_test.manage_host_events(under_test.spy_list_widget.currentRow())

# Host list equal to 1, cause one service is CRITICAL
self.assertEqual(1, under_test.host_list_widget.count())
self.assertEqual(
'Service Service_Name is CRITICAL',
under_test.host_list_widget.item(0).data(Qt.DisplayRole)
)

# If CRITICAL service is removed, text change
data_manager.remove_item('service', '_id1')

# Manage problems again
under_test.manage_host_events(under_test.spy_list_widget.currentRow())

# Host list equal to 1, cause one service is CRITICAL
self.assertEqual(1, under_test.host_list_widget.count())
self.assertEqual(
'Hostname is DOWN but services of host seems managed.',
under_test.host_list_widget.item(0).data(Qt.DisplayRole)
)

0 comments on commit 03b500a

Please sign in to comment.