|
3 | 3 | import unittest |
4 | 4 | from unittest.mock import MagicMock, patch |
5 | 5 |
|
6 | | -import pytest |
7 | | - |
8 | 6 | sys.path.insert( |
9 | 7 | 0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../src")) |
10 | 8 | ) |
@@ -460,6 +458,149 @@ def stub_add_alias_entry(): |
460 | 458 | self.assertGreaterEqual(len(self.app.social_entries), 1) |
461 | 459 |
|
462 | 460 |
|
| 461 | +class TestUtilityFunctions(unittest.TestCase): |
| 462 | + def test_create_pr(self): |
| 463 | + from unittest.mock import MagicMock, patch |
| 464 | + |
| 465 | + from edit_python_pe.main import create_pr |
| 466 | + |
| 467 | + file_content = "Test content" |
| 468 | + current_file = None |
| 469 | + repo_path = "/fake/repo" |
| 470 | + original_repo = MagicMock() |
| 471 | + forked_repo = MagicMock() |
| 472 | + forked_repo.owner.login = "forkowner" |
| 473 | + token = "fake-token" |
| 474 | + aliases = ["alias1"] |
| 475 | + name = "Test Name" |
| 476 | + email = "test@email.com" |
| 477 | + with ( |
| 478 | + patch("edit_python_pe.main.write_file") as mock_write_file, |
| 479 | + patch( |
| 480 | + "edit_python_pe.main.commit_and_push" |
| 481 | + ) as mock_commit_and_push, |
| 482 | + ): |
| 483 | + mock_commit_and_push.return_value = ( |
| 484 | + "Added testfile.md", |
| 485 | + MagicMock(), |
| 486 | + MagicMock(), |
| 487 | + MagicMock(), |
| 488 | + ) |
| 489 | + original_repo.create_pull = MagicMock() |
| 490 | + result = create_pr( |
| 491 | + file_content, |
| 492 | + current_file, |
| 493 | + repo_path, |
| 494 | + original_repo, |
| 495 | + forked_repo, |
| 496 | + token, |
| 497 | + aliases, |
| 498 | + name, |
| 499 | + email, |
| 500 | + ) |
| 501 | + mock_write_file.assert_called() |
| 502 | + mock_commit_and_push.assert_called() |
| 503 | + original_repo.create_pull.assert_called() |
| 504 | + self.assertIn("guardado", result) |
| 505 | + |
| 506 | + def test_compute_file_name_alias_used(self): |
| 507 | + from edit_python_pe.main import compute_file_name |
| 508 | + |
| 509 | + aliases = ["CoolAlias"] |
| 510 | + name = "John Doe" |
| 511 | + email = "john@example.com" |
| 512 | + filename = compute_file_name(aliases, name, email) |
| 513 | + self.assertTrue(filename.startswith("coolalias-")) |
| 514 | + self.assertTrue(filename.endswith(".md")) |
| 515 | + self.assertIn("-", filename) |
| 516 | + self.assertEqual(filename.count("-"), 1) |
| 517 | + |
| 518 | + def test_compute_file_name_name_used_if_no_alias(self): |
| 519 | + from edit_python_pe.main import compute_file_name |
| 520 | + |
| 521 | + aliases = [] |
| 522 | + name = "Jane Doe" |
| 523 | + email = "jane@example.com" |
| 524 | + filename = compute_file_name(aliases, name, email) |
| 525 | + self.assertTrue(filename.startswith("jane_doe-")) |
| 526 | + self.assertTrue(filename.endswith(".md")) |
| 527 | + |
| 528 | + def test_compute_file_name_uniqueness(self): |
| 529 | + from edit_python_pe.main import compute_file_name |
| 530 | + |
| 531 | + aliases = ["Alias"] |
| 532 | + name = "Name" |
| 533 | + email1 = "email1@example.com" |
| 534 | + email2 = "email2@example.com" |
| 535 | + filename1 = compute_file_name(aliases, name, email1) |
| 536 | + filename2 = compute_file_name(aliases, name, email2) |
| 537 | + self.assertNotEqual(filename1, filename2) |
| 538 | + |
| 539 | + def test_write_file(self): |
| 540 | + import builtins |
| 541 | + from unittest.mock import MagicMock, patch |
| 542 | + |
| 543 | + from edit_python_pe.main import write_file |
| 544 | + |
| 545 | + file_content = "Hello, world!" |
| 546 | + file_path = "/tmp/testdir/testfile.txt" |
| 547 | + with ( |
| 548 | + patch("os.makedirs") as makedirs, |
| 549 | + patch("builtins.open", MagicMock()) as mock_open, |
| 550 | + ): |
| 551 | + write_file(file_content, file_path) |
| 552 | + makedirs.assert_called_with("/tmp/testdir", exist_ok=True) |
| 553 | + mock_open.assert_called_with(file_path, "w", encoding="utf-8") |
| 554 | + handle = mock_open.return_value.__enter__.return_value |
| 555 | + handle.write.assert_called_with(file_content) |
| 556 | + |
| 557 | + def test_commit_and_push(self): |
| 558 | + from unittest.mock import MagicMock, patch |
| 559 | + |
| 560 | + from edit_python_pe.main import commit_and_push |
| 561 | + |
| 562 | + repo_path = "/fake/repo" |
| 563 | + token = "fake-token" |
| 564 | + was_changed = True |
| 565 | + name_file = "test.md" |
| 566 | + file_path = "/fake/repo/blog/members/test.md" |
| 567 | + name = "Test Name" |
| 568 | + email = "test@email.com" |
| 569 | + with patch("pygit2.repository.Repository") as RepoMock: |
| 570 | + repo_instance = RepoMock.return_value |
| 571 | + repo_instance.index.add = MagicMock() |
| 572 | + repo_instance.index.write = MagicMock() |
| 573 | + repo_instance.index.write_tree = MagicMock(return_value="treeid") |
| 574 | + repo_instance.head_is_unborn = False |
| 575 | + repo_instance.head = MagicMock() |
| 576 | + repo_instance.head.target = "commitid" |
| 577 | + repo_instance.create_commit = MagicMock() |
| 578 | + repo_instance.remotes = {"origin": MagicMock()} |
| 579 | + repo_instance.remotes["origin"].push = MagicMock() |
| 580 | + with ( |
| 581 | + patch("pygit2.Signature") as SignatureMock, |
| 582 | + patch( |
| 583 | + "pygit2.callbacks.RemoteCallbacks" |
| 584 | + ) as RemoteCallbacksMock, |
| 585 | + ): |
| 586 | + SignatureMock.return_value = MagicMock() |
| 587 | + RemoteCallbacksMock.return_value = MagicMock() |
| 588 | + commit_msg, repo, remote, callbacks = commit_and_push( |
| 589 | + repo_path, |
| 590 | + token, |
| 591 | + was_changed, |
| 592 | + name_file, |
| 593 | + file_path, |
| 594 | + name, |
| 595 | + email, |
| 596 | + ) |
| 597 | + repo_instance.index.add.assert_called() |
| 598 | + repo_instance.index.write.assert_called() |
| 599 | + repo_instance.create_commit.assert_called() |
| 600 | + repo_instance.remotes["origin"].push.assert_called() |
| 601 | + self.assertEqual(commit_msg, f"Changed {name_file}") |
| 602 | + |
| 603 | + |
463 | 604 | class TestGetRepo(unittest.TestCase): |
464 | 605 | @patch("edit_python_pe.main.getpass.getpass", return_value="valid-token") |
465 | 606 | @patch("edit_python_pe.main.Github") |
|
0 commit comments