Skip to content

Commit

Permalink
chore(api/tests): adjusted tests to tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Jan 4, 2024
1 parent 288b397 commit a6a8b4b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
37 changes: 37 additions & 0 deletions api/outdated/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
from __future__ import annotations

from functools import partial
from typing import TYPE_CHECKING

import pytest
from pytest_factoryboy import register
from rest_framework.test import APIClient

from .oidc_auth.models import OIDCUser
from .outdated import factories
from .outdated.tracking import Tracker
from .user.factories import UserFactory

if TYPE_CHECKING:
from collections.abc import Callable
from unittest.mock import MagicMock

from pytest_mock import MockerFixture

register(factories.DependencyFactory)
register(factories.VersionFactory)
register(factories.ReleaseVersionFactory)
Expand Down Expand Up @@ -57,3 +67,30 @@ def client(db, settings, get_claims):
client.force_authenticate(user=user)
client.user = user
return client


@pytest.fixture
def tracker_mock(mocker: MockerFixture) -> Callable[[str], MagicMock]:
"""
Generate mock for Tracker.
Example:
-------
```
def test_tracker_thing(project, tracker_mock):
tracker_sync_mock = tracker_mock('sync')
Tracker(project).sync()
tracker_sync_mock.assert_called_once()
```
"""

def _tracker_mock(target: str) -> MagicMock:
return mocker.patch.object(Tracker, target)

return _tracker_mock


@pytest.fixture
def tracker_init_mock(mocker: MockerFixture) -> MagicMock:
"""Mock for the Trackers __init__ method."""
return mocker.patch.object(Tracker, "__init__", return_value=None)
12 changes: 5 additions & 7 deletions api/outdated/outdated/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,10 @@ def test_maintainer(client, maintainer):


@pytest.mark.django_db(transaction=True)
def test_sync_project_endpoint(client, project_factory):
generated_project = project_factory(repo="github.com/adfinis/outdated")
url = reverse("project-sync", args=[generated_project.id])
def test_sync_project_endpoint(client, project, tracker_init_mock, tracker_mock):
tracker_sync_mock = tracker_mock("sync")
url = reverse("project-sync", args=[project.id])
resp = client.post(url)
assert resp.status_code == http_status.HTTP_200_OK
assert generated_project.versioned_dependencies.count() > 0
url = reverse("project-sync", args=["eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"])
resp = client.post(url)
assert resp.status_code == http_status.HTTP_500_INTERNAL_SERVER_ERROR
tracker_init_mock.assert_called_once_with(project)
tracker_sync_mock.assert_called_once()
12 changes: 4 additions & 8 deletions api/outdated/outdated/tests/test_syncproject.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
from unittest.mock import ANY

import pytest
from django.core.management import call_command

from outdated.outdated.synchroniser import Synchroniser


@pytest.mark.django_db(transaction=True)
def test_syncproject(project_factory, mocker):
project = project_factory.create(repo="github.com/projectcaluma/caluma")
sync_init_mocker = mocker.spy(Synchroniser, "__init__")
def test_syncproject(project, tracker_mock, tracker_init_mock):
tracker_sync_mock = tracker_mock("sync")
call_command("syncproject", project.name)
sync_init_mocker.assert_called_once_with(ANY, project) # ANY == self
tracker_init_mock.assert_called_once_with(project)
tracker_sync_mock.assert_called_once()

0 comments on commit a6a8b4b

Please sign in to comment.