-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #381 from LokasWiki/sandbox_v2
add clean architecture for sandbox task
Showing
14 changed files
with
302 additions
and
67 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class PageEntity: | ||
def __init__(self, title: str, text: str, summary: str): | ||
""" | ||
Initializes a new instance of the `PageEntity` class. | ||
Args: | ||
title (str): The title of the page. | ||
text (str): The text content of the page. | ||
summary (str): A summary of the page. | ||
Returns: | ||
None | ||
""" | ||
self.title = title | ||
self.text = text | ||
self.summary = summary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from tasks.sandbox.entities.page_entity import PageEntity | ||
from tasks.sandbox.repositories.pywikibot_page_repository import PywikibotPageRepository | ||
from tasks.sandbox.use_cases.update_page_use_case import UpdatePageUseCase | ||
|
||
|
||
def main() -> int: | ||
page_entity = PageEntity( | ||
title="ويكيبيديا:ملعب", | ||
text="{{عنوان الملعب}}\n<!-- مرحبا! خذ راحتك في تجربة مهارتك في التنسيق والتحرير أسفل هذا السطر. هذه الصفحة لتجارب التعديل ، سيتم تفريغ هذه الصفحة كل 12 ساعة. -->", | ||
summary="بوت: إفراغ الصفحة تلقائيا!" | ||
) | ||
|
||
repository = PywikibotPageRepository() | ||
use_case = UpdatePageUseCase(repository) | ||
use_case.execute(page_entity) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
raise SystemExit(main()) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import pywikibot | ||
|
||
from tasks.sandbox.entities.page_entity import PageEntity | ||
from tasks.sandbox.use_cases.update_page_use_case import PageRepository | ||
|
||
|
||
class PywikibotPageRepository(PageRepository): | ||
def __init__(self): | ||
""" | ||
Initializes the PywikibotPageRepository class. | ||
This method creates an instance of the PywikibotPageRepository class and sets the `site` attribute to the current site object obtained from the pywikibot library. | ||
Parameters: | ||
None | ||
Returns: | ||
None | ||
""" | ||
self.site = pywikibot.Site() | ||
|
||
def save_page(self, page: PageEntity) -> None: | ||
""" | ||
Saves a page using the Pywikibot library. | ||
Args: | ||
page (PageEntity): The page entity to be saved. | ||
Returns: | ||
None: This function does not return anything. | ||
Raises: | ||
pywikibot.Error: If there is an error while saving the page. | ||
This function takes a `PageEntity` object as input and saves it using the Pywikibot library. It creates a `pywikibot.Page` object with the `title` attribute of the `page` parameter and sets its `text` attribute to the `text` attribute of the `page` parameter. Finally, it saves the page with the `summary` attribute of the `page` parameter. | ||
Note: | ||
- The `PageEntity` object should have the `title` and `text` attributes. | ||
- The `summary` attribute of the `page` parameter is used as the summary for the page save operation. | ||
""" | ||
pywikibot_page = pywikibot.Page(self.site, page.title) | ||
pywikibot_page.text = page.text | ||
pywikibot_page.save(summary=page.summary) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from typing import Protocol | ||
|
||
from tasks.sandbox.entities.page_entity import PageEntity | ||
|
||
|
||
class PageRepository(Protocol): | ||
def save_page(self, page: PageEntity) -> None: | ||
""" | ||
Save a PageEntity object to the repository. | ||
Args: | ||
page (PageEntity): The PageEntity object to be saved. | ||
Returns: | ||
None: This function does not return anything. | ||
""" | ||
... | ||
|
||
|
||
class UpdatePageUseCase: | ||
def __init__(self, repository: PageRepository): | ||
""" | ||
Initializes a new instance of the `UpdatePageUseCase` class. | ||
Args: | ||
repository (PageRepository): The repository object used to save the page. | ||
Returns: | ||
None | ||
""" | ||
self.repository = repository | ||
|
||
def execute(self, page: PageEntity) -> None: | ||
""" | ||
Execute the update page use case. | ||
This method updates a page entity by saving it to the repository. | ||
Parameters: | ||
page (PageEntity): The page entity to be saved. | ||
Returns: | ||
None: This function does not return anything. | ||
""" | ||
self.repository.save_page(page) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from tasks.sandbox.main import main | ||
|
||
|
||
class TestMain(unittest.TestCase): | ||
@patch('tasks.sandbox.main.UpdatePageUseCase') | ||
@patch('tasks.sandbox.main.PywikibotPageRepository') | ||
@patch('tasks.sandbox.main.PageEntity') | ||
def test_main(self, mock_page_entity, mock_repository, mock_use_case): | ||
""" | ||
Test the main function of the sandbox.main module. | ||
This function tests the main function of the sandbox.main module by mocking the dependencies and | ||
verifying that the function returns 0 and that the necessary methods are called. | ||
Parameters: | ||
self (TestMain): The test case instance. | ||
mock_page_entity (Mock): A mock object for the PageEntity class. | ||
mock_repository (Mock): A mock object for the PywikibotPageRepository class. | ||
mock_use_case (Mock): A mock object for the UpdatePageUseCase class. | ||
Returns: | ||
None | ||
Raises: | ||
AssertionError: If the result of the main function is not equal to 0 or if the necessary methods | ||
are not called. | ||
""" | ||
# Arrange | ||
mock_page_entity.return_value = mock_page_entity | ||
mock_use_case_instance = Mock() | ||
mock_use_case.return_value = mock_use_case_instance | ||
|
||
# Act | ||
result = main() | ||
|
||
# Assert | ||
self.assertEqual(result, 0) | ||
mock_use_case_instance.execute.assert_called_once() | ||
mock_page_entity.assert_called_once_with( | ||
title="ويكيبيديا:ملعب", | ||
text="{{عنوان الملعب}}\n<!-- مرحبا! خذ راحتك في تجربة مهارتك في التنسيق والتحرير أسفل هذا السطر. هذه الصفحة لتجارب التعديل ، سيتم تفريغ هذه الصفحة كل 12 ساعة. -->", | ||
summary="بوت: إفراغ الصفحة تلقائيا!" | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import unittest | ||
|
||
from tasks.sandbox.entities.page_entity import PageEntity | ||
|
||
|
||
class TestPageEntity(unittest.TestCase): | ||
def test_initialization(self): | ||
""" | ||
Test the initialization of the PageEntity class. | ||
This test case checks if the attributes of the PageEntity object are correctly initialized | ||
when creating a new instance of the class. It creates a PageEntity object with the | ||
specified title, text, and summary values. Then, it asserts that the title, text, and | ||
summary attributes of the object are equal to the expected values. | ||
Parameters: | ||
self (TestPageEntity): The current test case instance. | ||
Returns: | ||
None | ||
""" | ||
page = PageEntity( | ||
title="Test Title", | ||
text="Test Text", | ||
summary="Test Summary" | ||
) | ||
self.assertEqual(page.title, "Test Title") | ||
self.assertEqual(page.text, "Test Text") | ||
self.assertEqual(page.summary, "Test Summary") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import unittest | ||
from unittest.mock import Mock, patch | ||
|
||
from tasks.sandbox.entities.page_entity import PageEntity | ||
from tasks.sandbox.repositories.pywikibot_page_repository import PywikibotPageRepository | ||
|
||
|
||
class TestPywikibotPageRepository(unittest.TestCase): | ||
@patch('tasks.sandbox.repositories.pywikibot_page_repository.pywikibot.Page') | ||
@patch('tasks.sandbox.repositories.pywikibot_page_repository.pywikibot.Site') | ||
def test_save_page(self, mock_site, mock_page): | ||
""" | ||
Test case for the `save_page` method of the `PywikibotPageRepository` class. | ||
This test case verifies that the `save_page` method of the `PywikibotPageRepository` class | ||
correctly saves a page using the `pywikibot` library. It does this by mocking the `pywikibot.Page` | ||
and `pywikibot.Site` classes and asserting that the `save_page` method calls the appropriate methods | ||
on the mocked objects. | ||
Parameters: | ||
- self: The test case instance. | ||
- mock_site: A mock object representing the `pywikibot.Site` class. | ||
- mock_page: A mock object representing the `pywikibot.Page` class. | ||
Returns: | ||
None | ||
""" | ||
# Arrange | ||
mock_site.return_value = Mock() | ||
mock_page_instance = Mock() | ||
mock_page.return_value = mock_page_instance | ||
|
||
repository = PywikibotPageRepository() | ||
page = PageEntity( | ||
title="Test Title", | ||
text="Test Text", | ||
summary="Test Summary" | ||
) | ||
|
||
# Act | ||
repository.save_page(page) | ||
|
||
# Assert | ||
mock_page.assert_called_once_with(mock_site.return_value, page.title) | ||
mock_page_instance.text = page.text | ||
mock_page_instance.save.assert_called_once_with(summary=page.summary) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import unittest | ||
from unittest.mock import Mock | ||
|
||
from tasks.sandbox.entities.page_entity import PageEntity | ||
from tasks.sandbox.use_cases.update_page_use_case import UpdatePageUseCase | ||
|
||
|
||
class TestUpdatePageUseCase(unittest.TestCase): | ||
def test_execute(self): | ||
""" | ||
Test the execution of the UpdatePageUseCase. | ||
This test case verifies that the execute method of the UpdatePageUseCase class | ||
correctly calls the save_page method of the mock repository with the provided page entity. | ||
Parameters: | ||
self (TestUpdatePageUseCase): The test case instance. | ||
Returns: | ||
None | ||
Raises: | ||
AssertionError: If the save_page method of the mock repository is not called once with the provided page entity. | ||
""" | ||
# Arrange | ||
mock_repository = Mock() | ||
use_case = UpdatePageUseCase(mock_repository) | ||
page = PageEntity( | ||
title="Test Title", | ||
text="Test Text", | ||
summary="Test Summary" | ||
) | ||
|
||
# Act | ||
use_case.execute(page) | ||
|
||
# Assert | ||
mock_repository.save_page.assert_called_once_with(page) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters