示例#1
0
def test_sort_12(monty_sort, mongo_sort):
    docs = [{"a": bson.Regex("^a")}, {"a": bson.Regex("^b")}]
    sort = [("a", -1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]
示例#2
0
def test_qop_nin_10(monty_find, mongo_find):
    docs = [
        {
            "a": [bson.Regex("*")]
        },
    ]
    spec = {"a": {"$nin": [[bson.Regex("*")]]}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert count_documents(mongo_c, spec) == 0
    assert count_documents(monty_c, spec) == count_documents(mongo_c, spec)
def test_qop_lte_28(monty_find, mongo_find):
    regex_0 = bson.Regex("^0")
    regex_a = bson.Regex("^a")
    docs = [
        {
            "a": regex_0
        },
    ]
    spec = {"a": {"$lte": regex_a}}

    monty_c = monty_find(docs, spec)

    # Can't have RegEx as arg to predicate
    with pytest.raises(OperationFailure):
        next(monty_c)
示例#4
0
def test_qop_regex_9(monty_find, mongo_find):
    docs = [
        {"a": "apple"}
    ]
    spec = {"a": bson.Regex("^a")}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert count_documents(mongo_c, spec) == 1
    assert count_documents(monty_c, spec) == count_documents(mongo_c, spec)
def test_qop_in_12(monty_find, mongo_find):
    docs = [
        {
            "a": "apple"
        },
    ]
    spec = {"a": {"$in": [bson.Regex("*")]}}

    monty_c = monty_find(docs, spec)

    # Regular expression is invalid
    with pytest.raises(OperationFailure):
        next(monty_c)
示例#6
0
def test_qop_regex_4(monty_find, mongo_find):
    docs = [
        {"a": "apple"},
        {"a": "abble"}
    ]
    spec = {"a": {"$regex": bson.Regex("^a"), "$nin": ["abble"]}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert count_documents(mongo_c, spec) == 1
    assert count_documents(monty_c, spec) == count_documents(mongo_c, spec)
    assert next(monty_c) == next(mongo_c)
示例#7
0
def test_qop_not_4(monty_find, mongo_find):
    docs = [
        {
            "a": "apple"
        },
    ]
    spec = {"a": {"$not": bson.Regex("^a")}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert count_documents(mongo_c, spec) == 0
    assert count_documents(monty_c, spec) == count_documents(mongo_c, spec)
def test_qop_in_9(monty_find, mongo_find):
    docs = [
        {
            "a": "apple"
        },
    ]
    spec = {"a": {"$in": [bson.Regex("^a")]}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    assert count_documents(mongo_c, spec) == 1
    assert count_documents(monty_c, spec) == count_documents(mongo_c, spec)
    assert next(mongo_c) == next(monty_c)
示例#9
0
def test_sort_13(monty_sort, mongo_sort):
    docs = [
        {
            "a": bson.Regex("^a")
        },
        {
            "a": bson.Regex("^a", "i")
        },
        {
            "a": bson.Regex("^a", "ix")
        },
        {
            "a": bson.Regex("^b")
        },
        {
            "a": bson.Regex("^b", "i")
        },
        {
            "a": bson.Regex("^b", "ix")
        },
        {
            "a": re.compile("^c")
        },
        {
            "a": re.compile("^c", re.IGNORECASE)
        },
        {
            "a": re.compile("^c", re.IGNORECASE | re.VERBOSE)
        },
        {
            "a": bson.Regex("~a")
        },
    ]
    sort = [("a", -1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]
示例#10
0
def test_qop_regex_6(monty_find, mongo_find, mongo_version):
    docs = [
        {"a": "Apple"}
    ]
    spec = {"a": {"$regex": bson.Regex("^a", "i"), "$options": "x"}}

    monty_c = monty_find(docs, spec)
    mongo_c = mongo_find(docs, spec)

    if mongo_version[:2] == [4, 2]:
        # options set in both $regex and $options
        with pytest.raises(MongoOpFail):
            next(mongo_c)
        with pytest.raises(MontyOpFail):
            next(monty_c)
        return

    # If not using `SON` or `OrderedDict`, then depend on the dict key order,
    # if the first key is `$regex`, `$options` will override `regex.flags`,
    # vice versa.
    count = 0 if next(iter(spec["a"])) == "$regex" else 1
    assert count_documents(mongo_c, spec) == count
    assert count_documents(monty_c, spec) == count_documents(mongo_c, spec)
示例#11
0
def test_weighted_19():
    data = bson.Regex("^a", "ix")
    assert Weighted(data) == (11, "^a", "ix")
示例#12
0
def test_sort_19(monty_sort, mongo_sort):
    docs = [
        {
            "a": ["x", True]
        },
        {
            "a": None
        },
        {
            "a": []
        },
        {
            "a": [5, []]
        },
        {
            "a": {
                "s": 7
            }
        },
        {
            "a": {
                "s": [9]
            }
        },
        {
            "a": {
                "s": 10
            }
        },
        {
            "a": 6
        },
        {
            "a": 4
        },
        {
            "a": [5, None]
        },
        {
            "a": [5, [1]]
        },
        {
            "a": [bson.Decimal128("4.5"),
                  bson.Binary(b"0")]
        },
        {
            "a": [{
                "s": 5
            }, False]
        },
        {
            "a": [{
                "s": 9
            }]
        },
        {
            "a": [True, "y"]
        },
        {
            "a": bson.Binary(b"a")
        },
        {
            "a": b"bytes"
        },
        {
            "a": ["abc"]
        },
        {
            "a": "banana"
        },
        {
            "a": "appple"
        },
        {
            "a": [bson.Regex("^a", "ix")]
        },
        {
            "a": bson.Regex("^b")
        },
        {
            "a": bson.Code("x", {"m": 0})
        },
        {
            "a": bson.Code("y")
        },
        {
            "a": bson.Code("y", {})
        },
        {
            "a": bson.Code("y", {"m": 0})
        },
        {
            "a": bson.MinKey()
        },
        {
            "a": bson.MaxKey()
        },
        {
            "a": bson.Timestamp(0, 1)
        },
        {
            "a": bson.Timestamp(1, 1)
        },
        {
            "a": bson.ObjectId(b"000000000000")
        },
        {
            "a": bson.ObjectId(b"000000000001")
        },
        {
            "a": datetime(1900, 1, 1)
        },
        {
            "a": datetime(1900, 1, 2)
        },
    ]
    sort = [("_id", 1), ("a", 1)]

    monty_c = monty_sort(docs, sort)
    mongo_c = mongo_sort(docs, sort)

    for i in range(len(docs)):
        assert next(mongo_c)["_id"] == next(monty_c)["_id"]