Ejemplo n.º 1
0
def test_objectify_dir_works():
    """Test objectify dir works."""
    fixture = {'image': True}
    obj1 = data.Objectify(fixture)
    assert dir(obj1) == sorted(['_dct', '_sentinel', 'image'])
    obj2 = data.Objectify(['anything'])
    assert dir(obj2) == sorted(['_dct', '_sentinel', '_0'])
Ejemplo n.º 2
0
def test_objectify_get_empty_returns_raw():
    """Test objectify get empty returns raw."""
    fixture = {'image': True}
    # Empty get should return data structure by default, but allow
    # objectify parameter to be passed as True
    assert data.Objectify(fixture)._get() == fixture
    assert data.Objectify(fixture)._get(objectify=False) == fixture
    assert data.Objectify(fixture)._get(objectify=True)._dct == fixture
Ejemplo n.º 3
0
def test_objectify_traversal():
    """Test objectify traversal."""
    fixture = {
        'assignment': {
            'order': {
                'requirements': {
                    'height': 480,
                    'exposure': '0.2s'
                }
            },
            'requirements': {
                'width': 640
            }
        },
        'requirements': {
            'duration': '10s',
            'exposure': '1s'
        }
    }
    obj = data.Objectify(fixture)
    t = obj._get_traverser([
        'requirements', 'assignment.requirements',
        'assignment.order.requirements'
    ])
    assert t.width == 640
    assert t.height == 480
    assert t.duration == '10s'
    assert t.exposure == '1s'
Ejemplo n.º 4
0
def test_objectify_iteration_works():
    """Test objectify iteration works."""
    for fixture in ({'image0': 0, 'image1': 1, 'image2': 2}, [0, 1, 2]):
        contents = {0, 1, 2}
        for content in data.Objectify(fixture):
            contents.remove(content)
        assert not contents
Ejemplo n.º 5
0
def test_item_retrieval_works_with_default_value():
    """Test item retrieval works with default value."""
    obj = data.Objectify({'a': {'b': 42}})
    with pytest.raises(KeyError):
        assert obj['a.non_existent']
    obj._sentinel = 'fourty two'
    assert obj['a.non_existent'] == 'fourty two'
Ejemplo n.º 6
0
def test_objectify_works_chainned_dict():
    """Test objectify works chainned dict."""
    fixture = {'image': {'type': 'pictorial', 'dimensions': [640, 480]}}
    obj = data.Objectify(fixture)
    assert isinstance(obj.image, data.Objectify)
    assert isinstance(obj._dct, dict)
    assert obj.image._dct == {'type': 'pictorial', 'dimensions': [640, 480]}
    assert obj.image.type == 'pictorial'
    assert obj.image.dimensions._0 == 640
Ejemplo n.º 7
0
def test_objectify_get_works_with_default():
    """Test objectify get works with default."""
    fixture = {'image': {'type': 'pictorial', 'dimensions': [640, 480]}}
    obj = data.Objectify(fixture)
    # Deep path failure
    assert obj._get('image.dimensions._3', None) is None
    # Early path failure
    assert obj._get('image.other_dimensions._0', None) is None
    # raises even if obj._sentinel is set:
    with pytest.raises(AttributeError):
        obj._sentinel = None
        obj._get('image.dimensions._3')
    obj._sentinel = data.objectify_sentinel
    # Retrieves data structure
    assert obj._get('image.dimensions', objectify=False) == [640, 480]
Ejemplo n.º 8
0
def test_objectify_works_single_dict():
    """Test objectify works single dict."""
    fixture = {'image': True}
    assert data.Objectify(fixture).image == fixture['image']
Ejemplo n.º 9
0
def test_objectify_content_equals_equivalent_mapping():
    """Test objectify content equals equivalent mapping."""
    fixture = {'a': {'b': 42}}
    obj = data.Objectify(fixture)
    assert obj == deepcopy(fixture)
Ejemplo n.º 10
0
def test_objectify_content_equals_other_objectify():
    """Test objectify content equals other objectify."""
    fixture = {'a': {'b': 42}}
    obj1 = data.Objectify(fixture)
    obj2 = data.Objectify(deepcopy(fixture))
    assert obj1 == obj2
