Exemplo n.º 1
0
 def test_delete_multiple(self, conn):
     expected = [{
         'id': 'sally-id',
         'name': 'sally'
     }, {
         'id': 'joe-id',
         'name': 'joe'
     }]
     expected_changes = [{
         'old_val': {
             'id': 'sam-id',
             'name': 'sam'
         },
         'new_val': None
     }, {
         'old_val': {
             'id': 'tom-id',
             'name': 'tom'
         },
         'new_val': None
     }]
     report = r.db('ephemeral').table('people').get_all(
         'sam-id', 'tom-id').delete(return_changes=True).run(conn)
     assertEqUnordered(expected_changes, report['changes'])
     assertEqual(2, report['deleted'])
     result = r.db('ephemeral').table('people').run(conn)
     assertEqUnordered(expected, list(result))
Exemplo n.º 2
0
    def test_update_one(self, conn):
        expected = {
            'id': 'kermit-id',
            'species': 'frog',
            'name': 'New Kermit',
            'new_key': 'new_key_val'
        }
        expected_changes = [{
            'old_val': {
                'id': 'kermit-id',
                'species': 'frog',
                'name': 'Kermit'
            },
            'new_val': {
                'id': 'kermit-id',
                'species': 'frog',
                'name': 'New Kermit',
                'new_key': 'new_key_val'
            }
        }]

        result_obj = r.db('things').table('muppets').get('kermit-id').update(
            {
                'name': 'New Kermit',
                'new_key': 'new_key_val'
            },
            return_changes=True).run(conn)

        assertEqual(expected_changes, result_obj['changes'])
        assertEqual(1, result_obj['replaced'])
        assertEqual(0, result_obj['inserted'])
        result = r.db('things').table('muppets').get('kermit-id').run(conn)
        assertEqual(expected, result)
Exemplo n.º 3
0
    def test_update_merge_deep(self, conn):
        # this behavior is pretty weird, but it's what rethink does
        expected = {
            'id': 'one',
            'points': {
                'pt1': {
                    'x': 'x-1',
                    'y': 'y-1'
                }
            },
            'pt1': {
                'x': 'x-1',
                'y': 'y-1',
                'z': 'z-1'
            }
        }

        r.db('things').table('points').filter({
            'id': 'one'
        }).update(r.row['points'].merge({'pt1': {
            'z': 'z-1'
        }})).run(conn)
        result = r.db('things').table('points').get('one').run(conn)
        pprint({'merge_deep': result})
        assertEqual(expected, result)
Exemplo n.º 4
0
 def test_inner_join_1(self, conn):
     expected = [{
         'left': {
             'id': 'joe-emp-id',
             'person': 'joe-id',
             'job': 'lawyer-id'
         },
         'right': {
             'id': 'joe-id',
             'name': 'Joe'
         }
     }, {
         'left': {
             'id': 'arnold-emp-id',
             'person': 'arnold-id',
             'job': 'nurse-id'
         },
         'right': {
             'id': 'arnold-id',
             'name': 'Arnold'
         }
     }]
     result = r.db('jezebel').table('employees').inner_join(
         r.db('jezebel').table('people'),
         lambda employee, person: employee['person'] == person['id']).run(
             conn)
     assertEqUnordered(expected, list(result))
Exemplo n.º 5
0
 def test_insert_array(self, conn):
     expected = [{
         'id': 'kermit-id',
         'species': 'frog',
         'name': 'Kermit'
     }, {
         'id': 'piggy-id',
         'species': 'pig',
         'name': 'Ms. Piggy'
     }, {
         'id': 'elmo-id',
         'species': 'methhead',
         'name': 'Elmo'
     }, {
         'id': 'fonz-id',
         'species': 'guido',
         'name': 'The Fonz'
     }]
     r.db('things').table('muppets').insert([{
         'id': 'elmo-id',
         'species': 'methhead',
         'name': 'Elmo'
     }, {
         'id': 'fonz-id',
         'species': 'guido',
         'name': 'The Fonz'
     }]).run(conn)
     result = r.db('things').table('muppets').run(conn)
     assertEqUnordered(expected, list(result))
Exemplo n.º 6
0
 def test_nested_literal_throws_merge(self, conn):
     with pytest.raises(RqlRuntimeError):
         r.db('things').table('points').filter({
             'id': 'one'
         }).map(lambda doc: doc.merge(
             {'points': r.literal({'pt1': r.literal({'z': 'z-1'})})})).run(
                 conn)
