def test_not_in_condition(self):
     qc = OJAIQueryCondition().not_in_(
         'age', [20, 21, 22, 23, 24, 25]).close().build()
     self.assertEqual(qc.as_dictionary(),
                      {'$notin': {
                          'age': [20, 21, 22, 23, 24, 25]
                      }})
 def test_query_condition_multiple_is(self):
     qc = OJAIQueryCondition().is_('age', QueryOp.GREATER_OR_EQUAL, 55).is_('age', QueryOp.NOT_EQUAL, 18) \
         .close().build()
     self.assertEqual(qc.as_dictionary(), {
         '$ge': {
             'age': 55
         },
         '$ne': {
             'age': 18
         }
     })
    def test_empty_condition(self):
        qc = OJAIQueryCondition()
        self.assertTrue(qc.is_empty())

        qc.is_('name', QueryOp.EQUAL, 'Doe').close()
        self.assertTrue(qc.is_empty())

        qc.build()
        self.assertFalse(qc.is_empty())
예제 #4
0
    def test_check_and_update(self):
        connection = ConnectionFactory.get_connection(
            connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS)

        if connection.is_store_exists(store_path=UpdateTest.store_name):
            document_store = connection.get_store(
                store_path=UpdateTest.store_name)
        else:
            document_store = connection.create_store(
                store_path=UpdateTest.store_name)
        self.assertEqual(
            {
                '_id': 'id02',
                'mystr': 'str',
                'test_int': 51,
                'test_str': 'strstr'
            }, document_store.find_by_id('id02'))
        mutation = OJAIDocumentMutation().set_or_replace(
            'new_field', {
                'd': 55,
                'g': 'text'
            })
        from mapr.ojai.ojai_query.OJAIQueryCondition import OJAIQueryCondition
        false_condition = OJAIQueryCondition().equals_(
            'test_str', 'rtsrts').close().build()
        self.assertFalse(
            document_store.check_and_update('id02',
                                            mutation=mutation,
                                            query_condition=false_condition))
        self.assertEqual(
            {
                '_id': 'id02',
                'mystr': 'str',
                'test_int': 51,
                'test_str': 'strstr'
            }, document_store.find_by_id('id02'))
        true_condition = OJAIQueryCondition().equals_(
            'test_str', 'strstr').close().build()
        document_store.check_and_update('id02',
                                        mutation=mutation,
                                        query_condition=true_condition)
        self.assertEqual(
            {
                '_id': 'id02',
                'mystr': 'str',
                'test_int': 51,
                'test_str': 'strstr',
                'new_field': {
                    'd': 55,
                    'g': 'text'
                }
            }, document_store.find_by_id('id02'))
 def test_eq_condition(self):
     qc = OJAIQueryCondition().and_().not_equals_('name', 'Joh').equals_(
         'name', 'David').close().build()
     self.assertEqual(
         qc.as_dictionary(),
         {'$and': [{
             '$ne': {
                 'name': 'Joh'
             }
         }, {
             '$eq': {
                 'name': 'David'
             }
         }]})
    def test_check_and_delete_false_condition(self):
        connection = ConnectionFactory.get_connection(
            connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS)
        if connection.is_store_exists(store_path='/check-delete-test-store1'):
            document_store = connection.get_store(
                store_path='/check-delete-test-store1')
        else:
            document_store = connection.create_store(
                store_path='/check-delete-test-store1')

        before_action = document_store.find_by_id('id07')
        self.assertEqual(before_action, {
            '_id': 'id07',
            'test_int': 51,
            'test_str': 'strstr'
        })
        document_store.check_and_delete(
            'id06',
            OJAIQueryCondition().is_('test_int', QueryOp.LESS_OR_EQUAL,
                                     50).close().build())
        after_action = document_store.find_by_id('id07')
        self.assertEqual(after_action, {
            '_id': 'id07',
            'test_int': 51,
            'test_str': 'strstr'
        })
