Пример #1
0
def test_file_cleanup():
    clean()
    smartobject.define_storage(smartobject.JSONStorage())
    smartobject.define_storage(smartobject.DummyStorage(), 'db1')
    factory = smartobject.SmartObjectFactory(Employee)
    names = ['Mike', 'Betty', 'Kate', 'John', 'Boris', 'Ivan']
    for n in names:
        factory.create(opts={'name': n})
    factory.save()
    for n in names:
        p = Path(f'test_data/coders___{n.lower()}.json')
        assert p.exists() and p.is_file()
    factory.clear()
    factory.load_all()
    factory.get('coders/mike')
    factory.get('coders/betty')
    factory.remove('coders/mike')
    factory.remove('coders/betty')
    with pytest.raises(KeyError):
        factory.get('coders/mike')
        factory.get('coders/betty')
    factory.cleanup_storage()
    factory.clear()
    factory.load_all()
    with pytest.raises(KeyError):
        factory.get('coders/mike')
        factory.get('coders/betty')
    factory.clear()
Пример #2
0
def test_factory():
    smartobject.define_storage(smartobject.JSONStorage())
    smartobject.define_storage(smartobject.DummyStorage(), 'db1')
    factory = smartobject.SmartObjectFactory(Employee)
    employee = factory.create(opts={'name': 'John Doe'})
    pk = employee.id
    factory.set_prop(pk, {'personal_code': '0xFF'})
    factory.save()
    factory.save(pk, force=True)
    factory.serialize(pk)
    s = employee.serialize('salary')
    assert len(s) == 2
    with pytest.raises(RuntimeError):
        employee = factory.create({'name': 'John Doe'})
    employee = factory.create({'name': 'John Doe'}, override=True, load=False)
    assert employee.personal_code is None
    factory.load(pk)
    assert employee.personal_code == 255
    factory.delete(pk)
    employee = factory.create({'name': 'John Doe'}, override=True, load=False)
    employee.delete()
    with pytest.raises(KeyError):
        factory.get(employee.id)
    employee = Employee(name='Jane Doe')
    factory.append(employee)
Пример #3
0
def test_storage(tp):
    employee = Employee('John Doe')
    employee.set_prop('salary', 1500)
    storage = getattr(smartobject, tp)()
    storage.allow_empty = False
    smartobject.define_storage(storage)
    smartobject.define_storage(smartobject.DummyStorage(), 'db1')
    employee.save()
    employee2 = Employee('John Doe')
    employee2.load()
    assert employee2.salary == 1500 * 100
    employee2.delete()
    employee3 = Employee('John Doe')
    with pytest.raises(FileNotFoundError):
        employee3.load()
    storage.allow_empty = True
Пример #4
0
def test_load_from_dir():
    clean()
    smartobject.define_storage(smartobject.JSONStorage())
    smartobject.define_storage(smartobject.DummyStorage(), 'db1')
    factory = smartobject.SmartObjectFactory(Employee)
    factory.create(opts={'name': 'John Doe'})
    factory.create(opts={'name': 'Jane Doe'})
    e = factory.create(opts={'name': 'Jack Daniels'})
    e.set_prop('salary', 150)
    key = e.id
    factory.save()
    factory.clear()
    with pytest.raises(KeyError):
        factory.get(key)
    factory.load_all()
    assert len(factory.get()) == 3
    assert factory.get(key).salary == 150 * 100
Пример #5
0
def test_postpone_delete():
    storage = smartobject.JSONStorage()
    storage.instant_delete = False
    storage.allow_empty = False
    smartobject.define_storage(storage)
    smartobject.define_storage(smartobject.DummyStorage(), 'db1')
    employee = Employee('John Doe')
    employee.save()
    employee.delete()
    employee2 = Employee('John Doe')
    employee2.load()
    employee2.delete()
    smartobject.purge()
    employee3 = Employee('John Doe')
    with pytest.raises(FileNotFoundError):
        employee3.load()
    storage.allow_empty = True