|
8 | 8 | TestSearchIssues: A class to test the search_issues function.
|
9 | 9 | TestGetPerIssueMetrics: A class to test the get_per_issue_metrics function.
|
10 | 10 | TestGetEnvVars: A class to test the get_env_vars function.
|
11 |
| -
|
| 11 | + TestEvaluateMarkdownFileSize: A class to test the evaluate_markdown_file_size function. |
12 | 12 | """
|
13 | 13 |
|
14 | 14 | import os
|
15 | 15 | import unittest
|
16 | 16 | from datetime import datetime, timedelta
|
17 |
| -from unittest.mock import MagicMock, patch |
| 17 | +from unittest.mock import MagicMock, call, patch |
18 | 18 |
|
19 | 19 | from issue_metrics import (
|
20 | 20 | IssueWithMetrics,
|
| 21 | + evaluate_markdown_file_size, |
21 | 22 | get_env_vars,
|
22 | 23 | get_per_issue_metrics,
|
23 | 24 | measure_time_to_close,
|
@@ -176,6 +177,7 @@ def test_get_per_issue_metrics_with_hide_envs(self):
|
176 | 177 | "HIDE_TIME_TO_ANSWER": "false",
|
177 | 178 | "HIDE_TIME_TO_CLOSE": "false",
|
178 | 179 | "HIDE_TIME_TO_FIRST_RESPONSE": "false",
|
| 180 | + "OUTPUT_FILE": "test_issue_metrics.md" |
179 | 181 | },
|
180 | 182 | )
|
181 | 183 | def test_get_per_issue_metrics_without_hide_envs(self):
|
@@ -478,5 +480,59 @@ def test_get_per_issue_metrics_with_discussion_with_hide_envs(self):
|
478 | 480 | self.assertEqual(metrics[0][1].time_to_first_response, None)
|
479 | 481 |
|
480 | 482 |
|
| 483 | +class TestEvaluateMarkdownFileSize(unittest.TestCase): |
| 484 | + """Test suite for the evaluate_markdown_file_size function.""" |
| 485 | + |
| 486 | + @patch.dict( |
| 487 | + os.environ, |
| 488 | + { |
| 489 | + "GH_TOKEN": "test_token", |
| 490 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 491 | + "OUTPUT_FILE": "test_issue_metrics.md" |
| 492 | + } |
| 493 | + ) |
| 494 | + @patch("issue_metrics.markdown_too_large_for_issue_body") |
| 495 | + def test_markdown_too_large_for_issue_body_called_with_output_file(self, mock_evaluate): |
| 496 | + """ |
| 497 | + Test that the function uses the output_file. |
| 498 | + """ |
| 499 | + mock_evaluate.return_value = False |
| 500 | + evaluate_markdown_file_size("test_issue_metrics.md") |
| 501 | + |
| 502 | + mock_evaluate.assert_called_with("test_issue_metrics.md", 65535) |
| 503 | + |
| 504 | + @patch.dict( |
| 505 | + os.environ, |
| 506 | + { |
| 507 | + "GH_TOKEN": "test_token", |
| 508 | + "SEARCH_QUERY": "is:issue is:open repo:user/repo", |
| 509 | + "OUTPUT_FILE": "test_issue_metrics.md" |
| 510 | + } |
| 511 | + ) |
| 512 | + @patch("issue_metrics.print") |
| 513 | + @patch("shutil.move") |
| 514 | + @patch("issue_metrics.split_markdown_file") |
| 515 | + @patch("issue_metrics.markdown_too_large_for_issue_body") |
| 516 | + def test_split_markdown_file_when_file_size_too_large(self, mock_evaluate, mock_split, mock_move, mock_print): |
| 517 | + """ |
| 518 | + Test that the function is called with the output_file |
| 519 | + environment variable. |
| 520 | + """ |
| 521 | + mock_evaluate.return_value = True |
| 522 | + evaluate_markdown_file_size("test_issue_metrics.md") |
| 523 | + |
| 524 | + mock_split.assert_called_with("test_issue_metrics.md", 65535) |
| 525 | + mock_move.assert_has_calls([ |
| 526 | + call("test_issue_metrics.md", "test_issue_metrics_full.md"), |
| 527 | + call("test_issue_metrics_0.md", "test_issue_metrics.md") |
| 528 | + ]) |
| 529 | + mock_print.assert_called_with( |
| 530 | + "Issue metrics markdown file is too large for GitHub issue body and has been \ |
| 531 | +split into multiple files. ie. test_issue_metrics.md, test_issue_metrics_1.md, etc. \ |
| 532 | +The full file is saved as test_issue_metrics_full.md\n\ |
| 533 | +See https://github.com/github/issue-metrics/blob/main/docs/dealing-with-large-issue-metrics.md" |
| 534 | + ) |
| 535 | + |
| 536 | + |
481 | 537 | if __name__ == "__main__":
|
482 | 538 | unittest.main()
|
0 commit comments