|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | import logging
|
| 4 | +from unittest.mock import AsyncMock |
| 5 | +from unittest.mock import MagicMock |
4 | 6 |
|
5 | 7 | import pytest
|
6 | 8 | from asgiref.sync import sync_to_async
|
7 | 9 | from django.conf import settings
|
8 | 10 | from model_bakery import baker
|
9 | 11 |
|
| 12 | +from django_github_app.github import AsyncGitHubAPI |
| 13 | + |
10 | 14 | from .settings import DEFAULT_SETTINGS
|
11 | 15 | from .utils import seq
|
12 | 16 |
|
@@ -52,37 +56,90 @@ def pytest_configure(config):
|
52 | 56 |
|
53 | 57 |
|
54 | 58 | @pytest.fixture
|
55 |
| -def id_sequence_start(): |
56 |
| - return 1000 |
| 59 | +def installation_id(): |
| 60 | + return seq.next() |
57 | 61 |
|
58 | 62 |
|
59 | 63 | @pytest.fixture
|
60 |
| -def installation_id(id_sequence_start): |
61 |
| - return seq(id_sequence_start) |
| 64 | +def repository_id(): |
| 65 | + return seq.next() |
62 | 66 |
|
63 | 67 |
|
64 | 68 | @pytest.fixture
|
65 |
| -def installation_id_iter(id_sequence_start): |
66 |
| - return seq.iter(id_sequence_start) |
| 69 | +def installation(): |
| 70 | + return baker.make("django_github_app.Installation", installation_id=seq.next()) |
67 | 71 |
|
68 | 72 |
|
69 | 73 | @pytest.fixture
|
70 |
| -def repository_id(id_sequence_start): |
71 |
| - return seq(id_sequence_start) |
| 74 | +async def ainstallation(): |
| 75 | + return await sync_to_async(baker.make)( |
| 76 | + "django_github_app.Installation", installation_id=seq.next() |
| 77 | + ) |
72 | 78 |
|
73 | 79 |
|
74 | 80 | @pytest.fixture
|
75 |
| -def repository_id_iter(id_sequence_start): |
76 |
| - return seq.iter(id_sequence_start) |
| 81 | +def mock_github_api(): |
| 82 | + mock_api = AsyncMock(spec=AsyncGitHubAPI) |
| 83 | + |
| 84 | + async def mock_getiter(*args, **kwargs): |
| 85 | + test_issues = [ |
| 86 | + { |
| 87 | + "number": 1, |
| 88 | + "title": "Test Issue 1", |
| 89 | + "state": "open", |
| 90 | + }, |
| 91 | + { |
| 92 | + "number": 2, |
| 93 | + "title": "Test Issue 2", |
| 94 | + "state": "closed", |
| 95 | + }, |
| 96 | + ] |
| 97 | + for issue in test_issues: |
| 98 | + yield issue |
| 99 | + |
| 100 | + mock_api.getiter = mock_getiter |
| 101 | + mock_api.__aenter__.return_value = mock_api |
| 102 | + mock_api.__aexit__.return_value = None |
| 103 | + |
| 104 | + return mock_api |
77 | 105 |
|
78 | 106 |
|
79 | 107 | @pytest.fixture
|
80 |
| -def installation(installation_id): |
81 |
| - return baker.make("django_github_app.Installation", installation_id=installation_id) |
| 108 | +def repository(installation, mock_github_api): |
| 109 | + repository = baker.make( |
| 110 | + "django_github_app.Repository", |
| 111 | + repository_id=seq.next(), |
| 112 | + full_name="owner/repo", |
| 113 | + installation=installation, |
| 114 | + ) |
| 115 | + |
| 116 | + mock_github_api.installation_id = repository.installation.installation_id |
| 117 | + |
| 118 | + if isinstance(repository, list): |
| 119 | + for repo in repository: |
| 120 | + repo.get_gh_client = MagicMock(mock_github_api) |
| 121 | + else: |
| 122 | + repository.get_gh_client = MagicMock(return_value=mock_github_api) |
| 123 | + |
| 124 | + return repository |
82 | 125 |
|
83 | 126 |
|
84 | 127 | @pytest.fixture
|
85 |
| -async def ainstallation(installation_id): |
86 |
| - return await sync_to_async(baker.make)( |
87 |
| - "django_github_app.Installation", installation_id=installation_id |
| 128 | +async def arepository(ainstallation, mock_github_api): |
| 129 | + installation = await ainstallation |
| 130 | + repository = await sync_to_async(baker.make)( |
| 131 | + "django_github_app.Repository", |
| 132 | + repository_id=seq.next(), |
| 133 | + full_name="owner/repo", |
| 134 | + installation=installation, |
88 | 135 | )
|
| 136 | + |
| 137 | + mock_github_api.installation_id = repository.installation.installation_id |
| 138 | + |
| 139 | + if isinstance(repository, list): |
| 140 | + for repo in repository: |
| 141 | + repo.get_gh_client = MagicMock(mock_github_api) |
| 142 | + else: |
| 143 | + repository.get_gh_client = MagicMock(return_value=mock_github_api) |
| 144 | + |
| 145 | + return repository |
0 commit comments