Skip to content

Commit 7147043

Browse files
authored
Merge pull request #2483 from bagerard/mongodb44_ci
Add Mongodb 44 ci and fix incompat
2 parents 2d1c9af + b9b5361 commit 7147043

File tree

9 files changed

+63
-28
lines changed

9 files changed

+63
-28
lines changed

.github/workflows/github-actions.yml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ on:
1111
tags:
1212
- 'v[0-9]+\.[0-9]+\.[0-9]+*'
1313
env:
14-
MONGODB_3_4: 3.4.19
15-
MONGODB_3_6: 3.6.13
16-
MONGODB_4_0: 4.0.13
14+
MONGODB_3_6: 3.6.14
15+
MONGODB_4_0: 4.0.23
16+
MONGODB_4_2: 4.2
17+
MONGODB_4_4: 4.4
1718

1819
PYMONGO_3_4: 3.4
1920
PYMONGO_3_6: 3.6
@@ -47,14 +48,14 @@ jobs:
4748
MONGODB: [$MONGODB_4_0]
4849
PYMONGO: [$PYMONGO_3_11]
4950
include:
50-
- python-version: 3.7
51-
MONGODB: $MONGODB_3_4
52-
PYMONGO: $PYMONGO_3_6
5351
- python-version: 3.7
5452
MONGODB: $MONGODB_3_6
5553
PYMONGO: $PYMONGO_3_9
5654
- python-version: 3.7
57-
MONGODB: $MONGODB_3_6
55+
MONGODB: $MONGODB_4_2
56+
PYMONGO: $PYMONGO_3_6
57+
- python-version: 3.7
58+
MONGODB: $MONGODB_4_4
5859
PYMONGO: $PYMONGO_3_11
5960
steps:
6061
- uses: actions/checkout@v2

.github/workflows/install_mongo.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22

33
MONGODB=$1
44

5+
# Mongo > 4.0 follows different name convention for download links
56
mongo_build=mongodb-linux-x86_64-${MONGODB}
7+
8+
if [[ "$MONGODB" == *"4.2"* ]]; then
9+
mongo_build=mongodb-linux-x86_64-ubuntu1804-v${MONGODB}-latest
10+
elif [[ "$MONGODB" == *"4.4"* ]]; then
11+
mongo_build=mongodb-linux-x86_64-ubuntu1804-v${MONGODB}-latest
12+
fi
13+
614
wget http://fastdl.mongodb.org/linux/$mongo_build.tgz
715
tar xzf $mongo_build.tgz
8-
${PWD}/$mongo_build/bin/mongod --version
16+
17+
mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*")
18+
$mongodb_dir/bin/mongod --version

.github/workflows/start_mongo.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
MONGODB=$1
44

5-
mongodb_dir=${PWD}/mongodb-linux-x86_64-${MONGODB}
5+
mongodb_dir=$(find ${PWD}/ -type d -name "mongodb-linux-x86_64*")
6+
67
mkdir $mongodb_dir/data
78
$mongodb_dir/bin/mongod --dbpath $mongodb_dir/data --logpath $mongodb_dir/mongodb.log --fork
89
mongo --eval 'db.version();' # Make sure mongo is awake

docs/changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Development
77
===========
88
- (Fill this out as you fix issues and develop your features).
99
- Bugfix: manually setting SequenceField in DynamicDocument doesn't increment the counter #2471
10+
- Add MongoDB 4.2 and 4.4 to CI
1011

1112
Changes in 0.22.1
1213
=================

mongoengine/mongodb_support.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# get_mongodb_version()
99
MONGODB_34 = (3, 4)
1010
MONGODB_36 = (3, 6)
11+
MONGODB_42 = (4, 2)
12+
MONGODB_44 = (4, 4)
1113

1214

1315
def get_mongodb_version():

mongoengine/queryset/base.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,21 +1355,18 @@ def map_reduce(
13551355

13561356
MapReduceDocument = _import_class("MapReduceDocument")
13571357

1358-
if not hasattr(self._collection, "map_reduce"):
1359-
raise NotImplementedError("Requires MongoDB >= 1.7.1")
1360-
13611358
map_f_scope = {}
13621359
if isinstance(map_f, Code):
13631360
map_f_scope = map_f.scope
13641361
map_f = str(map_f)
1365-
map_f = Code(queryset._sub_js_fields(map_f), map_f_scope)
1362+
map_f = Code(queryset._sub_js_fields(map_f), map_f_scope or None)
13661363

13671364
reduce_f_scope = {}
13681365
if isinstance(reduce_f, Code):
13691366
reduce_f_scope = reduce_f.scope
13701367
reduce_f = str(reduce_f)
13711368
reduce_f_code = queryset._sub_js_fields(reduce_f)
1372-
reduce_f = Code(reduce_f_code, reduce_f_scope)
1369+
reduce_f = Code(reduce_f_code, reduce_f_scope or None)
13731370

13741371
mr_args = {"query": queryset._query}
13751372

@@ -1379,7 +1376,7 @@ def map_reduce(
13791376
finalize_f_scope = finalize_f.scope
13801377
finalize_f = str(finalize_f)
13811378
finalize_f_code = queryset._sub_js_fields(finalize_f)
1382-
finalize_f = Code(finalize_f_code, finalize_f_scope)
1379+
finalize_f = Code(finalize_f_code, finalize_f_scope or None)
13831380
mr_args["finalize"] = finalize_f
13841381

13851382
if scope:

tests/document/test_indexes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from mongoengine import *
99
from mongoengine.connection import get_db
10+
from mongoengine.mongodb_support import MONGODB_42, get_mongodb_version
1011

1112

1213
class TestIndexes(unittest.TestCase):
@@ -452,9 +453,11 @@ class Test(Document):
452453
.get("stage")
453454
== "IXSCAN"
454455
)
456+
mongo_db = get_mongodb_version()
457+
PROJECTION_STR = "PROJECTION" if mongo_db < MONGODB_42 else "PROJECTION_COVERED"
455458
assert (
456459
query_plan.get("queryPlanner").get("winningPlan").get("stage")
457-
== "PROJECTION"
460+
== PROJECTION_STR
458461
)
459462

460463
query_plan = Test.objects(a=1).explain()

tests/queryset/test_queryset.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@
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

2730
class 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):

tests/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def get_as_pymongo(doc):
3434
return doc.__class__.objects.as_pymongo().get(id=doc.id)
3535

3636

37+
def requires_mongodb_lt_42(func):
38+
return _decorated_with_ver_requirement(func, (4, 2), oper=operator.lt)
39+
40+
3741
def requires_mongodb_gte_44(func):
3842
return _decorated_with_ver_requirement(func, (4, 4), oper=operator.ge)
3943

0 commit comments

Comments
 (0)