test_version.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. """Unit tests for version comparison utilities."""
  2. import pytest
  3. from cli.core.version import compare_versions, is_compatible, parse_version
  4. class TestParseVersion:
  5. """Tests for parse_version function."""
  6. def test_parse_simple_version(self):
  7. """Test parsing simple version string."""
  8. assert parse_version("1.0") == (1, 0)
  9. assert parse_version("1.2") == (1, 2)
  10. assert parse_version("2.5") == (2, 5)
  11. def test_parse_version_with_v_prefix(self):
  12. """Test parsing version with 'v' prefix."""
  13. assert parse_version("v1.0") == (1, 0)
  14. assert parse_version("v2.3") == (2, 3)
  15. def test_parse_version_empty_string(self):
  16. """Test parsing empty string raises ValueError."""
  17. with pytest.raises(ValueError, match="cannot be empty"):
  18. parse_version("")
  19. def test_parse_version_invalid_format(self):
  20. """Test parsing invalid format raises ValueError."""
  21. with pytest.raises(ValueError, match="Invalid version format"):
  22. parse_version("1")
  23. with pytest.raises(ValueError, match="Invalid version format"):
  24. parse_version("1.2.3")
  25. with pytest.raises(ValueError, match="Invalid version format"):
  26. parse_version("invalid")
  27. class TestCompareVersions:
  28. """Tests for compare_versions function."""
  29. def test_compare_equal_versions(self):
  30. """Test comparing equal versions."""
  31. assert compare_versions("1.0", "1.0") == 0
  32. assert compare_versions("2.5", "2.5") == 0
  33. def test_compare_major_version_difference(self):
  34. """Test comparing versions with different major numbers."""
  35. assert compare_versions("2.0", "1.0") == 1
  36. assert compare_versions("1.0", "2.0") == -1
  37. def test_compare_minor_version_difference(self):
  38. """Test comparing versions with different minor numbers."""
  39. assert compare_versions("1.2", "1.0") == 1
  40. assert compare_versions("1.0", "1.2") == -1
  41. def test_compare_with_v_prefix(self):
  42. """Test comparing versions with 'v' prefix."""
  43. assert compare_versions("v1.0", "v1.0") == 0
  44. assert compare_versions("v1.2", "v1.0") == 1
  45. @pytest.mark.parametrize(
  46. "v1,v2,expected",
  47. [
  48. ("1.0", "1.0", 0),
  49. ("1.1", "1.0", 1),
  50. ("1.0", "1.1", -1),
  51. ("2.0", "1.9", 1),
  52. ("0.9", "1.0", -1),
  53. ],
  54. )
  55. def test_compare_versions_parametrized(self, v1, v2, expected):
  56. """Test comparing various version combinations."""
  57. assert compare_versions(v1, v2) == expected
  58. class TestIsCompatible:
  59. """Tests for is_compatible function."""
  60. def test_compatible_equal_versions(self):
  61. """Test compatibility with equal versions."""
  62. assert is_compatible("1.0", "1.0") is True
  63. def test_compatible_newer_version(self):
  64. """Test compatibility with newer current version."""
  65. assert is_compatible("1.2", "1.0") is True
  66. assert is_compatible("2.0", "1.0") is True
  67. def test_incompatible_older_version(self):
  68. """Test incompatibility with older current version."""
  69. assert is_compatible("1.0", "1.2") is False
  70. assert is_compatible("1.0", "2.0") is False
  71. def test_incompatible_invalid_versions(self):
  72. """Test that invalid versions return False for safety."""
  73. assert is_compatible("invalid", "1.0") is False
  74. assert is_compatible("1.0", "invalid") is False
  75. @pytest.mark.parametrize(
  76. "current,required,expected",
  77. [
  78. ("1.0", "1.0", True),
  79. ("1.2", "1.0", True),
  80. ("2.0", "1.0", True),
  81. ("1.0", "1.2", False),
  82. ("0.9", "1.0", False),
  83. ],
  84. )
  85. def test_is_compatible_parametrized(self, current, required, expected):
  86. """Test compatibility checks with various version combinations."""
  87. assert is_compatible(current, required) is expected