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

refactored logic.py method filter_cites #8

Merged
merged 8 commits into from
Dec 10, 2023
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ Ohtu miniprojekti
- [Käytänteet](/docs/kaytanteet.md)
- [Arkkitehtuuri](/docs/arkkitehtuuri.md)
- [Retrodokumentti](retro.md)
- [Hyväksymistestit](/src/tests/robot/)
- [Hyväksymistestit](/src/tests/robot/)

3 changes: 3 additions & 0 deletions src/entities/cite.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ def __gt__(self, other: "Cite") -> bool:

def __ge__(self, other: "Cite") -> bool:
return self > other or self == other

def __hash__(self) -> int:
return hash(self.id)
26 changes: 13 additions & 13 deletions src/services/logic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Callable

from entities.cite import Cite
from repositories.cite_repository import cite_repository
from repositories.cite_repository import CiteRepository, cite_repository
from services.filter_service import FilterService


Expand All @@ -14,8 +16,12 @@ def __init__(self, repository=cite_repository, filter_service=FilterService()):
oletuksena cite_repository.py:n sisällä luotu
"""

self.repository = repository
self.__filter_service = filter_service
self.repository: CiteRepository = repository
self.__filters: dict[str, Callable] = {
"id": filter_service.filter_by_id,
"name": filter_service.filter_by_name,
"author": filter_service.filter_by_author,
}

def filter_cites(self, search: str, filters: set[str]) -> list[Cite]:
"""Hakee hakusanan ja tyypin mukaiset viitteet
Expand All @@ -27,19 +33,13 @@ def filter_cites(self, search: str, filters: set[str]) -> list[Cite]:
Returns:
list[Cite]: lista Cite olioita
"""
cites = []

if "name" in filters:
cites.append(self.__filter_service.filter_by_name(search))
elif "author" in filters:
cites.append(self.__filter_service.filter_by_author(search))
elif "tag" in filters:
cites.append(self.__filter_service.filter_by_id(search))
result = set()

if "id" in filters:
cites.append(self.__filter_service.filter_by_id(search))
for filter in filters:
result = result | set(self.__filters[filter](search))

return cites[0]
return list(result)

def create_cite(self, id: str, type: str, authors: list[str], fields: dict):
"""Lisää uuden viitteen.
Expand Down
82 changes: 56 additions & 26 deletions src/tests/services/test_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,74 @@


class TestLogic(unittest.TestCase):
def initialize_filter_service(self) -> Mock:
filter_service = Mock()
filter_service.filter_by_id.return_value = [self.id_test]
filter_service.filter_by_name.return_value = [self.name_test]
filter_service.filter_by_author.return_value = [self.author_test]

return filter_service

def initialize_cite_repository(self) -> Mock:
cite_repository = Mock()
cite_repository.get_all_cites.return_value = self.cites

return cite_repository

def setUp(self):
self.repository_mock = Mock()
self.filter_service_mock = Mock()
self.logic = Logic(self.repository_mock, self.filter_service_mock)
self.cites = [
Cite(1, "book", ["Author1", "Author2"], {"title": "Testi1", "year": 1234}),
Cite(2, "article", ["Pekka"], {"title": "123tes12", "year": 1000}),
Cite(
3,
"book",
["Author", "Author2", "Mikko"],
{"title": "Moiit", "year": 1234},
),
]
self.id_test = Cite("id_test", "book", fields={"title": ""})
self.name_test = Cite("title", "article", fields={"title": "title_test"})
self.author_test = Cite(
"author", "journal", ["author_test"], fields={"title": ""}
)
self.cites = [self.id_test, self.name_test, self.author_test]

self.filter_service_mock = self.initialize_filter_service()
self.cite_repository_mock = self.initialize_cite_repository()

self.logic = Logic(self.cite_repository_mock, self.filter_service_mock)

def test_create_cite(self):
create = self.logic.create_cite(
"123", "book", [], {"year": 1900, "author": "Hello"}
)
self.repository_mock.add_cite.assert_called()
self.logic.create_cite("123", "book", [], {"year": 1900, "author": "Hello"})
self.cite_repository_mock.add_cite.assert_called_once()

def test_logic_get_all_cites(self):
self.repository_mock.get_all_cites.return_value = self.cites
get_all = self.logic.get_all_cites()

self.assertEqual(get_all, self.cites)

def test_filter_cites_calls_filter_by_name_when_name_searched(self):
self.logic.filter_cites("Tes", {"name"})
self.filter_service_mock.filter_by_name.assert_called()
def test_filter_cites_by_name(self):
result = self.logic.filter_cites("Test", {"name"})

self.assertEqual(len(result), 1)
self.assertIn(self.name_test, result)

def test_filter_cites_by_id(self):
result = self.logic.filter_cites("Test", {"id"})

self.assertEqual(len(result), 1)
self.assertIn(self.id_test, result)

def test_filter_cites_by_author(self):
result = self.logic.filter_cites("Test", {"author"})

self.assertEqual(len(result), 1)
self.assertIn(self.author_test, result)

def test_filter_cites_by_multiple_filters_and_multiple_hit(self):
result = self.logic.filter_cites("Test", {"id", "name", "author"})

self.assertEqual(len(result), 3)
self.assertEqual(sorted(self.cites), sorted(result))

# def test_filter_cites_doesnt_call_filter_by_name_when_not_search(self):
# self.logic.filter_cites("Tes", {"authors"})
# self.filter_service_mock.filter_by_name.assert_not_called()
def test_filter_cites_with_nonexistent_filter(self):
result = self.logic.filter_cites("Test", set())
self.assertEqual(len(result), 0)

def test_remove_cite(self):
self.logic.remove_cite("123")
self.repository_mock.remove_cite.assert_called()
self.cite_repository_mock.remove_cite.assert_called()

def test_remova_all_cites(self):
self.logic.remove_all_cites()
self.repository_mock.remove_all_cites.assert_called()
self.cite_repository_mock.remove_all_cites.assert_called()