예제 #7
0
    def test_check_and_replace_false_condition(self):
        connection = ConnectionFactory.get_connection(
            connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS)
        if connection.is_store_exists(store_path='/check-replace-test-store1'):
            document_store = connection.get_store(
                store_path='/check-replace-test-store1')
        else:
            document_store = connection.create_store(
                store_path='/check-replace-test-store1')

        before_action = document_store.find_by_id('id06')
        self.assertEqual(before_action, {
            '_id': 'id06',
            'new_field': 123,
            'new_array': [1, 2, 3]
        })

        new_document = connection.new_document().set_id('id06').set(
            'false_field', 321).set('false_array', [5, 5, 5])
        condition = OJAIQueryCondition().or_().is_('test_int', QueryOp.GREATER_OR_EQUAL, 60)\
            .is_('test_str', QueryOp.EQUAL, 'falsestr')\
            .close().build()
        document_store.check_and_replace(new_document, condition)
        after_action = document_store.find_by_id('id06')
        self.assertEqual(after_action, {
            '_id': 'id06',
            'new_field': 123,
            'new_array': [1, 2, 3]
        })
    def test_check_and_delete(self):
        connection = ConnectionFactory.get_connection(
            connection_str=CONNECTION_STR, options=CONNECTION_OPTIONS)
        if connection.is_store_exists(store_path='/check-delete-test-store1'):
            document_store = connection.get_store(
                store_path='/check-delete-test-store1')
        else:
            document_store = connection.create_store(
                store_path='/check-delete-test-store1')

        for doc in DICT_STREAM:
            document_store.insert_or_replace(doc=connection.new_document(
                dictionary=doc))

        before_action = document_store.find_by_id('id06')
        self.assertEqual(before_action, {
            '_id': 'id06',
            'test_int': 51,
            'test_str': 'strstr'
        })
        document_store.check_and_delete(
            'id06',
            OJAIQueryCondition().is_('test_int', QueryOp.LESS_OR_EQUAL,
                                     52).close().build())
        after_action = document_store.find_by_id('id06')
        self.assertEqual(after_action, {})
    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'
                            }
                        }]
                    }]
                }
            })
예제 #10
0
    def test_condition(self):
        query_condition = OJAIQueryCondition() \
            .and_() \
            .is_('age', QueryOp.GREATER_OR_EQUAL, 18) \
            .is_('city', QueryOp.EQUAL, 'London').close().close()

        query_condition.build()
        self.assertEqual(
            query_condition.as_dictionary(),
            {'$and': [{
                '$ge': {
                    'age': 18
                }
            }, {
                '$eq': {
                    'city': 'London'
                }
            }]})
예제 #11
0
    def test_and_with_separate_is(self):
        qc = OJAIQueryCondition() \
            .and_() \
            .and_() \
            .is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18) \
            .is_(field_path='city', op=QueryOp.EQUAL, value='London') \
            .close() \
            .and_() \
            .is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22) \
            .is_(field_path='city', op=QueryOp.EQUAL, value='NY') \
            .close() \
            .is_('card', QueryOp.EQUAL, 'visa') \
            .close()

        qc.build()

        self.assertEqual(
            qc.as_dictionary(), {
                '$and': [{
                    '$and': [{
                        '$ge': {
                            'age': 18
                        }
                    }, {
                        '$eq': {
                            'city': 'London'
                        }
                    }]
                }, {
                    '$and': [{
                        '$ge': {
                            'age': 22
                        }
                    }, {
                        '$eq': {
                            'city': 'NY'
                        }
                    }]
                }, {
                    '$eq': {
                        'card': 'visa'
                    }
                }]
            })
    def test_find_by_id_with_condition(self):
        connection = ConnectionFactory.get_connection(connection_str=CONNECTION_STR,
                                                      options=CONNECTION_OPTIONS)

        if connection.is_store_exists(store_path='/find-by-id-test-store1'):
            document_store = connection.get_store(store_path='/find-by-id-test-store1')
        else:
            document_store = connection.create_store(store_path='/find-by-id-test-store1')

        self.assertTrue(connection.is_store_exists('/find-by-id-test-store1'))

        condition = OJAIQueryCondition().exists_('false_field').close().build()
        doc = document_store.find_by_id('id008', condition=condition, results_as_document=True)
        self.assertEqual(doc.as_dictionary(), {})
        condition = OJAIQueryCondition().exists_('test_list').close().build()
        doc = document_store.find_by_id('id008', condition=condition, results_as_document=True,
                                        field_paths=['test_null', 'test_dict', '_id', 'test_str'])
        self.assertEqual(doc.as_dictionary(),
                         {'_id': 'id008', 'test_dict': {'test_int': 5},
                          'test_null': None, 'test_str': 'strstr'})
