Skip to content

Commit

Permalink
tests: update to the latest unittest-fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
enku committed Feb 21, 2025
1 parent 2c72e1f commit ac3160d
Show file tree
Hide file tree
Showing 20 changed files with 1,151 additions and 1,095 deletions.
505 changes: 277 additions & 228 deletions pdm.lock

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions tests/test_cli_addmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@
# pylint: disable=missing-docstring
from argparse import ArgumentParser, Namespace

import unittest_fixtures as fixture
from gbp_testkit import DjangoTestCase as TestCase
from unittest_fixtures import Fixtures, given

from gentoo_build_publisher import publisher
from gentoo_build_publisher.cli import addmachine
from gentoo_build_publisher.types import MachineJob, Repo


@fixture.requires("gbp", "console")
@given("gbp", "console")
class AddMachineTestCase(TestCase):
def test_calls_graphql_with_the_expected_args(self) -> None:
def test_calls_graphql_with_the_expected_args(self, fixtures: Fixtures) -> None:
args = Namespace(
name="base",
repo="https://github.com/enku/gbp-machines.git",
branch="master",
deps=["gentoo"],
)
console = self.fixtures.console
exit_status = addmachine.handler(args, self.fixtures.gbp, console)
console = fixtures.console
exit_status = addmachine.handler(args, fixtures.gbp, console)

self.assertEqual(exit_status, 0)

def test_when_item_already_exists(self) -> None:
def test_when_item_already_exists(self, fixtures: Fixtures) -> None:
job = MachineJob(
name="base",
repo=Repo(url="https://github.com/enku/gbp-machines.git", branch="master"),
Expand All @@ -39,8 +39,8 @@ def test_when_item_already_exists(self) -> None:
branch="master",
deps=["gentoo"],
)
console = self.fixtures.console
exit_status = addmachine.handler(args, self.fixtures.gbp, console)
console = fixtures.console
exit_status = addmachine.handler(args, fixtures.gbp, console)

self.assertEqual(exit_status, 1)
self.assertEqual(console.err.file.getvalue(), "error: FileExistsError: base\n")
Expand Down
18 changes: 9 additions & 9 deletions tests/test_cli_addrepo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@
from argparse import ArgumentParser, Namespace

from gbp_testkit import DjangoTestCase as TestCase
from unittest_fixtures import requires
from unittest_fixtures import Fixtures, given

from gentoo_build_publisher.build_publisher import BuildPublisher
from gentoo_build_publisher.cli import addrepo
from gentoo_build_publisher.jenkins import ProjectPath
from gentoo_build_publisher.types import EbuildRepo


@requires("publisher", "gbp", "console")
@given("publisher", "gbp", "console")
class AddRepoTestCase(TestCase):
def test_calls_grapql_with_the_expected_args(self) -> None:
def test_calls_graphql_with_the_expected_args(self, fixtures: Fixtures) -> None:
args = Namespace(
name="gentoo",
repo="https://anongit.gentoo.org/git/repo/gentoo.git",
branch="master",
)
console = self.fixtures.console
exit_status = addrepo.handler(args, self.fixtures.gbp, console)
console = fixtures.console
exit_status = addrepo.handler(args, fixtures.gbp, console)

self.assertEqual(exit_status, 0)

def test_when_item_already_exists(self) -> None:
publisher: BuildPublisher = self.fixtures.publisher
def test_when_item_already_exists(self, fixtures: Fixtures) -> None:
publisher: BuildPublisher = fixtures.publisher
publisher.jenkins.make_folder(ProjectPath("repos"))
publisher.jenkins.create_repo_job(
EbuildRepo(name="gentoo", url="foo", branch="master")
Expand All @@ -37,8 +37,8 @@ def test_when_item_already_exists(self) -> None:
repo="https://anongit.gentoo.org/git/repo/gentoo.git",
branch="master",
)
console = self.fixtures.console
exit_status = addrepo.handler(args, self.fixtures.gbp, console)
console = fixtures.console
exit_status = addrepo.handler(args, fixtures.gbp, console)

self.assertEqual(exit_status, 1)
self.assertEqual(
Expand Down
66 changes: 32 additions & 34 deletions tests/test_cli_apikey.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
from dataclasses import replace
from unittest.mock import Mock, patch

import unittest_fixtures as fixture
from django.conf import settings
from gbp_testkit import DjangoTestCase, TestCase
from gbp_testkit.helpers import LOCAL_TIMEZONE
from unittest_fixtures import requires
from unittest_fixtures import Fixtures, given, where

from gentoo_build_publisher import models, publisher, utils
from gentoo_build_publisher.cli import apikey
from gentoo_build_publisher.types import ApiKey
from gentoo_build_publisher.utils import time


@requires("console")
@given("console")
class GBPCreateTests(DjangoTestCase):
def test_create_api_key_with_given_name(self) -> None:
console = self.fixtures.console
def test_create_api_key_with_given_name(self, fixtures: Fixtures) -> None:
console = fixtures.console
mock_gbp = Mock(name="gbp")
namespace = Namespace(action="create", name="test")

Expand All @@ -41,15 +40,15 @@ def test_create_api_key_with_given_name(self) -> None:
key,
)

def test_name_is_case_insensitive(self) -> None:
console = self.fixtures.console
def test_name_is_case_insensitive(self, fixtures: Fixtures) -> None:
console = fixtures.console
apikey.handler(Namespace(action="create", name="TEST"), Mock(), console)

self.assertFalse(models.ApiKey.objects.filter(name="TEST").exists())
self.assertTrue(models.ApiKey.objects.filter(name="test").exists())

