From 5cb0b8f8b21dff968051ddadccc587df3154fbfc Mon Sep 17 00:00:00 2001 From: MohammadMahdi Khalaj Date: Sat, 4 Nov 2023 10:42:43 +0330 Subject: [PATCH] Add more tests for regex --- tests/test_aggify.py | 284 +++++++++++++++++++++++++------------------ 1 file changed, 167 insertions(+), 117 deletions(-) diff --git a/tests/test_aggify.py b/tests/test_aggify.py index 2735557..96d0675 100644 --- a/tests/test_aggify.py +++ b/tests/test_aggify.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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): @@ -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"))