Skip to content

Commit

Permalink
Add properties to VersionInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
jimporter committed Nov 1, 2023
1 parent 9a76560 commit 27e7cba
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 6 deletions.
27 changes: 21 additions & 6 deletions mike/versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re
from verspec.loose import LooseVersion as Version

from . import jsonpath


def _ensure_version(version):
if not isinstance(version, Version):
Expand All @@ -10,7 +12,7 @@ def _ensure_version(version):


class VersionInfo:
def __init__(self, version, title=None, aliases=[]):
def __init__(self, version, title=None, aliases=[], properties=None):
self._check_version(str(version), 'version')
for i in aliases:
self._check_version(i, 'alias')
Expand All @@ -19,18 +21,23 @@ def __init__(self, version, title=None, aliases=[]):
self.version = _ensure_version(version)
self.title = version_name if title is None else title
self.aliases = set(aliases)
self.properties = properties

if str(self.version) in self.aliases:
raise ValueError('duplicated version and alias')

@classmethod
def from_json(cls, data):
return cls(data['version'], data['title'], data['aliases'])
return cls(data['version'], data['title'], data['aliases'],
data.get('properties'))

def to_json(self):
return {'version': str(self.version),
data = {'version': str(self.version),
'title': self.title,
'aliases': list(self.aliases)}
if self.properties:
data['properties'] = self.properties
return data

@classmethod
def loads(cls, data):
Expand All @@ -48,11 +55,13 @@ def _check_version(version, kind):
def __eq__(self, rhs):
return (str(self.version) == str(rhs.version) and
self.title == rhs.title and
self.aliases == rhs.aliases)
self.aliases == rhs.aliases and
self.properties == rhs.properties)