Exemplo n.º 7
0
    def test_conflict_has_error_as_default(self, conn):
        expected = [
            {
                'id': 'kermit-id',
                'species': 'frog',
                'name': 'Kermit'
            },
            {
                'id': 'piggy-id',
                'species': 'pig',
                'name': 'Ms. Piggy'
            },
        ]

        # try to insert and assert that errors are raised
        result_obj = r.db('things').table('muppets').insert(
            {
                'id': 'kermit-id',
            }, conflict='error').run(conn)
        assertEqual(1, result_obj['errors'])
        assertEqual(0, result_obj['inserted'])
        assertEqual(0, result_obj['replaced'])

        # ensure the table really is unchanged.
        result = r.db('things').table('muppets').run(conn)
        assertEqUnordered(expected, list(result))
Exemplo n.º 8
0
    def test_conflict_replace(self, conn):
        expected = [
            {
                'id': 'kermit-id',
                'x-key': 'x-val',
                'name': 'New Kermit'
            },
            {
                'id': 'piggy-id',
                'species': 'pig',
                'name': 'Ms. Piggy'
            },
        ]

        result_obj = r.db('things').table('muppets').insert(
            {
                'id': 'kermit-id',
                'x-key': 'x-val',
                'name': 'New Kermit'
            },
            conflict='replace').run(conn)
        assertEqual(1, result_obj['replaced'])
        assertEqual(0, result_obj['inserted'])
        assertEqual(0, result_obj['errors'])

        result = r.db('things').table('muppets').run(conn)
        assertEqUnordered(expected, list(result))
Exemplo n.º 9
0
 def test_multi_join(self, conn):
     query = r.db('x').table('employees').eq_join(
         'person', r.db('x').table('people')
     ).map(
         lambda d: d['left'].merge({'person': d['right']['name']})
     ).eq_join(
         'job', r.db('x').table('jobs')
     ).map(
         lambda d: d['left'].merge({'job': d['right']['name']})
     )
     expected = [
         {
             'id': 'joe-employee-id',
             'person': 'joe',
             'job': 'Lawyer'
         },
         {
             'id': 'tim-employee-id',
             'person': 'tim',
             'job': 'Nurse'
         },
         {
             'id': 'bob-employee-id',
             'person': 'bob',
             'job': 'Assistant'
         },
         {
             'id': 'todd-employee-id',
             'person': 'todd',
             'job': 'Lawyer'
         }
     ]
     assertEqUnordered(expected, list(query.run(conn)))
Exemplo n.º 10
0
    def test_insert_one(self, conn):
        expected = [{
            'id': 'kermit-id',
            'species': 'frog',
            'name': 'Kermit'
        }, {
            'id': 'piggy-id',
            'species': 'pig',
            'name': 'Ms. Piggy'
        }, {
            'id': 'elmo-id',
            'species': 'methhead',
            'name': 'Elmo'
        }]

        elmo_doc = {'id': 'elmo-id', 'species': 'methhead', 'name': 'Elmo'}
        expected_changes = [{'old_val': None, 'new_val': elmo_doc}]

        result_obj = r.db('things').table('muppets').insert(
            elmo_doc, return_changes=True).run(conn)

        assertEqual(result_obj['changes'], expected_changes)

        result = r.db('things').table('muppets').run(conn)
        assertEqUnordered(expected, list(result))
Exemplo n.º 11
0
 def open_spider(self, spider):
     if self.rethinkdb_settings:
         self.table_name = self.rethinkdb_settings.pop('table_name')
         self.db_name = self.rethinkdb_settings['db']
         self.conn = r.connect(**self.rethinkdb_settings)
         table_list = r.db(self.db_name).table_list().run(self.conn)
         if self.table_name not in table_list:
             r.db(self.db_name).table_create(self.table_name).run(self.conn)
Exemplo n.º 12
0
    def test_index_wait_all_works(self, conn):
        expected = [{'id': 'bob', 'first_name': 'Bob', 'last_name': 'Builder'}]

        r.db('s').table('people').index_create('last_name').run(conn)
        r.db('s').table('people').index_wait().run(conn)
        result = r.db('s').table('people').get_all('Builder',
                                                   index='last_name').run(conn)
        assertEqual(expected, list(result))