def test_name_already_exists(self) -> None:
console = self.fixtures.console
def test_name_already_exists(self, fixtures: Fixtures) -> None:
console = fixtures.console
api_key = ApiKey(
name="test", key=apikey.create_api_key(), created=time.localtime()
)
Expand All @@ -66,16 +65,16 @@ def test_name_already_exists(self) -> None:
self.assertTrue(models.ApiKey.objects.filter(name="test").exists())
self.assertFalse(models.ApiKey.objects.filter(name="TEST").exists())

def test_create_empty_name(self) -> None:
console = self.fixtures.console
def test_create_empty_name(self, fixtures: Fixtures) -> None:
console = fixtures.console

status = apikey.handler(Namespace(action="create", name=""), Mock(), console)

self.assertEqual(status, 2)
self.assertEqual(console.err.file.getvalue(), "''\n")

def test_create_badchars_in_name(self) -> None:
console = self.fixtures.console
def test_create_badchars_in_name(self, fixtures: Fixtures) -> None:
console = fixtures.console

status = apikey.handler(
Namespace(action="create", name="b😈d"), Mock(), console
Expand All @@ -84,8 +83,8 @@ def test_create_badchars_in_name(self) -> None:
self.assertEqual(status, 2)
self.assertEqual(console.err.file.getvalue(), "'b😈d'\n")

def test_create_name_too_long(self) -> None:
console = self.fixtures.console
def test_create_name_too_long(self, fixtures: Fixtures) -> None:
console = fixtures.console
name = "x" * 129

status = apikey.handler(Namespace(action="create", name=name), Mock(), console)
Expand All @@ -98,8 +97,8 @@ def test_create_name_too_long(self) -> None:
)

@patch("gentoo_build_publisher.cli.apikey.create_secret_key")
def test_root_key(self, create_secret_key: Mock) -> None:
console = self.fixtures.console
def test_root_key(self, create_secret_key: Mock, fixtures: Fixtures) -> None:
console = fixtures.console
gbp = Mock()
create_secret_key.return_value = b"thisisatest"

Expand All @@ -110,11 +109,11 @@ def test_root_key(self, create_secret_key: Mock) -> None:
self.assertEqual(console.out.file.getvalue(), "thisisatest\n")


@requires("console")
@given("console")
@patch("gentoo_build_publisher.utils.time.LOCAL_TIMEZONE", new=LOCAL_TIMEZONE)
class GBPListTests(DjangoTestCase):
def test(self) -> None:
console = self.fixtures.console
def test(self, fixtures: Fixtures) -> None:
console = fixtures.console
timestamp = dt.datetime(2024, 2, 22, 22, 0, tzinfo=dt.UTC)
for name in ["this", "that", "the", "other"]:
api_key = ApiKey(name=name, key=apikey.create_api_key(), created=timestamp)
Expand All @@ -139,8 +138,8 @@ def test(self) -> None:
"""
self.assertEqual(console.out.file.getvalue(), expected)

def test_with_no_keys(self) -> None:
console = self.fixtures.console
def test_with_no_keys(self, fixtures: Fixtures) -> None:
console = fixtures.console
gbp = Mock()

status = apikey.handler(Namespace(action="list"), gbp, console)
Expand All @@ -149,12 +148,11 @@ def test_with_no_keys(self) -> None:
self.assertEqual(console.out.file.getvalue(), "No API keys registered.\n")


@fixture.requires("tmpdir", "publisher", "api_keys", "console")
@given("tmpdir", "publisher", "api_keys", "console")
@where(api_keys={"api_key_names": ["this", "that", "the", "other"]})
class GBPDeleteTests(DjangoTestCase):
options = {"api_key_names": ["this", "that", "the", "other"]}

def test_delete(self) -> None:
console = self.fixtures.console
def test_delete(self, fixtures: Fixtures) -> None:
console = fixtures.console
namespace = Namespace(action="delete", name="that")

status = apikey.handler(namespace, Mock(), console)
Expand All @@ -163,8 +161,8 @@ def test_delete(self) -> None:
key_query = models.ApiKey.objects.filter(name="that")
self.assertFalse(key_query.exists(), "key not deleted")

def test_delete_is_case_insensitive(self) -> None:
console = self.fixtures.console
def test_delete_is_case_insensitive(self, fixtures: Fixtures) -> None:
console = fixtures.console
namespace = Namespace(action="delete", name="THAT")

status = apikey.handler(namespace, Mock(), console)
Expand All @@ -173,8 +171,8 @@ def test_delete_is_case_insensitive(self) -> None:
key_query = models.ApiKey.objects.filter(name="that")
self.assertFalse(key_query.exists(), "key not deleted")

def test_delete_name_does_not_exist(self) -> None:
console = self.fixtures.console
def test_delete_name_does_not_exist(self, fixtures: Fixtures) -> None:
console = fixtures.console
namespace = Namespace(action="delete", name="bogus")

status = apikey.handler(namespace, Mock(), console)
Expand All @@ -183,10 +181,10 @@ def test_delete_name_does_not_exist(self) -> None:
self.assertEqual(console.err.file.getvalue(), "No key exists with that name.\n")


@requires("console")
@given("console")
class GBPAPIKeyTests(TestCase):
def test_unknown_action(self) -> None:
console = self.fixtures.console
def test_unknown_action(self, fixtures: Fixtures) -> None:
console = fixtures.console
namespace = Namespace(action="bogus")

status = apikey.handler(namespace, Mock(), console)
Expand Down
Loading

0 comments on commit ac3160d

Please sign in to comment.