def product(self, product_id, user_id=None): query = self.client.query(self.namespace, data_type) if user_id: query.select('specialized') query.where(predicates.equals('id', product_id)) else: query.select('general') query.where(predicates.equals('id', _id)) return query.results()
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_predexp_and_predicate(self): """ Ensure that Scan.execute_background() gets applied to records that match the predicate NOTE: the predicate overrides the predexp """ test_bin = 'Stpredold' keys = [(TEST_NS, TEST_SET, i) for i in range(50)] predexp = [ as_predexp.integer_bin('number'), as_predexp.integer_value(2), as_predexp.integer_equal(), as_predexp.integer_bin('number'), as_predexp.integer_value(3), as_predexp.integer_equal(), as_predexp.predexp_or(2) ] number_predicate = predicates.equals('number', 4) policy = {'predexp': predexp} 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) # Give time for the scan to finish 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_with_ops_and_preds(self): """ Ensure that Scan.execute_background() applies ops to records that match the predicate """ test_bin = 'St1' keys = [(TEST_NS, TEST_SET, i) for i in range(50)] scan = self.as_connection.scan(TEST_NS, TEST_SET) number_predicate = predicates.equals('number', 3) # scan.apply(TEST_UDF_MODULE, TEST_UDF_FUNCTION, [test_bin]) ops = [operations.append(test_bin, 'new_val')] scan.add_ops(ops) scan.where(number_predicate) job_id = scan.execute_background() # Give time for the scan to finish wait_for_job_completion(self.as_connection, job_id) _, _, num_5_record = self.as_connection.get((TEST_NS, TEST_SET, 5)) assert num_5_record[test_bin] == 'aerospike' _, _, num_3_record = self.as_connection.get((TEST_NS, TEST_SET, 3)) assert num_3_record[test_bin] == 'aerospikenew_val' # cleanup ops = [operations.write(test_bin, 'aerospike')] scan.add_ops(ops) job_id = scan.execute_background() wait_for_job_completion(self.as_connection, job_id) validate_records(self.as_connection, keys, lambda rec: rec[test_bin] == 'aerospike')
def queryTweetsByUsername(self): print("\n********** Query Tweets By Username **********\n") # Get username username = str() username = input("Enter username: "******"test", "tweets", "username", "username_index", None) #time.sleep(5) #print("\nString Secondary Index Created ") # Create Query and Set equality Filter on username # Exercise Q3 tweetQuery = self.client.query("test", "tweets") # Select bin(s) you would like to retrieve tweetQuery.select('tweet') tweetQuery.where(p.equals('username',username)) # Define the Call back to print Tweets for given Username # Exercise Q3 def tweetQueryCallback(input_tuple): (key, meta, record)=input_tuple print(record["tweet"]) # Execute query and for each record invoke the callback # Exercise Q3 tweetQuery.foreach(tweetQueryCallback) except Exception as e : print("error: {0}".format(e), file=sys.stderr)
def test_query_with_put_in_callback(self): """ Invoke query() with put in callback """ policy = { 'timeout': 1000 } query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = [] def callback((key,metadata,record)): records.append(record) key = ('test', 'demo', 'put_in_callback') rec = { 'name' : 'name%s' % (str(8)), 'test_age' : 8, } TestQuery.client.put(key, rec) query.foreach(callback, policy) key = ('test', 'demo', 'put_in_callback') key1, meta, bins = TestQuery.client.get( key ) key = ('test', 'demo', 'put_in_callback') TestQuery.client.remove(key) assert bins == { 'test_age': 8, 'name': 'name8'}
def test_query_with_put_in_callback(self): """ Invoke query() with put in callback """ policy = {'timeout': 1000} query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = [] def callback(input_tuple): _, _, record = input_tuple records.append(record) key = ('test', 'demo', 'put_in_callback') rec = { 'name': 'name%s' % (str(8)), 'test_age': 8, } self.client.put(key, rec) query.foreach(callback, policy) key = ('test', 'demo', 'put_in_callback') _, _, bins = self.client.get(key) key = ('test', 'demo', 'put_in_callback') self.client.remove(key) assert bins == {'test_age': 8, 'name': 'name8'}
def aerospike_search_by_name(self): start = time.time() query = self.aero.query('uia-schedule', 'departments') query.where(p.equals('name', 'Pedagogikk masterprogram 2.sem, valgretning spesialpedagogikk')) data = query.results({'timeout' : 20000}) elapsed = time.time() - start self.stats['aerospike']['search_by_name'] = elapsed
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 aerospike_search_by_id(self): start = time.time() query = self.aero.query('uia-schedule', 'departments') query.where(p.equals('id', '#SPLUSE2E830')) data = query.results({'timeout' : 20000}) elapsed = time.time() - start self.stats['aerospike']['search_by_id'] = elapsed
def get_ltv_by_phone_query(phone_number): query = client.query(namespace, customerSet) query.where(p.equals("phone_number", phone_number)) query.select("lifetime_value") for record in query.results(): (key, meta, bins) = record return bins.get("lifetime_value") logging.error('Requested phone number is not found ' + str(phone_number))
def test_query_with_results_invalid_nobins_options(self): """ Invoke query() with correct arguments """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) with pytest.raises(e.ParamError): records = query.results(options={'nobins': "false"})
def get_ltv_by_phone(client, namespace, set, phone_number): try: results = client.query(namespace, set).select('phone', 'ltv').where(p.equals('phone', phone_number)).results() if len(results) > 0: return results[0][2]['ltv'] else: logging.error('Requested phone number \'{0}\' is not found'.format(phone_number)) except ex.AerospikeError as e: logging.error("Error: {0} [{1}]".format(e.msg, e.code))
def get_ltv_by_phone(phone_number, namespace='test', aero_set='demo'): query = client.query(namespace, aero_set) query.select('ltv') query.where(p.equals('phone', phone_number)) try: (key, metadata, record) = query.results()[0] return record['ltv'] except IndexError: logging.error('Requested phone number is not found ' + str(phone_number))
def get_ltv_by_phone(phone_number): query = client.query("test", "cust_set") query.where(p.equals("phone", phone_number)) query.select("ltv") print(query) for record in query.results(): (key, meta, bins) = record return bins.get("ltv") logging.error('Requested phone number is not found ' + str(phone_number))
def test_query_with_unicode_binnames_in_select_and_where(self): """ Invoke query() with unicode bin names in select """ query = TestQuery.client.query('test', 'demo') query.select(u'name', u'test_age', 'addr') query.where(p.equals(u'test_age', 7)) records = query.results() assert len(records) == 1 assert records[0][2] == {'test_age': 7, 'name': u'name7', 'addr': u'name7'} query = TestQuery.client.query('test', 'demo') query.select(u'name', 'addr') query.where(p.equals(u'addr', u'name9')) records = query.results() assert records[0][2] == {'name': 'name9', 'addr': u'name9'}
def manager(self, data_type, _id=None): if _id: query = self.client.query(self.namespace, data_type) query.where(predicates.equals('id', _id)) return query.results() else: # return all results all_objects = self.client.scan(self.namespace, data_type) data = all_objects.results() return data
def test_query_with_results_nobins_options(self): """ Invoke query() with correct arguments """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = query.results(options={'nobins': True}) assert len(records) == 1
def test_query_with_results_method(self): """ Invoke query() with correct arguments """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = query.results() assert len(records) == 1
def get_ltv_by_phone(client, phone_number): query = client.query('test', 'customer') query.select('ltv') query.where(p.equals('phone_number', phone_number)) records = query.results() for record in records: return (record[2]['ltv']) logging.error( 'Requested phone number %s is not found in (\'test\',\'customer\')' % str(phone_number))
def test_query_with_where_none_value(self): """ Invoke query() with where is null value """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') with pytest.raises(Exception) as exception: query.where(p.equals('test_age', None)) assert exception.value[0] == -2L assert exception.value[1] == 'predicate is invalid.'
def get_ltv_by_phone(phone_number): query = client.query('test', 'demo') query.select('ltv') query.where(p.equals('phone', phone_number)) ltvs = [] def matched_names((key, metadata, bins)): ltvs.append(bins['ltv']) query.foreach(matched_names, {'total_timeout':2000} ) return ltvs
def get_ltv_by_phone(self, phone_number): """ Get LTV value using phone_number""" query = self.client.query(self.key['namespace'], self.key['set']) try: (key, meta, record) = query.where(p.equals('phone', phone_number)).select('ltv').results()[0] logging.debug(record) except ex.AerospikeError as e: logging.error('Requested phone number is not found ' + str(phone_number)) logging.debug(e) record = {'ltv': None} return record['ltv']
def get_ltv_by_phone(phone_number): query = client.query("test", "customers").select("phone_number", "ltv") query.where(p.equals("phone_number", phone_number)) try: results = query.results() except exception.IndexNotFound: logging.error("There is no secondary index defined for 'phone_number'") return None if results: return results[0][2].get("ltv") logging.error( "Record with phone_number → {} was not found!".format(phone_number))
def test_query_with_where_none_value(self): """ Invoke query() with where is null value """ query = self.client.query('test', 'demo') query.select('name', 'test_age') try: query.where(p.equals('test_age', None)) except e.ParamError as exception: assert exception.code == -2 assert exception.msg == 'predicate is invalid.'
def test_query_with_where_none_value(self): """ Invoke query() with where is null value """ query = self.client.query('test', 'demo') query.select('name', 'test_age') try: query.where(p.equals('test_age', None)) except ParamError as exception: assert exception.code == -2L assert exception.msg == 'predicate is invalid.'
def get_ltv_by_phone(phone_number): try: query = _client.query(_namespace, _set) records = query.select('phone', 'ltv').where(p.equals('phone', phone_number)).results() if len(records) > 0: #print(records) #print(records[0][2]) return records[0][2]['ltv'] else: logging.error('Requested phone number is not found {0}'.format(str(phone_number))) except ex.AerospikeError as e: logging.error('error: {0}'.format(e))
def test_query_with_incorrect_bin_name(self): """ Invoke query() with incorrect bin name """ query = TestQuery.client.query('test', 'demo') query.select('name1', 'age1') query.where(p.equals('age1', 1)) records = [] def callback((key,metadata,record)): records.append(record) query.foreach(callback) assert len(records) == 0
def get_ltv_by_phone(phone_number): try: # Создаем запрос для поиска телефона # Оказывается, однажды созданный экземпляр Query при последующих вызовах других select и where никак на это не реагирует. # Приходится создавать новый, освобождая существующий. aeroQuery = client.query(aeroNameSpace, aeroSet) aeroQuery.select( "lifetime_value" ) aeroQuery.where( predicates.equals("phone_number", phone_number) ) records = aeroQuery.results( {'total_timeout':2000}) (dummy1, dummy2, pDict) = records[0] # first found string return pDict.get( "lifetime_value" ) except Exception as e: logging.error('Requested phone number is not found ' + str(phone_number))
def test_query_with_unicode_binnames_in_select_and_where(self): """ Invoke query() with unicode bin names in select """ query = self.client.query('test', 'demo') query.select(u'name', u'test_age', 'addr') query.where(p.equals(u'test_age', 7)) records = query.results() assert len(records) == 1 assert records[0][2] == { 'test_age': 7, 'name': u'name7', 'addr': u'name7' } query = self.client.query('test', 'demo') query.select(u'name', 'addr') query.where(p.equals(u'addr', u'name9')) records = query.results() assert records[0][2] == {'name': 'name9', 'addr': u'name9'}
def test_query_with_where_incorrect(self): """ Invoke query() with where is incorrect """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 165)) records = [] def callback((key,metadata,record)): records.append(record) query.foreach(callback) assert len(records) == 0
def as_select_by_columns(self, table, where_field=None, where_value=None, columns=None): query = self.client.query(self.namespace, table) if type(columns) is list or type(columns) is set: query.select(*columns) elif not columns is None: query.select(columns) if where_field: query.where(p.equals(where_field, where_value)) return query
def test_aggregate_with_where_equals_value(self): """ Invoke aggregate() with where is equal """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 2)) query.apply('stream_example', 'count') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert records[0] == 1
def test_query_with_multiple_foreach_on_same_query_object(self): """ Invoke query() with multple foreach() call on same query object """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = [] def callback((key,metadata,record)): records.append(key) query.foreach(callback) assert len(records) == 1
def test_query_with_incorrect_bin_name(self): """ Invoke query() with incorrect bin name """ query = self.client.query('test', 'demo') query.select('name1', 'age1') query.where(p.equals('age1', 1)) records = [] def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 0
def test_query_with_invalid_nobins_value(self): """ Invoke query() with options['nobins'] type """ policy = {'total_timeout': 1000} query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback(input_tuple): pass with pytest.raises(e.ParamError): query.foreach(callback, policy, {'nobins': 'False'})
def get_ltv_by_phone(phone_number): query = client.query("test", "demo") query.select("ltv") query.where(p.equals("phone", phone_number)) ltvs = [] def matches_phone(tuple_1): key, metadata, bins = tuple_1 ltvs.append(bins["ltv"]) query.foreach(matches_phone, {"total_timeout": 2000}) return ltvs[0]
def test_query_with_where_incorrect(self): """ Invoke query() with where is incorrect """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 165)) records = [] def callback((key, metadata, record)): records.append(record) query.foreach(callback) assert len(records) == 0
def test_query_without_callback_parameter(self): """ Invoke query() with without callback """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback((key,metadata,record)): assert metadata['gen'] != None with pytest.raises(TypeError) as typeError: query.foreach() assert "Required argument 'callback' (pos 1) not found" in typeError.value
def test_query_with_nonindexed_bin(self): """ Invoke query() with non-indexed bin """ with pytest.raises(Exception) as exception: query = TestQuery.client.query('test', 'demo') query.select('name', 'no') query.where(p.equals('no', 1)) def callback((key,metadata,record)): assert metadata['gen'] != None query.foreach(callback) assert exception.value[0] == 201L assert exception.value[1] == 'AEROSPIKE_ERR_INDEX_NOT_FOUND'
def test_query_with_where_none_value(self): """ Invoke query() with equality predicate comparing to None This is more of a predicate test """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') try: query.where(p.equals('test_age', None)) except e.ParamError as exception: assert exception.code == -2 assert exception.msg == 'predicate is invalid.'
def test_query_with_invalid_options_argument_type(self): """ Invoke query() with incorrect options type, should be a dictionary """ policy = {'timeout': 1000} query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback(input_tuple): _, metadata, _ = input_tuple assert metadata['gen'] is None with pytest.raises(e.ParamError): query.foreach(callback, policy, "")
def test_query_with_correct_parameters(self): """ Invoke query() with correct arguments """ query = TestQuery.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = [] def callback((key,metadata,record)): records.append(key) query.foreach(callback) assert records assert len(records) == 1
def test_neg_aggregate_with_where_incorrect(self): """ Invoke aggregate() with where is incorrect """ query = self.as_connection.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 165)) query.apply('stream_example', 'count') records = [] def user_callback(value): records.append(value) query.foreach(user_callback) assert records == []
def test_query_with_incorrect_bin_name(self): """ Invoke query() with incorrect bin name """ query = self.client.query('test', 'demo') query.select('name1', 'age1') query.where(p.equals('age1', 1)) records = [] def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 0
def test_query_with_policy(self): """ Invoke query() with policy """ policy = {'timeout': 1000} query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = [] def callback((key, metadata, record)): records.append(record) query.foreach(callback, policy) assert len(records) == 1
def test_query_with_where_incorrect(self): """ Invoke query() with where is incorrect """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 165)) records = [] def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 0
def test_query_with_incorrect_bin_name(self): """ Invoke query() with predicate comparing using non-extant bin name """ query = self.as_connection.query('test', 'demo') query.select('name1', 'age1') query.where(p.equals('age1', 1)) records = [] def callback(input_tuple): _, _, record = input_tuple records.append(record) query.foreach(callback) assert len(records) == 0
def test_query_with_multiple_foreach_on_same_query_object(self): """ Invoke query() with multple foreach() call on same query object """ query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) records = [] def callback((key, metadata, record)): records.append(key) query.foreach(callback) assert len(records) == 1
def test_query_with_incorrect_ns_set(self): """ Invoke query() with incorrect ns and set """ with pytest.raises(Exception) as exception: query = TestQuery.client.query('test1', 'demo1') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback((key,metadata,record)): assert metadata['gen'] != None query.foreach(callback) assert exception.value[0] == 4L assert exception.value[1] == 'AEROSPIKE_ERR_REQUEST_INVALID'
def test_query_with_ns_not_string(self): """ Invoke query() with incorrect ns and set """ try: query = self.client.query(1, 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback((key,metadata,record)): assert metadata['gen'] != None query.foreach(callback) except ParamError as exception: assert exception.code == -2L assert exception.msg == 'Namespace should be a string'
def test_query_with_select_bytearray(self): """ Invoke query() with correct arguments """ query = self.client.query('test', 'demo') query.select(bytearray("sal\0kj", "utf-8")) query.where(p.equals(bytearray("sal\0kj", "utf-8"), 80000)) records = [] def callback((key, metadata, record)): records.append(key) query.foreach(callback) assert records assert len(records) == 1
def test_query_with_set_int(self): """ Invoke query() with incorrect ns and set """ try: query = self.client.query('test', 1) query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback((key,metadata,record)): assert metadata['gen'] != None query.foreach(callback) except ParamError as exception: assert exception.code == -2L assert exception.msg == 'Set should be string, unicode or None'
def test_query_with_extra_argument(self): """ Invoke query() with extra argument """ policy = {'timeout': 1000} query = self.client.query('test', 'demo') query.select('name', 'test_age') query.where(p.equals('test_age', 1)) def callback((key, metadata, record)): assert metadata['gen'] != None with pytest.raises(TypeError) as typeError: query.foreach(callback, policy, "") assert "foreach() takes at most 2 arguments (3 given)" in typeError.value
def test_query_with_multiple_results_call_on_same_query_object(self): """ Invoke query() with multple results() call on same query object """ query = TestQuery.client.query('test', 'demo') query.select(u'name', u'test_age', 'addr') query.where(p.equals(u'test_age', 7)) records = query.results() assert len(records) == 1 assert records[0][2] == {'test_age': 7, 'name': u'name7', 'addr': u'name7'} records = [] records = query.results() assert len(records) == 1 assert records[0][2] == {'test_age': 7, 'name': u'name7', 'addr': u'name7'}