Exemplo n.º 13
0
 def test_map_missing_field_no_default(self, conn):
     err = None
     try:
         r.db('x').table('people').map(lambda p: p['missing'] > 15).run(
             conn)
     except RqlRuntimeError as e:
         err = e
     assert (isinstance(err, RqlRuntimeError))
Exemplo n.º 14
0
    def test_func_index_create(self, conn):
        expected = ['first_and_last']
        r.db('s').table('people').index_create(
            'first_and_last',
            lambda doc: doc['first_name'] + doc['last_name']).run(conn)
        result = r.db('s').table('people').index_list().run(conn)

        assertEqUnordered(expected, list(result))
Exemplo n.º 15
0
 def remove_balances(self):
     total = r.db(self.db_name).table("balance").count().run()
     if total > 50:
         try:
             r.db(self.db_name).table("balance").order_by("timestamp").limit(
                 total - 30
             ).delete().run()
         except Exception as e:
             pass
Exemplo n.º 16
0
 def test_error_with_less_than_4_args(self, conn):
     try:
         r.db('unimportant').table('very').update({
             'update_time':
             r.time(2014, 3, 24)
         }).run(conn)
     except RqlCompileError as e:
         err = e
     assert ('expected between 4 and 7' in err.message.lower())
Exemplo n.º 17
0
    def test_table_drop_2(self, conn):
        expected_1 = set(['one_x', 'one_y'])
        expected_2 = set(['two_x'])
        r.db('db_two').table_drop('two_y').run(conn)
        result_1 = r.db('db_one').table_list().run(conn)
        assertEqual(expected_1, set(list(result_1)))

        result_2 = r.db('db_two').table_list().run(conn)
        assertEqual(expected_2, set(list(result_2)))
Exemplo n.º 18
0
    def test_insert_array_with_update(self, conn):
        expected = [{
            'id': 'kermit-id',
            'species': 'frog',
            'name': 'New Kermit'
        }, {
            'id': 'piggy-id',
            'species': 'pig',
            'name': 'Ms. Piggy'
        }, {
            'id': 'elmo-id',
            'species': 'methhead',
            'name': 'Elmo'
        }, {
            'id': 'fonz-id',
            'species': 'guido',
            'name': 'The Fonz'
        }]

        to_insert = [{
            'id': 'elmo-id',
            'species': 'methhead',
            'name': 'Elmo'
        }, {
            'id': 'fonz-id',
            'species': 'guido',
            'name': 'The Fonz'
        }, {
            'id': 'kermit-id',
            'name': 'New Kermit'
        }]
        expected_changes = [{
            'old_val': {
                'id': 'kermit-id',
                'species': 'frog',
                'name': 'Kermit'
            },
            'new_val': {
                'id': 'kermit-id',
                'species': 'frog',
                'name': 'New Kermit'
            }
        }, {
            'old_val': None,
            'new_val': to_insert[0]
        }, {
            'old_val': None,
            'new_val': to_insert[1]
        }]

        result_obj = r.db('things').table('muppets').insert(
            to_insert, return_changes=True, conflict='update').run(conn)

        assertEqUnordered(expected_changes, result_obj['changes'])

        result = r.db('things').table('muppets').run(conn)
        assertEqUnordered(expected, list(result))
Exemplo n.º 19
0
 def test_insert_one_no_id(self, conn):
     r.db('things').table('muppets').insert({'name': 'joe'}).run(conn)
     result = r.db('things').table('muppets').filter({
         'name': 'joe'
     }).run(conn)
     result = list(result)
     assertEqual(1, len(result))
     joe = result[0]
     assert (isinstance(joe['id'], text_type))
Exemplo n.º 20
0
 def test_distinct_secondary_index(self, conn):
     r.db('d').table('people').index_create('last_name').run(conn)
     r.db('d').table('people').index_wait().run(conn)
     result = r.db('d').table('people').distinct(
         index='last_name').run(conn)
     result = list(result)
     pprint({'result': result})
     assertEqual(2, len(result))
     assertEqual(set(['Sanders', 'Fudd']), set(result))