Ejemplo n.º 11
0
def test_objectify__items():
    """Test objectify  items."""
    obj = data.Objectify({'a': {'b': 42}})
    key, value = next(iter(obj._items()))
    assert key == 'a'
    assert value._dct == {'b': 42}
Ejemplo n.º 12
0
def test_objectify__values():
    """Test objectify  values."""
    obj = data.Objectify({'a': {'b': 42}})
    assert next(iter(obj._values()))._dct == {'b': 42}
Ejemplo n.º 13
0
def test_objectify__keys():
    """Test objectify  keys."""
    obj = data.Objectify({'a': {'b': 42}})
    assert list(obj._keys()) == ['a']
Ejemplo n.º 14
0
def test_objectify_get_works_deep():
    """Test objectify get works deep."""
    fixture = {'image': {'type': 'pictorial', 'dimensions': [640, 480]}}
    obj = data.Objectify(fixture)
    assert obj._get('image.dimensions._0') == 640
Ejemplo n.º 15
0
def test_objectify_containement_test_works_deep_index():
    """Test objectify containement test works deep index."""
    obj = data.Objectify({'a': {'b': 42}})
    assert 'a.b' in obj
    assert 'a.c' not in obj
Ejemplo n.º 16
0
def test_objectify_customize_missing_attribute_deep():
    """Test objectify customize missing attribute deep."""
    fixture = {'image': {'type': 'pictorial', 'dimensions': [640, 480]}}
    obj = data.Objectify(fixture)
    obj._sentinel = None
    assert obj.image.dimensions.third is None
Ejemplo n.º 17
0
def test_objectify_dont_double_wrap():
    """Test objectify dont double wrap."""
    fixture = {'image': True}
    obj1 = data.Objectify(fixture)
    assert data.Objectify(obj1)._dct == fixture
Ejemplo n.º 18
0
def test_objectify_deep_path_index_get():
    """Test objectify deep path index get."""
    obj = data.Objectify({'a': {'b': 42}})
    assert obj['a.b'] == 42
Ejemplo n.º 19
0
def test_objectify_bool_works():
    """Test objectify bool works."""
    fixture = {'image': True}
    assert data.Objectify(fixture)
    assert not data.Objectify({})
Ejemplo n.º 20
0
def test_objectify_get_raises_attribute_error():
    """Test objectify get raises attribute error."""
    fixture = {'image': {'type': 'pictorial', 'dimensions': [640, 480]}}
    obj = data.Objectify(fixture)
    with pytest.raises(AttributeError):
        obj._get('image.dimensions._3')
Ejemplo n.º 21
0
def test_objectify_works_single_list():
    """Test objectify works single list."""
    fixture = ['image']
    assert data.Objectify(fixture)._0 == 'image'
    assert data.Objectify(fixture)._dct == fixture
Ejemplo n.º 22
0
def test_objectify_iterate_over_values_by_default():
    """Test objectify iterate over values by default."""
    obj = data.Objectify({'a': {'b': 42}})
    iterated = list(obj)
    assert len(iterated) == 1
    assert iterated[0]._dct == {'b': 42}
Ejemplo n.º 23
0
def test_objectify_containement_test_works_shallow_index():
    """Test objectify containement test works shallow index."""
    obj = data.Objectify({'a': {'b': 42}})
    assert 'a' in obj
    assert 'b' not in obj
Ejemplo n.º 24
0
def test_objectify_get_works():
    """Test objectify get works."""
    fixture = {'image': True}
    assert data.Objectify(fixture)._get('image')
Ejemplo n.º 25
0
def test_objectify_raise_attribute_error():
    """Test objectify raise attribute error."""
    fixture = {'image': True}
    obj = data.Objectify(fixture)
    with pytest.raises(AttributeError):
        obj.video
Ejemplo n.º 26
0
def test_objectify_customize_missing_attribute_shallow():
    """Test objectify customize missing attribute shallow."""
    fixture = {'image': True}
    obj = data.Objectify(fixture)
    obj._sentinel = None
    assert obj.video is None