def test_filter_newer_than_now(self, conn): table = r.db('d').table('people') new = r.row["last_updated"] >= r.now() result = table.filter(new).run(conn) result = list(result) assertEqual(1, len(result))
def test_index_drop_works(self, conn): r.db('s').table('people').index_create('last_name').run(conn) indexes = list(r.db('s').table('people').index_list().run(conn)) assertEqual(['last_name'], indexes) r.db('s').table('people').index_drop('last_name').run(conn) indexes = list(r.db('s').table('people').index_list().run(conn)) assertEqual([], indexes)
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_sort_multi_1_desc_2(self, conn): expected = [{ 'id': 'todd', 'age': 52, 'score': 15 }, { 'id': 'pale', 'age': 52, 'score': 30 }, { 'id': 'bill', 'age': 35, 'score': 78 }, { 'id': 'glen', 'age': 26, 'score': 15 }, { 'id': 'joe', 'age': 26, 'score': 60 }] result = r.db('y').table('scores').order_by(r.desc('age'), 'score').run(conn) assertEqual(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_filter_older_than_now(self, conn): table = r.db('d').table('people') old = r.row["last_updated"] <= r.now() result = table.filter(old).run(conn) result = list(result) assertEqual(2, len(result))
def test_inner_join(self): left = list(range(1, 5)) right = list(range(1, 5)) expected = [{ 'left': 2, 'right': 1 }, { 'left': 3, 'right': 1 }, { 'left': 3, 'right': 2 }, { 'left': 4, 'right': 1 }, { 'left': 4, 'right': 2 }, { 'left': 4, 'right': 3 }] def pred(x, y): return x > y assertEqual(expected, joins.do_inner_join(pred, left, right))
def test_outer_join(self): left = list(range(1, 6)) right = list(range(1, 6)) expected = [{ 'left': 1 }, { 'left': 2 }, { 'left': 3, 'right': 1 }, { 'left': 3, 'right': 2 }, { 'left': 4 }, { 'left': 5, 'right': 1 }, { 'left': 5, 'right': 2 }, { 'left': 5, 'right': 3 }, { 'left': 5, 'right': 4 }] def pred(x, y): return (x % 2 == 1) and (x > y) assertEqual(expected, joins.do_outer_join(pred, left, right))
def test_error1(self, conn): try: r.error('msg').run(conn) except RqlRuntimeError as err: rql_err = err assertEqual('msg', err.message) assert (isinstance(rql_err, RqlRuntimeError))
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)
def test_map_with(self): def add_1(x): return x + 1 nums = [10, 20, 30] # map_fn = util.map_with(add_1) assertEqual([11, 21, 31], util.map_with(add_1)(nums)) assertEqual([11, 21, 31], util.map_with(add_1, nums))
def test_filter_dict_match_bitwise(self, conn): """ Test bitwise operators and other bitwise operators """ expected = [{ 'id': 'kimye-id', 'name': 'kimye', 'age': 17 }, { 'id': 'bill-id', 'name': 'bill', 'age': 35 }] not_joe = r.row["id"] != "joe-id" not_bob = r.row["id"] != "bob-id" bill = r.row["id"] == "bill-id" kimye = r.row["name"] == "kimye" result = r.db('x').table('people').filter( not_joe & not_bob & (kimye | bill)).run( conn) # not joe and not bob and either kimye or bill assertEqual(sorted(expected, key=lambda i: i['age']), sorted(list(result), key=lambda i: i['age']))
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))
def test_min_of_table_field(self, conn): expected = { 'id': 'joe', 'age': 26, 'hobbies': ['sand', 'water', 'cats'] } result = r.db('x').table('people').min('age').run(conn) assertEqual(expected, result)
def test_table_eq_elem_count(self, conn): expected = 1 result = r.db('x').table('people').count({ 'id': 'bill', 'age': 48, 'hobbies': ['watermelon'] }).run(conn) assertEqual(expected, result)
def test_date(self, conn): expected = set([ datetime.datetime(2014, 8, 25, tzinfo=r.make_timezone('00:00')), datetime.datetime(2014, 6, 3, tzinfo=r.make_timezone('00:00')) ]) result = r.db('d').table('people').map( lambda doc: doc['last_updated'].date()).run(conn) assertEqual(expected, set(list(result)))
def test_day_of_week(self, conn): expected = set([ 1, # 2014 August 25 -> Monday 2 # 2014 June 3 -> Tuesday ]) result = r.db('d').table('people').map( lambda doc: doc['last_updated'].day_of_week()).run(conn) assertEqual(expected, set(list(result)))
def test_epoch_time(self, conn): results = r.db('d').table('people').map(lambda d: d.merge( {'as_epoch': d['last_updated'].to_epoch_time()})).run(conn) results = list(results) jan1 = datetime.datetime(1970, 1, 1, tzinfo=r.make_timezone('00:00')) for doc in results: expected = int((doc['last_updated'] - jan1).total_seconds()) assertEqual(expected, doc['as_epoch'])
def test_update(self): expected_result = [{ 'id': 'a', 'name': 'a-name', 'age': 'a-age' }, { 'id': 'b', 'name': 'b-name', 'age': 'b-age' }, { 'id': 'c', 'name': 'new c name', 'new_c_key': 'new_c_val', 'age': 'c-age' }, { 'id': 'd', 'name': 'deshawn' }] expected_report = { 'replaced': 1, 'inserted': 1, 'errors': 0, 'changes': [{ 'old_val': { 'id': 'c', 'name': 'c-name', 'age': 'c-age' }, 'new_val': { 'id': 'c', 'name': 'new c name', 'new_c_key': 'new_c_val', 'age': 'c-age' } }, { 'old_val': None, 'new_val': { 'id': 'd', 'name': 'deshawn' } }] } to_insert = [{ 'id': 'c', 'name': 'new c name', 'new_c_key': 'new_c_val' }, { 'id': 'd', 'name': 'deshawn' }] result, report = db.insert_into_table_with_conflict_setting( db_insert_starting_data(), to_insert, conflict='update') assertEqual(expected_result, result) keys = ('replaced', 'inserted', 'errors', 'changes') self.assert_key_equality(keys, expected_report, report)
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)))
def test_contains_table_dict_multi_false(self, conn): result = r.db('d').table('people').contains({ 'id': 'sam-id', 'age': 45 }, { 'id': 'tara-muse-id', 'age': 'timeless' }).run(conn) assertEqual(False, result)
def test_contains_table_dict_multi_true(self, conn): result = r.db('d').table('people').contains({ 'id': 'sam-id', 'age': 45 }, { 'id': 'joe-id', 'age': 36 }).run(conn) assertEqual(True, result)
def test_map_merge_literal(self, conn): expected = {'id': 'one', 'points': {'pt1': {'z': 'z-1'}}} result = r.db('things').table('points').filter({ 'id': 'one' }).map( lambda doc: doc.merge({'points': { 'pt1': r.literal({'z': 'z-1'}) }})).run(conn) assertEqual(expected, list(result)[0])
def test_get_all_just_one(self, conn): expected = [ { 'id': 'bob-id', 'name': 'bob' }, ] result = r.db('x').table('people').get_all('bob-id').run(conn) assertEqual(expected, list(result))
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))
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))
def test_pipeline(self): def add_5(x): return x + 5 def mul_2(x): return x * 2 assertEqual(24, util.pipeline(add_5, mul_2)(7)) assertEqual(19, util.pipeline(mul_2, add_5)(7))
def test_as_obj(self): expected = { 'x': 'x-val', 'y': 'y-val' } pairs = [ ['x', 'x-val'], ['y', 'y-val'] ] assertEqual(expected, util.as_obj(pairs))
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)
def test_getter_obj(self): class Thing(object): def __init__(self, a_dict): for k, v in list(a_dict.items()): setattr(self, k, v) thing = Thing({'x': 'x-val'}) assertEqual('x-val', util.getter('x')(thing)) assertEqual(None, util.getter('y')(thing))