예제 #13
0
    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.")
예제 #14
0
 def test_element_and(self):
     query_condition = OJAIQueryCondition() \
         .element_and('grades[]') \
         .equals_('dsc', 'history') \
         .equals_('ev', 12) \
         .close() \
         .build()
     self.assertEqual(
         {
             '$elementAnd': {
                 'grades[]': [{
                     '$eq': {
                         'dsc': 'history'
                     }
                 }, {
                     '$eq': {
                         'ev': 12
                     }
                 }]
             }
         }, query_condition.as_dictionary())
예제 #15
0
    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()
예제 #16
0
    def test_query_condition_dict(self):
        condition_to_add = OJAIQueryCondition() \
            .or_() \
            .is_('age', QueryOp.GREATER_OR_EQUAL, 21) \
            .is_('city', QueryOp.EQUAL, 'NY') \
            .close().build()

        qc = OJAIQueryCondition() \
            .or_() \
            .condition_(condition_to_add.as_dictionary()) \
            .is_('age', QueryOp.GREATER_OR_EQUAL, 18).close().build()

        self.assertEqual(
            qc.as_dictionary(), {
                '$or': [{
                    '$or': [{
                        '$ge': {
                            'age': 21
                        }
                    }, {
                        '$eq': {
                            'city': 'NY'
                        }
                    }]
                }, {
                    '$ge': {
                        'age': 18
                    }
                }]
            })
예제 #17
0
    def test_nested_and(self):
        qc = OJAIQueryCondition().and_() \
            .is_(field_path='card', op=QueryOp.NOT_EQUAL, value="visa") \
            .or_() \
            .is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22) \
            .is_(field_path='city', op=QueryOp.EQUAL, value='NY') \
            .and_() \
            .is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18) \
            .is_(field_path='city', op=QueryOp.EQUAL, value='London').close().close().close().build()

        self.assertEqual(
            qc.as_dictionary(), {
                '$and': [{
                    '$ne': {
                        u'card': u'visa'
                    }
                }, {
                    '$or': [{
                        '$ge': {
                            u'age': 22
                        }
                    }, {
                        '$eq': {
                            u'city': u'NY'
                        }
                    }, {
                        '$and': [{
                            '$ge': {
                                u'age': 18
                            }
                        }, {
                            '$eq': {
                                u'city': u'London'
                            }
                        }]
                    }]
                }]
            })
예제 #18
0
    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
예제 #19
0
    def test_nested_condition(self):
        qc = OJAIQueryCondition().and_() \
            .condition_(condition_to_add=OJAIQueryCondition()
                        .or_()
                        .is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=22)
                        .is_(field_path='city', op=QueryOp.EQUAL, value='NY').close()) \
            .condition_(condition_to_add=OJAIQueryCondition()
                        .and_()
                        .is_(field_path='age', op=QueryOp.GREATER_OR_EQUAL, value=18)
                        .is_(field_path='city', op=QueryOp.EQUAL, value='London').close())

        qc.close().build()

        self.assertEqual(
            qc.as_dictionary(), {
                '$and': [{
                    '$or': [{
                        '$ge': {
                            u'age': 22
                        }
                    }, {
                        '$eq': {
                            u'city': u'NY'
                        }
                    }]
                }, {
                    '$and': [{
                        '$ge': {
                            u'age': 18
                        }
                    }, {
                        '$eq': {
                            u'city': u'London'
                        }
                    }]
                }]
            })
예제 #20
0
 def test_not_like(self):
     qc = OJAIQueryCondition().not_like_('card', 'visa').close().build()
     self.assertEqual(qc.as_dictionary(), {'$notlike': {'card': 'visa'}})
예제 #21
0
 def new_condition(self):
     return OJAIQueryCondition()
예제 #22
0
 def test_not_exists(self):
     qc = OJAIQueryCondition().not_exists_('city').close().build()
     self.assertEqual(qc.as_dictionary(), {'$notexists': 'city'})
예제 #23
0
 def test_query_check_existing_condition(self):
     qc = OJAIQueryCondition().is_('age', QueryOp.GREATER_OR_EQUAL, 55).is_('age', QueryOp.GREATER_OR_EQUAL, 18) \
         .close().build()
     self.assertEqual(qc.as_dictionary(), {'$ge': {'age': 18}})