def test_get_connection(self): with patch.object(rethink, 'connect', return_value=None) as \ connect_method: get_connection() connect_method.assert_called_once() connect_method.assert_called_with(host='localhost', port=28015, auth_key='', db=DEFAULT_DB_NAME)
def test_get_connection(self): with patch.object(rethink, 'connect', return_value=None) as \ connect_method: get_connection() connect_method.assert_called_once_with(host='localhost', port=28015, auth_key='', db=DEFAULT_DB_NAME)
def delete_specific(table_name, filters=None): """ Deletes all records in a table matching the filter """ if not filters: filters = {} with get_connection() as conn: return rethink.table(table_name).filter(filters).delete().run(conn)
def drop_database(): """ Deletes the RethinkDB database """ try: with get_connection() as conn: rethink.db_drop(DEFAULT_DB_NAME).run(conn) except (RqlRuntimeError, RqlDriverError) as err: raise err
def drop_database(): """ Deletes the RethinkDB database """ try: with get_connection() as conn: rethink.db_drop(DEFAULT_DB_NAME).run(conn) except (RqlRuntimeError, RqlDriverError) as err: print(err.message)
def list_all(table_name, order_by='epoch', filters=None): """ Gets a full list of records - no pagination. """ if not filters: filters = {} with get_connection() as conn: return list(rethink.table(table_name) .order_by(order_by).filter(filters).run(conn))
def query(table_name, order_by='epoch', filters=None): """ Query for record(s) """ if not filters: filters = {} with get_connection() as conn: return rethink.table(table_name)\ .order_by(order_by).filter(filters).run(conn)
def list_all(table_name, order_by='epoch', filters=None): """ Gets a full list of records - no pagination. """ if not filters: filters = {} with get_connection() as conn: return list( rethink.table(table_name).order_by(order_by).filter( filters).run(conn))
def list(table_name, start, limit, order_by='epoch', filters=None): """ Gets a list of records, meant for pagination. """ if not filters: filters = {} with get_connection() as conn: return rethink.table(table_name)\ .filter(filters).order_by(order_by) \ .slice(start, limit).run(conn)
def insert(table_name, **kwargs): """ Inserts a new record. id and epoch are common to all records. """ if 'id' not in kwargs: kwargs['id'] = str(uuid.uuid4()) kwargs['epoch'] = time() with get_connection() as conn: rethink.table(table_name).insert(kwargs).run(conn) return kwargs
def update(table_name, record_id=None, filters=None, updates=None): """ Perform an update on one more fields """ if not filters: filters = {} if not updates: updates = {} with get_connection() as conn: if record_id: return rethink.table(table_name).get(record_id)\ .update(updates).run(conn) else: return rethink.table(table_name).filter(filters)\ .update(updates).run(conn)
def create_database(): """ Creates a new RethinkDB database with tables if it doesn't already exist """ try: with get_connection() as conn: db_list = rethink.db_list().run(conn) if DEFAULT_DB_NAME not in db_list: # Default db doesn't exist so add it rethink.db_create(DEFAULT_DB_NAME).run(conn) table_list = rethink.db(DEFAULT_DB_NAME).table_list().run(conn) for table_name in DEFAULT_TABLE_NAMES: if table_name not in table_list: # Add the missing table(s) rethink.db(DEFAULT_DB_NAME).table_create(table_name)\ .run(conn) except (RqlRuntimeError, RqlDriverError) as err: raise err
def create_database(): """ Creates a new RethinkDB database with tables if it doesn't already exist """ try: with get_connection() as conn: db_list = rethink.db_list().run(conn) if DEFAULT_DB_NAME not in db_list: # Default db doesn't exist so add it rethink.db_create(DEFAULT_DB_NAME).run(conn) table_list = rethink.db(DEFAULT_DB_NAME).table_list().run(conn) for table_name in DEFAULT_TABLE_NAMES: if table_name not in table_list: # Add the missing table(s) rethink.db(DEFAULT_DB_NAME).table_create(table_name)\ .run(conn) except (RqlRuntimeError, RqlDriverError) as err: print(err.message)
def get(table_name, record_id): """ Get a single record based on id. """ with get_connection() as conn: return rethink.table(table_name).get(record_id).run(conn)
def delete(table_name, record_id): """ Deletes a single record in a specified table """ with get_connection() as conn: return rethink.table(table_name).get(record_id).delete().run(conn)
def delete_all(table_name): """ Deletes all records in a specified table """ with get_connection() as conn: return rethink.table(table_name).delete().run(conn)