Source

metadata / test_metadata.py

import pytest

from metadata import Metadata


@pytest.fixture
def metadata():
    ALLOWED_KEYS = {
        'id': int,
        'name': str,
        'lat': float,
        'long': float
    }
    return Metadata(ALLOWED_KEYS)


def test_attr(metadata):
    metadata.name = 'foo'
    assert metadata.name == 'foo'
    metadata.id = 23
    assert metadata.id == 23
    metadata.lat = 36.03000
    assert metadata.lat == 36.03000


def test_key(metadata):
    metadata['name'] = 'foo'
    assert metadata['name'] == 'foo'
    metadata['id'] = 23
    assert metadata['id'] == 23
    metadata['lat'] = 36.03000
    assert metadata['lat'] == 36.03000


def test_mixed(metadata):
    metadata['name'] = 'foo'
    assert metadata.name == 'foo'
    metadata.id = 23
    assert metadata['id'] == 23


def test_attribute_error(metadata):
    with pytest.raises(AttributeError) as excinfo:
        metadata.foo = 23
        assert excinfo.value == 'attribute "foo" is not allowed'


def test_type_error(metadata):
    with pytest.raises(TypeError) as excinfo:
        metadata.name = 23
        assert excinfo.value == 'attribute "name" only allows type "str"'


def test_key_error(metadata):
    with pytest.raises(KeyError):
        metadata.name


def test_reserved_key():
    ALLOWED_KEYS = {
        'id': int,
        'name': str,
        '_allowed_keys': dict
    }
    with pytest.raises(KeyError) as excinfo:
        Metadata(ALLOWED_KEYS)
        assert excinfo.value == '_allowed_keys is a reserved key'