Skip to content

Commit

Permalink
adding history implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
diegorubin committed Feb 18, 2022
1 parent cc143ec commit 3880d51
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
8 changes: 6 additions & 2 deletions lifeguard_mongodb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
34 changes: 33 additions & 1 deletion lifeguard_mongodb/repositories.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
lifeguard==0.0.14
lifeguard==0.0.39
pymongo==3.11.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="[email protected]",
Expand Down
64 changes: 64 additions & 0 deletions tests/test_repositories.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down

0 comments on commit 3880d51

Please sign in to comment.