Skip to content

Commit

Permalink
Change: Migrate tests for Version and VersionCalculator
Browse files Browse the repository at this point in the history
Both classes are now abstract and have two implementations. One for
PEP440 and the other for SemVer.
  • Loading branch information
bjoernricks authored and y0urself committed Mar 14, 2023
1 parent 647f359 commit 2c6ca13
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 72 deletions.
16 changes: 16 additions & 0 deletions tests/version/schemes/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (C) 2023 Greenbone Networks GmbH
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,44 @@
import unittest
from datetime import datetime

from pontos.version.calculator import VersionCalculator
from pontos.version.errors import VersionError
from pontos.version.version import Version
from pontos.version.schemes._pep440 import PEP440Version as Version
from pontos.version.schemes._pep440 import (
PEP440VersionCalculator as VersionCalculator,
)


class VersionCalculatorTestCase(unittest.TestCase):
class PEP440VersionTestCase(unittest.TestCase):
def test_parse_version(self):
versions = [
"0.0.1",
"1.2.3",
"1.2.3.post1",
"1.2.3a1",
"1.2.3b1",
"1.2.3rc1",
"1.2.3a1+dev1",
"22.4.1",
"22.4.1.dev1",
"22.4.1.dev3",
]
for version in versions:
self.assertEqual(Version.from_string(version), Version(version))

def test_parse_error(self):
versions = [
"abc",
"1.2.3d",
]

for version in versions:
with self.assertRaisesRegex(
VersionError, "^Invalid version: '.*'$"
):
Version.from_string(version)


class PEP440VersionCalculatorTestCase(unittest.TestCase):
def test_next_patch_version(self):
calculator = VersionCalculator()

Expand Down Expand Up @@ -56,10 +88,12 @@ def test_next_patch_version(self):
current_versions, assert_versions
):
release_version = calculator.next_patch_version(
Version(current_version)
Version.from_string(current_version)
)

self.assertEqual(release_version, Version(assert_version))
self.assertEqual(
release_version, Version.from_string(assert_version)
)

def test_next_calendar_versions(self):
calculator = VersionCalculator()
Expand All @@ -83,21 +117,25 @@ def test_next_calendar_versions(self):
current_versions, assert_versions
):
release_version = calculator.next_calendar_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))

def test_next_calendar_version_error(self):
calculator = VersionCalculator()
today = datetime.today()
year_short = today.year % 100

with self.assertRaisesRegex(VersionError, "'.+' is higher than '.+'."):
calculator.next_calendar_version(Version(f"{year_short + 1}.1.0"))
calculator.next_calendar_version(
Version.from_string(f"{year_short + 1}.1.0")
)

with self.assertRaisesRegex(VersionError, "'.+' is higher than '.+'."):
calculator.next_calendar_version(
Version(f"{year_short}.{today.month + 1}.0")
Version.from_string(f"{year_short}.{today.month + 1}.0")
)

def test_next_minor_version(self):
Expand Down Expand Up @@ -132,9 +170,11 @@ def test_next_minor_version(self):
current_versions, assert_versions
):
release_version = calculator.next_minor_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))

def test_next_major_version(self):
calculator = VersionCalculator()
Expand Down Expand Up @@ -168,9 +208,11 @@ def test_next_major_version(self):
current_versions, assert_versions
):
release_version = calculator.next_major_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))

def test_next_alpha_version(self):
calculator = VersionCalculator()
Expand Down Expand Up @@ -204,9 +246,11 @@ def test_next_alpha_version(self):
current_versions, assert_versions
):
release_version = calculator.next_alpha_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))

def test_next_beta_version(self):
calculator = VersionCalculator()
Expand Down Expand Up @@ -240,9 +284,11 @@ def test_next_beta_version(self):
current_versions, assert_versions
):
release_version = calculator.next_beta_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))

def test_next_release_candidate_version(self):
calculator = VersionCalculator()
Expand Down Expand Up @@ -276,9 +322,11 @@ def test_next_release_candidate_version(self):
current_versions, assert_versions
):
release_version = calculator.next_release_candidate_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))

def test_next_dev_version(self):
calculator = VersionCalculator()
Expand Down Expand Up @@ -312,6 +360,8 @@ def test_next_dev_version(self):
current_versions, assert_versions
):
release_version = calculator.next_dev_version(
Version(current_version)
Version.from_string(current_version)
)
self.assertEqual(
release_version, Version.from_string(assert_version)
)
self.assertEqual(release_version, Version(assert_version))
Loading

0 comments on commit 2c6ca13

Please sign in to comment.