Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidlatwe committed Nov 13, 2020
1 parent 7b810dd commit b0f3224
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 45 deletions.
30 changes: 6 additions & 24 deletions tests/test_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ def test_select_app(self):
}
},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

with self.wait_signal(self.ctrl.state_changed, "ready"):
self.ctrl.select_profile("foo")
Expand Down Expand Up @@ -82,10 +79,7 @@ def test_app_environ(self):
}
},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

with self.wait_signal(self.ctrl.state_changed, "ready"):
self.ctrl.select_profile("foo")
Expand Down Expand Up @@ -134,10 +128,7 @@ def test_app_failed_independently_1(self):
"app_A": {"1": {"name": "app_A", "version": "1"}},
"app_B": {"1": {"name": "app_B", "version": "1"}},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

context_a = self.ctrl.state["rezContexts"]["app_A==1"]
context_b = self.ctrl.state["rezContexts"]["app_B==1"]
Expand All @@ -160,10 +151,7 @@ def test_app_failed_independently_2(self):
},
"app_B": {"1": {"name": "app_B", "version": "1"}},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

context_a = self.ctrl.state["rezContexts"]["app_A==None"]
context_b = self.ctrl.state["rezContexts"]["app_B==1"]
Expand Down Expand Up @@ -193,10 +181,7 @@ def test_app_failed_independently_3(self):
},
"app_B": {"1": {"name": "app_B", "version": "1"}},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

context_a = self.ctrl.state["rezContexts"]["app_A==1"]
context_b = self.ctrl.state["rezContexts"]["app_B==1"]
Expand All @@ -220,10 +205,7 @@ def test_app_failed_independently_4(self):
"app_A": {"1": {"name": "app_A", "version": "1"}},
"app_B": {"1": {"name": "app_B", "version": "1"}},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

context_a = self.ctrl.state["rezContexts"]["app_A==2"]
context_b = self.ctrl.state["rezContexts"]["app_B==1"]
Expand Down
69 changes: 61 additions & 8 deletions tests/test_docks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class TestDocks(util.TestBase):

def test_feature_blocked_on_failed_app(self):
"""Test feature blocked if application is broken"""
self.set_preference("showAdvancedControls", True)

util.memory_repository({
"foo": {
"1.0.0": {
Expand All @@ -19,22 +21,16 @@ def test_feature_blocked_on_failed_app(self):
},
"app_B": {"1": {"name": "app_B", "version": "1"}},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

context_a = self.ctrl.state["rezContexts"]["app_A==None"]
context_b = self.ctrl.state["rezContexts"]["app_B==1"]

self.assertFalse(context_a.success)
self.assertTrue(context_b.success)

self.show_advance_controls()

for app, state in {"app_A==None": False, "app_B==1": True}.items():
self.ctrl.select_application(app)
self.wait(100)
self.select_application(app)

dock = self.show_dock("environment", on_page="diagnose")
self.assertEqual(dock._widgets["compute"].isEnabled(), state)
Expand All @@ -44,3 +40,60 @@ def test_feature_blocked_on_failed_app(self):

dock = self.show_dock("app")
self.assertEqual(dock._widgets["launchBtn"].isEnabled(), state)

def test_version_editable_on_show_all_versions(self):
"""Test version is editable when show all version enabled"""
self._test_version_editable(show_all_version=True)

def test_version_editable_on_not_show_all_versions(self):
"""Test version is not editable when show all version disabled"""
self._test_version_editable(show_all_version=False)

def _test_version_editable(self, show_all_version):
self.set_preference("showAdvancedControls", True)
self.set_preference("showAllVersions", show_all_version)

util.memory_repository({
"foo": {
"1": {"name": "foo", "version": "1",
"requires": ["~app_A", "~app_B"]},
"2": {"name": "foo", "version": "2",
"requires": ["~app_A", "~app_B"]},
},
"app_A": {"1": {"name": "app_A", "version": "1"}},
"app_B": {"1": {"name": "app_B", "version": "1",
"requires": ["bar"]}},
"bar": {"1": {"name": "bar", "version": "1"},
"2": {"name": "bar", "version": "2"}}
})
self.ctrl_reset(["foo"])
self.select_application("app_B==1")

dock = self.show_dock("packages")
view = dock._widgets["view"]
proxy = view.model()
model = proxy.sourceModel()

for pkg, state in {"foo": False, # profile can't change version here
"bar": show_all_version,
"app_B": False}.items():
index = model.findIndex(pkg)
index = proxy.mapFromSource(index)

rect = view.visualRect(index)
position = rect.center()
with util.patch_cursor_pos(view.mapToGlobal(position)):
dock.on_right_click(position)
menu = self.get_menu(dock)
edit_action = next((a for a in menu.actions()
if a.text() == "Edit"), None)
if edit_action is None:
self.fail("No version edit action.")

self.assertEqual(
edit_action.isEnabled(), state,
"Package '%s' version edit state is incorrect." % pkg
)

self.wait(200)
menu.close()
5 changes: 1 addition & 4 deletions tests/test_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ def test_launch_subprocess(self):
}
},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

with self.wait_signal(self.ctrl.state_changed, "ready"):
self.ctrl.select_profile("foo")
Expand Down
5 changes: 1 addition & 4 deletions tests/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,7 @@ def test_profile_list_apps(self):
}
},
})
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(["foo"])
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")
self.ctrl_reset(["foo"])

