def test_anyset_init3_add(self): items = [1, 2, {1}, 4, 4, {1}] result = AnySet() for item in items: result.add(item) expected = ({1, 2, 4}, {6008617170096987129613061240357171817: {1}}) assert expected == result
def test_anyset_pop1(self): items = [1, 2, {1}, 4, 4, {1}] result = AnySet(items) while result: result_len = len(result) item = result.pop() assert item in items assert len(result) == result_len - 1
def test_anyset_init3_add(self): items = [1, 2, {1}, 4, 4, {1}] result = AnySet() for item in items: result.add(item) expected = ({1, 2, 4}, { 'e298e5a6cfa50a5b9d2cd4392c6c34a867d325e8de2966a8183c4cdf9a93120d': {1} }) assert expected == result
def _do_ignore_order(self): """ 't1': [5, 1, 1, 1, 6], 't2': [7, 1, 1, 1, 8], 'iterable_items_added_at_indexes': { 'root': { 0: 7, 4: 8 } }, 'iterable_items_removed_at_indexes': { 'root': { 4: 6, 0: 5 } } """ fixed_indexes = self.diff.get('iterable_items_added_at_indexes', dict_()) remove_indexes = self.diff.get('iterable_items_removed_at_indexes', dict_()) paths = set(fixed_indexes.keys()) | set(remove_indexes.keys()) for path in paths: # In the case of ignore_order reports, we are pointing to the container object. # Thus we add a [0] to the elements so we can get the required objects and discard what we don't need. elem_and_details = self._get_elements_and_details( "{}[0]".format(path)) if elem_and_details: _, parent, parent_to_obj_elem, parent_to_obj_action, obj, _, _ = elem_and_details else: continue # pragma: no cover. Due to cPython peephole optimizer, this line doesn't get covered. https://github.com/nedbat/coveragepy/issues/198 # copying both these dictionaries since we don't want to mutate them. fixed_indexes_per_path = fixed_indexes.get(path, dict_()).copy() remove_indexes_per_path = remove_indexes.get(path, dict_()).copy() fixed_indexes_values = AnySet(fixed_indexes_per_path.values()) new_obj = [] # Numpy's NdArray does not like the bool function. if isinstance(obj, np_ndarray): there_are_old_items = obj.size > 0 else: there_are_old_items = bool(obj) old_item_gen = self._do_ignore_order_get_old( obj, remove_indexes_per_path, fixed_indexes_values, path_for_err_reporting=path) while there_are_old_items or fixed_indexes_per_path: new_obj_index = len(new_obj) if new_obj_index in fixed_indexes_per_path: new_item = fixed_indexes_per_path.pop(new_obj_index) new_obj.append(new_item) elif there_are_old_items: try: new_item = next(old_item_gen) except StopIteration: there_are_old_items = False else: new_obj.append(new_item) else: # pop a random item from the fixed_indexes_per_path dictionary self._raise_or_log( INDEXES_NOT_FOUND_WHEN_IGNORE_ORDER.format( fixed_indexes_per_path)) new_item = fixed_indexes_per_path.pop( next(iter(fixed_indexes_per_path))) new_obj.append(new_item) if isinstance(obj, tuple): new_obj = tuple(new_obj) # Making sure that the object is re-instated inside the parent especially if it was immutable # and we had to turn it into a mutable one. In such cases the object has a new id. self._simple_set_elem_value(obj=parent, path_for_err_reporting=path, elem=parent_to_obj_elem, value=new_obj, action=parent_to_obj_action)
def test_anyset_init1(self): items = [1, 2, 4, 4] result = AnySet(items) expected = ({1, 2, 4}, {}) assert expected == result assert repr(result) == r'< AnySet OrderedSet([1, 2, 4]), {} >'
def test_iter_anyset(self): items = [1, 2, {1}, 4, 4, {1}, {3: 3}] obj = AnySet(items) result = [i for i in obj] assert [1, 2, 4, {1}, {3: 3}] == result