Exemplo n.º 21
0
 def connect(self):
     self.conn = r.connect(self.host, self.port).repl()
     tables = r.db('dashboard').table_list().run(self.conn)
     print('tables list: {}'.format(tables))
     if ('temperatures' not in tables):
         print('creating temperatures table')
         r.db('dashboard').table_create('temperatures').run(self.conn)
     if ('humidity' not in tables):
         print('creating humidity table')
         r.db('dashboard').table_create('humidity').run(self.conn)
Exemplo n.º 22
0
 def test_error_with_no_timezone(self, conn):
     date = datetime.datetime(2014, 3, 24, 12)
     try:
         r.db('unimportant').table('very').update({
             'update_time': date
         }).run(conn)
     except ReqlDriverCompileError as e:
         err = e
     assert ('datetime' in err.message.lower())
     assert ('timezone' in err.message.lower())
Exemplo n.º 23
0
 def test_update_merge_array(self, conn):
     expected = {'id': 'three', 'things': {'x': [1, 3, 5, 7, 9]}}
     r.db('things').table('points').filter({
         'id': 'three'
     }).update(r.row.merge({'things': {
         'x': [7, 9]
     }})).run(conn)
     result = r.db('things').table('points').get('three').run(conn)
     pprint(result)
     assertEqUnordered(expected, result)
Exemplo n.º 24
0
    def test_field_index_create_works(self, conn):
        expected = [{'id': 'bob', 'first_name': 'Bob', 'last_name': 'Builder'}]

        r.db('s').table('people').index_create('first_name').run(conn)
        r.db('s').table('people').index_wait('first_name').run(conn)
        result = r.db('s').table('people').get_all(
            'Bob', index='first_name').run(conn)
        result = list(result)
        pprint(result)
        assertEqUnordered(expected, result)
Exemplo n.º 25
0
 def test_update_nonatomic_error_is_default(self, conn):
     err = None
     try:
         r.db('things').table('muppets').get('kermit-id').update(
             lambda doc: doc.merge(
                 r.db('things').table('muppets').get('piggy-id').pluck(
                     'species'))).run(conn)
     except RqlRuntimeError as e:
         err = e
     assert (isinstance(err, RqlRuntimeError))
Exemplo n.º 26
0
 def test_update_literal(self, conn):
     expected = {'id': 'one', 'points': {'pt1': {'z': 'z-1'}}}
     r.db('things').table('points').filter({
         'id': 'one'
     }).update({
         'points': r.literal({'pt1': {
             'z': 'z-1'
         }})
     }).run(conn)
     result = r.db('things').table('points').get('one').run(conn)
     assertEqual(expected, result)
Exemplo n.º 27
0
    def test_time_year_month_day_tz(self, conn):
        r.db('unimportant').table('very').update(lambda doc: doc.merge(
            {'updated': r.time(2014, 6, 10, 'Z')})).run(conn)

        result = r.db('unimportant').table('very').get('say_anything').run(
            conn)
        update_time = result['updated']
        assertEqual(2014, update_time.year)
        assertEqual(6, update_time.month)
        assertEqual(10, update_time.day)
        assert (isinstance(update_time.tzinfo, RqlTzinfo))
Exemplo n.º 28
0
def real_stock_data_load(data, connection):
    for db in list(r.db_list().run(connection)):
        if db == "rethinkdb":
            # This db is special and can't be deleted.
            continue
        r.db_drop(db).run(connection)
    for db_name, db_data in iteritems(data['dbs']):
        r.db_create(db_name).run(connection)
        for table_name, table_data in iteritems(db_data['tables']):
            r.db(db_name).table_create(table_name).run(connection)
            r.db(db_name).table(table_name).insert(table_data).run(connection)
Exemplo n.º 29
0
 def test_update_one(self, conn):
     expected = {
         'id': 'kermit-id',
         'species': 'green frog',
         'name': 'Kermit'
     }
     r.db('things').table('muppets').get('kermit-id').update({
         'species':
         'green frog'
     }).run(conn)
     result = r.db('things').table('muppets').get('kermit-id').run(conn)
     assertEqual(expected, result)
Exemplo n.º 30
0
 def test_delete_get_all(self, conn):
     expected = [{
         'id': 'sally-id',
         'name': 'sally'
     }, {
         'id': 'joe-id',
         'name': 'joe'
     }]
     r.db('ephemeral').table('people').get_all('sam-id',
                                               'tom-id').delete().run(conn)
     result = r.db('ephemeral').table('people').run(conn)
     assertEqUnordered(expected, list(result))
