Skip to content

Commit

Permalink
Merge pull request #14 from ak2ls2py/riskmatrix
Browse files Browse the repository at this point in the history
Now we are moving to the final stage of integrating basic application.
  • Loading branch information
asselapathirana committed Dec 11, 2016
2 parents 27ed998 + b3740e0 commit 7d4ed9a
Show file tree
Hide file tree
Showing 16 changed files with 849 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ install:
- conda create -y -c anaconda --name $PYN python=$PY `head -n1 requirements_conda.txt`
- source activate $PYN
- python ./service/installreq.py
- python setup.py install
- python setup.py develop
# Builds Complete
- virtualenv --version
- easy_install --version
Expand Down
271 changes: 214 additions & 57 deletions src/rrpam_wds/gui/dialogs.py

Large diffs are not rendered by default.

46 changes: 46 additions & 0 deletions src/rrpam_wds/gui/images/network.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 38 additions & 0 deletions src/rrpam_wds/gui/images/risk.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 40 additions & 0 deletions src/rrpam_wds/gui/images/wlc.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions src/rrpam_wds/gui/monkey_patch_guiqwt_guidata.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,30 @@ def _patch_all():
_patch_item_list()
_patch_curve_do_autoscale()
_patch_curveitem_hit_test()
_patch_curveplot___del__()


def _patch_curveplot___del__():
"""1. Close a subwindow within qmidarea. 2. Close the application
It crashes with the message:
Traceback (most recent call last):
File "/home/user/miniconda3/envs/py34/lib/python3.5/site-packages/guiqwt/curve.py", line 1404, in __del__
canvas.removeEventFilter(self.filter)
RuntimeError: wrapped C/C++ object of type QwtPlotCanvas has been deleted
Currently I assume this is a bug in the curve.__del__ routine, not an issure of my implementation.
So, just adding a try: except clause
"""
orig___del__ = CurvePlot.__del__

def custom__del__(self):
try:
orig___del__(self)
except:
print("Better to fix me later.")

# now monkey patch
CurvePlot.__del__ = custom__del__


def _patch_curveitem_hit_test():
Expand Down
2 changes: 1 addition & 1 deletion src/rrpam_wds/gui/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def _get_type(epanet_network_item):
if(epanet_network_item == "Risk"):
return "R:", 'link.png'

return "None", None
return "None", "curve.png"
12 changes: 6 additions & 6 deletions tests/test_closable_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def tearDown(self):
def test_closable_graph_can_be_closed_by_user(self):
dummytitle = uniquestring()
title = uniquestring()
self.dummy = CurveDialogWithClosable(wintitle=dummytitle)
self.dummy = CurveDialogWithClosable(wintitle=dummytitle, mainwindow=self.aw)
self.aw.addSubWindow(self.dummy)
self.graph = CurveDialogWithClosable(wintitle=title)
self.graph = CurveDialogWithClosable(wintitle=title, mainwindow=self.aw)
self.aw.addSubWindow(self.graph)
self.assertNotEqual(self.graph.windowTitle, self.dummy.windowTitle)
self.assertEqual(self.aw.mdi.subWindowList()[-1].windowTitle(), title)
Expand All @@ -51,9 +51,9 @@ def test_closable_graph_can_be_closed_by_user(self):
def test_closable_graph_closable_false_minized(self):
dummytitle = uniquestring()
title = uniquestring()
self.dummy = CurveDialogWithClosable(wintitle=dummytitle)
self.dummy = CurveDialogWithClosable(wintitle=dummytitle, mainwindow=self.aw)
self.aw.addSubWindow(self.dummy)
self.graph = CurveDialogWithClosable(wintitle=title)
self.graph = CurveDialogWithClosable(wintitle=title, mainwindow=self.aw)
self.graph.setClosable(False)
self.aw.addSubWindow(self.graph)
self.assertNotEqual(self.graph.windowTitle, self.dummy.windowTitle)
Expand All @@ -66,9 +66,9 @@ def test_closable_graph_closable_false_minized(self):
def test_presseing_esc_does_not_close_or_clear_the_closable_graph(self):
dummytitle = uniquestring()
title = uniquestring()
self.dummy = CurveDialogWithClosable(wintitle=dummytitle)
self.dummy = CurveDialogWithClosable(wintitle=dummytitle, mainwindow=self.aw)
self.aw.addSubWindow(self.dummy)
self.graph = CurveDialogWithClosable(wintitle=title)
self.graph = CurveDialogWithClosable(wintitle=title, mainwindow=self.aw)
self.graph.setClosable(False)
self.aw.addSubWindow(self.graph)
self.aw.show()
Expand Down
144 changes: 144 additions & 0 deletions tests/test_keeping_records_of_adding_removing_plot_items.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
from rrpam_wds.gui import set_pyqt_api # isort:skip # NOQA
import os
import sys
import time
import unittest

import mock
import numpy as np
import pytest
from guiqwt import tests
from guiqwt.curve import CurveItem
from guiqwt.plot import CurveDialog
from guiqwt.shapes import EllipseShape
from numpy.testing import assert_array_almost_equal
from PyQt5.QtCore import Qt
from PyQt5.QtCore import QTimer
from PyQt5.QtTest import QTest
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMdiArea

from rrpam_wds.gui.dialogs import CurveDialogWithClosable
from rrpam_wds.gui.dialogs import MainWindow
from rrpam_wds.gui.dialogs import NetworkMap
from rrpam_wds.gui.dialogs import RiskMatrix
from rrpam_wds.gui.dialogs import optimalTimeGraph


