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)
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()
def test_t2_save_to_file(): clean() smartobject.define_storage(smartobject.JSONStorage()) factory = smartobject.SmartObjectFactory(T2, autosave=True) i = factory.create().id assert i is not None factory.clear() factory.load_all() factory.get(i)
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
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
def test_factory_lru(): clean() smartobject.define_storage(smartobject.JSONStorage()) factory = smartobject.SmartObjectFactory(T2, autoload=True, autosave=True, maxsize=2) o1 = factory.create() o2 = factory.create() o3 = factory.create() assert len(factory._objects) == 2 factory.get(o3.id) assert len(factory._objects) == 2 factory.get(o2.id) assert len(factory._objects) == 2 factory.get(o1.id) assert len(factory._objects) == 2 with pytest.raises(RuntimeError): factory.cleanup_storage()
def test_factory_indexes(): clean() smartobject.define_storage(smartobject.JSONStorage()) factory = smartobject.SmartObjectFactory(T2, autosave=True) factory.add_index('login') o1 = factory.create() o2 = factory.create() o3 = factory.create() o1.set_prop('login', 'test') o2.set_prop('login', 'test2') o3.set_prop('login', 'test') factory.reindex(o1.id) factory.reindex(o2.id) factory.reindex(o3.id) factory.reindex(o3.id) assert len(factory.get('test', prop='login')) == 2 factory.remove(o1) assert len(factory.get('test', prop='login')) == 1 factory.remove(o3) assert len(factory.get('test', prop='login')) == 0
def test_db_storage(): smartobject.define_storage(smartobject.JSONStorage()) clean() db = sa.create_engine('sqlite:///test_data/test.db') def get_connection(): return db.connect() get_connection().execute(""" create table pr ( id varchar(30) not null, projects_created int not null, heartbeat int, primary key(id) ) """) storage = smartobject.SQLAStorage(get_connection, 'pr', 'id') storage.allow_empty = False smartobject.define_storage(storage, 'db1') employee = Employee('John Doe', etest='db1') with pytest.raises(LookupError): employee.load() storage.allow_empty = True employee.load() employee.save(force=True) employee.heartbeat = 150 assert employee.heartbeat == 150 employee.set_prop('projects_created', 10, save=True) employee2 = Employee('John Doe', etest='db1') employee2.load() assert employee2.projects_created == 10 employee.set_prop('projects_created', 15) employee.save() employee2.load() assert employee2.heartbeat == 150 assert employee2.projects_created == 15 employee2.delete() storage.purge()
def test_factory_autoload(): clean() storage = smartobject.JSONStorage() storage.allow_empty = False smartobject.define_storage(storage) factory = smartobject.SmartObjectFactory(T2, autoload=True, autosave=True) o1 = factory.create() o2 = factory.create() o3 = factory.create() o1.set_prop('login', 'test1') o2.set_prop('login', 'test2') o3.set_prop('login', 'test3') factory.save() pk1 = o1.id pk2 = o2.id pk3 = o3.id factory.clear() import os os.unlink(f'test_data/{pk3}.json') assert factory.get(pk1).serialize()['login'] == 'test1' assert factory.get(pk2).serialize()['login'] == 'test2' with pytest.raises(FileNotFoundError): assert factory.get(pk3)
import sys sys.path.insert(0, '..') import smartobject class Person(smartobject.SmartObject): def __init__(self, name): self.name = name self.load_property_map('person.yml') self.apply_property_map() smartobject.config.storage_dir = 'data' smartobject.define_storage(smartobject.JSONStorage()) smartobject.define_storage(smartobject.RedisStorage(), 'r1') people = smartobject.SmartObjectFactory(Person) # create objects with factory people.create(opts={'name': 'John'}) people.create(opts={'name': 'Jane'}) # create object manually jack = Person('Jack') # you can set a single prop people.set_prop('John', 'sex', 'male') people.set_prop('Jane', 'sex', 'female') # or multiple props with dict