예제 #1
0
def test_destroy_stored_object():
    d = Database()
    o1 = d.create_object()
    o2 = d.create_object()
    p = o1.add_property('prop', d.object_class, o2)

    def inner():
        """Actually perform the testing."""
        with raises(IsValueError) as exc:
            d.destroy_object(o2)
        obj, prop = exc.value.args
        assert obj is o1
        assert prop is p

    inner()
    p.type = list
    p.set([1, 2, o2])
    inner()
    p.type = dict
    p.set(dict(obj=o2))
    inner()
    p.set(dict(objects=[o1, o2]))
    inner()
    p.value = None
    d.destroy_object(o2)
    assert o2.id not in d.objects
예제 #2
0
def test_create_object_with_parents():
    d = Database()
    parent_1 = d.create_object()
    parent_2 = d.create_object()
    child = d.create_object(parent_1, parent_2)
    assert not parent_1.parents
    assert not parent_2.parents
    assert child.parents == [parent_1, parent_2]
예제 #3
0
def test_dump_method():
    d = Database()
    name = 'test'
    code = 'def %s(self):\n    return 1234' % name
    m = Method(d, code)
    actual = d.dump_method(m)
    expected = dict(name=name, code=code)
    assert actual == expected
예제 #4
0
def test_load_property_malformed():
    d = Database()
    o = d.create_object()
    with raises(LoadPropertyError) as err:
        d.load_property(o, d)
    assert err.value.args[0] is o
    assert err.value.args[1] is d
    assert isinstance(err.value.__cause__, TypeError)
예제 #5
0
def test_property_types():
    d = Database()
    assert d.property_types['obj'] is Object
    d = Database(object_class=CustomObject)
    assert d.property_types['obj'] is CustomObject
    o = d.create_object()
    o.this = o
    assert o._properties['this'].type is d.object_class
예제 #6
0
def load_method_malformed():
    d = Database()
    o = d.create_object()
    with raises(LoadMethodError) as err:
        d.load_metod(o, d)
    assert err.value.args[0] is o
    assert err.value.args[1] is d
    assert isinstance(err.value.__cause__, TypeError)
예제 #7
0
def test_location_custom_object():
    db = Database(object_class=CustomObject)
    loc = db.create_object()
    assert isinstance(loc, CustomObject)
    obj = db.create_object()
    assert isinstance(obj, CustomObject)
    obj.location = loc
    assert obj.location is loc
    assert obj._location == loc.id
    assert loc.contents == [obj]
예제 #8
0
def test_find_property():
    db = Database()
    parent = db.create_object()
    name = 'test'
    value = 'Test value'
    description = 'Test property for testing purposes.'
    p = parent.add_property(name, str, value, description=description)
    o = db.create_object(parent)
    assert o.find_property(name) is p
    assert o.find_property('fake') is None
예제 #9
0
def test_dump_objectreference():
    d = Database()
    f1 = d.create_object()
    f2 = d.create_object()
    o = d.create_object()
    p = o.add_property('friends', list, [f1, f2])
    assert p.value == [f1, f2]
    data = d.dump_property(p)
    for entry in data['value']:
        assert isinstance(entry, ObjectReference), 'Invalid entry: %r.' % entry
    assert data['value'] == [ObjectReference(f1.id), ObjectReference(f2.id)]
예제 #10
0
def test_dump_value():
    d = Database()
    o1 = d.create_object()
    o2 = d.create_object()
    data = dict(names=['hello', 'world'], age=25, objects=[o1, o2])
    res = d.dump_value(data)
    assert res == dict(
        names=['hello', 'world'], age=25, objects=[
            ObjectReference(o1.id), ObjectReference(o2.id)
        ]
    )
예제 #11
0
def test_load_value():
    d = Database()
    o1 = d.create_object()
    o2 = d.create_object()
    data = dict(
        names=['hello', 'world'], objects=[
            ObjectReference(o1.id), ObjectReference(o2.id)
        ], age=29
    )
    value = d.load_value(data)
    assert value == dict(
        names=['hello', 'world'], objects=[o1, o2], age=29
    )
