def test_paging(self): print "\nTest paging\n" q = Query(regex('name', '.*')) q.order({'name': ASCENDING}) q.limit(1) expected = sorted([record['name'] for record in self.data]) actual = list() try: for i, record in enumerate(expected): q.offset(i) result = self.test_collection.find(q.build()) self.assertEqual( len(result.raw_data), 1, "Only expected 1 result {0}".format(result.raw_data)) actual.append(result.raw_data[0]['name']) except KeyError as e: self.fail("Unexpected results, {0}".format(e)) self.assertEqual( expected, actual, "Result set not ordered, [{0}]".format(', '.join(actual)))
def test_grouping_by_field(self): print '\nTest grouping by field\n' q = Query(regex('name', '.*')) q.add_grouping(GroupSpec(field='name')) result = self.test_collection.find(q.build()) self.assertTrue(len(result.groups) > 0)
def group_by_queries(field, regex1, regex2): print '\nGroup by query for field, {field}'.format(field=field) q = Query(regex(field, '.*')) group = GroupSpec(sort={'metric': 'asc'}) group.add_group_query(regex(field, regex1)) group.add_group_query(regex(field, regex2)) q.add_grouping(group) query_pprint(q.build()) query = json.dumps(q.build()) print '\n' + curl('PUT', 'query/all', data=query) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def test_categorize_by_query(self): q = Query(regex('name', '.*')) cat_spec = CategorizeSpec() cat_spec.add_categorize_query(eq('name', 'Dan')) q.add_categorization(cat_spec) q.limit(0) result = self.test_collection.find(q.build()) self.assertTrue(len(result.categories) > 0)
def test_result_iter(self): print "\nTest iteration on paged result set\n" q = Query(regex('name', '.*')) q.limit(1) expected_count = len(self.data) result = self.test_collection.find(q.build()) count = len(list(result.objects())) self.assertEqual(count, expected_count, "Expected {0} iterations, got {1}".format(expected_count, count))
def categorize_by_query(field, regex1, regex2): print '\nCategorize by range for field, {field}'.format(field=field) q = Query(regex(field, '.*')) q.limit(0) category = CategorizeSpec() category.add_categorize_query(regex(field, regex1)) category.add_categorize_query(regex(field, regex2)) q.add_categorization(category) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def find_regex(field, value): print '\n Query with regex, {regex}'.format(regex=value) q = Query(regex(field, value)) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def find_and(field, value, low_field, low_value, high_field, high_value): print '\n Query with boolean and' q = Query(and_args(regex(field, value), gte(low_field, low_value), lte(high_field, high_value))) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def test_categorize_with_stats(self): print '\nTest categorize with stats\n' q = Query(regex('name', '.*')) cat_spec = CategorizeSpec() cat_spec.stats = StatsSpec('name', 'age') q.add_categorization(cat_spec) q.limit(0) result = self.test_collection.find(q.build()) self.assertTrue(len(result.categories) > 0)
def test_categorize_by_field(self): print '\nTest categorize by field\n' q = Query(regex('name', '.*')) cat_spec = CategorizeSpec() cat_spec.field = 'name' q.add_categorization(cat_spec) q.limit(0) result = self.test_collection.find(q.build()) self.assertTrue(len(result.categories) > 0)
def test_result_iter(self): print "\nTest iteration on paged result set\n" q = Query(regex('name', '.*')) q.limit(1) expected_count = len(self.data) result = self.test_collection.find(q.build()) count = len(list(result.objects())) self.assertEqual( count, expected_count, "Expected {0} iterations, got {1}".format(expected_count, count))
def test_sort(self): print "\nTest sorting\n" q = Query(regex('name', '.*')) q.order({'name': ASCENDING}) result = self.test_collection.find(q.build()) try: ordered = [record['name'] for record in result.raw_data] self.assertEqual(ordered, sorted([record['name'] for record in self.data]), "Result set not ordered, [{0}]".format(', '.join(ordered))) except KeyError as e: self.fail("Unexpected results, {0}, {1}".format(e, result))
def find_and(field, value, low_field, low_value, high_field, high_value): print '\n Query with boolean and' q = Query( and_args(regex(field, value), gte(low_field, low_value), lte(high_field, high_value))) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def test_sort(self): print "\nTest sorting\n" q = Query(regex('name', '.*')) q.order({'name': ASCENDING}) result = self.test_collection.find(q.build()) try: ordered = [record['name'] for record in result.raw_data] self.assertEqual( ordered, sorted([record['name'] for record in self.data]), "Result set not ordered, [{0}]".format(', '.join(ordered))) except KeyError as e: self.fail("Unexpected results, {0}, {1}".format(e, result))
def test_categorize_by_range(self): print '\nTest categorize by range' q = Query(regex('name', '.*')) range_spec = RangeSpec('age', 1, 10, 1) cat_spec = CategorizeSpec() cat_spec.range_spec = range_spec q.add_categorization(cat_spec) q.limit(0) result = self.test_collection.find(q.build()) self.assertTrue(len(result.categories) > 0)
def group_by_field(query_field, field): print '\nGroup by value for field, {field}'.format(field=field) q = Query(regex(query_field, '.*')) group = GroupSpec(field=field, limit=10, sort={'metric': 'asc'}) q.add_grouping(group) query_pprint(q.build()) query = json.dumps(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def categorize_with_stats(field, stat_field): print '\nCategorize with stats on field, {stat_field}'.format(stat_field=stat_field) q = Query(regex(field, '.*')) q.limit(0) category = CategorizeSpec() category.stats = StatsSpec(field, stat_field) q.add_categorization(category) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def partial_name_search(self, name_prefix, start_year=MAX_YEAR, stop_year=MAX_YEAR): q = Query(and_args(regex('name', name_prefix + '.*'), gte('year', start_year), lte('year', stop_year))) q.limit(0) categorize = CategorizeSpec() categorize.stats = StatsSpec('name', 'count') q.add_categorization(categorize) #print q.build() result = self.names.find(q.build()) name_count = dict() for field, stat in result.categories['name'].items(): name_count[field] = stat['sum'] return name_count
def categorize_with_stats(field, stat_field): print '\nCategorize with stats on field, {stat_field}'.format( stat_field=stat_field) q = Query(regex(field, '.*')) q.limit(0) category = CategorizeSpec() category.stats = StatsSpec(field, stat_field) q.add_categorization(category) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def categorize_by_range(query_field, field, start, end, increment): print '\nCategorize by range for field, {field}'.format(field=field) q = Query(regex(query_field, '.*')) q.limit(0) category = CategorizeSpec() _range = RangeSpec(field, start, end, increment) category.range_spec = _range q.add_categorization(category) query = json.dumps(q.build()) query_pprint(q.build()) print '\n' + curl('PUT', 'query/all', data=query, accept_header=True) result = demo_collection.find(q.build()) json_pprint(result.response_doc)
def test_paging(self): print "\nTest paging\n" q = Query(regex('name', '.*')) q.order({'name': ASCENDING}) q.limit(1) expected = sorted([record['name'] for record in self.data]) actual = list() try: for i, record in enumerate(expected): q.offset(i) result = self.test_collection.find(q.build()) self.assertEqual(len(result.raw_data), 1, "Only expected 1 result {0}".format(result.raw_data)) actual.append(result.raw_data[0]['name']) except KeyError as e: self.fail("Unexpected results, {0}".format(e)) self.assertEqual(expected, actual, "Result set not ordered, [{0}]".format(', '.join(actual)))
def _delete_all_records(): q = Query(regex('name', '.*')) result = demo_collection.find(q.build()) for obj in result.objects(): del_result = demo_collection.delete(obj['_id']) print obj['_id'], del_result