Fixing apply_order_updates method

remotes/origin/issue/4795/notification_even_they_are_disabled
Alejandro Alonso 2016-10-11 09:49:07 +02:00
parent bdd312e9e8
commit cda0d6451c
2 changed files with 30 additions and 0 deletions

View File

@ -36,6 +36,11 @@ def apply_order_updates(base_orders: dict, new_orders: dict):
The elements where no order update is needed will be removed. The elements where no order update is needed will be removed.
""" """
updated_order_ids = set() updated_order_ids = set()
# Remove the elements from new_orders non existint in base_orders
invalid_keys = new_orders.keys() - base_orders.keys()
[new_orders.pop(id, None) for id in invalid_keys]
# We will apply the multiple order changes by the new position order # We will apply the multiple order changes by the new position order
sorted_new_orders = [(k, v) for k, v in new_orders.items()] sorted_new_orders = [(k, v) for k, v in new_orders.items()]
sorted_new_orders = sorted(sorted_new_orders, key=lambda e: e[1]) sorted_new_orders = sorted(sorted_new_orders, key=lambda e: e[1])

View File

@ -161,3 +161,28 @@ def test_apply_order_updates_multiple_elements_duplicated_orders():
"e": 5, "e": 5,
"f": 6 "f": 6
} }
def test_apply_order_invalid_new_order():
orders = {
"a": 1,
"b": 2,
"c": 3,
"d": 3,
"e": 3,
"f": 4
}
new_orders = {
"c": 3,
"d": 3,
"x": 3,
"a": 4
}
apply_order_updates(orders, new_orders)
assert orders == {
"c": 3,
"d": 3,
"a": 4,
"e": 5,
"f": 6
}