Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test for collection sorting #164

Merged
merged 2 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pyblish_qml/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,36 @@ def __toggle_item(self, model, index):

item.isToggled = new_value

def refresh(self):

self.item_model.beginResetModel()

# getting instance names in host order
host_order = []
for instance in self.host.context():
host_order.append(str(instance))

# index order of item_model, organized to host order
plugin_index = len(self.item_model.plugins) + 1
index_reorder = [i for i in range(0, plugin_index)]
for instance in self.host.context():
for item in self.item_model.items:
if item.id == str(instance):
index_reorder.append(self.item_model.items.index(item))

# constructing item_model in current host order
model_reorder = []
for i in index_reorder:
model_reorder.append(self.item_model.items[i])

# reordering item_model
for item in model_reorder:
index = model_reorder.index(item)
self.item_model.items[index] = item

self.item_model.endResetModel()


def echo(self, data):
"""Append `data` to result model"""
self.result_model.add_item(data)
Expand Down Expand Up @@ -642,6 +672,8 @@ def on_finished(plugins, context):

self.initialised.emit()

self.refresh()

self.host.emit("reset", context=context)

def on_run(plugins, context):
Expand Down
47 changes: 47 additions & 0 deletions tests/test_control.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,50 @@ def on_validated(context):
validate(c)

assert count["#"] == 1, count


@with_setup(lib.clean)
def test_gui_vs_host_order():
"""gui order is the same as the host order"""

class Collector(pyblish.api.Collector):

def process(self, context):
instance = context.create_instance("AC")
instance.set_data("family", "FamilyA")
instance = context.create_instance("AB")
instance.set_data("family", "FamilyA")

instance = context.create_instance("BC")
instance.set_data("family", "FamilyB")
instance = context.create_instance("BA")
instance.set_data("family", "FamilyB")

class CollectSorting(pyblish.api.Collector):
""" Sorts the context by family and name """

order = pyblish.api.Collector.order + 0.1

def process(self, context):

context[:] = sorted(context,
key=lambda instance: (instance.data("family"),
instance.data("name")))

pyblish.api.register_plugin(Collector)
pyblish.api.register_plugin(CollectSorting)

c = reset()

host_order = []
for instance in c.host.context():
host_order.append(str(instance))

gui_order = []
for item in c.item_model.items:
if item.itemType == 'instance' and str(item) != 'Context':
gui_order.append(str(item.id))

msg = "\n%s >> GUI order" % gui_order
msg += "\n%s >> Host order" % host_order
assert host_order == gui_order, msg