Esempio n. 1
0
def test_compile_query_no_steps(mocker):
    mock_compile_query_step = mocker.patch.object(_query,
                                                  '_compile_query_step')
    _query._compile_query(
        collection_name='collection_name',
        attrs={},
        query=Query([]),
        watch_mode=False,
    )
    mock_compile_query_step.assert_not_called()
Esempio n. 2
0
def test_compile_query_one_step(mocker):
    mock_compile_query_step = mocker.patch.object(_query,
                                                  '_compile_query_step')
    _query._compile_query(
        collection_name='collection_name',
        attrs={},
        query=Query([{
            'attr': 'match_term'
        }]),
        watch_mode=False,
    )
    mock_compile_query_step.assert_called_once_with(
        aggregate_prefix=[
            {
                '$match': {
                    '__deleted': {
                        '$exists': False
                    }
                }
            },
            {
                '$match': {
                    '__create_draft': {
                        '$exists': False
                    }
                }
            },
            {
                '$match': {
                    '__update_draft': {
                        '$exists': False
                    }
                }
            },
        ],
        aggregate_suffix=[{
            '$group': {
                '_id': '$_id'
            }
        }],
        aggregate_match=[],
        collection_name='collection_name',
        attrs={},
        step={'attr': 'match_term'},
        watch_mode=False,
    )
Esempio n. 3
0
def test_compile_query_geo_near():
    skip, limit, sort, group, aggregate_query = _query._compile_query(
        collection_name='collection_name',
        attrs={},
        query=Query([{
            '$geo_near': {
                'val': [21.422507, 39.826181],
                'attr': 'str',
                'dist': 1000
            }
        }]),
        watch_mode=False,
    )
    assert skip == None
    assert limit == None
    assert sort == {'_id': -1}
    assert group == None
    assert aggregate_query == [
        {
            '$geoNear': {
                'near': {
                    'type': 'Point',
                    'coordinates': [21.422507, 39.826181]
                },
                'distanceField': 'str.__distance',
                'maxDistance': 1000,
                'spherical': True,
            }
        },
        {
            '$match': {
                '__deleted': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__create_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__update_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$group': {
                '_id': '$_id'
            }
        },
    ]
Esempio n. 4
0
def test_compile_query_attrs_mixed_in_attrs():
    skip, limit, sort, group, aggregate_query = _query._compile_query(
        collection_name='collection_name',
        attrs={'attr1': ATTR.ANY()},
        query=Query([{
            '$attrs': ['attr1', 'attr2']
        }]),
        watch_mode=False,
    )
    assert skip == None
    assert limit == None
    assert sort == {'_id': -1}
    assert group == None
    assert aggregate_query == [
        {
            '$match': {
                '__deleted': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__create_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__update_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$group': {
                '_id': '$_id',
                'attr1': {
                    '$first': '$attr1'
                }
            }
        },
    ]
Esempio n. 5
0
def test_compile_query_group():
    skip, limit, sort, group, aggregate_query = _query._compile_query(
        collection_name='collection_name',
        attrs={},
        query=Query([{
            '$group': [{
                'by': 'price',
                'count': 10
            }]
        }]),
        watch_mode=False,
    )
    assert skip == None
    assert limit == None
    assert sort == {'_id': -1}
    assert group == [{'by': 'price', 'count': 10}]
    assert aggregate_query == [
        {
            '$match': {
                '__deleted': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__create_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__update_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$group': {
                '_id': '$_id'
            }
        },
    ]
Esempio n. 6
0
def test_compile_query_invalid_query():
    with pytest.raises(InvalidQueryException):
        _query._compile_query(collection_name='collection_name',
                              attrs={},
                              query=[],
                              watch_mode=False)
Esempio n. 7
0
def test_compile_query_search():
    skip, limit, sort, group, aggregate_query = _query._compile_query(
        collection_name='collection_name',
        attrs={},
        query=Query([{
            '$search': 'search_term'
        }]),
        watch_mode=False,
    )
    assert skip == None
    assert limit == None
    assert sort == {'_id': -1}
    assert group == None
    assert aggregate_query == [
        {
            '$match': {
                '$text': {
                    '$search': 'search_term'
                }
            }
        },
        {
            '$match': {
                '__deleted': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__create_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$match': {
                '__update_draft': {
                    '$exists': False
                }
            }
        },
        {
            '$project': {
                '_id': '$_id',
                '__score': {
                    '$meta': 'textScore'
                }
            }
        },
        {
            '$match': {
                '__score': {
                    '$gt': 0.5
                }
            }
        },
        {
            '$group': {
                '_id': '$_id'
            }
        },
    ]