def test_background_execute_expressions_and_predicate(self): """ Ensure that Scan.execute_background() gets applied to records that match the predicate. NOTE: the predicate overrides the expressions """ test_bin = 'Stpredold' keys = [(TEST_NS, TEST_SET, i) for i in range(50)] expr = exp.Or( exp.Eq(exp.IntBin('number'), 2), exp.Eq(exp.IntBin('number'), 3) ) policy = { 'expressions': expr.compile() } number_predicate = predicates.equals('number', 4) scan = self.as_connection.scan(TEST_NS, TEST_SET) scan.where(number_predicate) scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) job_id = scan.execute_background(policy) wait_for_job_completion(self.as_connection, job_id) for key in keys: _, _, bins = self.as_connection.get(key) if bins['number'] == 4: assert(bins[test_bin] == 'aerospike') else: assert(bins.get(test_bin) is None)
def test_background_execute_expressions_everywhere(self): """ Ensure that Scan.execute_background() gets applied to records that match the expressions. """ test_bin = 'number' keys = [(TEST_NS, TEST_SET, i) for i in range(50)] expr = exp.Or( exp.Eq(exp.IntBin('number'), 2), exp.Eq(exp.IntBin('number'), 3) ) policy = { 'expressions': expr.compile() } scan = self.as_connection.scan(TEST_NS, TEST_SET) scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin, 1]) job_id = scan.execute_background(policy) wait_for_job_completion(self.as_connection, job_id) for i, key in enumerate(keys): _, _, bins = self.as_connection.get(key) if i == 2 or i == 3: assert(bins[test_bin] == i + 1) else: assert(bins.get(test_bin) == i)
def test_background_execute_with_ops_and_expressions(self): """ Ensure that Scan.execute_background() applies ops to records that match the expressions. """ test_bin = 'Stops_preds' keys = [(TEST_NS, TEST_SET, i) for i in range(50)] scan = self.as_connection.scan(TEST_NS, TEST_SET) # scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) ops = [ operations.write(test_bin, 'new_val') ] expr = exp.Or( exp.Eq(exp.IntBin('number'), 2), exp.Eq(exp.IntBin('number'), 3) ) policy = { 'expressions': expr.compile() } scan.add_ops(ops) job_id = scan.execute_background(policy) wait_for_job_completion(self.as_connection, job_id) for key in keys: _, _, bins = self.as_connection.get(key) if bins['number'] == 2 or bins['number'] == 3: assert(bins[test_bin] == 'new_val') else: assert(bins.get(test_bin) is None)
def test_query_apply_with_new_expressions(self): """ Invoke query_apply() with correct policy and expressions """ expr = exp.Or( exp.Eq(exp.IntBin('age'), 2), exp.Eq(exp.IntBin('val'), 3) ) policy = {'total_timeout': 0, 'expressions': expr.compile()} query_id = self.as_connection.query_apply( "test", "demo", self.age_range_pred, "query_apply", "mark_as_applied", ['name', 2], policy) self._wait_for_query_complete(query_id) recs = [] for i in range(1, 10): key = ('test', 'demo', i) _, _, bins = self.as_connection.get(key) if bins['name'] == 'aerospike': recs.append(bins) assert len(recs) == 2 for rec in recs: assert rec['age'] == 2 or rec['val'] == 3
def test_background_execute_with_ops_and_predexp(self, clean_test_background): """ Ensure that Query.execute_background() applies ops to records that match the expressions. """ test_bin = 'tops_preds' keys = [(TEST_NS, TEST_SET, i) for i in range(500)] query = self.as_connection.query(TEST_NS, TEST_SET) ops = [ operations.write(test_bin, 'new_val') ] expr = exp.Or( exp.Eq(exp.IntBin('number'), 2), exp.Eq(exp.IntBin('number'), 3) ) policy = { 'expressions': expr.compile() } query.add_ops(ops) query.execute_background(policy=policy) # Give time for the query to finish time.sleep(5) for key in keys: _, _, bins = self.as_connection.get(key) if bins['number'] == 2 or bins['number'] == 3: assert(bins[test_bin] == 'new_val') else: assert(bins.get(test_bin) is None)
def test_background_execute_predexp_and_predicate(self, clean_test_background): """ Ensure that Query.execute_background() gets applied to records that match the predicate NOTE: the predicate overrides the predexp """ test_bin = 'tpredold' keys = [(TEST_NS, TEST_SET, i) for i in range(500)] expr = exp.Or( exp.Eq(exp.IntBin('number'), 2), exp.Eq(exp.IntBin('number'), 3) ) number_predicate = predicates.equals('number', 4) policy = { 'expressions': expr.compile() } query = self.as_connection.query(TEST_NS, TEST_SET) query.where(number_predicate) query.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) query.execute_background(policy) # Give time for the query to finish time.sleep(5) for key in keys: _, _, bins = self.as_connection.get(key) if bins['number'] == 4: assert(bins[test_bin] == 'aerospike') else: assert(bins.get(test_bin) is None)
def test_or(self): expr = exp.Or( exp.Eq(exp.IntBin('positive_i'), 5), exp.Eq(exp.IntBin('positive_i'), 10) ) results = self.query.results(policy={'expressions': expr.compile()}) assert len(results) == 2 assert_each_record_bins( results, lambda b: b['positive_i'] in (5, 10))
def test_query_apply_with_bad_new_expressions(self): """ Invoke query_apply() with incorrect policy and expressions """ expr = exp.Or( exp.Eq(exp.IntBin(5), 2), exp.Eq(exp.IntBin('val'), 3) ) policy = {'total_timeout': 0, 'expressions': expr} with pytest.raises(e.ParamError): query_id = self.as_connection.query_apply( "test", "demo", self.age_range_pred, "query_apply", "mark_as_applied", ['name', 2], policy)
def test_string_equal(self): expr = exp.Eq(exp.StrBin('name'), 'Alice') results = self.query.results(policy={'expressions': expr.compile()}) assert len(results) == 25 assert_each_record_bins( results, lambda b: b['name'] == 'Alice')
def test_scan_apply_with_none_set_and_expressions(self): """ Invoke scan_apply() with set argument as None It should invoke the function on all records in NS that match the expressions """ expr = exp.And(exp.Eq(exp.StrBin('name'), 'name2'), exp.NE(exp.IntBin('age'), 3)) policy = {'timeout': 1000, 'expressions': expr.compile()} scan_id = self.as_connection.scan_apply("test", None, "bin_lua", "mytransform", ['age', 2], policy) wait_for_job_completion(self.as_connection, scan_id) for i in range(5): key = ('test', 'demo', i) _, _, bins = self.as_connection.get(key) if bins['name'] == 'name2': assert bins['age'] == i + 2 else: assert bins['age'] == i _, _, rec = self.as_connection.get(('test', None, 'no_set')) assert rec['age'] == 10
def test_put_with_expressions_filtered_out(self): ''' Call put with expressions in policy with expected failure. ''' expr = exp.Eq(exp.IntBin('account_id'), 4) with pytest.raises(e.FilteredOut): self.as_connection.put(self.keys[0], {'newkey': 'newval'}, policy={'expressions': expr.compile()})
def test_get_with_expressions_filtered_out(self): ''' Call to get with expressions in policy with expected failures. ''' expr = exp.Eq(exp.IntBin('account_id'), 3) with pytest.raises(e.FilteredOut): self.as_connection.get(self.keys[0], {'expressions': expr.compile()})
def test_remove_bin_with_expressions_filtered_out(self): ''' Call remove_bin with expressions in policy with expected failure. ''' expr = exp.Eq(exp.IntBin('account_id'), 4) with pytest.raises(e.FilteredOut): self.as_connection.remove_bin( self.keys[0], ['account_id', 'user_name'], policy={'expressions': expr.compile()})
def test_get_with_expressions(self): ''' Call to get with expressions in policy. ''' expr = exp.Eq(exp.IntBin('account_id'), 1) record = self.as_connection.get(self.keys[0], {'expressions': expr.compile()}) assert record[2]['account_id'] == 1
def test_put_with_expressions(self): ''' Call put with expressions in policy. ''' expr = exp.Eq(exp.IntBin('account_id'), 1) self.as_connection.put(self.keys[0], {'newkey': 'newval'}, policy={'expressions': expr.compile()}) rec = self.as_connection.get(self.keys[0]) assert rec[2]['newkey'] == 'newval'
def test_with_invalid_predicate(self): ''' This passes something which isn't a predicate ''' expr = exp.Or( exp.Eq(1, 1), 'bad_expression' ) with pytest.raises(e.InvalidRequest): self.query.results(policy={'expressions': expr.compile()})
def test_pos_remove_with_expressions(self): ''' Call remove with expressions in policy. ''' expr = exp.Eq(exp.IntBin('account_id'), 1) records = self.as_connection.remove(self.keys[0], {'expressions': expr.compile()}) rec = self.as_connection.exists(self.keys[0]) assert rec[1] is None
def test_mapvalue_iterate_and(self): expr = exp.Eq( exp.MapGetByValue(None, aerospike.LIST_RETURN_COUNT, 3, 'map'), 0 ) query = self.as_connection.query('test', 'demo3') results = query.results(policy={'expressions': expr.compile()}) assert len(results) == 1 assert_each_record_bins( results, lambda b: all([b['map'][key] != 3 for key in b['map']]))
def test_mapkey_iterate_and(self): expr = exp.Eq( exp.MapGetByKey(None, aerospike.LIST_RETURN_COUNT, exp.ResultType.INTEGER, 'Bob', 'map'), 0 ) query = self.as_connection.query('test', 'demo3') results = query.results(policy={'expressions': expr.compile()}) assert len(results) == 1 assert_each_record_bins( results, lambda b: all([key != 'Bob' for key in b['map']]))
def test_exists_many_with_large_expressions(self): ''' Proper call to exists_many with expressions in policy. ''' expr = exp.Or( exp.Eq(exp.IntBin('account_id'), 4), exp.Eq(exp.StrBin('user_name'), 'user3'), exp.LT( exp.ListGetByRank(None, aerospike.LIST_RETURN_VALUE, exp.ResultType.INTEGER, -1, 'charges'), 12)) matched_recs = [] records = self.as_connection.exists_many( self.keys, {'expressions': expr.compile()}) for rec in records: if rec[1] is not None: matched_recs.append(rec[1]) assert len(matched_recs) == 3
def test_scan_apply_with_correct_policy_and_invalid_expressions(self): """ Invoke scan_apply() with invalid expressions. """ expr = exp.Eq(exp.StrBin('name'), 4) policy = {'timeout': 1000, 'expressions': expr.compile()} with pytest.raises(e.InvalidRequest): scan_id = self.as_connection.scan_apply("test", None, "bin_lua", "mytransform", ['age', 2], policy)
def test_list_and_str(self): expr = exp.Eq( exp.ListGetByValue(None, aerospike.LIST_RETURN_COUNT, 'Bob', 'slist'), 0 ) # Only one friend list without Bob query = self.as_connection.query('test', 'demo2') results = query.results(policy={'expressions': expr.compile()}) assert len(results) == 1 assert_each_record_bins( results, lambda b: all([name != 'Bob' for name in b['slist']]))
def test_remove_bin_with_expressions(self): ''' Call remove_bin with expressions in policy. ''' expr = exp.Eq(exp.IntBin('account_id'), 1) self.as_connection.remove_bin(self.keys[0], ['account_id', 'user_name'], policy={'expressions': expr.compile()}) rec = self.as_connection.get(self.keys[0]) assert rec[2].get('account_id') is None and rec[2].get( 'user_name') is None
def test_put_new_record_with_expressions(self): # should this fail? ''' Call put a new record with expressions in policy. ''' expr = exp.Eq(exp.IntBin('account_id'), 1) key = ("test", "demo", 10) self.as_connection.put(key, {'newkey': 'newval'}, policy={'expressions': expr.compile()}) rec = self.as_connection.get(key) self.as_connection.remove(key) assert rec[2]['newkey'] == 'newval'
def test_select_with_expressions_filtered_out(self): ''' Call to select with expressions in policy with expected failures. ''' expr = exp.And( exp.Eq(exp.IntBin('acct_balance'), 20), exp.LT( exp.ListGetByRank(None, aerospike.LIST_RETURN_VALUE, exp.ResultType.INTEGER, -1, 'charges'), 10)) with pytest.raises(e.FilteredOut): self.as_connection.select(self.keys[1], ['account_id', 'acct_balance'], {'expressions': expr.compile()})
def test_select_with_expressions(self): ''' Call to select with expressions in policy. ''' expr = exp.And( exp.Eq(exp.IntBin('acct_balance'), 20), exp.LT( exp.ListGetByRank(None, aerospike.LIST_RETURN_VALUE, exp.ResultType.INTEGER, -1, 'charges'), 20)) result = self.as_connection.select(self.keys[1], ['account_id', 'acct_balance'], {'expressions': expr.compile()}) assert result[2]['account_id'] == 2 and result[2]['acct_balance'] == 20
def test_scan_apply_with_correct_policy_and_expressions(self): """ Invoke scan_apply() with correct policy. It should invoke the function on all records in the set that match the expressions. """ expr = exp.And( exp.Eq(exp.StrBin('name'), 'name4'), exp.NE(exp.IntBin('age'), 3), ) policy = {'timeout': 1000, 'expressions': expr.compile()} scan_id = self.as_connection.scan_apply("test", None, "bin_lua", "mytransform", ['age', 2], policy) wait_for_job_completion(self.as_connection, scan_id) for i in range(5): key = ('test', 'demo', i) _, _, bins = self.as_connection.get(key) if bins['name'] == 'name4': assert bins['age'] == i + 2 else: assert bins['age'] == i
def test_not(self): expr = exp.Not(exp.Eq(exp.IntBin('positive_i'), 5)) results = self.query.results(policy={'expressions': expr.compile()}) assert len(results) == 99 assert_each_record_bins(results, lambda b: b['positive_i'] != 5)
def test_integer_equals(self): expr = exp.Eq(exp.IntBin('positive_i'), 5) results = self.query.results(policy={'expressions': expr.compile()}) assert len(results) == 1 assert_each_record_bins(results, lambda b: b['positive_i'] == 5)
def test_with_eq_expr_too_big(self): expr = exp.Eq(1, 1 << 64) # This needs to be over 2 ^ 63 - 1 query = self.as_connection.query('test', 'or') with pytest.raises(e.ParamError): query.results(policy={'expressions': expr.compile()})