예제 #12
0
def test_dump_property():
    d = Database()
    name = 'test'
    desc = 'Test property.'
    type = str
    value = 'Test string.'
    p = Property(name, desc, type, value)
    actual = d.dump_property(p)
    expected = dict(name=name, description=desc, value=value, type='str')
    assert expected == actual
    p.type = Exception
    with raises(RuntimeError):
        d.dump_property(p)
예제 #13
0
def test_load_method():
    d = Database()
    o = d.create_object()
    name = 'test'
    code = 'import re\ndef %s(self, a, b):\n    return (a, b, re)' % name
    m = d.load_method(o, dict(name=name, code=code))
    assert isinstance(m, Method)
    assert o._methods[name] is m
    assert m.database is d
    assert m.name == name
    assert isinstance(m.func, FunctionType)
    assert m.code == code
    assert m.func(o, 1, 2) == (1, 2, re)
예제 #14
0
def test_load_property():
    d = Database()
    o = d.create_object()
    name = 'test'
    desc = 'Test date.'
    value = datetime.utcnow()
    p = d.load_property(
        o, dict(name=name, type='datetime', value=value, description=desc)
    )
    assert p.value == value
    assert p.type is datetime
    assert p.description == desc
    assert o._properties[p.name] is p
예제 #15
0
def test_create_object():
    db = Database()
    o1 = db.create_object()
    assert isinstance(o1, Object)
    assert o1.database is db
    assert o1.id == 0
    assert db.max_id == 1
    assert db.objects == {0: o1}
    o2 = db.create_object()
    assert isinstance(o2, Object)
    assert o2.database is db
    assert o2.id == 1
    assert db.max_id == 2
    assert db.objects == {0: o1, 1: o2}
예제 #16
0
def test_unregister_object():
    d = Database()
    o = d.create_object()
    name = 'test'
    d.register_object(name, o)
    d.unregister_object(name)
    assert not d.registered_objects
    with raises(AttributeError):
        print(d.test)
    with raises(KeyError):
        d.unregister_object(name)
예제 #17
0
def test_destroy_object_with_children():
    d = Database()
    parent = d.create_object()
    d.create_object(parent)
    d.create_object(parent)
    with raises(HasChildrenError) as exc:
        d.destroy_object(parent)
    assert exc.value.args[0]is parent
    for obj in parent.children:
        obj.remove_parent(parent)
    d.destroy_object(parent)
    assert parent.id not in d.objects
예제 #18
0
def test_destroy_object_with_contents():
    d = Database()
    room = d.create_object()
    obj_1 = d.create_object()
    obj_1.location = room
    obj_2 = d.create_object()
    obj_2.location = room
    with raises(HasContentsError) as exc:
        d.destroy_object(room)
    assert exc.value.args[0] is room
    for obj in room.contents:
        obj.location = None
    d.destroy_object(room)
    assert room.id not in d.objects
예제 #19
0
def test_create():
    db = Database()
    assert db.objects == {}
    assert db.max_id == 0
    assert db.method_globals == {'database': db, 'objects': db.objects}
    assert db.methods_dir == 'methods'
    assert db.object_class is Object
    assert db.property_class is Property
    assert db.method_class is Method
예제 #20
0
def test_destroy_object():
    db = Database()
    o = db.create_object()
    db.destroy_object(o)
    assert db.objects == {}
    o1 = db.create_object()
    o2 = db.create_object()
    db.destroy_object(o2)
    assert o1.id == 1
    assert db.objects == {1: o1}
    assert db.max_id == 3
예제 #21
0
def test_destroy_object_registered():
    d = Database()
    first = d.create_object()
    d.register_object('first', first)
    with raises(ObjectRegisteredError):
        d.destroy_object(first)
    assert d.first is first
    assert d.objects == {first.id: first}
예제 #22
0
def test_property_dynamic_add():
    db = Database()
    parent = db.create_object()
    name = 'test'
    value = 'Testing'
    other_value = 'This is a different value.'
    description = 'Test property.'
    parent.add_property(name, str, value, description=description)
    o = db.create_object(parent)
    o.test = other_value
    assert o.test == other_value
    p = o._properties[name]
    assert p.name == name
    assert p.value == other_value
    assert p.description == description
    o.other = value
    p = o._properties['other']
    assert p.name == 'other'
    assert p.value == value
    assert p.description == 'Added by __setattr__.'
    assert p.type is str
