Beispiel #1
0
def test_defining_inherited_classes_alters_mapping():
    """
    Test that we can safely define inherited classes and they will be used.
    """
    class MySubClass(ReadOnlyList):
        pass

    rl = readonly([[0]])
    assert_true(isinstance(rl, MySubClass))
    assert_true(isinstance(rl[0], MySubClass))

    reset_mapping()
    # Confirm reset has worked
    rl = readonly([[0]])
    assert_false(isinstance(rl, MySubClass))
    assert_false(isinstance(rl[0], MySubClass))
Beispiel #2
0
def test_get_writable_version_of_the_object():
    """
    Test get_writable returns a fully writable version of the mutable.
    """
    d = readonly({
        'list_of_dicts': [
            {'key': 'val'},
        ],
        'dict_of_lists': {
            "a": ["something", "here"],
        }
    })

    # Check that no exceptions are raised:
    w = d.get_writable()
    w['new_key'] = 3
    w['list_of_dicts'].append('another item')
    w['list_of_dicts'][0]['another_key'] = 'another_value'
    w['dict_of_lists']['b'] = []
    w['dict_of_lists']['a'].append('new one')

    # Check that:
    #   a) the original readonly doesn't contain any of these values
    #   b) new ``writable`` objects don't contain any of these values

    for o in [d, d.get_writable()]:
        assert_false('new_key' in o)
        assert_false('another item' in o['list_of_dicts'])
        assert_false('another_key' in o['list_of_dicts'][0])
        assert_false('b' in o['dict_of_lists'])
        assert_false('new_one' in o['dict_of_lists']['a'])
Beispiel #3
0
def test_get_writable_version_of_the_object():
    """
    Test get_writable returns a fully writable version of the mutable.
    """
    d = readonly({
        'list_of_dicts': [
            {
                'key': 'val'
            },
        ],
        'dict_of_lists': {
            "a": ["something", "here"],
        }
    })

    # Check that no exceptions are raised:
    w = d.get_writable()
    w['new_key'] = 3
    w['list_of_dicts'].append('another item')
    w['list_of_dicts'][0]['another_key'] = 'another_value'
    w['dict_of_lists']['b'] = []
    w['dict_of_lists']['a'].append('new one')

    # Check that:
    #   a) the original readonly doesn't contain any of these values
    #   b) new ``writable`` objects don't contain any of these values

    for o in [d, d.get_writable()]:
        assert_false('new_key' in o)
        assert_false('another item' in o['list_of_dicts'])
        assert_false('another_key' in o['list_of_dicts'][0])
        assert_false('b' in o['dict_of_lists'])
        assert_false('new_one' in o['dict_of_lists']['a'])
Beispiel #4
0
def test_blocks_writes_on_mix_of_dicts_and_lists():
    d = readonly({
        'list_of_dicts': [
            {'key': 'val'},
        ],
        'dict_of_lists': {
            "a": ["something", "here"],
        }
    })
    raised = False
    try:
        d['dict_of_lists']['a'].append(3)
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    raised = False
    try:
        d['list_of_dicts'][0].append(3)
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    raised = False
    try:
        d['list_of_dicts'][0]['g'] = 2
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #5
0
def test_defining_inherited_classes_alters_mapping():
    """
    Test that we can safely define inherited classes and they will be used.
    """
    class MySubClass(ReadOnlyList):
        pass

    rl = readonly([[0]])
    assert_true(isinstance(rl, MySubClass))
    assert_true(isinstance(rl[0], MySubClass))

    reset_mapping()
    # Confirm reset has worked
    rl = readonly([[0]])
    assert_false(isinstance(rl, MySubClass))
    assert_false(isinstance(rl[0], MySubClass))
