Skip to content

Commit

Permalink
Add get_version_tuple function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 committed Jun 5, 2024
1 parent 589d89d commit 799830e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
23 changes: 14 additions & 9 deletions pymongo/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@

__version__ = "4.8.0.dev1"

pattern = r"(?P<major>\d+).(?P<minor>\d+).(?P<patch>\d+)(?P<rest>.*)"
match = re.match(pattern, __version__)
if match:
parts: List[Union[int, str]] = [int(match[part]) for part in ["major", "minor", "patch"]]
if match["rest"]:
parts.append(match["rest"])
else:
parts = []
version_tuple: Tuple[Union[int, str], ...] = tuple(parts)

def get_version_tuple(version: str) -> Tuple[Union[int, str], ...]:
pattern = r"(?P<major>\d+).(?P<minor>\d+).(?P<patch>\d+)(?P<rest>.*)"
match = re.match(pattern, version)
if match:
parts: List[Union[int, str]] = [int(match[part]) for part in ["major", "minor", "patch"]]
if match["rest"]:
parts.append(match["rest"])
else:
parts = []
return tuple(parts)


version_tuple = get_version_tuple(__version__)
version = __version__


Expand Down
6 changes: 6 additions & 0 deletions test/test_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@
from test import unittest

import pymongo
from pymongo._version import get_version_tuple


class TestPyMongo(unittest.TestCase):
def test_mongo_client_alias(self):
# Testing that pymongo module imports mongo_client.MongoClient
self.assertEqual(pymongo.MongoClient, pymongo.mongo_client.MongoClient)

def test_get_version_tuple(self):
self.assertEqual(get_version_tuple("4.8.0.dev1"), (4, 8, 0, ".dev1"))
self.assertEqual(get_version_tuple("4.8.1"), (4, 8, 1))
self.assertEqual(get_version_tuple("5.0.0rc1"), (5, 0, 0, "rc1"))


if __name__ == "__main__":
unittest.main()

0 comments on commit 799830e

Please sign in to comment.