Debian Codegraph @

tests/legacy/objects/repository/test_traffic.py

raw
1 # pylint: disable=missing-docstring, redefined-outer-name, unused-import
2 import datetime
3 import json
4
5 import pytest
6
7 from aiogithubapi import AIOGitHubAPINotModifiedException, GitHub
8
9 from tests.common import TOKEN
10 from tests.legacy.responses.repos.traffic.clones_fixtrue import clones_fixtrue_response
11 from tests.legacy.responses.repos.traffic.pageviews_fixtrue import (
12 pageviews_fixtrue_response,
13 )
14 from tests.legacy.responses.repository_fixture import repository_response
15
16
17 @pytest.mark.asyncio
18 async def test_get_clones(aresponses, repository_response, clones_fixtrue_response):
19 aresponses.add(
20 "api.github.com",
21 "/repos/octocat/Hello-World",
22 "get",
23 aresponses.Response(
24 text=json.dumps(repository_response),
25 status=200,
26 headers={
27 "X-RateLimit-Remaining": "1337",
28 "Content-Type": "application/json",
29 "Etag": "xyz..zyx",
30 },
31 ),
32 )
33 aresponses.add(
34 "api.github.com",
35 "/repos/octocat/Hello-World/traffic/clones",
36 "get",
37 aresponses.Response(
38 text=json.dumps(clones_fixtrue_response),
39 status=200,
40 headers={
41 "X-RateLimit-Remaining": "1337",
42 "Content-Type": "application/json",
43 "Etag": "xyz..zyx",
44 },
45 ),
46 )
47 aresponses.add(
48 "api.github.com",
49 "/repos/octocat/Hello-World/traffic/clones",
50 "get",
51 aresponses.Response(status=304),
52 )
53
54 async with GitHub(TOKEN) as github:
55 repository = await github.get_repo("octocat/Hello-World")
56 clones = await repository.traffic.get_clones()
57 assert clones.count == 173
58
59 with pytest.raises(AIOGitHubAPINotModifiedException):
60 await repository.traffic.get_clones(etag=github.client.last_response.etag)
61
62
63 @pytest.mark.asyncio
64 async def test_get_views(aresponses, repository_response, pageviews_fixtrue_response):
65 aresponses.add(
66 "api.github.com",
67 "/repos/octocat/Hello-World",
68 "get",
69 aresponses.Response(
70 text=json.dumps(repository_response),
71 status=200,
72 headers={
73 "X-RateLimit-Remaining": "1337",
74 "Content-Type": "application/json",
75 "Etag": "xyz..zyx",
76 },
77 ),
78 )
79 aresponses.add(
80 "api.github.com",
81 "/repos/octocat/Hello-World/traffic/views",
82 "get",
83 aresponses.Response(
84 text=json.dumps(pageviews_fixtrue_response),
85 status=200,
86 headers={
87 "X-RateLimit-Remaining": "1337",
88 "Content-Type": "application/json",
89 "Etag": "xyz..zyx",
90 },
91 ),
92 )
93 aresponses.add(
94 "api.github.com",
95 "/repos/octocat/Hello-World/traffic/views",
96 "get",
97 aresponses.Response(status=304),
98 )
99
100 async with GitHub(TOKEN) as github:
101 repository = await github.get_repo("octocat/Hello-World")
102 views = await repository.traffic.get_views()
103 assert views.count == 14850
104
105 with pytest.raises(AIOGitHubAPINotModifiedException):
106 await repository.traffic.get_views(etag=github.client.last_response.etag)