with self.wait_signal(self.ctrl.state_changed, "ready"):
self.ctrl.select_profile("foo")
Expand Down
87 changes: 82 additions & 5 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@


def memory_repository(packages):
from rezplugins.package_repository import memory
from allzpark import _rezapi as rez

class MemoryVariantRes(memory.MemoryVariantResource):
def _root(self): # implement `root` to work with localz
return MEMORY_LOCATION

manager = rez.package_repository_manager
repository = manager.get_repository(MEMORY_LOCATION)
repository.pool.resource_classes[MemoryVariantRes.key] = MemoryVariantRes
repository.data = packages


Expand All @@ -27,6 +33,9 @@ def setUp(self):
app, ctrl = cli.initialize(clean=True, verbose=3)
window = cli.launch(ctrl)

size = window.size()
window.resize(size.width() + 80, size.height() + 80)

self.app = app
self.ctrl = ctrl
self.window = window
Expand All @@ -38,17 +47,47 @@ def tearDown(self):
self.window.close()
time.sleep(0.1)

def show_advance_controls(self):
def set_preference(self, name, value):
"""Setup preference
This should be called before ctrl reset.
(NOTE) Some preference change may calling ctrl.reset, so ctrl.reset
will be monkey-patched to do nothing, this is to prevent error
raised from resting without profile.
If not doing this, and setup preference after reset with test
profiles, calling reset again on preference changed may leads
to some weird profile model reset error. (item.internalPointer
returning random object and AttributeError raised)
Args:
name: preference name
value: preference value
Returns:
None
"""
preferences = self.window._docks["preferences"]
arg = next(opt for opt in preferences.options
if opt["name"] == "showAdvancedControls")
arg.write(True)
arg = next((opt for opt in preferences.options
if opt["name"] == name), None)
if not arg:
self.fail("Preference doesn't have this setting: %s" % name)

origin_reset = getattr(self.ctrl, "reset")
setattr(self.ctrl, "reset", lambda: None)
try:
arg.write(value)
except Exception as e:
self.fail("Preference '%s' set failed: %s" % (name, str(e)))
finally:
setattr(self.ctrl, "reset", origin_reset)

def show_dock(self, name, on_page=None):
dock = self.window._docks[name]
dock.toggle.setChecked(True)
dock.toggle.clicked.emit()
self.wait(timeout=200)
self.wait(timeout=50)

if on_page is not None:
tabs = dock._panels["central"]
Expand All @@ -58,6 +97,23 @@ def show_dock(self, name, on_page=None):

return dock

def ctrl_reset(self, profiles):
with self.wait_signal(self.ctrl.resetted):
self.ctrl.reset(profiles)
self.wait(timeout=200)
self.assertEqual(self.ctrl.state.state, "ready")

def select_application(self, app_request):
apps = self.window._widgets["apps"]
proxy = apps.model()
model = proxy.sourceModel()
index = model.findIndex(app_request)
index = proxy.mapFromSource(index)

sel_model = apps.selectionModel()
sel_model.select(index, sel_model.ClearAndSelect | sel_model.Rows)
self.wait(50)

def wait(self, timeout=1000):
from allzpark.vendor.Qt import QtCore

Expand Down Expand Up @@ -106,3 +162,24 @@ def on_timeout():
if not state["received"]:
timer.start(timeout)
loop.exec_()

def get_menu(self, widget):
from allzpark.vendor.Qt import QtWidgets
menus = widget.findChildren(QtWidgets.QMenu, "")
menu = next((m for m in menus if m.isVisible()), None)
if menu:
return menu
else:
self.fail("This widget doesn't have menu.")


@contextlib.contextmanager
def patch_cursor_pos(point):
from allzpark.vendor.Qt import QtGui

origin_pos = getattr(QtGui.QCursor, "pos")
setattr(QtGui.QCursor, "pos", lambda: point)
try:
yield
finally:
setattr(QtGui.QCursor, "pos", origin_pos)

0 comments on commit b0f3224

Please sign in to comment.