Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ignore duplication error in mongodb when fetch notices #25

Merged
merged 2 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions ted_sws/data_manager/adapters/notice_repository.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import logging
from typing import Iterator
from pymongo import MongoClient
from ted_sws.domain.adapters.repository_abc import NoticeRepositoryABC
from ted_sws.domain.model.notice import Notice

logger = logging.getLogger(__name__)


class NoticeRepository(NoticeRepositoryABC):
"""
Expand All @@ -25,7 +28,10 @@ def add(self, notice: Notice):
"""
notice_dict = notice.dict()
notice_dict["_id"] = notice_dict["ted_id"]
self.collection.insert_one(notice_dict)
try:
self.collection.insert_one(notice_dict)
except Exception as e:
logger.warning(f"Failed to add notice with id={notice_dict['ted_id']}, with error message: "+str(e))

def update(self, notice: Notice):
"""
Expand All @@ -35,7 +41,7 @@ def update(self, notice: Notice):
"""
notice_dict = notice.dict()
notice_dict["_id"] = notice_dict["ted_id"]
self.collection.update_one({'_id':notice_dict["_id"]}, {"$set": notice_dict})
self.collection.update_one({'_id': notice_dict["_id"]}, {"$set": notice_dict})

def get(self, reference) -> Notice:
"""
Expand Down
3 changes: 1 addition & 2 deletions tests/e2e/data_manager/test_notice_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def test_notice_repository_create(mongodb_client):
result_notices = list(notice_repository.list())
assert result_notices
assert len(result_notices) == 1
with pytest.raises(Exception):
notice_repository.add(notice)
notice_repository.add(notice)
notice.original_metadata = TEDMetadata(**{"AA": "Updated metadata"})
notice_repository.update(notice)
result_notice = notice_repository.get(reference=NOTICE_TED_ID)
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/data_manager/test_notice_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def test_notice_repository_create(mongodb_client):
result_notices = list(notice_repository.list())
assert result_notices
assert len(result_notices) == 1
with pytest.raises(Exception):
notice_repository.add(notice)
notice_repository.add(notice)
notice.original_metadata = TEDMetadata(**{"AA": "Updated metadata"})
notice_repository.update(notice)
result_notice = notice_repository.get(reference=NOTICE_TED_ID)
Expand Down