Exemplo n.º 31
0
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        connection.run(r.db_create(DB_NAME))
        connection.run(r.db(DB_NAME).table_create('tools'))
        connection.run(r.db(DB_NAME).table_create('users'))
        connection.run(r.db(DB_NAME).table_create('categories'))
        connection.run(r.db(DB_NAME).table_create('toolboxes'))
        print 'Database setup completed. Now run the app without --setup.'
    except Exception:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()
Exemplo n.º 32
0
 def get_table_query(self, table=None):
     if table is None:
         table = self.table
     # Build a base query: starting from default DB from RDBdefaults.
     base = r.db(self.db)
     # Create
     if table not in base.table_list().run():
         base.table_create(table).run()
     # Use the table
     return base.table(table)
def dbSetup():
    connection = r.connect(host=RDB_HOST, port=RDB_PORT)
    try:
        connection.run(r.db_create(TODO_DB))
        connection.run(r.db(TODO_DB).table_create('todos'))
        print 'Database setup completed. Now run the app without --setup.'
    except Exception:
        print 'App database already exists. Run the app without --setup.'
    finally:
        connection.close()
Exemplo n.º 34
0
# Copyright 2010-2012 RethinkDB, all rights reserved.

from rethinkdb import r

# Connections
conn = r.connect('newton', 5151)
conn = r.connect('newton')              # Default port
conn = r.connect()                      # Localhost, default port
conn = r.connect([('newton', 5151),
                  ('dr-doom', 5252)])   # A list in case of failure
  # Note: reconnect automatically or force them to call connect again?

# Get the entire table (the following queries are equivalent)
q = r.db('foo').table('bar')
q = r.table('foo.bar')

# Default database
conn = r.connect('newton', db='foo')
q = r.table('bar').run(conn)

conn.run(q)       # Either throws an error, or returns a Python iterator (or an element, see below)
# => iterator or scalar
conn.run([q1, q2, q3]) # Packing queries, asynchronisity of queries?
# => [a1, a2, a3]

# We can also start a query off a connection
r.db('foo').table('bar').run(conn)
q.run(conn)

# Default connection mode
r.set_default_conn(conn)
def main(mmCIFPath, logPath):
    # Start time
    start = time.time()
    # Logging
    logging.basicConfig(
        filename=logPath,
        level=logging.DEBUG
    )

    # Connect to DB
    try:
        conn = r.connect()
        logging.info('Connected to DB')
        print('Connected to DB')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Create DB and connect to it
    try:
        r.db_create('pdb_compounds').run()
        conn.use('pdb_compounds')
        logging.info('Created DB and connected to it')
        print('Created DB and connected to it')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Create table
    try:
        r.db('pdb_compounds') \
            .table_create('compounds', primary_key='_chem_comp.id') \
            .run()
        logging.info('Created Table: compounds')
        print('Created Table: compounds')
    except Exception as e:
        logging.debug(e)
        print(e)

    # Iterate through the mmCIF files and write to DB
    for cifFile in glob.iglob(os.path.join(mmCIFPath, '*.cif')):
        try:
            data = MMCIF2Dict(cifFile)
            dataJSON = json.dumps(data)         # Creates JSON string
            dataDecoded = json.loads(dataJSON)  # Needed to create valid JSON
            # Insert the data into the DB
            result = r.table('compounds').insert(dataDecoded).run()
            logging.info(
                'Insertion: ID: {id} | Error: {error} | ' \
                'Inserted: {inserted}'.format(
                    id=data['_chem_comp.id'],
                    error=result['errors'],
                    inserted=result['inserted']
                )
            )
            print('Success: ', cifFile)
        except Exception as e:
            logging.debug(
                'File: {filename} | Error: {error}'.format(
                    filename=cifFile,
                    error=e
                )
            )
            print('Error: ', cifFile)
    # Close DB Connection
    conn.close()
    logging.info('Disconnected to DB')
    print('Disconnected from DB')

    # End time
    end = time.time()
    timeTaken = (end - start) / 60
    logging.info('Time Taken: {time} mins'.format(time=timeTaken))
    print('Time Taken: {time} mins'.format(time=timeTaken))
Exemplo n.º 36
0
 def get_query(self):
     return r.db(self.db)
Exemplo n.º 37
0
def get_user_posts(user_id):
    return r.db('posts').filter({ 'user_id': user_id})