diff --git a/lifeguard_mongodb/__init__.py b/lifeguard_mongodb/__init__.py index 6da3ba4..12018d7 100644 --- a/lifeguard_mongodb/__init__.py +++ b/lifeguard_mongodb/__init__.py @@ -3,13 +3,17 @@ """ from lifeguard.repositories import declare_implementation -from lifeguard_mongodb.repositories import (MongoDBNotificationRepository, - MongoDBValidationRepository) +from lifeguard_mongodb.repositories import ( + MongoDBHistoryRepository, + MongoDBNotificationRepository, + MongoDBValidationRepository, +) class LifeguardMongoDBPlugin: def __init__(self, lifeguard_context): self.lifeguard_context = lifeguard_context + declare_implementation("history", MongoDBHistoryRepository) declare_implementation("notification", MongoDBNotificationRepository) declare_implementation("validation", MongoDBValidationRepository) diff --git a/lifeguard_mongodb/repositories.py b/lifeguard_mongodb/repositories.py index cca429e..2eca4f3 100644 --- a/lifeguard_mongodb/repositories.py +++ b/lifeguard_mongodb/repositories.py @@ -1,7 +1,7 @@ """ Implementation of repositories using MongoDB """ -from lifeguard.notifications import NotificationStatus +from lifeguard.notifications import NotificationStatus, NotificationOccurrence from lifeguard.validations import ValidationResponse from pymongo import MongoClient @@ -24,6 +24,38 @@ def save_or_update(collection, query, data): collection.insert_one(data) +class MongoDBHistoryRepository: + def __init__(self): + self.collection = DATABASE.history + + def append_notification(self, notification_occurrence): + self.collection.insert_one( + { + "validation_name": notification_occurrence.validation_name, + "details": notification_occurrence.details, + "status": notification_occurrence.status, + "notification_type": notification_occurrence.notification_type, + "created_at": notification_occurrence.created_at, + } + ) + + def fetch_notifications(self, start_interval, end_interval, filters): + filters.update({"created_at": {"$gte": start_interval, "$lte": end_interval}}) + return [ + self.__convert_to_occurrence(entry) + for entry in self.collection.find(filters) + ] + + def __convert_to_occurrence(self, entry): + return NotificationOccurrence( + validation_name=entry["validation_name"], + details=entry["details"], + status=entry["status"], + notification_type=entry["notification_type"], + created_at=entry["created_at"], + ) + + class MongoDBValidationRepository: def __init__(self): self.collection = DATABASE.validations diff --git a/requirements.txt b/requirements.txt index 090b923..3c861e0 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -lifeguard==0.0.14 +lifeguard==0.0.39 pymongo==3.11.0 diff --git a/setup.py b/setup.py index 498ecae..19d73ec 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name="lifeguard-mongodb", - version="0.0.8", + version="0.0.10", url="https://github.com/LifeguardSystem/lifeguard-mongodb", author="Diego Rubin", author_email="contact@diegorubin.dev", diff --git a/tests/test_repositories.py b/tests/test_repositories.py index 55c2214..03d7072 100644 --- a/tests/test_repositories.py +++ b/tests/test_repositories.py @@ -1,15 +1,79 @@ from datetime import datetime +import json + import unittest from unittest.mock import patch, MagicMock from lifeguard.notifications import NotificationStatus from lifeguard.validations import ValidationResponse from lifeguard_mongodb.repositories import ( + MongoDBHistoryRepository, MongoDBValidationRepository, MongoDBNotificationRepository, ) +class TestMongoDBHistoryRepository(unittest.TestCase): + @patch("lifeguard_mongodb.repositories.DATABASE") + def setUp(self, mock_database): + self.collection = MagicMock(name="history") + mock_database.history = self.collection + self.repository = MongoDBHistoryRepository() + + def test_append_notification(self): + notification_occurrence = MagicMock(name="notification_occurrence") + notification_occurrence.validation_name = "validation_name" + notification_occurrence.details = "details" + notification_occurrence.status = "status" + notification_occurrence.notification_type = "notification_type" + notification_occurrence.created_at = "created_at" + + self.repository.append_notification(notification_occurrence) + + self.collection.insert_one.assert_called_with( + { + "validation_name": "validation_name", + "details": "details", + "status": "status", + "notification_type": "notification_type", + "created_at": "created_at", + } + ) + + def test_fetch_notifications(self): + start_interval = MagicMock(name="start_interval") + end_interval = MagicMock(name="end_interval") + filters = {} + + self.collection.find.return_value = [ + { + "validation_name": "validation_name", + "details": "details", + "status": "status", + "notification_type": "notification_type", + "created_at": "created_at", + } + ] + + result = self.repository.fetch_notifications( + start_interval, end_interval, filters + ) + + self.collection.find.assert_called_with( + {"created_at": {"$gte": start_interval, "$lte": end_interval}} + ) + self.assertEqual( + result[0].__dict__, + { + "_created_at": "created_at", + "_details": "details", + "_notification_type": "notification_type", + "_status": "status", + "_validation_name": "validation_name", + }, + ) + + class TestMongoDBValidationRepository(unittest.TestCase): @patch("lifeguard_mongodb.repositories.DATABASE") def setUp(self, mock_database):