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 f44e99e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
13 changes: 10 additions & 3 deletions eventtracking/backends/mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,20 @@ 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')
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)
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
7 changes: 5 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,10 @@ 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
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
10 changes: 8 additions & 2 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 @@ -55,8 +56,13 @@ def test_sequential_events(self):
mem_events[event['data']['sequence']] = event
self.assertEqual(len(mem_events), 10)

cursor = self.mongo_backend.collection.find()
self.assertEqual(cursor.count_documents({}), 10)
if pymongo.version_tuple[0] == 3:
cursor = self.mongo_backend.collection.find()
self.assertEqual(cursor.count(), 10)
else:
collection = self.mongo_backend.collection
cursor = collection.find()
self.assertEqual(collection.count_documents({}), 10)

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

0 comments on commit f44e99e

Please sign in to comment.