def test_database_should_accept_new_redis_connection_settings(self): some_settings = TEST_CONNECTION_SETTINGS.copy() some_settings['db'] = 14 database = model.RedisDatabase(**some_settings) self.assertEqual(database.connection_settings['db'], 14) connection = database.connection database.reset(**TEST_CONNECTION_SETTINGS) self.assertEqual(database.connection_settings['db'], TEST_CONNECTION_SETTINGS['db']) self.assertNotEqual(connection, database.connection)
def test_use_database_shoud_be_threadsafe(self): """ Check if when we use a new database, it's updated for the model on all threads """ db1 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) db2 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) db3 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) class ThreadableModel(model.RedisModel): database = db1 foo = fields.StringField() class Thread(threading.Thread): def __init__(self, test): self.test = test super(Thread, self).__init__() def run(self): # no reason to fail, but still test it self.test.assertEqual(ThreadableModel.database, db1) # wait a little to let the main thread set database to db2 time.sleep(0.2) self.test.assertEqual(ThreadableModel.database, db2) # will be tested in main thread ThreadableModel.use_database(db3) thread = Thread(self) thread.start() # will be tested in child thread ThreadableModel.use_database(db2) # wait a little to let the child thread set database to db3 time.sleep(0.4) self.assertEqual(ThreadableModel.database, db3)
def test_scan_keys(self): db = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) keys = {'foo', 'foo.bar', 'foo.bar.baz'} for key in keys: db.connection.set(key, 0) generator = db.scan_keys('fo*') self.assertIn(next(generator), keys) self.assertIn(next(generator), keys) self.assertIn(next(generator), keys) with self.assertRaises(StopIteration): next(generator) self.assertSetEqual(set(db.scan_keys('fo*')), keys) generator = db.scan_keys('fo*', count=1) self.assertIn(next(generator), keys) self.assertIn(next(generator), keys) self.assertIn(next(generator), keys) with self.assertRaises(StopIteration): next(generator) self.assertSetEqual(set(db.scan_keys('fo*', count=1)), keys)
def test_database_could_transfer_its_models_and_relations_to_another(self): """ Move of models is tested in tests.models.DatabaseTest.test_database_could_transfer_its_models_to_another The move of relations is tested here. """ db1 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) db2 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) db3 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) class M(RelatedModel): namespace = 'transfert-db-relations' abstract = True foo = fields.StringField() class A(M): database = db1 b = FKStringField('B', related_name='a_set') class B(M): database = db1 a = FKStringField(A, related_name='b_set') class C(M): database = db2 b = FKStringField( B, related_name='c_set') # link to a model on another database ! # getting list of linked C objects from a B object will fail because # both models are not on the same database, so B is not aware of a link # to him made on C. In fact C has created a relation on a B field on its # database, but which is not defined b = B(foo='bar') with self.assertRaises(AttributeError): b.c_set() # the link A <-> B should work self.assertListEqual(list(b.a_set()), []) # move B to db2 to allow relation to work B.use_database(db2) b = B(foo='bar') self.assertListEqual(list(b.c_set()), []) # now the link A <-> B should be broken with self.assertRaises(AttributeError): b.a_set() # move all to db3 A.use_database(db3) B.use_database(db3) C.use_database(db3) # create and link objects a = A(foo='bar') b = B(foo='bar') c = C(foo='bar') a.b.set(b) b.a.set(a) c.b.set(b) # all relation should work self.assertListEqual(list(a.b_set()), [b._pk]) self.assertListEqual(list(b.a_set()), [a._pk]) self.assertListEqual(list(b.c_set()), [c._pk])
def test_database_could_transfer_its_models_to_another(self): db1 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) db2 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) db3 = model.RedisDatabase(**TEST_CONNECTION_SETTINGS) class M(model.RedisModel): namespace = 'transfert-db-models' abstract = True class A(M): abstract = True class B(M): abstract = True class BA(B): database = db1 class BAA(BA): pass class BAB(BA): pass class BB(B): database = db2 class BBA(BB): pass class BBB(BB): abstract = True class BBBA(BBB): pass def assertModelsInDatabase(database, *models): """ Test that the database contains all non-abstract models in the given list and that each model has the correct database. """ self.assertEqual( database._models, dict((m._name, m) for m in models if not m.abstract)) for m in models: self.assertEqual(m.database, database) def assertNoDatabase(*models): for m in models: self.assertFalse(hasattr(m, 'database')) # starting point assertNoDatabase(M, A, B) assertModelsInDatabase(db1, BA, BAA, BAB) assertModelsInDatabase(db2, BB, BBA, BBB, BBBA) assertModelsInDatabase(db3) # move B to db1 B.use_database(db1) assertNoDatabase(M, A) # B moved, alone, from here.... assertModelsInDatabase(db1, B, BA, BAA, BAB) # ...to here assertModelsInDatabase(db2, BB, BBA, BBB, BBBA) assertModelsInDatabase(db3) # move some models to db3 B.use_database(db3) assertNoDatabase(M, A) assertModelsInDatabase(db1) # B and submodels... assertModelsInDatabase(db3, B, BA, BAA, BAB) # ...moved to db3 assertModelsInDatabase(db2, BB, BBA, BBB, BBBA) # models in db2 are still here # move back some to db1 BA.use_database(db1) assertNoDatabase(M, A) assertModelsInDatabase(db1, BA, BAA, BAB) # BA and submodels are here now... assertModelsInDatabase(db3, B) # ...not here anymore assertModelsInDatabase(db2, BB, BBA, BBB, BBBA) # move some from db2 to db3 BBB.use_database(db3) assertNoDatabase(M, A) assertModelsInDatabase(db1, BA, BAA, BAB) assertModelsInDatabase(db2, BB, BBA) # BBB and submodel have moved... assertModelsInDatabase(db3, B, BBB, BBBA) # ...here # move B alone in db2 (no direct submodel in db3) B.use_database(db2) assertNoDatabase(M, A) assertModelsInDatabase(db1, BA, BAA, BAB) assertModelsInDatabase(db2, B, BB, BBA) # B is here now... assertModelsInDatabase(db3, BBB, BBBA) # move all from db3, to have a full chain BBB.use_database(db2) assertNoDatabase(M, A) assertModelsInDatabase(db1, BA, BAA, BAB) assertModelsInDatabase(db2, B, BB, BBA, BBB, BBBA) # all from db3 is now here assertModelsInDatabase(db3) # and now move the B+BB chain in db3 B.use_database(db3) assertNoDatabase(M, A) assertModelsInDatabase(db1, BA, BAA, BAB) assertModelsInDatabase(db2) # nothing here anymore assertModelsInDatabase(db3, B, BB, BBA, BBB, BBBA) # move M, abstract without DB should move it's subclass A without DB M.use_database(db1) assertModelsInDatabase(db1, M, A, BA, BAA, BAB) # hello M & A assertModelsInDatabase(db2) assertModelsInDatabase(db3, B, BB, BBA, BBB, BBBA)
from limpyd import model import time from . import encoding from . import pydub_helpers from urllib.parse import urlparse import os url = os.environ.get('REDIS_URL') parsed = urlparse(url) database = model.RedisDatabase(host=parsed.hostname, username=parsed.username, password=parsed.password, port=parsed.port, decode_responses=False) class Room(model.RedisModel): database = database title = model.StringField() expiration = model.StringField() program = model.HashField() bulletin = model.StringField() index = model.StringField(default=-1) singing = model.StringField(default=0) users = model.SetField() def get_users(self):