def __repr__(self):
return '<VersionInfo({!r}, {!r}, {{{}}})>'.format(
self.version, self.title, ', '.join(repr(i) for i in self.aliases)
return '<VersionInfo({!r}, {!r}, {{{}}}{})>'.format(
self.version, self.title, ', '.join(repr(i) for i in self.aliases),
', {!r}'.format(self.properties) if self.properties else ''
)

def update(self, title=None, aliases=[]):
Expand All @@ -69,6 +78,12 @@ def update(self, title=None, aliases=[]):
self.aliases |= aliases
return added

def get_property(self, expr, **kwargs):
return jsonpath.get_value(self.properties, expr, **kwargs)

def set_property(self, expr, value):
self.properties = jsonpath.set_value(self.properties, expr, value)


class Versions:
def __init__(self):
Expand Down
48 changes: 48 additions & 0 deletions test/unit/test_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import unittest
from verspec.loose import LooseVersion as Version

from mike.jsonpath import Deleted
from mike.versions import VersionInfo, Versions


Expand All @@ -11,21 +12,31 @@ def test_create(self):
self.assertEqual(v.version, Version('1.0'))
self.assertEqual(v.title, '1.0')
self.assertEqual(v.aliases, set())
self.assertEqual(v.properties, None)

v = VersionInfo('1.0', '1.0.0')
self.assertEqual(v.version, Version('1.0'))
self.assertEqual(v.title, '1.0.0')
self.assertEqual(v.aliases, set())
self.assertEqual(v.properties, None)

v = VersionInfo('1.0', aliases=['latest'])
self.assertEqual(v.version, Version('1.0'))
self.assertEqual(v.title, '1.0')
self.assertEqual(v.aliases, {'latest'})
self.assertEqual(v.properties, None)

v = VersionInfo('1.0', properties={'prop': 'val'})
self.assertEqual(v.version, Version('1.0'))
self.assertEqual(v.title, '1.0')
self.assertEqual(v.aliases, set())
self.assertEqual(v.properties, {'prop': 'val'})

v = VersionInfo(Version('1.0'))
self.assertEqual(v.version, Version('1.0'))
self.assertEqual(v.title, '1.0')
self.assertEqual(v.aliases, set())
self.assertEqual(v.properties, None)

with self.assertRaisesRegex(ValueError, "^'' is not a valid version$"):
VersionInfo('')
Expand Down Expand Up @@ -58,19 +69,29 @@ def test_equality(self):
self.assertNotEqual(v, VersionInfo('1.0', aliases=['latest']))
self.assertNotEqual(v, VersionInfo('1.0.0'))
self.assertNotEqual(v, VersionInfo('1.0.0', '1.0'))
self.assertNotEqual(v, VersionInfo('1.0', properties={'prop': 'val'}))

v = VersionInfo('1.0', '1.0.0')
self.assertNotEqual(v, VersionInfo('1.0'))
self.assertNotEqual(v, VersionInfo('1.1'))
self.assertEqual(v, VersionInfo('1.0', '1.0.0'))
self.assertNotEqual(v, VersionInfo('1.0', aliases=['latest']))
self.assertNotEqual(v, VersionInfo('1.0.0'))
self.assertNotEqual(v, VersionInfo('1.0', properties={'prop': 'val'}))

v = VersionInfo('1.0', aliases=['latest'])
self.assertNotEqual(v, VersionInfo('1.0'))
self.assertNotEqual(v, VersionInfo('1.1'))
self.assertNotEqual(v, VersionInfo('1.0', '1.0.0'))
self.assertEqual(v, VersionInfo('1.0', aliases=['latest']))
self.assertNotEqual(v, VersionInfo('1.0', properties={'prop': 'val'}))

v = VersionInfo('1.0', properties={'prop': 'val'})
self.assertNotEqual(v, VersionInfo('1.0'))
self.assertNotEqual(v, VersionInfo('1.1'))
self.assertNotEqual(v, VersionInfo('1.0', '1.0.0'))
self.assertNotEqual(v, VersionInfo('1.0', aliases=['latest']))
self.assertEqual(v, VersionInfo('1.0', properties={'prop': 'val'}))

def test_from_json(self):
self.assertEqual(VersionInfo.from_json({
Expand All @@ -81,6 +102,11 @@ def test_from_json(self):
'version': '1.0', 'title': '1.0.0', 'aliases': ['latest']
}), VersionInfo('1.0', '1.0.0', ['latest']))

self.assertEqual(VersionInfo.from_json({
'version': '1.0', 'title': '1.0.0', 'aliases': [],
'properties': {'prop': 'val'}
}), VersionInfo('1.0', '1.0.0', [], {'prop': 'val'}))

def test_to_json(self):
v = VersionInfo('1.0')
self.assertEqual(v.to_json(), {
Expand All @@ -92,6 +118,12 @@ def test_to_json(self):
'version': '1.0', 'title': '1.0.0', 'aliases': ['latest']
})

v = VersionInfo('1.0', '1.0.0', [], {'prop': 'val'})
self.assertEqual(v.to_json(), {
'version': '1.0', 'title': '1.0.0', 'aliases': [],
'properties': {'prop': 'val'}
})

def test_loads(self):
self.assertEqual(VersionInfo.loads(
'{"version": "1.0", "title": "1.0", "aliases": []}'
Expand Down Expand Up @@ -127,6 +159,22 @@ def test_update(self):
"^'foo/bar' is not a valid alias$"):
v.update(aliases=['foo/bar'])

def test_get_property(self):
v = VersionInfo('1.0', properties={'prop': 'val'})
self.assertEqual(v.get_property('prop'), 'val')

v = VersionInfo('1.0', properties={'prop': ['foo', 'bar']})
self.assertEqual(v.get_property('prop[1]'), 'bar')

def test_set_property(self):
v = VersionInfo('1.0', properties={'prop': 'val'})
v.set_property('hello', 'world')
self.assertEqual(v.properties, {'prop': 'val', 'hello': 'world'})

v = VersionInfo('1.0', properties={'prop': ['foo', 'bar']})
v.set_property('prop[1]', Deleted)
self.assertEqual(v.properties, {'prop': ['foo']})


class TestVersions(unittest.TestCase):
def test_add(self):
Expand Down

0 comments on commit 27e7cba

Please sign in to comment.