예제 #23
0
def test_register_object():
    d = Database()
    o = d.create_object()
    name = 'test'
    d.register_object(name, o)
    assert d.registered_objects == {name: o}
    assert d.test is o
    o = Object(d)
    with raises(RuntimeError):
        d.register_object(name, o)
예제 #24
0
def test_destroy_object_with_parents():
    d = Database()
    g = d.create_object()
    p = d.create_object(g)
    # I know we've done these inheritence tests elsewhere, I just feel more
    # secure knowing they're in two separate places.
    assert g.children == [p]
    assert p.parents == [g]
    c = d.create_object(p)
    assert c.parents == [p]
    assert p.children == [c]
    d.destroy_object(c)
    assert not p.children
    assert g.children == [p]
예제 #25
0
def test_dump():
    d = Database()
    o1 = d.create_object()
    o2 = d.create_object()
    name = 'test'
    d.register_object(name, o1)
    data = d.dump()
    assert data['registered_objects'] == {name: o1.id}
    assert len(data['objects']) == 2
    assert data['objects'][0]['id'] == o1.id
    assert data['objects'][1]['id'] == o2.id
예제 #26
0
def test_clear_func_cache():
    d = Database()
    parent = d.create_object()
    parent.add_method('def test1(self):\n    return 1')
    child = d.create_object()
    child.add_method('def test2(self):\n    return 2')
    child.add_parent(parent)
    parent.test1()
    assert len(parent._method_cache) == 1
    child.test1()
    child.test2()
    assert len(child._method_cache) == 2
    d.clear_method_cache()
    assert not parent._method_cache
    assert not child._method_cache
예제 #27
0
def test_dump_object():
    d = Database()
    parent_1 = d.create_object()
    parent_2 = d.create_object()
    assert d.dump_object(parent_1) == dict(
        location=None, id=parent_1.id, parents=[], properties=[], methods=[]
    )
    o = d.create_object()
    for parent in (parent_1, parent_2):
        o.add_parent(parent)
    actual = d.dump_object(o)
    expected = dict(
        location=None, id=o.id, properties=[], methods=[],
        parents=[parent_1.id, parent_2.id]
    )
    assert actual == expected
    parent_2.location = parent_1
    res = d.dump_object(parent_2)
    assert 'location' not in res['properties']
    assert res['location'] == parent_1.id
예제 #28
0
def test_load_object():
    d = Database()
    id = 18
    p = Property('property', 'Test property.', bool, False)
    m = Method(d, 'import re\ndef method(self, a, b):\n    return (a, b, re)')
    data = dict(
        id=id, methods=[d.dump_method(m)], properties=[d.dump_property(p)],
        parents=[]
    )
    o = d.load_object(data)
    assert d.objects[id] is o
    assert d.max_id == (id + 1)
    assert len(o._methods) == 1
    m.func = None
    for method in (m, o._methods[m.name]):
        method.created.clear()
    o._methods[m.name].func = None  # Otherwise they'll never match.
    assert o._methods[m.name] == m
    assert not o._properties
    assert not o._parents
    assert not o._children
    data = dict(id=1, location=2)
    o = d.load_object(data)
    assert o._location == 2
예제 #29
0
"""Test the events framework."""

from attr import attrs, attrib
from pytest import raises
from carehome import Database, methods
from carehome.exc import NoSuchEventError

db = Database()


@attrs
class Stuff:
    args = attrib()
    kwargs = attrib()


stuff = Stuff(None, None)
methods.stuff = stuff
code = '''def %s(self, *args, **kwargs):
    stuff.args = args
    stuff.kwargs = kwargs
'''


def test_valid_event():
    o = db.create_object()
    o.add_method(
        'def on_event(self, *args, **kwargs):\n    return (self, args, kwargs)'
    )
    self, args, kwargs = o.do_event('on_event', 1, 2, 3, hello='world')
    assert self is o
예제 #30
0
def test_add_method_custom():
    d = Database(method_class=CustomMethod)
    o = d.create_object()
    m = o.add_method('def test(self):\n    pass\n')
    assert isinstance(m, CustomMethod)