Beispiel #1
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))
Beispiel #2
0
 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 = [set(d) for d in result]
     assertEqUnordered(expected, result)
Beispiel #3
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))
Beispiel #4
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))
Beispiel #5
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))
Beispiel #6
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))
Beispiel #7
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))
Beispiel #8
0
 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)
Beispiel #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)))
    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_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)
Beispiel #12
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))
    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)
Beispiel #14
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)
 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_between_id_closed_right(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', 'tom', right_bound='closed'
     ).run(conn)
     result = list(result)
     assertEqUnordered(expected, result)
Beispiel #17
0
 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))
Beispiel #18
0
 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))
Beispiel #19
0
 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))
Beispiel #20
0
 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))
Beispiel #21
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))
 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))
Beispiel #23
0
 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))
Beispiel #24
0
 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))
Beispiel #25
0
 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_sub_without_list(self, conn):
     expected = [{
         'id': 'joe-id'
     }, {
         'id': 'bob-id'
     }, {
         'id': 'bill-id'
     }, {
         'id': 'kimye-id'
     }]
     result = r.db('x').table('people').map(
         lambda p: p.without(['name', 'hobby'])).run(conn)
     assertEqUnordered(expected, list(result))
Beispiel #27
0
 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))
Beispiel #28
0
 def test_update_many(self, conn):
     expected = [{
         'id': 'kermit-id',
         'species': 'unknown',
         'name': 'Kermit'
     }, {
         'id': 'piggy-id',
         'species': 'unknown',
         'name': 'Ms. Piggy'
     }]
     r.db('things').table('muppets').update(
         r.row.merge({'species': 'unknown'})).run(conn)
     result = r.db('things').table('muppets').run(conn)
     assertEqUnordered(expected, list(result))
 def test_between_index_default_range(self, conn):
     expected = [
         {'id': 'bob', 'first_name': 'Bob', 'last_name': 'Builder'},
         {'id': 'tom', 'first_name': 'Tom', 'last_name': 'Generic'}
     ]
     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').between(
         'Builder', 'Smith', index='last_name'
     ).run(conn)
     result = list(result)
     assertEqUnordered(expected, result)
Beispiel #30
0
 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)