Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmpr committed Jan 26, 2024
1 parent 08a3b41 commit cecfc4d
Show file tree
Hide file tree
Showing 15 changed files with 1,223 additions and 40 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ attrs==23.2.0
charset-normalizer==3.3.2
frozenlist==1.4.1
pytest==7.4.4
pytest-asyncio==0.23.3
idna==3.6
multidict==6.0.4
xmltodict==0.13.0
Expand Down
8 changes: 6 additions & 2 deletions src/kataloger/catalog_updater.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def __init__(
update_resolvers: list[UpdateResolver],
verbose: bool = False,
):
if not library_repositories or not plugin_repositories:
if not (library_repositories or plugin_repositories):
raise KatalogerConfigurationException("No repositories provided!")

if not update_resolvers:
Expand All @@ -50,7 +50,11 @@ async def get_artifact_updates(self, artifacts: list[Artifact]) -> list[Artifact
library_updates, plugin_updates = await self.get_updates(libraries, plugins)
return library_updates + plugin_updates

async def get_updates(self, libraries: list[Library], plugins: list[Plugin]) -> tuple[list[ArtifactUpdate], list[ArtifactUpdate]]:
async def get_updates(
self,
libraries: list[Library],
plugins: list[Plugin],
) -> tuple[list[ArtifactUpdate], list[ArtifactUpdate]]:
library_updates = await self.get_library_updates(libraries)
plugin_updates = await self.get_plugin_updates(plugins)
return library_updates, plugin_updates
Expand Down
19 changes: 10 additions & 9 deletions src/kataloger/catalog_updater_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@

class CatalogUpdaterBuilder:

repositories_path: Optional[Path] = None
library_repositories: list[Repository] = []
plugin_repositories: list[Repository] = []
update_resolvers: list[UpdateResolver] = []
verbose: bool = False
def __init__(self):
self.repositories_path: Optional[Path] = None
self.library_repositories: list[Repository] = []
self.plugin_repositories: list[Repository] = []
self.update_resolvers: list[UpdateResolver] = []
self.verbose: bool = False

def set_repositories_path(self, path: Path) -> Self:
if not (path.exists() and path.is_file()):
Expand Down Expand Up @@ -50,8 +51,8 @@ def build(self) -> CatalogUpdater:
self.plugin_repositories.extend(plugin_repos)

return CatalogUpdater(
self.library_repositories,
self.plugin_repositories,
self.update_resolvers,
self.verbose,
library_repositories=self.library_repositories,
plugin_repositories=self.plugin_repositories,
update_resolvers=self.update_resolvers,
verbose=self.verbose,
)
14 changes: 0 additions & 14 deletions src/kataloger/data/artifact_metadata.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from dataclasses import dataclass
from functools import total_ordering


@dataclass(frozen=True)
@total_ordering
class ArtifactMetadata:
latest_version: str
release_version: str
Expand All @@ -12,15 +10,3 @@ class ArtifactMetadata:

def __repr__(self):
return f"{len(self.versions)}/{self.latest_version}"

def __eq__(self, other) -> bool:
if not isinstance(other, ArtifactMetadata):
return False

return self.last_updated == other.last_updated

def __lt__(self, other) -> bool:
if not isinstance(other, ArtifactMetadata):
return False

return self.last_updated == other.last_updated
4 changes: 2 additions & 2 deletions src/kataloger/helpers/xml_parse_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

def try_parse_maven_group_metadata(response: str) -> Optional[ArtifactMetadata]:
try:
metadata = xmltodict.parse(response)
metadata = xmltodict.parse(response.strip())
except ExpatError:
return None

Expand All @@ -24,5 +24,5 @@ def try_parse_maven_group_metadata(response: str) -> Optional[ArtifactMetadata]:
versions=versions,
last_updated=int(version_info.get("lastUpdated", 0)),
)
except KeyError:
except (KeyError, TypeError):
return None
13 changes: 13 additions & 0 deletions tests/data/artifact/test_library.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from kataloger.data.artifact.library import Library


class TestLibrary:
def test_library_to_path_should_return_path_part_from_library_coordinates(self):
library: Library = Library(
name="library",
coordinates="com.library.group:library-artifact-id",
version="1.0.0",
)
expected_path_part: str = "com/library/group/library-artifact-id"

assert library.to_path() == expected_path_part
13 changes: 13 additions & 0 deletions tests/data/artifact/test_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from kataloger.data.artifact.plugin import Plugin


class TestPlugin:
def test_plugin_to_path_should_return_path_part_from_plugin_coordinates(self):
plugin: Plugin = Plugin(
name="plugin",
coordinates="com.plugin.artifact-id",
version="1.0.0",
)
expected_path_part: str = "com/plugin/artifact-id/com.plugin.artifact-id.gradle.plugin"

assert plugin.to_path() == expected_path_part
50 changes: 50 additions & 0 deletions tests/data/test_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from typing import Optional

from yarl import URL

from kataloger.data.repository import Repository


class TestRepository:
def test_repository_should_require_authorization_when_user_and_password_is_not_none(self):
self._test_require_authorization(
user="repoUser",
password="repoPassword",
expected_requires_authorization=True,
)

def test_repository_should_not_require_authorization_when_user_is_not_none_but_password_is_none(self):
self._test_require_authorization(
user="repoUser",
password=None,
expected_requires_authorization=False,
)

def test_repository_should_not_require_authorization_when_password_is_not_none_but_user_is_none(self):
self._test_require_authorization(
user=None,
password="repoPassword",
expected_requires_authorization=False,
)

def test_repository_should_not_require_authorization_when_user_and_password_is_none(self):
self._test_require_authorization(
user=None,
password=None,
expected_requires_authorization=False,
)

@staticmethod
def _test_require_authorization(
user: Optional[str],
password: Optional[str],
expected_requires_authorization: bool,
):
repository: Repository = Repository(
name="repository",
address=URL("https://reposito,ry/"),
user=user,
password=password,
)

assert repository.requires_authorization() == expected_requires_authorization
60 changes: 60 additions & 0 deletions tests/entity_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
from yarl import URL

from kataloger.data.artifact.library import Library
from kataloger.data.artifact.plugin import Plugin
from kataloger.data.artifact_update import ArtifactUpdate
from kataloger.data.repository import Repository


class EntityFactory:
@staticmethod
def create_repository(
name: str = "default_repository",
address: URL = "https://reposito.ry/",
user: str | None = None,
password: str | None = None,
) -> Repository:
return Repository(
name=name,
address=address,
user=user,
password=password,
)

@staticmethod
def create_library(
name: str = "default_library",
coordinates: str = "com.library.group:library",
version: str = "1.0.0",
) -> Library:
return Library(
name=name,
coordinates=coordinates,
version=version,
)

@staticmethod
def create_plugin(
name: str = "default_plugin",
coordinates: str = "com.library.group:library",
version: str = "1.0.0",
) -> Plugin:
return Plugin(
name=name,
coordinates=coordinates,
version=version,
)

@staticmethod
def create_artifact_update(
name: str = "artifact_name",
update_repository_name: str = "update_repository_name",
current_version: str = "0.1.0",
available_version: str = "1.0.0",
) -> ArtifactUpdate:
return ArtifactUpdate(
name=name,
update_repository_name=update_repository_name,
current_version=current_version,
available_version=available_version,
)
Loading

0 comments on commit cecfc4d

Please sign in to comment.