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
                    }
                }]
            })
 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_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_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'
                }
            }]})
    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_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())
    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'
                            }
                        }]
                    }]
                }]
            })
    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'
                        }
                    }]
                }]
            })
 def test_not_like(self):
     qc = OJAIQueryCondition().not_like_('card', 'visa').close().build()
     self.assertEqual(qc.as_dictionary(), {'$notlike': {'card': 'visa'}})
 def test_not_exists(self):
     qc = OJAIQueryCondition().not_exists_('city').close().build()
     self.assertEqual(qc.as_dictionary(), {'$notexists': 'city'})
 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}})