def test_where_dict(self): query = OJAIQuery().where({'$ne': {'name': 'Joh'}}).build() self.assertEqual({'$where': { '$ne': { 'name': 'Joh' } }}, query.query_dict())
def test_where_json_str(self): query = OJAIQuery().where( "{\"$eq\": {\"address.zipCode\": 95196}}").build() self.assertEqual({'$where': { '$eq': { 'address.zipCode': 95196 } }}, query.query_dict())
def test_order_by(self): query = OJAIQuery().order_by('name').build() self.assertEqual(query.query_dict(), {'$orderby': {'name': 'asc'}}) query2 = OJAIQuery().order_by(['name', 'age']).build() self.assertEqual(query2.query_dict(), {'$orderby': [{ 'name': 'asc' }, { 'age': 'asc' }]}) query3 = OJAIQuery().order_by(['address.city', 'address.postal_code'], 'desc').build() self.assertEqual( query3.query_dict(), { '$orderby': [{ 'address.city': 'desc' }, { 'address.postal_code': 'desc' }] })
def test_empty_where(self): with self.assertRaises(AttributeError): OJAIQuery().where({}).build() with self.assertRaises(AttributeError): OJAIQuery().where('').build() with self.assertRaises(AttributeError): OJAIQuery().where(u'').build() with self.assertRaises(AttributeError): OJAIQuery().where(OJAIQueryCondition().build()).build()
def test_simple_find(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path='/find-test-store1'): document_store = connection.get_store( store_path='/find-test-store1') else: document_store = connection.create_store( store_path='/find-test-store1') document = connection.new_document( dictionary={ '_id': 'id008', 'test_int': 51, 'test_str': 'strstr', 'test_dict': { 'test_int': 5 }, 'test_list': [5, 6], 'test_null': None }) document_store.insert_or_replace(doc=document) query = OJAIQuery().select([ '_id', 'test_int', 'test_str', 'test_dict', 'test_list', 'test_null' ]).build() self.assertTrue(connection.is_store_exists('/find-test-store1')) doc_stream = document_store.find(query) for doc in doc_stream: self.assertEqual(doc, document.as_dictionary())
def test_find_on_empty_table(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path='/find-test-store2'): document_store = connection.get_store( store_path='/find-test-store2') else: document_store = connection.create_store( store_path='/find-test-store2') query = OJAIQuery().select(['_id', 'test_int', 'test_str', 'test_dict', 'test_list', 'test_null'])\ .build() self.assertTrue(connection.is_store_exists('/find-test-store2')) doc_stream = document_store.find(query) size = 0 for doc in doc_stream: size += 1 print(doc) self.assertEqual(size, 0)
def test_full_query(self): qc = OJAIQueryCondition().and_() \ .condition_(OJAIQueryCondition() .or_() .is_('age', QueryOp.GREATER_OR_EQUAL, 22) .is_('city', QueryOp.EQUAL, 'NY').close()) \ .condition_(OJAIQueryCondition() .and_() .is_('age', QueryOp.GREATER_OR_EQUAL, 18) .is_('city', QueryOp.EQUAL, 'London').close()).close().build() query = OJAIQuery().select( 'name', 'age', 'city').where(qc).order_by('name').offset(500).limit(5).build() self.assertEqual( query.query_dict(), { '$orderby': { 'name': 'asc' }, '$offset': 500, '$select': ['name', 'age', 'city'], '$limit': 5, '$where': { '$and': [{ '$or': [{ '$ge': { 'age': 22 } }, { '$eq': { 'city': 'NY' } }] }, { '$and': [{ '$ge': { 'age': 18 } }, { '$eq': { 'city': 'London' } }] }] } })
def test_incompatible_str_type(self): from mapr.ojai.ojai_query.OJAIQuery import OJAIQuery from mapr.ojai.ojai_query.OJAIQueryCondition import OJAIQueryCondition id_field = '_id' try: condition = OJAIQueryCondition().equals_(id_field, id_field).close().build() query = OJAIQuery().select([id_field]) except BaseException: self.fail("Probably incompatible str type.")
def test_find_table_not_found(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) query = OJAIQuery().select(['_id', 'test_int', 'test_str', 'test_dict', 'test_list', 'test_null'])\ .build() connection.delete_store('/find-test-store3') self.assertFalse(connection.is_store_exists('/find-test-store3')) with self.assertRaises(StoreNotFoundError): document_store = connection.get_store( store_path='/find-test-store3') query_result = document_store.find(query) for doc in query_result: print(doc)
def test_find_multiple_records(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) if connection.is_store_exists(store_path='/find-test-store4'): document_store = connection.get_store( store_path='/find-test-store4') else: document_store = connection.create_store( store_path='/find-test-store4') document_list = [] for i in range(1, 10): document_list.append( connection.new_document( dictionary={ '_id': 'id00%s' % i, 'test_int': i, 'test_str': 'strstr', 'test_dict': { 'test_int': i }, 'test_list': [5, 6], 'test_null': None })) document_store.insert_or_replace(doc_stream=document_list) query = OJAIQuery().select([ '_id', 'test_int', 'test_str', 'test_dict', 'test_list', 'test_null' ]).build() doc_stream = document_store.find(query) index = 0 for doc in doc_stream: self.assertEqual(doc, document_list[index].as_dictionary()) index += 1
def test_find_with_condition(self): connection = ConnectionFactory.get_connection( connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS) document_list = [] for i in range(3, 7): document_list.append( connection.new_document( dictionary={ '_id': 'id00%s' % i, 'test_int': i, 'test_str': 'strstr', 'test_dict': { 'test_int': i }, 'test_list': [5, 6], 'test_null': None })) if connection.is_store_exists(store_path='/find-test-store4'): document_store = connection.get_store( store_path='/find-test-store4') else: document_store = connection.create_store( store_path='/find-test-store4') query = OJAIQuery().select(['_id', 'test_int', 'test_str', 'test_dict', 'test_list', 'test_null']) \ .where(OJAIQueryCondition() .and_() .is_('test_int', QueryOp.GREATER_OR_EQUAL, 3) .is_('test_int', QueryOp.LESS_OR_EQUAL, 6).close().close().build()).build() doc_stream = document_store.find(query) index = 0 for doc in doc_stream: self.assertEqual(doc, document_list[index].as_dictionary()) index += 1
def new_query(self, query_json=None): ojai_query = OJAIQuery() if query_json is not None: ojai_query.from_json(query_json) return ojai_query
def test_empty_value_order_by(self): with self.assertRaises(TypeError): OJAIQuery().order_by('').build().to_json_str() with self.assertRaises(TypeError): OJAIQuery().order_by([]).build().to_json_str()
def test_where_json_str_format(self): query = OJAIQuery().where( "{\"$eq\": {\"address.zipCode\": 95196}}").build() self.assertEqual('{"$where": {"$eq": {"address.zipCode": 95196}}}', query.to_json_str())
def test_offset_bool(self): with self.assertRaises(TypeError): query = OJAIQuery().offset(True).build()
def test_empty_query(self): query = OJAIQuery().build() self.assertEqual(query.query_dict(), {}) self.assertEqual(query.to_json_str(), '{}')
def test_order_by_empty(self): with self.assertRaises(TypeError): OJAIQuery().order_by([]).build() with self.assertRaises(TypeError): OJAIQuery().order_by('').build()
def test_simple_query(self): query = OJAIQuery().select([True, 5, 123123124123]).build() self.assertEqual(query.query_dict(), {'$select': ['True', '5', '123123124123']})