|
| 1 | +from pytest import raises |
| 2 | + |
| 3 | +from graphql.pyutils import ReadOnlyError, ReadOnlyList |
| 4 | + |
| 5 | + |
| 6 | +def describe_read_only_list(): |
| 7 | + def can_read(): |
| 8 | + rol = ReadOnlyList([1, 2, 3]) |
| 9 | + assert rol == [1, 2, 3] |
| 10 | + assert list(i for i in rol) == rol |
| 11 | + assert rol.copy() == rol |
| 12 | + assert 2 in rol |
| 13 | + assert 4 not in rol |
| 14 | + assert rol + [4, 5] == [1, 2, 3, 4, 5] |
| 15 | + assert [4, 5] + rol == [4, 5, 1, 2, 3] |
| 16 | + assert rol * 2 == [1, 2, 3, 1, 2, 3] |
| 17 | + assert 2 * rol == [1, 2, 3, 1, 2, 3] |
| 18 | + assert rol[1] == 2 |
| 19 | + with raises(IndexError): |
| 20 | + rol[3] |
| 21 | + assert rol[1:4] == [2, 3] |
| 22 | + assert rol[::2] == [1, 3] |
| 23 | + assert len(rol) == 3 |
| 24 | + assert min(rol) == 1 |
| 25 | + assert max(rol) == 3 |
| 26 | + assert sum(rol) == 6 |
| 27 | + assert rol.index(2) == 1 |
| 28 | + with raises(ValueError): |
| 29 | + rol.index(4) |
| 30 | + assert rol.count(2) == 1 |
| 31 | + assert rol.count(4) == 0 |
| 32 | + assert list(reversed(rol)) == [3, 2, 1] |
| 33 | + assert sorted(rol) == [1, 2, 3] |
| 34 | + |
| 35 | + def cannot_write(): |
| 36 | + rol = ReadOnlyList([1, 2, 3]) |
| 37 | + with raises(ReadOnlyError): |
| 38 | + rol[1] = 4 |
| 39 | + with raises(ReadOnlyError): |
| 40 | + rol[1:4] = [4] |
| 41 | + with raises(ReadOnlyError): |
| 42 | + del rol[1] |
| 43 | + with raises(ReadOnlyError): |
| 44 | + del rol[1:4] |
| 45 | + with raises(ReadOnlyError): |
| 46 | + rol[1::2] = [4] |
| 47 | + with raises(ReadOnlyError): |
| 48 | + del rol[::2] |
| 49 | + with raises(ReadOnlyError): |
| 50 | + rol.append(4) |
| 51 | + with raises(ReadOnlyError): |
| 52 | + rol.clear() |
| 53 | + with raises(ReadOnlyError): |
| 54 | + rol.extend([4]) |
| 55 | + with raises(ReadOnlyError): |
| 56 | + rol += [4] |
| 57 | + with raises(ReadOnlyError): |
| 58 | + rol *= 2 |
| 59 | + with raises(ReadOnlyError): |
| 60 | + rol.insert(1, 4) |
| 61 | + with raises(ReadOnlyError): |
| 62 | + rol.pop() |
| 63 | + with raises(ReadOnlyError): |
| 64 | + rol.remove(2) |
| 65 | + with raises(ReadOnlyError): |
| 66 | + rol.sort() |
| 67 | + with raises(ReadOnlyError): |
| 68 | + rol.reverse() |
| 69 | + |
| 70 | + def can_add_rol(): |
| 71 | + rol1 = ReadOnlyList([1, 2]) |
| 72 | + rol2 = ReadOnlyList([3, 4]) |
| 73 | + assert rol1 + rol2 == [1, 2, 3, 4] |
| 74 | + |
| 75 | + def can_add_tuple(): |
| 76 | + rol = ReadOnlyList([1, 2]) |
| 77 | + assert rol + (3, 4) == [1, 2, 3, 4] |
| 78 | + |
| 79 | + def read_only_error_is_type_error(): |
| 80 | + assert issubclass(ReadOnlyError, TypeError) |
0 commit comments