From cfd8b2fcaed33d83a2685d8f91cf25d3011f108e Mon Sep 17 00:00:00 2001 From: axif Date: Sun, 25 Aug 2024 18:27:39 +0600 Subject: [PATCH 1/5] fix: version message, add test for version --- src/scribe_data/cli/upgrade.py | 2 +- src/scribe_data/cli/version.py | 4 +- tests/cli/version.py | 83 ++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 4 deletions(-) create mode 100644 tests/cli/version.py diff --git a/src/scribe_data/cli/upgrade.py b/src/scribe_data/cli/upgrade.py index 70060b4d3..4197209ba 100644 --- a/src/scribe_data/cli/upgrade.py +++ b/src/scribe_data/cli/upgrade.py @@ -32,7 +32,7 @@ from scribe_data.cli.version import get_latest_version, get_local_version -def upgrade(): +def upgrade_cli(): local_version = get_local_version() latest_version = get_latest_version() latest_version = latest_version.split("v")[-1] diff --git a/src/scribe_data/cli/version.py b/src/scribe_data/cli/version.py index 15fa90daa..1324c62cb 100644 --- a/src/scribe_data/cli/version.py +++ b/src/scribe_data/cli/version.py @@ -56,9 +56,7 @@ def get_version_message(): if local_version == latest_version: return f"{local_version}" - update_message = ( - f"Scribe-Data v{local_version} (Update available: v{latest_version})\n" - ) + update_message = f"{local_version} (Update available: {latest_version})\n" update_message += "To update: pip scribe-data --upgrade" return update_message diff --git a/tests/cli/version.py b/tests/cli/version.py new file mode 100644 index 000000000..c9e69881f --- /dev/null +++ b/tests/cli/version.py @@ -0,0 +1,83 @@ +""" +Tests for the version file functions. + +.. raw:: html + +""" + +import unittest +from unittest.mock import patch +from scribe_data.cli.version import ( + get_local_version, + get_latest_version, + get_version_message, +) +import pkg_resources + + +class TestVersionFunctions(unittest.TestCase): + @patch("pkg_resources.get_distribution") + def test_get_local_version_installed(self, mock_get_distribution): + mock_get_distribution.return_value.version = "1.0.0" + self.assertEqual(get_local_version(), "1.0.0") + + @patch( + "pkg_resources.get_distribution", side_effect=pkg_resources.DistributionNotFound + ) + def test_get_local_version_not_installed(self, mock_get_distribution): + self.assertEqual(get_local_version(), "Unknown (Not installed via pip)") + + @patch("requests.get") + def test_get_latest_version(self, mock_get): + mock_get.return_value.status_code = 200 + mock_get.return_value.json.return_value = {"name": "v1.0.1"} + self.assertEqual(get_latest_version(), "v1.0.1") + + @patch("requests.get", side_effect=Exception("Unable to fetch version")) + def test_get_latest_version_failure(self, mock_get): + self.assertEqual(get_latest_version(), "Unknown (Unable to fetch version)") + + @patch("scribe_data.cli.version.get_local_version", return_value="1.0.0") + @patch( + "scribe_data.cli.version.get_latest_version", return_value="Scribe-Data v1.0.0" + ) + def test_get_version_message_up_to_date( + self, mock_latest_version, mock_local_version + ): + """ + Tests the scenario where the local version is up to date with the latest version + """ + expected_message = "Scribe-Data v1.0.0" + self.assertEqual(get_version_message(), expected_message) + + @patch("scribe_data.cli.version.get_local_version", return_value="1.0.0") + @patch( + "scribe_data.cli.version.get_latest_version", return_value="Scribe-Data v1.0.1" + ) + def test_get_version_message_update_available( + self, mock_latest_version, mock_local_version + ): + """ + Tests the scenario where a newer version is available, suggesting an update + """ + expected_message = "Scribe-Data v1.0.0 (Update available: Scribe-Data v1.0.1)\nTo update: pip scribe-data --upgrade" + self.assertEqual(get_version_message(), expected_message) + + +if __name__ == "__main__": + unittest.main() From 9108b612169e8ee3f6d0a4811a8570602b6a94a1 Mon Sep 17 00:00:00 2001 From: axif Date: Sun, 25 Aug 2024 18:50:41 +0600 Subject: [PATCH 2/5] rename version test file --- tests/cli/{version.py => test_version.py} | 4 ---- 1 file changed, 4 deletions(-) rename tests/cli/{version.py => test_version.py} (98%) diff --git a/tests/cli/version.py b/tests/cli/test_version.py similarity index 98% rename from tests/cli/version.py rename to tests/cli/test_version.py index c9e69881f..2c2ae98fb 100644 --- a/tests/cli/version.py +++ b/tests/cli/test_version.py @@ -77,7 +77,3 @@ def test_get_version_message_update_available( """ expected_message = "Scribe-Data v1.0.0 (Update available: Scribe-Data v1.0.1)\nTo update: pip scribe-data --upgrade" self.assertEqual(get_version_message(), expected_message) - - -if __name__ == "__main__": - unittest.main() From 6ee057e9866ba1eb606a0e1cd37cf0e57d7a3814 Mon Sep 17 00:00:00 2001 From: axif Date: Sun, 25 Aug 2024 18:54:33 +0600 Subject: [PATCH 3/5] rename update to Upgrade --- src/scribe_data/cli/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scribe_data/cli/version.py b/src/scribe_data/cli/version.py index 1324c62cb..0267e14d4 100644 --- a/src/scribe_data/cli/version.py +++ b/src/scribe_data/cli/version.py @@ -56,7 +56,7 @@ def get_version_message(): if local_version == latest_version: return f"{local_version}" - update_message = f"{local_version} (Update available: {latest_version})\n" + update_message = f"{local_version} (Upgrade available: {latest_version})\n" update_message += "To update: pip scribe-data --upgrade" return update_message From e5e1bfda7c0b0cd1e6976b5ad83d0df8f5e7191e Mon Sep 17 00:00:00 2001 From: axif Date: Sun, 25 Aug 2024 18:59:18 +0600 Subject: [PATCH 4/5] fix test message --- tests/cli/test_version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cli/test_version.py b/tests/cli/test_version.py index 2c2ae98fb..edb13f327 100644 --- a/tests/cli/test_version.py +++ b/tests/cli/test_version.py @@ -75,5 +75,5 @@ def test_get_version_message_update_available( """ Tests the scenario where a newer version is available, suggesting an update """ - expected_message = "Scribe-Data v1.0.0 (Update available: Scribe-Data v1.0.1)\nTo update: pip scribe-data --upgrade" + expected_message = "Scribe-Data v1.0.0 (Upgrade available: Scribe-Data v1.0.1)\nTo update: pip scribe-data --upgrade" self.assertEqual(get_version_message(), expected_message) From 5a02908b2bbd6d9254ef045f1b3aa4b0177d32bd Mon Sep 17 00:00:00 2001 From: Andrew Tavis McAllister Date: Mon, 26 Aug 2024 01:43:19 +0200 Subject: [PATCH 5/5] Minor formatting and fixing usage of renamed function --- src/scribe_data/cli/main.py | 4 ++-- tests/cli/test_version.py | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/scribe_data/cli/main.py b/src/scribe_data/cli/main.py index b48144c4c..057be1f0e 100644 --- a/src/scribe_data/cli/main.py +++ b/src/scribe_data/cli/main.py @@ -28,7 +28,7 @@ from scribe_data.cli.interactive import start_interactive_mode from scribe_data.cli.list import list_wrapper from scribe_data.cli.total import get_total_lexemes -from scribe_data.cli.upgrade import upgrade +from scribe_data.cli.upgrade import upgrade_cli from scribe_data.cli.version import get_version_message LIST_DESCRIPTION = "List languages, data types and combinations of each that Scribe-Data can be used for." @@ -196,7 +196,7 @@ def main() -> None: args = parser.parse_args() if args.upgrade: - upgrade() + upgrade_cli() return if not args.command: diff --git a/tests/cli/test_version.py b/tests/cli/test_version.py index edb13f327..09f56b96e 100644 --- a/tests/cli/test_version.py +++ b/tests/cli/test_version.py @@ -1,5 +1,5 @@ """ -Tests for the version file functions. +Tests for the CLI version functionality. .. raw:: html