2121 QuerySetManager ,
2222 queryset_manager ,
2323)
24- from tests .utils import requires_mongodb_gte_44
24+ from tests .utils import (
25+ requires_mongodb_gte_44 ,
26+ requires_mongodb_lt_42 ,
27+ )
2528
2629
2730class db_ops_tracker (query_counter ):
@@ -1490,6 +1493,7 @@ class BlogPost(Document):
14901493
14911494 BlogPost .drop_collection ()
14921495
1496+ @requires_mongodb_lt_42
14931497 def test_exec_js_query (self ):
14941498 """Ensure that queries are properly formed for use in exec_js."""
14951499
@@ -1527,6 +1531,7 @@ class BlogPost(Document):
15271531
15281532 BlogPost .drop_collection ()
15291533
1534+ @requires_mongodb_lt_42
15301535 def test_exec_js_field_sub (self ):
15311536 """Ensure that field substitutions occur properly in exec_js functions."""
15321537
@@ -2660,6 +2665,8 @@ class BlogPost(Document):
26602665 title = StringField (primary_key = True )
26612666 tags = ListField (StringField ())
26622667
2668+ BlogPost .drop_collection ()
2669+
26632670 post1 = BlogPost (title = "Post #1" , tags = ["mongodb" , "mongoengine" ])
26642671 post2 = BlogPost (title = "Post #2" , tags = ["django" , "mongodb" ])
26652672 post3 = BlogPost (title = "Post #3" , tags = ["hitchcock films" ])
@@ -2688,20 +2695,22 @@ class BlogPost(Document):
26882695 }
26892696 """
26902697
2691- results = BlogPost .objects .map_reduce (map_f , reduce_f , "myresults" )
2698+ results = BlogPost .objects .order_by ("_id" ).map_reduce (
2699+ map_f , reduce_f , "myresults2"
2700+ )
26922701 results = list (results )
26932702
2694- assert results [0 ].object == post1
2695- assert results [1 ].object == post2
2696- assert results [2 ].object == post3
2703+ assert len (results ) == 3
2704+ assert results [0 ].object .id == post1 .id
2705+ assert results [1 ].object .id == post2 .id
2706+ assert results [2 ].object .id == post3 .id
26972707
26982708 BlogPost .drop_collection ()
26992709
27002710 def test_map_reduce_custom_output (self ):
27012711 """
27022712 Test map/reduce custom output
27032713 """
2704- register_connection ("test2" , "mongoenginetest2" )
27052714
27062715 class Family (Document ):
27072716 id = IntField (primary_key = True )
@@ -2774,6 +2783,7 @@ class Person(Document):
27742783 family.persons.push(person);
27752784 family.totalAge += person.age;
27762785 });
2786+ family.persons.sort((a, b) => (a.age > b.age))
27772787 }
27782788 });
27792789
@@ -2802,10 +2812,10 @@ class Person(Document):
28022812 "_id" : 1 ,
28032813 "value" : {
28042814 "persons" : [
2815+ {"age" : 17 , "name" : "Tayza Mariana" },
28052816 {"age" : 21 , "name" : "Wilson Jr" },
2806- {"age" : 45 , "name" : "Wilson Father" },
28072817 {"age" : 40 , "name" : "Eliana Costa" },
2808- {"age" : 17 , "name" : "Tayza Mariana " },
2818+ {"age" : 45 , "name" : "Wilson Father " },
28092819 ],
28102820 "totalAge" : 123 ,
28112821 },
@@ -2815,9 +2825,9 @@ class Person(Document):
28152825 "_id" : 2 ,
28162826 "value" : {
28172827 "persons" : [
2828+ {"age" : 10 , "name" : "Igor Gabriel" },
28182829 {"age" : 16 , "name" : "Isabella Luanna" },
28192830 {"age" : 36 , "name" : "Sandra Mara" },
2820- {"age" : 10 , "name" : "Igor Gabriel" },
28212831 ],
28222832 "totalAge" : 62 ,
28232833 },
@@ -2827,8 +2837,8 @@ class Person(Document):
28272837 "_id" : 3 ,
28282838 "value" : {
28292839 "persons" : [
2830- {"age" : 30 , "name" : "Arthur WA" },
28312840 {"age" : 25 , "name" : "Paula Leonel" },
2841+ {"age" : 30 , "name" : "Arthur WA" },
28322842 ],
28332843 "totalAge" : 55 ,
28342844 },
@@ -3109,6 +3119,7 @@ class Person(Document):
31093119 freq = Person .objects .item_frequencies ("city" , normalize = True , map_reduce = True )
31103120 assert freq == {"CRB" : 0.5 , None : 0.5 }
31113121
3122+ @requires_mongodb_lt_42
31123123 def test_item_frequencies_with_null_embedded (self ):
31133124 class Data (EmbeddedDocument ):
31143125 name = StringField ()
@@ -3137,6 +3148,7 @@ class Person(Document):
31373148 ot = Person .objects .item_frequencies ("extra.tag" , map_reduce = True )
31383149 assert ot == {None : 1.0 , "friend" : 1.0 }
31393150
3151+ @requires_mongodb_lt_42
31403152 def test_item_frequencies_with_0_values (self ):
31413153 class Test (Document ):
31423154 val = IntField ()
@@ -3151,6 +3163,7 @@ class Test(Document):
31513163 ot = Test .objects .item_frequencies ("val" , map_reduce = False )
31523164 assert ot == {0 : 1 }
31533165
3166+ @requires_mongodb_lt_42
31543167 def test_item_frequencies_with_False_values (self ):
31553168 class Test (Document ):
31563169 val = BooleanField ()
@@ -3165,6 +3178,7 @@ class Test(Document):
31653178 ot = Test .objects .item_frequencies ("val" , map_reduce = False )
31663179 assert ot == {False : 1 }
31673180
3181+ @requires_mongodb_lt_42
31683182 def test_item_frequencies_normalize (self ):
31693183 class Test (Document ):
31703184 val = IntField ()
@@ -3551,7 +3565,8 @@ class Book(Document):
35513565 Book .objects .create (title = "The Stories" , authors = [mark_twain , john_tolkien ])
35523566
35533567 authors = Book .objects .distinct ("authors" )
3554- assert authors == [mark_twain , john_tolkien ]
3568+ authors_names = {author .name for author in authors }
3569+ assert authors_names == {mark_twain .name , john_tolkien .name }
35553570
35563571 def test_distinct_ListField_EmbeddedDocumentField_EmbeddedDocumentField (self ):
35573572 class Continent (EmbeddedDocument ):
@@ -3588,7 +3603,8 @@ class Book(Document):
35883603 assert country_list == [scotland , tibet ]
35893604
35903605 continent_list = Book .objects .distinct ("authors.country.continent" )
3591- assert continent_list == [europe , asia ]
3606+ continent_list_names = {c .continent_name for c in continent_list }
3607+ assert continent_list_names == {europe .continent_name , asia .continent_name }
35923608
35933609 def test_distinct_ListField_ReferenceField (self ):
35943610 class Bar (Document ):
0 commit comments