Passed
Push — master ( 96da92...a1b572 )
by Konstantinos
37s queued 14s
created

test_utils.test_registry   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 10

6 Functions

Rating   Name   Duplication   Size   Complexity  
A registry_infra() 0 5 1
A test_registry_remove_method() 0 9 2
A test_add_item_with_existing_key() 0 9 2
A test_registry_get_method() 0 10 2
A test_registry_pop_method() 0 10 2
A test_sanity_check() 0 5 1
1
import pytest
2
3
4
@pytest.fixture
5
def registry_infra():
6
    from so_magic.utils import ObjectRegistry, ObjectRegistryError
7
    return type('DummyClass', (object,), {'object': ObjectRegistry({'key1': 1}), 'error': ObjectRegistryError,
8
                                          'existing_key': 'key1', 'non_existing_key': 'key2'})
9
10
11
def test_sanity_check(registry_infra):
12
    runtime_repr = repr(registry_infra.object)
13
    assert runtime_repr == repr(registry_infra.object.objects)
14
    assert runtime_repr == '{' + ', '.join(f"'{k}': {v}" for k, v in registry_infra.object.objects.items()) + '}'
15
    assert list(iter(registry_infra.object)) == list(iter(registry_infra.object.objects.items()))
16
17
18
def test_registry_remove_method(registry_infra):
19
    assert registry_infra.existing_key in registry_infra.object
20
21
    registry_infra.object.remove(registry_infra.existing_key)
22
    assert registry_infra.object.objects == {}
23
24
    with pytest.raises(registry_infra.error,
25
                       match=f'Requested to remove item with key {registry_infra.existing_key}, which does not exist.'):
26
        registry_infra.object.remove(registry_infra.existing_key)
27
28
29
def test_registry_pop_method(registry_infra):
30
    assert registry_infra.existing_key in registry_infra.object
31
32
    value = registry_infra.object.pop(registry_infra.existing_key)
33
    assert value == 1
34
    assert registry_infra.object.objects == {}
35
36
    with pytest.raises(registry_infra.error,
37
                       match=f'Requested to pop item with key {registry_infra.existing_key}, which does not exist.'):
38
        registry_infra.object.pop(registry_infra.existing_key)
39
40
41
def test_registry_get_method(registry_infra):
42
    assert registry_infra.existing_key in registry_infra.object
43
44
    value = registry_infra.object.get(registry_infra.existing_key)
45
    assert value == 1
46
    assert registry_infra.object.objects == {registry_infra.existing_key: 1}
47
48
    with pytest.raises(registry_infra.error, match=f'Requested to get item with key {registry_infra.non_existing_key}, '
49
                                                   f'which does not exist.'):
50
        _ = registry_infra.object.get(registry_infra.non_existing_key)
51
52
53
@pytest.mark.parametrize('item_value', [9])
54
def test_add_item_with_existing_key(item_value, registry_infra):
55
    assert registry_infra.existing_key in registry_infra.object
56
57
    with pytest.raises(registry_infra.error, match=f"Requested to insert value '{item_value}' in already existing key "
58
                                                   f"'{registry_infra.existing_key}'. All keys are "
59
                                                   rf"\[{', '.join(_ for _ in registry_infra.object.objects)}\]"
60
                       ):
61
        registry_infra.object.add(registry_infra.existing_key, item_value)
62