mirror of
https://github.com/fhswf/aki_prj23_transparenzregister.git
synced 2025-05-14 17:58:45 +02:00
test: Adding unit tests for news_mongo_service
This commit is contained in:
parent
b788ee3659
commit
4b5f63ee62
@ -38,7 +38,7 @@ class MongoNewsService:
|
|||||||
"""
|
"""
|
||||||
result = list(self.collection.find({"_id": id}))
|
result = list(self.collection.find({"_id": id}))
|
||||||
if len(result) == 1:
|
if len(result) == 1:
|
||||||
return MongoEntryTransformer.transform_outgoing(list(result)[0])
|
return MongoEntryTransformer.transform_outgoing(result[0])
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def insert(self, news: News):
|
def insert(self, news: News):
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
from unittest.mock import Mock
|
from unittest.mock import Mock, patch
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from aki_prj23_transparenzregister.utils.news_mongo_service import MongoNewsService
|
from aki_prj23_transparenzregister.models.news import News
|
||||||
|
from aki_prj23_transparenzregister.utils.news_mongo_service import (
|
||||||
|
MongoEntryTransformer,
|
||||||
|
MongoNewsService,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
@ -42,3 +46,70 @@ def test_init(mock_mongo_connector, mock_collection):
|
|||||||
mock_mongo_connector.database = {"news": mock_collection}
|
mock_mongo_connector.database = {"news": mock_collection}
|
||||||
service = MongoNewsService(mock_mongo_connector)
|
service = MongoNewsService(mock_mongo_connector)
|
||||||
assert service.collection == mock_collection
|
assert service.collection == mock_collection
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_all(mock_mongo_connector, mock_collection):
|
||||||
|
mock_mongo_connector.database = {"news": mock_collection}
|
||||||
|
service = MongoNewsService(mock_mongo_connector)
|
||||||
|
|
||||||
|
mock_collection.find.return_value = []
|
||||||
|
assert service.get_all() == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_by_id_with_result(mock_mongo_connector, mock_collection):
|
||||||
|
mock_mongo_connector.database = {"news": mock_collection}
|
||||||
|
service = MongoNewsService(mock_mongo_connector)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"aki_prj23_transparenzregister.utils.news_mongo_service.MongoEntryTransformer.transform_outgoing"
|
||||||
|
) as mock_out:
|
||||||
|
mock_collection.find.return_value = [{}]
|
||||||
|
mock_out.return_value = {}
|
||||||
|
assert service.get_by_id("foadh") == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_by_id_no_result(mock_mongo_connector, mock_collection):
|
||||||
|
mock_mongo_connector.database = {"news": mock_collection}
|
||||||
|
service = MongoNewsService(mock_mongo_connector)
|
||||||
|
|
||||||
|
mock_collection.find.return_value = []
|
||||||
|
assert service.get_by_id("foadh") is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_insert(mock_mongo_connector, mock_collection):
|
||||||
|
mock_mongo_connector.database = {"news": mock_collection}
|
||||||
|
service = MongoNewsService(mock_mongo_connector)
|
||||||
|
|
||||||
|
with patch(
|
||||||
|
"aki_prj23_transparenzregister.utils.news_mongo_service.MongoEntryTransformer.transform_ingoing"
|
||||||
|
) as mock_in:
|
||||||
|
mock_collection.insert_one.return_value = {}
|
||||||
|
mock_in.return_value = {}
|
||||||
|
assert service.insert({}) == {}
|
||||||
|
|
||||||
|
|
||||||
|
def test_transform_ingoing():
|
||||||
|
news = News("42", None, None, None, None)
|
||||||
|
result = MongoEntryTransformer.transform_ingoing(news)
|
||||||
|
assert result["_id"] == "42"
|
||||||
|
assert "id" not in result
|
||||||
|
|
||||||
|
|
||||||
|
def test_transform_outgoing():
|
||||||
|
data = {
|
||||||
|
"_id": "4711",
|
||||||
|
"title": "Hello",
|
||||||
|
"date": "Today",
|
||||||
|
"text": "World",
|
||||||
|
"source_url": "chat.openai.com",
|
||||||
|
}
|
||||||
|
expected_result = News(
|
||||||
|
**{
|
||||||
|
"id": "4711",
|
||||||
|
"title": "Hello",
|
||||||
|
"date": "Today",
|
||||||
|
"text": "World",
|
||||||
|
"source_url": "chat.openai.com",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
assert MongoEntryTransformer.transform_outgoing(data) == expected_result
|
||||||
|
Loading…
x
Reference in New Issue
Block a user