def test_merge_toplevel(self, conn): expected = [ { 'id': 'id-1', 'x': { 'x-val': 'x-val-1' }, 'y': { 'y-val': 'y-val-1' }, 'z': 'Z-VALUE' }, { 'id': 'id-2', 'x': { 'x-val': 'x-val-2' }, 'y': { 'y-val': 'y-val-2' }, 'z': 'Z-VALUE' } ] result = r.db('jezebel').table('things').merge({'z': 'Z-VALUE'}).run(conn) assertEqUnordered(expected, list(result))
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))
def test_filter_lambda_lt(self, conn): expected = [ {'id': 'bob-id', 'name': 'bob', 'age': 19}, {'id': 'kimye-id', 'name': 'kimye', 'age': 17} ] result = r.db('x').table('people').filter(lambda p: p['age'] < 20).run(conn) assertEqUnordered(expected, list(result))
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))
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))
def test_filter_lambda_gt(self, conn): expected = [ {'id': 'joe-id', 'name': 'joe', 'age': 28}, {'id': 'bill-id', 'name': 'bill', 'age': 35} ] result = r.db('x').table('people').filter(lambda p: p['age'] > 20).run(conn) assertEqUnordered(expected, list(result))
def test_offsets_of_val(self, conn): expected = [[], [1, 4], [0, 2], [2]] result = r.db('scrumptious').table('cake').map( lambda doc: doc['letters'].offsets_of('b')).run(conn) result = list(result) pprint(result) assertEqUnordered(expected, result)
def test_get_all_by_id(self, conn): expected = [ {'id': 'anne-id', 'name': 'anne'}, {'id': 'joe-id', 'name': 'joe'}, ] result = r.db('x').table('people').get_all('anne-id', 'joe-id').run(conn) assertEqUnordered(expected, result)
def test_has_fields_array(self, conn): expected = [ {'id': 'todd', 'first_name': 'Todd', 'last_name': 'Last', 'age': 35}, {'id': 'sam', 'first_name': 'Sam', 'last_name': 'SamLast', 'age': 31} ] result = r.db('x').table('people').has_fields(['last_name', 'age']).run(conn) assertEqUnordered(expected, list(result))
def test_set_difference(self, conn): expected = [set(['x']), set(['x', 'z'])] result = r.db('z').table('t').map( lambda doc: doc['simple'].set_difference(['y'])).run(conn) result = list(result) result = map(lambda d: set(d), result) assertEqUnordered(expected, result)
def test_sub_sub_list(self, conn): expected = [ {'a': 'a-1', 'd': 'd-1'}, {'a': 'a-2', 'd': 'd-2'} ] result = r.db('some_db').table('things').map(lambda t: t['values'].pluck('a', 'd')).run(conn) assertEqUnordered(expected, list(result))
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)))
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))
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))
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))
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))
def test_ungroup_grouped_by_func(self, conn): expected = [ { 'group': 'bro', 'reduction': [ {'id': 'joe', 'type': 'bro'}, {'id': 'sam', 'type': 'bro'} ] }, { 'group': 'hipster', 'reduction': [ {'id': 'bill', 'type': 'hipster'}, {'id': 'todd', 'type': 'hipster'} ] }, { 'group': 'unknown', 'reduction': [ {'id': 'glenn', 'type': 'unknown'}, ] } ] result = r.db('x').table('people').group(lambda d: d['type']).ungroup().run(conn) result = list(result) assertEqual(3, len(result)) assertEqual(set(['bro', 'hipster', 'unknown']), set([doc['group'] for doc in result])) is_group = lambda group: lambda doc: doc['group'] == group for group in ('bro', 'hipster', 'unknown'): result_group = list(filter(is_group(group), result))[0] expected_group = list(filter(is_group(group), expected))[0] assertEqUnordered(expected_group['reduction'], result_group['reduction'])
def test_map_gt(self, conn): expected = [ True, False, True, False ] result = r.db('x').table('people').map( lambda p: p['age'] > 20 ).run(conn) assertEqUnordered(expected, list(result))
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))
def test_not(self, conn): expected = [ {'id': 'johnson'}, {'id': 'angela'} ] result = r.db('pdb').table('p').filter( lambda doc: ~doc['has_eyes'] ).pluck('id').run(conn) assertEqUnordered(expected, list(result))
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))
def test_gt(self, conn): expected = [ {'id': 'joe'}, {'id': 'angela'} ] result = r.db('pdb').table('p').filter( lambda doc: doc['age'] > 20 ).pluck('id').run(conn) assertEqUnordered(expected, list(result))
def test_change_at(self, conn): expected = [ ['wombat', 'cow'], ['wombat'] ] result = r.db('x').table('farms').map( lambda d: d['animals'].change_at(0, 'wombat') ).run(conn) assertEqUnordered(expected, list(result))
def test_append(self, conn): expected = [ ['frog', 'cow', 'pig'], ['horse', 'pig'] ] result = r.db('x').table('farms').map( lambda d: d['animals'].append('pig') ).run(conn) assertEqUnordered(expected, list(result))
def test_splice_at(self, conn): expected = [ ['frog', 'pig', 'chicken', 'cow'], ['horse', 'pig', 'chicken'] ] result = r.db('x').table('farms').map( lambda d: d['animals'].splice_at(1, ['pig', 'chicken']) ).run(conn) assertEqUnordered(expected, list(result))
def test_between_id_open_left(self, conn): expected = [ {'id': 'joe', 'first_name': 'Joseph', 'last_name': 'Smith'} ] result = r.db('s').table('people').between( 'bob', 'tom', left_bound='open' ).run(conn) result = list(result) assertEqUnordered(expected, result)
def test_lt(self, conn): expected = [ {'id': 'sam'}, {'id': 'johnson'} ] result = r.db('pdb').table('p').filter( lambda doc: doc['age'].lt(20) ).pluck('id').run(conn) assertEqUnordered(expected, list(result))
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))
def test_distinct_table(self, conn): expected = [ {'id': 'bob-id', 'first_name': 'Bob', 'last_name': 'Sanders', 'age': 35}, {'id': 'sam-id', 'first_name': 'Sam', 'last_name': 'Fudd', 'age': 17}, {'id': 'joe-id', 'first_name': 'Joe', 'last_name': 'Sanders', 'age': 62} ] result = r.db('d').table('people').distinct().run(conn) assertEqUnordered(expected, list(result))
def test_sub_pluck(self, conn): expected = [ {'id': 'joe-id', 'hobby': 'guitar'}, {'id': 'bob-id', 'hobby': 'pseudointellectualism'}, {'id': 'bill-id'}, {'id': 'kimye-id', 'hobby': 'being kimye'} ] result = r.db('x').table('people').map(lambda p: p.pluck('id', 'hobby')).run(conn) assertEqUnordered(expected, list(result))
def test_is_empty_nested(self, conn): expected = [ {'id': 'id-1', 'things_empty': True, 'things': []}, {'id': 'id-2', 'things_empty': False, 'things': ['x', 'y']} ] result = r.db('some_db').table('some_table').map( lambda d: d.merge({'things_empty': d['things'].is_empty()}) ).run(conn) assertEqUnordered(expected, list(result))
def test_update_with_json(self, conn): expected = [ {'id': 'one', 'nums': [1, 2, 3]}, {'id': 'two', 'nums': [1, 2, 3]} ] result = r.db('d').table('t').map( lambda doc: doc.merge(r.json('{"nums": [1, 2, 3]}')) ).run(conn) assertEqUnordered(expected, list(result))
def test_pluck_missing_attr_list(self, conn): expected = [ {'id': 'joe-id', 'hobby': 'guitar'}, {'id': 'bob-id', 'hobby': 'pseudointellectualism'}, {'id': 'bill-id'}, {'id': 'kimye-id', 'hobby': 'being kimye'} ] result = r.db('x').table('people').pluck(['id', 'hobby']).run(conn) assertEqUnordered(expected, list(result))
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)
def test_or(self, conn): expected = [ {'id': 'sam'}, {'id': 'angela'}, {'id': 'joe'} ] result = r.db('pdb').table('p').filter( lambda doc: doc['has_eyes'].or_(doc['age'].gt(20)) ).pluck('id').run(conn) assertEqUnordered(expected, list(result))
def test_neq(self, conn): expected = [ {'id': 'sam'}, {'id': 'angela'}, {'id': 'joe'} ] result = r.db('pdb').table('p').filter( lambda doc: doc['hair_color'] != 'blonde' ).pluck('id').run(conn) assertEqUnordered(expected, list(result))
def test_split_4(self, conn): expected = [ ['som', 'thing with spaces'], ['som', ',csv,file'], ['som', 'ething'] ] result = r.db('library').table('texts').map( lambda doc: doc['text'].split('e', 1) ).run(conn) assertEqUnordered(expected, list(result))
def test_set_intersection(self, conn): expected = [ set(['x', 'y']), set(['x']) ] result = r.db('z').table('t').map( lambda doc: doc['simple'].set_intersection(['x', 'y']) ).run(conn) result = map(lambda d: set(d), result) assertEqUnordered(expected, result)
def test_delete_at(self, conn): expected = [ ['cow'], [] ] result = r.db('x').table('farms').map( lambda d: d['animals'].delete_at(0) ).run(conn) res = list(result) assertEqUnordered(expected, res)
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)
def test_between_id_default_range(self, conn): expected = [ {'id': 'bob', 'first_name': 'Bob', 'last_name': 'Builder'}, {'id': 'joe', 'first_name': 'Joseph', 'last_name': 'Smith'}, {'id': 'tom', 'first_name': 'Tom', 'last_name': 'Generic'} ] result = r.db('s').table('people').between( 'bob', 'zuul' ).run(conn) assertEqUnordered(expected, list(result))
def test_table_union(self, conn): expected = [ {'id': 'thing1-1'}, {'id': 'thing1-2'}, {'id': 'thing2-1'}, {'id': 'thing2-2'} ] result = r.db('x').table('things_1').union( r.db('x').table('things_2') ).run(conn) assertEqUnordered(expected, list(result))
def test_merge_nested_with_prop(self, conn): expected = [{ 'x-val': 'x-val-1', 'y-val': 'y-val-1' }, { 'x-val': 'x-val-2', 'y-val': 'y-val-2' }] result = r.db('jezebel').table('things').map( lambda d: d['x'].merge(d['y'])).run(conn) assertEqUnordered(expected, list(result))
def test_merge_nested(self, conn): expected = [{ 'y-val': 'y-val-1', 'extra-y-val': 'extra' }, { 'y-val': 'y-val-2', 'extra-y-val': 'extra' }] result = r.db('jezebel').table('things').map( lambda d: d['y'].merge({'extra-y-val': 'extra'})).run(conn) assertEqUnordered(expected, list(result))
def test_between_id_open_left(self, conn): expected = [{ 'id': 'joe', 'first_name': 'Joseph', 'last_name': 'Smith' }] result = r.db('s').table('people').between('bob', 'tom', left_bound='open').run(conn) result = list(result) assertEqUnordered(expected, result)
def test_update_with_json(self, conn): expected = [{ 'id': 'one', 'nums': [1, 2, 3] }, { 'id': 'two', 'nums': [1, 2, 3] }] result = r.db('d').table('t').map( lambda doc: doc.merge(r.json('{"nums": [1, 2, 3]}'))).run(conn) assertEqUnordered(expected, list(result))
def test_without_missing_attr_list(self, conn): expected = [{ 'id': 'joe-id' }, { 'id': 'bob-id' }, { 'id': 'bill-id' }, { 'id': 'kimye-id' }] result = r.db('x').table('people').without(['name', 'hobby']).run(conn) assertEqUnordered(expected, list(result))
def test_during_closed_right(self, conn): expected = [{'id': 'joe', 'is_during': True}] result = r.db('d').table('people').map( lambda doc: { 'id': doc['id'], 'is_during': doc['last_updated'].during(r.time(2014, 6, 2, 'Z'), r.time(2014, 8, 25, 'Z'), right_bound='closed') }).run(conn) assertEqUnordered(expected, list(result))
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))
def test_filter_lambda_gt(self, conn): expected = [{ 'id': 'joe-id', 'name': 'joe', 'age': 28 }, { 'id': 'bill-id', 'name': 'bill', 'age': 35 }] result = r.db('x').table('people').filter(lambda p: p['age'] > 20).run( conn) assertEqUnordered(expected, list(result))
def test_is_empty_nested(self, conn): expected = [{ 'id': 'id-1', 'things_empty': True, 'things': [] }, { 'id': 'id-2', 'things_empty': False, 'things': ['x', 'y'] }] result = r.db('some_db').table('some_table').map(lambda d: d.merge( {'things_empty': d['things'].is_empty()})).run(conn) assertEqUnordered(expected, list(result))