Beispiel #6
0
def test_blocks_writes_on_mix_of_dicts_and_lists():
    d = readonly({
        'list_of_dicts': [
            {
                'key': 'val'
            },
        ],
        'dict_of_lists': {
            "a": ["something", "here"],
        }
    })
    raised = False
    try:
        d['dict_of_lists']['a'].append(3)
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    raised = False
    try:
        d['list_of_dicts'][0].append(3)
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    raised = False
    try:
        d['list_of_dicts'][0]['g'] = 2
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #7
0
def test_blocks_write_to_sub_list():
    d = readonly([[1, 2, 3, 4, 5]])
    raised = False
    try:
        d[0][2] = "hello"
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #8
0
def test_blocks_write_to_dict():
    d = readonly({'a': 2})
    raised = False
    try:
        d['b'] = 3
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #9
0
def test_blocks_write_to_dict():
    d = readonly({'a': 2})
    raised = False
    try:
        d['b'] = 3
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #10
0
def test_blocks_write_to_sub_list():
    d = readonly([[1, 2, 3, 4, 5]])
    raised = False
    try:
        d[0][2] = "hello"
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #11
0
def test_blocks_write_to_sub_dict():
    d = readonly({'a': {'sub_dict': 3}})
    raised = False
    try:
        d['a']['sub_dict_2'] = 3
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #12
0
def test_blocks_write_to_sub_dict():
    d = readonly({'a': {'sub_dict': 3}})
    raised = False
    try:
        d['a']['sub_dict_2'] = 3
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)
Beispiel #13
0
def test_blocks_write_during_iterate_through_list():
    d = readonly([{'s': [1, 2, 3, 4, 5], 'a': 2}])
    for item in d:
        raised = False
        try:
            item['new'] = False
        except ReadOnlyClassException:
            raised = True
        assert_true(raised)
Beispiel #14
0
def test_blocks_write_during_iterate_through_list():
    d = readonly([{'s': [1, 2, 3, 4, 5], 'a': 2}])
    for item in d:
        raised = False
        try:
            item['new'] = False
        except ReadOnlyClassException:
            raised = True
        assert_true(raised)
Beispiel #15
0
def test_writes_during_iterate_through_dict_dont_matter():
    d = readonly([{'s': [1, 2, 3, 4, 5], 'a': [2]}])

    for key, value in d[0].items():
        raised = False
        try:
            value.append(3)
        except ReadOnlyClassException:
            raised = True
        assert_true(raised)
Beispiel #16
0
def test_writes_during_iterate_through_dict_dont_matter():
    d = readonly([{'s': [1, 2, 3, 4, 5], 'a': [2]}])

    for key, value in d[0].items():
        raised = False
        try:
            value.append(3)
        except  ReadOnlyClassException:
            raised = True
        assert_true(raised)
Beispiel #17
0
def test_blocks_write_to_list():
    d = readonly([1, 2, 3, 4, 5])
    raised = False
    try:
        d[2] = "hello"
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    try:
        del d[2]
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    try:
        del d[0:1]
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    try:
        d[0:1] = [1, 2]
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    for method, args in [
                        (d.append, ("hello",)),
                        (d.insert, (3, "hello")),
                        (d.pop, ()),
                        (d.reverse, ()),
                        (d.sort, ()),
                        (d.extend, ([1, 2, 3]))
                        ]:
        raised = False
        try:
            method(*args)
        except ReadOnlyClassException:
            raised = True
        assert_true(raised)
Beispiel #18
0
def test_blocks_write_to_list():
    d = readonly([1, 2, 3, 4, 5])
    raised = False
    try:
        d[2] = "hello"
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    try:
        del d[2]
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    try:
        del d[0:1]
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    try:
        d[0:1] = [1, 2]
    except ReadOnlyClassException:
        raised = True
    assert_true(raised)

    for method, args in [(d.append, ("hello", )), (d.insert, (3, "hello")),
                         (d.pop, ()), (d.reverse, ()), (d.sort, ()),
                         (d.extend, ([1, 2, 3]))]:
        raised = False
        try:
            method(*args)
        except ReadOnlyClassException:
            raised = True
        assert_true(raised)
Beispiel #19
0
 def wrapped(*args, **kwargs):
     response = func(*args, **kwargs)
     return readonly(response)
Beispiel #20
0
def test_can_read_everything():
    d = readonly({'s': [1, 2, 3, 4, 5], 'a': 2})
    assert_equals(2, d['a'])
    assert_equals(3, d['s'][2])
Beispiel #21
0
def test_can_read_everything():
    d = readonly({'s': [1, 2, 3, 4, 5], 'a': 2})
    assert_equals(2, d['a'])
    assert_equals(3, d['s'][2])
 def wrapped(*args, **kwargs):
     response = func(*args, **kwargs)
     return readonly(response)