Skip to content

Commit

Permalink
add some tests for CollectionGroup
Browse files Browse the repository at this point in the history
  • Loading branch information
briantist committed Oct 8, 2023
1 parent 83d786d commit b710d08
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
8 changes: 5 additions & 3 deletions galactory/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ def is_prerelease(self) -> bool:

class CollectionGroup(UserDict):
"""
A dict-like object where the keys are collection names,
and the values are CollectionData objects.
A dict-like object that represents one or more
versions of a single collection. The keys are
VersionInfo objects and the values are
CollectionData objects.
"""
latest: CollectionData = None

Expand All @@ -121,7 +123,7 @@ def from_collection(cls, collection: CollectionData):
return instance

Check warning on line 123 in galactory/models.py

View check run for this annotation

Codecov / codecov/patch

galactory/models.py#L121-L123

Added lines #L121 - L123 were not covered by tests

@property
def versions(self) -> dict:
def versions(self) -> t.Dict[VersionInfo, CollectionData]:
return self.data

@cached_property
Expand Down
90 changes: 90 additions & 0 deletions tests/unit/models/test_collectiongroup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-
# (c) 2023 Brian Scholer (@briantist)

import pytest

import re
from datetime import datetime, timezone
from pytest_mock import MockFixture

from galactory.models import CollectionData, CollectionGroup


@pytest.fixture
def collection_data() -> CollectionData:
return CollectionData(
collection_info={},
namespace='ns',
name='name',
created_datetime=datetime.now(timezone.utc),
modified_datetime=datetime.now(timezone.utc),
filename='fake-file',
mime_type='fake-file',
sha256='m-m-m-my-sha-',
size=0,
version='0.0.0',
)


@pytest.mark.parametrize('name', ['collection_name', 'name2'])
@pytest.mark.parametrize('namespace', ['a_namespace', 'ns1'])
def test_collectiongroup_init(namespace, name):
colgroup = CollectionGroup(namespace=namespace, name=name)

assert isinstance(colgroup, CollectionGroup)
assert colgroup.namespace == namespace
assert colgroup.name == name
assert colgroup.latest is None
assert len(colgroup) == len(colgroup.versions) == 0


@pytest.mark.parametrize('name', ['collection_name'])
@pytest.mark.parametrize('namespace', ['a_namespace'])
@pytest.mark.parametrize('version', [
'abc.0',
'0.hello',
'99999.0.1111.6.4.3',
None,
4.4,
True,
[],
{},
])
@pytest.mark.parametrize('value', [None, "thing", 2, 3.14159, False, [], {}])
def test_collectiongroup_bad_entries(mocker: MockFixture, namespace, name, collection_data, version, value):
colgroup = CollectionGroup(namespace=namespace, name=name)

# bad value type
with pytest.raises(TypeError, match=r'^Values must be of type CollectionData\.$'):
colgroup[version] = value

# bad collection name
with pytest.raises(ValueError, match=rf"^Attempted to add collection '{collection_data.namespace}.{collection_data.name}' to group for '{namespace}\.{name}'\.$"):
colgroup[version] = collection_data

# bad key (version)
collection_data.namespace = namespace
collection_data.name = name
regver = re.escape(str(version))
spy = mocker.spy(colgroup, '_get_key')
with pytest.raises((TypeError, ValueError), match=rf"^(?:Only valid semantic versions can be used as keys\.|{regver} is not valid SemVer string)$"):
colgroup[version] = collection_data

spy.assert_called_once_with(version)


@pytest.mark.parametrize('name', ['collection_name'])
@pytest.mark.parametrize('namespace', ['a_namespace'])
def test_collectiongroup_add(mocker: MockFixture, namespace, name, collection_data):
spy = mocker.spy(CollectionGroup, '__setitem__')
colgroup = CollectionGroup(namespace=namespace, name=name)

collection_data.namespace = namespace
collection_data.name = name


colgroup.add(collection_data)
assert len(colgroup) == 1
assert colgroup.latest is collection_data

spy.assert_called_once_with(colgroup, collection_data.semver, collection_data)

0 comments on commit b710d08

Please sign in to comment.