Skip to content

Commit

Permalink
Add more tests for regex
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamadkhalaj committed Nov 4, 2023
1 parent 2705215 commit 5cb0b8f
Showing 1 changed file with 167 additions and 117 deletions.
284 changes: 167 additions & 117 deletions tests/test_aggify.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ def test_complex_conditional_expression_in_projection(self):
"$gt": ["$age", 30]
}
assert (
aggify.pipelines[0]["$project"]["custom_field"]["$cond"]["then"] == "Adult"
aggify.pipelines[0]["$project"]["custom_field"]["$cond"]["then"] == "Adult"
)
assert (
aggify.pipelines[0]["$project"]["custom_field"]["$cond"]["else"] == "Child"
aggify.pipelines[0]["$project"]["custom_field"]["$cond"]["else"] == "Child"
)

# Test filtering using not operator
Expand Down Expand Up @@ -185,27 +185,27 @@ def test_annotate_invalid_accumulator(self):
@pytest.mark.parametrize(
"accumulator",
(
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
),
)
def test_annotate_with_raw_f(self, accumulator):
Expand All @@ -217,27 +217,27 @@ def test_annotate_with_raw_f(self, accumulator):
@pytest.mark.parametrize(
"accumulator",
(
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
),
)
def test_annotate_with_f(self, accumulator):
Expand All @@ -251,27 +251,27 @@ def test_annotate_with_f(self, accumulator):
@pytest.mark.parametrize(
"accumulator",
(
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
),
)
def test_annotate_raw_value(self, accumulator):
Expand All @@ -285,27 +285,27 @@ def test_annotate_raw_value(self, accumulator):
@pytest.mark.parametrize(
"accumulator",
(
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
),
)
def test_annotate_raw_value_not_model_field(self, accumulator):
Expand All @@ -319,27 +319,27 @@ def test_annotate_raw_value_not_model_field(self, accumulator):
@pytest.mark.parametrize(
"accumulator",
(
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
"sum",
"avg",
"stdDevPop",
"stdDevSamp",
"push",
"addToSet",
"count",
"first",
"last",
"max",
"accumulator",
"min",
"median",
"mergeObjects",
"top",
"bottom",
"topN",
"bottomN",
"firstN",
"lastN",
"maxN",
),
)
def test_annotate_add_annotated_field_to_base_model(self, accumulator):
Expand All @@ -360,13 +360,13 @@ def test_out_with_project_stage_error(self):
@pytest.mark.parametrize(
("method", "args"),
(
("group", ("_id",)),
("order_by", ("field",)),
("raw", ({"$query": "query"},)),
("add_fields", ({"$field": "value"},)),
("filter", (Q(age=20),)),
("__getitem__", (slice(2, 10),)),
("unwind", ("path",)),
("group", ("_id",)),
("order_by", ("field",)),
("raw", ({"$query": "query"},)),
("add_fields", ({"$field": "value"},)),
("filter", (Q(age=20),)),
("__getitem__", (slice(2, 10),)),
("unwind", ("path",)),
),
)
def test_out_stage_error(self, method, args):
Expand Down Expand Up @@ -397,9 +397,9 @@ def test_unwind_just_path(self):
@pytest.mark.parametrize(
"params",
(
{"include_index_array": "Mahdi"},
{"preserve": True},
{"include_index_array": "Mahdi", "preserve": True},
{"include_index_array": "Mahdi"},
{"preserve": True},
{"include_index_array": "Mahdi", "preserve": True},
),
)
def test_unwind_with_parameters(self, params):
Expand All @@ -412,3 +412,53 @@ def test_unwind_with_parameters(self, params):
assert thing.pipelines[-1]["$unwind"]["includeArrayIndex"] == "Mahdi"
if preserve is not None:
assert thing.pipelines[-1]["$unwind"]["preserveNullAndEmptyArrays"] is True

def test_regex_exact(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__exact="Aggify"))
assert thing[-1]["$match"]["name"] == {"$eq": "Aggify"}

def test_regex_iexact(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__iexact="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "^Aggify$", "$options": "i"}

def test_regex_contains(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__contains="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "Aggify"}

def test_regex_icontains(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__icontains="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "Aggify", "$options": "i"}

def test_regex_startwith(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__startswith="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "^Aggify"}

def test_regex_istarstwith(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__istartswith="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "^Aggify", "$options": "i"}

def test_regex_endswith(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__endswith="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "Aggify$"}

def test_regex_iendswith(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__iendswith="Aggify"))
assert thing[-1]["$match"]["name"] == {"$regex": "Aggify$", "$options": "i"}

def test_regex_f_with_exact(self):
aggify = Aggify(BaseModel)
thing = list(aggify.filter(name__exact=F("age")))
assert thing[-1]["$match"] == {"$expr": {"$eq": ["$name", "$age"]}}

def test_regex_f_with_others(self):
aggify = Aggify(BaseModel)
with pytest.raises(ValueError):
aggify.filter(name__contains=F("age"))

0 comments on commit 5cb0b8f

Please sign in to comment.