Skip to content

Commit

Permalink
Added MongologUtilsTest
Browse files Browse the repository at this point in the history
  • Loading branch information
John Furr authored and John Furr committed Oct 2, 2016
1 parent e1c6ea5 commit b3da234
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 18 deletions.
1 change: 1 addition & 0 deletions mongolog/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

uuid_namespace = uuid.UUID('8296424f-28b7-5982-a434-e6ec8ef529b3')

# TODO Move to mongolog.models.Mongolog
def get_mongolog_handler(logger_name=None):
"""
Return the first MongoLogHander found in the list of defined loggers.
Expand Down
34 changes: 19 additions & 15 deletions mongolog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import pymongo
from pymongo import MongoClient
import json
pymongo_version = int(pymongo.version.split(".")[0])
if pymongo_version >= 3:
Expand All @@ -27,11 +28,23 @@ class Mongolog(object):
"""
This class provides a set of queriable functions.
"""
# If LOGGER is None we wil try and find the first available logger
LOGGER = None

@staticmethod
def find(logger=None, query=None, project=None, uuid=None, level=None, limit=None, **kwargs):
@classmethod
def find(cls, logger=None, query=None, project=None, uuid=None, level=None, limit=None, **kwargs):
"""
return self.collection.aggregate([
{"$match": query},
{"$project": proj},
{"$sort": {'created': pymongo.DESCENDING}},
{"$limit": limit},
])
"""
from mongolog.handlers import get_mongolog_handler
from pymongo import MongoClient

logger = cls.LOGGER if cls.LOGGER else logger

handler = get_mongolog_handler(logger_name=logger)
client = MongoClient(handler.connection)
db = client.mongolog
Expand All @@ -41,22 +54,15 @@ def find(logger=None, query=None, project=None, uuid=None, level=None, limit=Non
if not query:
query = {}


if uuid:
query.update({'uuid': uuid})

if level:
query.update({'level': level})

"""
return self.collection.aggregate([
{"$match": query},
{"$project": proj},
{"$sort": {'created': pymongo.DESCENDING}},
{"$limit": limit},
])
"""

if logger:
query.update({'name': logger})

aggregate_commands.append({"$match": query})

if project:
Expand All @@ -67,8 +73,6 @@ def find(logger=None, query=None, project=None, uuid=None, level=None, limit=Non
if limit:
aggregate_commands.append({"$limit": limit})

print("aggregate_commands(%s)" % aggregate_commands)

return db.mongolog.aggregate(aggregate_commands)


Expand Down
93 changes: 90 additions & 3 deletions mongolog/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
get_mongolog_handler, SimpleMongoLogHandler
)
from mongolog.exceptions import MissingConnectionError

from mongolog.models import Mongolog

from django.core.management import call_command
from django.test import TestCase
Expand Down Expand Up @@ -553,6 +553,8 @@ def setUp(self):
self.timestamp = self.handler.get_timestamp_collection()

self.remove_test_entries()

# TODO Do you need this?
self.remove_test_entries(test_key='msg.Test')
self.iterations = 100

Expand All @@ -573,9 +575,7 @@ def _check_embedded_results(self, results, iterations):

def test_embedded(self):
console.debug(self)

self.logger = logging.getLogger('test.embedded')

console.info("Starting embedded test: max_keep(%s) iteration(%s)", self.handler.max_keep, self.iterations)

start = time.time()
Expand Down Expand Up @@ -636,3 +636,90 @@ def test_reference(self):
results = list(self.collection.find({'msg.Test': True, 'msg.Test1': 1}))

self._check_reference_results(results, i)


class MongoLogUtilsTests(unittest.TestCase, TestRemoveEntriesMixin):
def setUp(self):
self.handler = get_mongolog_handler('test.embedded')

# Get some pointers to the collections for easy querying
self.collection = self.handler.get_collection()
self.timestamp = self.handler.get_timestamp_collection()
self.logger = logging.getLogger('test.embedded')

self.remove_test_entries()

def test_find_for_embedded(self):
console.debug(self)
Mongolog.LOGGER = 'test.embedded'
info_items = [
{'location': {'country': 'USA', 'state': 'WA', 'city': 'Puyallup'}},
{'location': {'country': 'USA', 'state': 'WA', 'city': 'Seattle'}},
{'location': {'country': 'USA', 'state': 'MA', 'city': 'Boston'}},
{'location': {'country': 'USA', 'state': 'MS', 'city': 'Greenville'}},
{'location': {'country': 'France', 'region': 'Normandy', 'city': 'Rouen'}},
{'location': {'country': 'France', 'region': 'Île-de-France', 'city': 'Paris'}},
]
for item in info_items:
self.logger.info(item)

warn_items = [
{'location': {'country': 'France', 'region': 'Normandy', 'city': 'Caen'}},
{'location': {'country': 'France', 'region': 'Normandy', 'city': 'Bayeux'}},
{'location': {'country': 'USA', 'state': 'MA', 'city': 'Framingham'}},
]
for item in warn_items:
self.logger.warn(item)

critical_items = [
{'location': {'country': 'Canada', 'Province': 'Quebec Ontario', 'city': 'Quebec City'}},
{'location': {'country': 'Canada', 'Province': 'Quebec Ontario', 'city': 'Toronto'}},
]

for item in critical_items:
self.logger.critical(item)

results = Mongolog.find(query={'msg.location': {'$exists': True}})
self.assertEqual(11, len(list(results)))

results = Mongolog.find(level='INFO')
self.assertEqual(6, len(list(results)))

# Test limit
results = Mongolog.find(level='INFO', limit=3)
self.assertEqual(3, len(list(results)))

# Test search by name
results = Mongolog.find(logger="test.embedded")
self.assertEqual(11, len(list(results)))

results = Mongolog.find(query={'msg.location.region': {'$exists': True}})
self.assertEqual(4, len(list(results)))

results = Mongolog.find(query={'msg.location.region': {'$exists': True}}, level='ERROR')
self.assertEqual(0, len(list(results)))

results = Mongolog.find(query={'msg.location.region': {'$exists': True}}, level='WARNING')
self.assertEqual(2, len(list(results)))

results = Mongolog.find(level='CRITICAL')
self.assertEqual(2, len(list(results)))

results = Mongolog.find(level='CRITICAL', query={'msg.location.city': 'Quebec City'})
self.assertEqual(1, len(list(results)))

# Now test projection
query={'msg.location.city': 'Quebec City'}
level='CRITICAL'
results = Mongolog.find(level=level, query=query, project={'msg': 1})
results = list(results)
self.assertEqual(set(['_id', 'msg']), set(results[0].keys()))

results = Mongolog.find(level=level, query=query, project={'msg': 1, '_id': 0})
results = list(results)
self.assertEqual(set(['msg']), set(results[0].keys()))

results = Mongolog.find(level=level, query=query, project={'msg': 1, '_id': 0, 'level': 1})
results = list(results)
self.assertEqual(set(['msg', 'level']), set(results[0].keys()))

0 comments on commit b3da234

Please sign in to comment.