def test_OverlappingReferences(): class RefClass(BaseTable): pass Drop(RefClass, confirm=True) obj = New(RefClass, name='Namie McNamerson') ref1 = FindOne(RefClass) ref1['name'] = 'ref1' ref2 = FindOne(RefClass) ref2['name'] = 'ref2' ref3 = FindOne(RefClass) ref3['name'] = 'ref3' ref4 = list(FindAll(RefClass))[0] ref4['name'] = 'ref4' ref5 = list(FindAll(RefClass))[0] ref5['name'] = 'ref5' ref6 = list(FindAll(RefClass))[0] ref6['name'] = 'ref6' print('ref1=', ref1) print('ref2=', ref2) print('ref3=', ref3) print('ref4=', ref4) print('ref5=', ref5) print('ref6=', ref6) ref1['name'] = 'ref1a' ref2['name'] = 'ref2a' ref3['name'] = 'ref3a' ref4['name'] = 'ref4a' ref5['name'] = 'ref5a' ref6['name'] = 'ref6a' ref1['name'] = 'ref1b' ref3['name'] = 'ref3b' ref4['name'] = 'ref4b' ref6['name'] = 'ref6b' ref5['name'] = 'ref5b' ref2['name'] = 'ref2b' finalRef = FindOne(RefClass) assert finalRef['name'] == 'ref2b' # keep references ref1['name'] = 'ref1c' ref2['name'] = 'ref2c' ref3['name'] = 'ref3c' ref4['name'] = 'ref4c' ref5['name'] = 'ref5c' ref6['name'] = 'ref6c'
def test_DoDelete(): class SimpleTestClass(BaseTable): pass print('Drop(SimpleTestClass, confirm=True)') Drop(SimpleTestClass, confirm=True) print('for i in range(10)') print(' New(SimpleTestClass, timeString=time.asctime(), count=i)') LENGTH = 10 for i in range(LENGTH): New(SimpleTestClass, timeString=time.asctime(), count=i) print('FindAll(SimpleTestClass)=') allObjs = list(FindAll(SimpleTestClass)) print('allObjs=', allObjs) assert len(allObjs) == LENGTH print('FindOne(SimpleTestClass, count=5)=') foundCount5 = list(FindAll(SimpleTestClass, count=5)) print('foundCount5=', foundCount5) assert len(foundCount5) == 1 print('for obj in FindAll(SimpleTestClass)') print(' obj["count"] += 10)') for obj in FindAll(SimpleTestClass): obj['count'] += 10 print('FindAll(SimpleTestClass)=') for obj in list(FindAll(SimpleTestClass)): print('FindAll(obj=', obj) print('# Delete every-other') print('for i in range(0, 10, 2') print(' Delete(FindOne(SimpleTestClass, count=i+10') for i in range(0, 10, 2): obj = FindOne(SimpleTestClass, count=i + 10) if obj: print('Delete(obj=', obj) Delete(obj) print('FindAll(SimpleTestClass)=') allSimpleTestClass = list(FindAll(SimpleTestClass)) print('len(allSimpleTestClass)=', len(allSimpleTestClass)) for obj in allSimpleTestClass: print('allSimpleTestClass obj=', obj) assert len(allSimpleTestClass) == (LENGTH / 2)
def test_Threading(): from threading import Thread class Shape(BaseTable): pass Drop(Shape, confirm=True) def Function(name): print(f'Function({name})') New(Shape, name=name) LENGTH = 3 for i in range(LENGTH): Thread(target=Function, args=(f'name{i}',)).start() count = 0 while count < 10: print('while count < 10; count=', count) # give the threads time to finish, but if they take too long, assume it falied allShapes = list(FindAll(Shape)) print('allShapes=', allShapes) if len(allShapes) < LENGTH: count += 1 time.sleep(1) else: break assert len(allShapes) == LENGTH
def test_MultipleSimultaneousInstances(): class MSIUserClass(BaseTable): pass Drop(MSIUserClass, confirm=True) New(MSIUserClass, name='username1', age='33') foundA = FindOne(MSIUserClass, name='username1') foundB = FindOne(MSIUserClass, name='username1') print('userA=', foundA) foundA['age'] = '99' print('userB=', foundB) foundB['age'] = '00' # the last statement should be truth print('userA=', foundA) print('userB=', foundB) for user in FindAll(MSIUserClass): print('user='******'age'] == '00' print('userA=', foundA) print('userB=', foundB)
def test_DeleteWhileInUse(): class Channel(BaseTable): pass Drop(Channel, confirm=True) channels = [New(Channel) for _ in range(3)] # for ch in channels: # if ch['id'] == 2: # Delete(ch) # pass # foundChannel = FindOne(Channel, id=2) Delete(foundChannel) foundAllChannels = list(FindAll(Channel)) print('foundAllChannels=', foundAllChannels) for ch in foundAllChannels: if ch['id'] == 2: raise Exception('Channel 2 should have been deleted') foundChannelAgain = FindOne(Channel, id=2) assert foundChannelAgain is None print('channels=', channels)
def test_Simple(): print('test_Simple(') class Simple(BaseTable): pass Drop(Simple, confirm=True) s = New(Simple, i=3888) print('s=', s) s['i'] = 99999 print('s=', s) print('s reference gone') obj = FindOne(Simple) assert obj is not None assert obj['i'] == 99999 foundObjs = list(FindAll(Simple)) assert len(foundObjs) == 1 for obj in foundObjs: print('obj=', obj) assert obj['i'] == 99999 print('test_Simple() complete')
def test_FindOne_None(): class BlerpClass(BaseTable): pass Drop(BlerpClass, confirm=True) res = FindOne(BlerpClass) assert res is None res = list(FindAll(BlerpClass)) assert len(res) is 0
def test_MultipleInstances(): class User(BaseTable): pass Drop(User, confirm=True) New(User, name='username1', age='33') userA = FindOne(User, name='username1') print('userA=', userA) userA['age'] = '99' userB = FindOne(User, name='username1') print('userB=', userB) userB['age'] = '00' for user in FindAll(User): print('user='******'age'] == '00'
def test_SimpleChild(): class Person(BaseTable): # Each subclass of BaseTable produces another table in the db pass class Animal(BaseTable): pass # For testing, delete all tables first # Comment these out to make data persistant Drop(Person, confirm=True) Drop(Animal, confirm=True) # Create tables with random data for i in range(10): # Instantiating a new Person newObj adds a new row in the db newPerson = New( Person, name='Name{}'.format(i), age=30 + i, ) print('newPerson=', newPerson) newAnimal = New( Animal, kind=random.choice(['Cat', 'Dog']), name='Fluffy{}'.format(i), age=i, ) print('newAnimal=', newAnimal) # FindAll() returns all items from the database that match # you can also use keywords like '_limit', '_reverse', '_orderBy' assert len(list(FindAll(Animal, age=5))) == 1 # FindOne() returns an newObj found in the database person5 = FindOne(Person, name='Name5') print('Age of Person5=', person5['age']) assert person5['age'] == 35