| 1 |
"""Test repos namespace.""" |
| 2 |
# pylint: disable=missing-docstring |
| 3 |
import pytest |
| 4 |
|
| 5 |
from aiogithubapi import GitHubAPI, GitHubRepositoryModel |
| 6 |
from aiogithubapi.const import HttpContentType |
| 7 |
|
| 8 |
from tests.common import ( |
| 9 |
HEADERS, |
| 10 |
HEADERS_TEXT, |
| 11 |
TEST_REPOSITORY_NAME, |
| 12 |
MockedRequests, |
| 13 |
MockResponse, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
@pytest.mark.asyncio |
| 18 |
async def test_get_repository(github_api: GitHubAPI, mock_requests: MockedRequests): |
| 19 |
response = await github_api.repos.get(TEST_REPOSITORY_NAME) |
| 20 |
assert response.status == 200 |
| 21 |
assert isinstance(response.data, GitHubRepositoryModel) |
| 22 |
assert response.data.name == "Hello-World" |
| 23 |
assert mock_requests.called == 1 |
| 24 |
assert mock_requests.last_request["url"] == "https://api.github.com/repos/octocat/hello-world" |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.asyncio |
| 28 |
async def test_list_commits(github_api: GitHubAPI, mock_requests: MockedRequests): |
| 29 |
response = await github_api.repos.list_commits(TEST_REPOSITORY_NAME, sha="main", path="README.md", author="awesome_author", since="2021-01-01", until="2021-01-02") |
| 30 |
assert response.status == 200 |
| 31 |
assert isinstance(response.data, list) |
| 32 |
assert response.data[0].commit.message == "Merge pull request #6" |
| 33 |
assert mock_requests.called == 1 |
| 34 |
assert ( |
| 35 |
mock_requests.last_request["url"] |
| 36 |
== "https://api.github.com/repos/octocat/hello-world/commits" |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.mark.asyncio |
| 41 |
async def test_list_tags(github_api: GitHubAPI, mock_requests: MockedRequests): |
| 42 |
response = await github_api.repos.list_tags(TEST_REPOSITORY_NAME) |
| 43 |
assert response.status == 200 |
| 44 |
assert isinstance(response.data, list) |
| 45 |
assert response.data[0].name == "v0.1" |
| 46 |
assert response.data[0].commit.sha == "c5b97d5ae6c19d5c5df71a34c7fbeeda2479ccbc" |
| 47 |
assert mock_requests.called == 1 |
| 48 |
assert ( |
| 49 |
mock_requests.last_request["url"] == "https://api.github.com/repos/octocat/hello-world/tags" |
| 50 |
) |
| 51 |
|
| 52 |
|
| 53 |
@pytest.mark.asyncio |
| 54 |
async def test_tarball( |
| 55 |
github_api: GitHubAPI, |
| 56 |
mock_requests: MockedRequests, |
| 57 |
mock_response: MockResponse, |
| 58 |
): |
| 59 |
mock_response.mock_headers = {**HEADERS, "content-type": HttpContentType.BASE_GZIP} |
| 60 |
response = await github_api.repos.tarball("octocat/hello-world") |
| 61 |
assert isinstance(response.data, bytes) |
| 62 |
assert mock_requests.called == 1 |
| 63 |
assert ( |
| 64 |
mock_requests.last_request["url"] |
| 65 |
== "https://api.github.com/repos/octocat/hello-world/tarball/" |
| 66 |
) |
| 67 |
response = await github_api.repos.tarball("octocat/hello-world", ref="main") |
| 68 |
assert isinstance(response.data, bytes) |
| 69 |
assert mock_requests.called == 2 |
| 70 |
assert ( |
| 71 |
mock_requests.last_request["url"] |
| 72 |
== "https://api.github.com/repos/octocat/hello-world/tarball/main" |
| 73 |
) |
| 74 |
|
| 75 |
|
| 76 |
@pytest.mark.asyncio |
| 77 |
async def test_zipball( |
| 78 |
github_api: GitHubAPI, |
| 79 |
mock_requests: MockedRequests, |
| 80 |
mock_response: MockResponse, |
| 81 |
): |
| 82 |
mock_response.mock_headers = {**HEADERS, "content-type": HttpContentType.BASE_ZIP} |
| 83 |
response = await github_api.repos.zipball("octocat/hello-world") |
| 84 |
assert isinstance(response.data, bytes) |
| 85 |
assert mock_requests.called == 1 |
| 86 |
assert ( |
| 87 |
mock_requests.last_request["url"] |
| 88 |
== "https://api.github.com/repos/octocat/hello-world/zipball/" |
| 89 |
) |
| 90 |
response = await github_api.repos.zipball("octocat/hello-world", ref="main") |
| 91 |
assert isinstance(response.data, bytes) |
| 92 |
assert mock_requests.called == 2 |
| 93 |
assert ( |
| 94 |
mock_requests.last_request["url"] |
| 95 |
== "https://api.github.com/repos/octocat/hello-world/zipball/main" |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.asyncio |
| 100 |
async def test_readme( |
| 101 |
github_api: GitHubAPI, |
| 102 |
mock_requests: MockedRequests, |
| 103 |
mock_response: MockResponse, |
| 104 |
): |
| 105 |
mock_response.mock_headers = HEADERS_TEXT |
| 106 |
|
| 107 |
response = await github_api.repos.readme(TEST_REPOSITORY_NAME) |
| 108 |
assert response.status == 200 |
| 109 |
assert isinstance(response.data, str) |
| 110 |
assert mock_requests.called == 1 |
| 111 |
assert ( |
| 112 |
mock_requests.last_request["url"] |
| 113 |
== "https://api.github.com/repos/octocat/hello-world/readme/" |
| 114 |
) |
| 115 |
|
| 116 |
response = await github_api.repos.readme(TEST_REPOSITORY_NAME, dir="test") |
| 117 |
assert response.status == 200 |
| 118 |
assert isinstance(response.data, str) |
| 119 |
assert mock_requests.called == 2 |
| 120 |
assert ( |
| 121 |
mock_requests.last_request["url"] |
| 122 |
== "https://api.github.com/repos/octocat/hello-world/readme/test" |
| 123 |
) |