class test_keeping_records(unittest.TestCase):
start = 0
stop = 0

def setUp(self):
global start
self.app = QApplication(sys.argv)
start = time.time()
self.aw = MainWindow()
self.aw.setWindowTitle("Records tests")

pass

def tearDown(self):
global stop
stop = time.time()
print("\ncalculation took %0.2f seconds." % (stop - start))
self.aw = None
pass

def runTest(self):
""" otherwise python 2.7 returns an error
ValueError: no such test method in <class 'myapp.tests.SessionTestCase'>: runTest"""
pass

def test_riskmatrix_report_adding_to_the_register(self):
rm = self.aw.riskmatrix

with mock.patch.object(rm, 'add_plot_item_to_record', autospec=True) as mock_add_plot_item_to_record:
self.assertFalse(mock_add_plot_item_to_record.called)
rm.plot_item("fox", [5000, 50], title="fox")
self.assertTrue(mock_add_plot_item_to_record.called)

def test_riskmatrix_report_removing_to_the_register(self):
rm = self.aw.riskmatrix

with mock.patch.object(rm, 'remove_plot_item_from_record', autospec=True) as mock_remove_plot_item_from_record:
self.assertFalse(mock_remove_plot_item_from_record.called)
rm.plot_item("fox", [5000, 50], title="fox")
self.assertFalse(mock_remove_plot_item_from_record.called)
risk_item = [x for x in rm.get_plot().get_items() if isinstance(x, EllipseShape)][0]
rm.get_plot().del_item(risk_item)
self.assertTrue(mock_remove_plot_item_from_record.called)

def test_networkmap_report_adding_to_the_register(self):
otg = self.aw.networkmap

with mock.patch.object(otg, 'add_plot_item_to_record', autospec=True) as mock_add_plot_item_to_record:
self.assertFalse(mock_add_plot_item_to_record.called)
otg.plot_item("fox", [(5, 10), (8, 3), (24, 1)], "fox")
self.assertTrue(mock_add_plot_item_to_record.called)

def test_networkmap_report_removing_to_the_register(self):
otg = self.aw.networkmap

with mock.patch.object(otg, 'remove_plot_item_from_record', autospec=True) as mock_remove_plot_item_from_record:
self.assertFalse(mock_remove_plot_item_from_record.called)
otg.plot_item("fox", [(5, 10), (8, 3), (24, 1)], "fox")
self.assertFalse(mock_remove_plot_item_from_record.called)
nw_item = [x for x in otg.get_plot().get_items() if isinstance(x, CurveItem)][0]
otg.get_plot().del_item(nw_item)
self.assertTrue(mock_remove_plot_item_from_record.called)

def test_optimaltimegraph_report_adding_to_the_register(self):
otg = list(self.aw.optimaltimegraphs.values())[0]

with mock.patch.object(otg, 'add_plot_item_to_record', autospec=True) as mock_add_plot_item_to_record:
self.assertFalse(mock_add_plot_item_to_record.called)
otg.plot_item(
"fox", [[1997, 1998, 2005, 2008], [5, 10, 25, 95], [100, 50, 25, 12]], "fox")
self.assertTrue(mock_add_plot_item_to_record.called)

def test_optimaltimegraph_report_removing_to_the_register(self):
otg = list(self.aw.optimaltimegraphs.values())[0]

with mock.patch.object(otg, 'remove_plot_item_from_record', autospec=True) as mock_remove_plot_item_from_record:
self.assertFalse(mock_remove_plot_item_from_record.called)
otg.plot_item(
"fox", [[1997, 1998, 2005, 2008], [5, 10, 25, 95], [100, 50, 25, 12]], "fox")
self.assertFalse(mock_remove_plot_item_from_record.called)
nw_item = [x for x in otg.get_plot().get_items() if isinstance(x, CurveItem)][0]
otg.get_plot().del_item(nw_item)
self.assertTrue(mock_remove_plot_item_from_record.called)

def test_deleting_a_curve_in_optimaltimegraph_removes_all_three_curves(self):
# for the heck of it add another optimal time graph
self.aw.add_optimaltimegraph()
l = list(self.aw.optimaltimegraphs.values())
otg1 = l[0]
otg2 = l[1]
self.assertEqual(len(otg1.myplotitems), 0)
otg1.plot_item("fox", [[1997, 1998, 2005, 2008], [5, 10, 25, 95], [100, 50, 25, 12]], "fox")
otg2.plot_item("fox", [[1997, 1998, 2005, 2008], [5, 10, 25, 95], [100, 50, 25, 12]], "fox")
self.assertEqual(len(otg1.myplotitems), 1)
self.assertEqual(len(otg2.myplotitems), 1)
otg_item = [x for x in otg1.get_plot().get_items() if isinstance(x, CurveItem)][0]
otg1.get_plot().del_item(otg_item)
self.assertEqual(len(otg1.myplotitems), 0)
otg_item = [x for x in otg2.get_plot().get_items() if isinstance(x, CurveItem)][
2] # pick a different curve from the set!
otg2.get_plot().del_item(otg_item)
self.assertEqual(len(otg2.myplotitems), 0)


def drive(test=True): # pragma: no cover
if(test):
unittest.main(verbosity=2)
else:
ot = test_keeping_records()
ot.setUp()
ot.test_deleting_a_curve_in_optimaltimegraph_removes_all_three_curves()
ot.aw.show()
sys.exit(ot.app.exec_())

if __name__ == '__main__': # pragma: no cover
drive(test=False)
Loading

0 comments on commit 7d4ed9a

Please sign in to comment.