Skip to content

Commit

Permalink
fix: count_document issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mumarkhan999 committed Jun 4, 2024
1 parent 591010d commit 68c18cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
15 changes: 12 additions & 3 deletions eventtracking/backends/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,22 @@ def _create_indexes(self):
# TODO: The creation of indexes can be moved to a Django
# management command or equivalent. There is also an option to
# run the indexing on the background, without locking.
self.collection.create_index([('time', pymongo.DESCENDING)])
self.collection.create_index('name')
# TODO: Remove pymongo3 backward compatibility code once pymongo4 upgrade is done
if pymongo.version_tuple[0] == 3:
self.collection.ensure_index([('time', pymongo.DESCENDING)])
self.collection.ensure_index('name')
else:
self.collection.create_index([('time', pymongo.DESCENDING)])
self.collection.create_index('name')

def send(self, event):
"""Insert the event in to the Mongo collection"""
try:
self.collection.insert_one(event)
# TODO: Remove pymongo3 backward compatibility code once pymongo4 upgrade is done
if pymongo.version_tuple[0] == 3:
self.collection.insert(event, manipulate=False)
else:
self.collection.insert_one(event)
except (PyMongoError, BSONError):
# The event will be lost in case of a connection error or any error
# that occurs when trying to insert the event into Mongo.
Expand Down
8 changes: 6 additions & 2 deletions eventtracking/backends/tests/test_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from unittest.mock import patch
from unittest.mock import sentinel

import pymongo
from pymongo.errors import PyMongoError
from bson.errors import BSONError

Expand All @@ -28,8 +29,11 @@ def test_mongo_backend(self):
self.backend.send(events[1])

# Check if we inserted events into the database

calls = self.backend.collection.insert_one.mock_calls
# TODO: Remove pymongo3 backward compatibility code once pymongo4 upgrade is done
if pymongo.version_tuple[0] == 3:
calls = self.backend.collection.insert.mock_calls
else:
calls = self.backend.collection.insert_one.mock_calls

self.assertEqual(len(calls), 2)

Expand Down
16 changes: 12 additions & 4 deletions eventtracking/backends/tests/test_mongodb_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from datetime import datetime
from uuid import uuid4

import pymongo
from pytz import UTC

from eventtracking.backends.mongodb import MongoBackend
Expand Down Expand Up @@ -48,16 +49,23 @@ def test_sequential_events(self):

# Ensure MongoDB has finished writing out the events before we
# run our query.
self.mongo_backend.connection.admin.command('fsync', lock=True)

# TODO: Remove pymongo3 backward compatibility code once pymongo4 upgrade is done
if pymongo.version_tuple[0] == 3:
self.mongo_backend.connection.fsync()
cursor = self.mongo_backend.collection.find()
self.assertEqual(cursor.count(), 10)
else:
self.mongo_backend.connection.admin.command('fsync', lock=True)
collection = self.mongo_backend.collection
cursor = collection.find()
self.assertEqual(collection.count_documents({}), 10)

mem_events = {}
for event in self.memory_backend.events:
mem_events[event['data']['sequence']] = event
self.assertEqual(len(mem_events), 10)

cursor = self.mongo_backend.collection.find()
self.assertEqual(cursor.count_documents({}), 10)

for event in cursor:
mem_event = mem_events[event['data']['sequence']]
mem_event['_id'] = event['_id']
Expand Down

0 comments on commit 68c18cf

Please sign in to comment.