Skip to content

Commit

Permalink
Merge pull request #381 from LokasWiki/sandbox_v2
Browse files Browse the repository at this point in the history
add clean architecture  for sandbox task
loka1 authored Aug 20, 2024
2 parents 3213b36 + e64c746 commit 46f74bb
Showing 14 changed files with 302 additions and 67 deletions.
17 changes: 0 additions & 17 deletions tasks/sandbox/clear.py

This file was deleted.

Empty file.
16 changes: 16 additions & 0 deletions tasks/sandbox/entities/page_entity.py
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
21 changes: 21 additions & 0 deletions tasks/sandbox/main.py
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.
43 changes: 43 additions & 0 deletions tasks/sandbox/repositories/pywikibot_page_repository.py
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.
45 changes: 45 additions & 0 deletions tasks/sandbox/use_cases/update_page_use_case.py
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)
49 changes: 0 additions & 49 deletions tests/tasks/sandbox/clear_test.py

This file was deleted.

51 changes: 51 additions & 0 deletions tests/tasks/sandbox/test_main.py
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()
33 changes: 33 additions & 0 deletions tests/tasks/sandbox/test_page_entity.py
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()
50 changes: 50 additions & 0 deletions tests/tasks/sandbox/test_pywikibot_page_repository.py
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()
42 changes: 42 additions & 0 deletions tests/tasks/sandbox/test_update_page_use_case.py
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()
2 changes: 1 addition & 1 deletion toolforge/jobs/clear_sandbox.sh
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ export PYWIKIBOT_DIR="$HOME/repos"
export PYTHONPATH="${PYTHONPATH}:$HOME/repos"


python3 "$HOME"/repos/tasks/sandbox/clear.py
python3 "$HOME"/repos/tasks/sandbox/main.py
python3 "$HOME"/repos/tasks/copypatrol/update.py

# Exit the script after running all the Python files

0 comments on commit 46f74bb

Please sign in to comment.