def comparison(self): s = ObservedList([[1, 2]]) assert s == ObservedList([[1, 2]]) assert s[0] == ObservedList([1, 2]) assert s == ObservedList(ObservedList([1, 2])) assert s == [[1, 2]] assert s[0] == [1, 2]
def test_append_nested_struct(self): s = ObservedList(pre_callbacks=[pre_callback], post_callbacks=[post_callback]) s.append([1]) assert pre_callback.call_args == [(s, ObservedOperation.Add, [0], None, [1])] assert post_callback.call_args == [(s, ObservedOperation.Add, [0], None, [1])] assert s == [[1]]
def test_add(self): s = ObservedList(pre_callbacks=[pre_callback], post_callbacks=[post_callback]) s.append(1) assert pre_callback.call_args == [(s, ObservedOperation.Add, [0], None, 1)] assert post_callback.call_args == [(s, ObservedOperation.Add, [0], None, 1)] assert s == [1]
def test_append_struct_as_nested_struct(self): s = ObservedList(pre_callbacks=[pre_callback], post_callbacks=[post_callback]) s.append(ObservedList([1])) assert pre_callback.call_args == [(s, ObservedOperation.Add, [0], None, [1])] assert post_callback.call_args == [(s, ObservedOperation.Add, [0], None, [1])] assert s == [[1]] assert s[0]._parent == s assert s[0]._reference_in_parent == 0
def test_instantiation_with_another_observed_struct_type(self): s = ObservedList([{"a": 1}]) assert s == [{"a": 1}] assert s[0] == {"a": 1} assert isinstance(s[0], ObservedDict) assert s[0]._parent is s assert s[0]._reference_in_parent == 0
def test_pop(self): s = ObservedList( [[1], [2]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s.pop(0) assert pre_callback.call_args == [(s, ObservedOperation.Remove, [0], [1], None)] assert post_callback.call_args == [(s, ObservedOperation.Remove, [0], [1], None)] assert s == [[2]] assert s[0]._parent is s assert s[0]._reference_in_parent == 0
def test_instantion_with_nested_list(self): s = ObservedList([[1]]) assert s == [[1]] assert s[0] == [1] assert s[0][0] == 1 assert isinstance(s[0], ObservedList) assert s[0]._parent is s assert s[0]._reference_in_parent == 0
def test_insert(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s.insert(0, [0]) assert pre_callback.call_args == [(s, ObservedOperation.Add, [0], None, [0])] assert post_callback.call_args == [(s, ObservedOperation.Add, [0], None, [0])] assert s == [[0], [1]] assert s[0]._parent is s assert s[0]._reference_in_parent == 0 assert s[1]._parent is s assert s[1]._reference_in_parent == 1
def test_update_struct_as_nested_struct(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) old_struct = s._struct[0] s[0] = ObservedList([2]) assert pre_callback.call_args == [(s, ObservedOperation.Update, [0], [1], [2])] assert post_callback.call_args == [(s, ObservedOperation.Update, [0], [1], [2])] assert s == [[2]] assert s[0]._parent == s assert s[0]._reference_in_parent == 0 assert old_struct._parent is None assert old_struct._reference_in_parent is None
def test_reverse(self): s = ObservedList( [[1], [2]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s.reverse() assert pre_callback.call_args == [ (s, ObservedOperation.Update, [0], [1], [2]), (s, ObservedOperation.Update, [1], [2], [1]), ] assert post_callback.call_args == [ (s, ObservedOperation.Update, [0], [1], [2]), (s, ObservedOperation.Update, [1], [2], [1]), ] assert s == [[2], [1]] assert s[0]._parent is s assert s[0]._reference_in_parent == 0 assert s[1]._parent is s assert s[1]._reference_in_parent == 1
def test_access_to_nested_struct(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) assert s[0] == [1] assert pre_callback.call_args == [(s, ObservedOperation.Access, [0], None, None)] assert post_callback.call_args == [(s, ObservedOperation.Access, [0], None, None)] assert s == [[1]]
def test_delete(self): s = ObservedList( [1], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) del s[0] assert pre_callback.call_args == [(s, ObservedOperation.Remove, [0], 1, None)] assert post_callback.call_args == [(s, ObservedOperation.Remove, [0], 1, None)] assert s == []
def test_access(self): s = ObservedList( [1], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) assert s[0] == 1 assert pre_callback.call_args == [(s, ObservedOperation.Access, [0], None, None)] assert post_callback.call_args == [(s, ObservedOperation.Access, [0], None, None)] assert s == [1]
def test_update(self): s = ObservedList( [1], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s[0] = 2 assert pre_callback.call_args == [(s, ObservedOperation.Update, [0], 1, 2)] assert post_callback.call_args == [(s, ObservedOperation.Update, [0], 1, 2)] assert s == [2]
def test_add_nested_struct(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s = s + [[2]] assert pre_callback.call_args == [] assert post_callback.call_args == [] assert s == [[1], [2]] assert s[0]._parent is s assert s[0]._reference_in_parent == 0 assert s[1]._parent is s assert s[1]._reference_in_parent == 1
def test_update_scalar_in_nested_struct(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s[0][0] = 2 assert pre_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Update, [0, 0], 1, 2), ] assert post_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Update, [0, 0], 1, 2), ] assert s == [[2]]
def test_iadd_nested_struct(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s += [[2]] assert pre_callback.call_args == [(s, ObservedOperation.Add, [1], None, [2])] assert post_callback.call_args == [(s, ObservedOperation.Add, [1], None, [2])] assert s == [[1], [2]] assert s[0]._parent is s assert s[0]._reference_in_parent == 0 assert s[1]._parent is s assert s[1]._reference_in_parent == 1
def test_append_scalar_to_nested_struct(self): s = ObservedList( [[]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) s[0].append(1) assert pre_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Add, [0, 0], None, 1), ] assert post_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Add, [0, 0], None, 1), ] assert s == [[1]]
def test_delete_scalar_from_nested_struct(self): s = ObservedList( [[1]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) del s[0][0] assert pre_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Remove, [0, 0], 1, None), ] assert post_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Remove, [0, 0], 1, None), ] assert s == [[]]
def test_delete_nested_struct_with_slice(self): s = ObservedList( [[1], [2]], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) old_struct = s._struct[0] del s[0:1] assert pre_callback.call_args == [(s, ObservedOperation.Remove, [0], [1], None)] assert post_callback.call_args == [(s, ObservedOperation.Remove, [0], [1], None)] assert s == [[2]] assert s[0]._parent is s assert s[0]._reference_in_parent == 0 assert old_struct._parent is None assert old_struct._reference_in_parent is None
def test_dict_in_list(self): s = ObservedList( [{ "a": 1 }], pre_callbacks=[pre_callback], post_callbacks=[post_callback], ) assert s[0]["a"] == 1 assert pre_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Access, [0, "a"], None, None), ] assert post_callback.call_args == [ (s, ObservedOperation.Access, [0], None, None), (s, ObservedOperation.Access, [0, "a"], None, None), ] assert s == [{"a": 1}]
def test_instantiation_without_arguments(self): s = ObservedList() assert s == []
def test_instantiation_with_another_observed_list(self): s = ObservedList(ObservedList([1])) assert s == [1] assert isinstance(s._struct, list)
def test_instantiation_with_list(self): s = ObservedList([1]) assert s == [1] assert s[0] == 1
def s(self): return ObservedList([1, 2, 3])
def test_insert(self): s = ObservedList([]) s.insert(0, 1) s.insert(0, 2) s.insert(0, 3) assert s == [3, 2, 1]
def test_insert_last(self): s = ObservedList([]) s.insert(10, 1) assert s == [1]
def test_resolve_slice_to_indexes(self): s = ObservedList([]) assert list(s._resolve_slice_to_indexes(slice(0, 0))) == [] assert list(s._resolve_slice_to_indexes(slice(0, 1))) == [] assert list(s._resolve_slice_to_indexes(slice(0, 2))) == [] s = ObservedList([1]) assert list(s._resolve_slice_to_indexes(slice(0, 0))) == [] assert list(s._resolve_slice_to_indexes(slice(0, 1))) == [0] assert list(s._resolve_slice_to_indexes(slice(0, 2, 2))) == [0] s = ObservedList([1, 2]) assert list(s._resolve_slice_to_indexes(slice(0, 0))) == [] assert list(s._resolve_slice_to_indexes(slice(0, 1))) == [0] assert list(s._resolve_slice_to_indexes(slice(0, 2))) == [0, 1] assert list(s._resolve_slice_to_indexes(slice(0, 2, 2))) == [0] assert list(s._resolve_slice_to_indexes(slice(0, 3))) == [0, 1]
def test_instantiation_with_empty_list(self): s = ObservedList([]) assert s == []