Skip to content

Commit 20b1624

Browse files
committed
test(client): move get functions to utils file
1 parent 1ff53dc commit 20b1624

File tree

3 files changed

+70
-84
lines changed

3 files changed

+70
-84
lines changed

tests/conftest.py

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import vcr
77

88
from pygitguardian import GGClient
9-
from pygitguardian.models import TeamsParameter
10-
from pygitguardian.models_utils import CursorPaginatedResponse
119

1210

1311
my_vcr = vcr.VCR(
@@ -31,60 +29,3 @@ def create_client(**kwargs: Any) -> GGClient:
3129
@pytest.fixture
3230
def client():
3331
return create_client()
34-
35-
36-
@pytest.fixture
37-
def get_team(client: GGClient):
38-
"""
39-
Return a function that fetches the first team available
40-
in the account, every account should have at least
41-
one team (all incidents) but we skip it since we cannot
42-
add sources to it
43-
"""
44-
45-
def inner():
46-
paginated_teams = client.list_teams(TeamsParameter(is_global=False))
47-
assert isinstance(
48-
paginated_teams, CursorPaginatedResponse
49-
), "Could not fetch teams from GitGuardian"
50-
51-
return paginated_teams.data[0]
52-
53-
return inner
54-
55-
56-
@pytest.fixture
57-
def get_source(client: GGClient):
58-
"""
59-
Return a function that fetches the first source available
60-
in the account, not all accounts have a source so testing
61-
from scratch will require installing a source first
62-
"""
63-
64-
def inner():
65-
paginated_sources = client.list_sources()
66-
assert isinstance(
67-
paginated_sources, CursorPaginatedResponse
68-
), "Could not fetch sources from GitGuardian"
69-
return paginated_sources.data[0]
70-
71-
return inner
72-
73-
74-
@pytest.fixture
75-
def get_invitation(client: GGClient):
76-
"""
77-
Return a function that fetches the first invitation available
78-
in the account, there is no invitation by default, one should be
79-
created to setup the test
80-
"""
81-
82-
def inner():
83-
paginated_teams = client.list_invitations()
84-
assert isinstance(
85-
paginated_teams, CursorPaginatedResponse
86-
), "Could not fetch members from GitGuardian"
87-
88-
return paginated_teams.data[0]
89-
90-
return inner

tests/test_client.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from collections import OrderedDict
55
from datetime import date
66
from io import BytesIO
7-
from typing import Any, Callable, Dict, List, Optional, Tuple, Type
7+
from typing import Any, Dict, List, Optional, Tuple, Type
88
from unittest.mock import Mock, patch
99

1010
import pytest
@@ -67,6 +67,7 @@
6767
)
6868

6969
from .conftest import create_client, my_vcr
70+
from .utils import get_invitation, get_source, get_team
7071

7172

7273
FILENAME = ".env"
@@ -1525,7 +1526,7 @@ def test_create_team(client: GGClient):
15251526

15261527

15271528
@my_vcr.use_cassette("test_get_team.yaml", ignore_localhost=False)
1528-
def test_get_team(client: GGClient, get_team: Callable[[], Team]):
1529+
def test_get_team(client: GGClient):
15291530
"""
15301531
GIVEN a client
15311532
WHEN calling GET /teams/{id} endpoint
@@ -1541,7 +1542,7 @@ def test_get_team(client: GGClient, get_team: Callable[[], Team]):
15411542

15421543

15431544
@my_vcr.use_cassette("test_update_team.yaml", ignore_localhost=False)
1544-
def test_update_team(client: GGClient, get_team: Callable[[], Team]):
1545+
def test_update_team(client: GGClient):
15451546
"""
15461547
GIVEN a client
15471548
WHEN calling PATCH /teams endpoint
@@ -1592,7 +1593,7 @@ def test_global_team(client: GGClient):
15921593

15931594

15941595
@my_vcr.use_cassette("test_delete_team.yaml", ignore_localhost=False)
1595-
def test_delete_team(client: GGClient, get_team: Callable[[], Team]):
1596+
def test_delete_team(client: GGClient):
15961597
"""
15971598
GIVEN a client
15981599
WHEN calling DELETE /teams/{id} endpoint
@@ -1606,11 +1607,7 @@ def test_delete_team(client: GGClient, get_team: Callable[[], Team]):
16061607

16071608

16081609
@my_vcr.use_cassette("test_create_team_invitation.yaml", ignore_localhost=False)
1609-
def test_create_team_invitation(
1610-
client: GGClient,
1611-
get_team: Callable[[], Team],
1612-
get_invitation: Callable[[], Invitation],
1613-
):
1610+
def test_create_team_invitation(client: GGClient):
16141611
"""
16151612
GIVEN a client
16161613
WHEN calling POST /teams/{id}/invitations endpoint
@@ -1633,7 +1630,7 @@ def test_create_team_invitation(
16331630

16341631

16351632
@my_vcr.use_cassette("test_list_team_invitations.yaml", ignore_localhost=False)
1636-
def test_list_team_invitations(client: GGClient, get_team: Callable[[], Team]):
1633+
def test_list_team_invitations(client: GGClient):
16371634
"""
16381635
GIVEN a client
16391636
WHEN calling GET /teams/{id}/invitations endpoint
@@ -1649,7 +1646,7 @@ def test_list_team_invitations(client: GGClient, get_team: Callable[[], Team]):
16491646

16501647

16511648
@my_vcr.use_cassette("test_search_team_invitations.yaml", ignore_localhost=False)
1652-
def test_search_team_invitations(client: GGClient, get_team: Callable[[], Team]):
1649+
def test_search_team_invitations(client: GGClient):
16531650
"""
16541651
GIVEN a client
16551652
WHEN calling GET /teams/{id}/invitations endpoint
@@ -1670,7 +1667,7 @@ def test_search_team_invitations(client: GGClient, get_team: Callable[[], Team])
16701667

16711668

16721669
@my_vcr.use_cassette("test_delete_team_invitation.yaml", ignore_localhost=False)
1673-
def test_delete_team_invitation(client: GGClient, get_team: Callable[[], Team]):
1670+
def test_delete_team_invitation(client: GGClient):
16741671
"""
16751672
GIVEN a client
16761673
WHEN calling DELETE /teams/{id}/invitations/{id} endpoint
@@ -1689,7 +1686,7 @@ def test_delete_team_invitation(client: GGClient, get_team: Callable[[], Team]):
16891686

16901687

16911688
@my_vcr.use_cassette("test_list_team_members.yaml", ignore_localhost=False)
1692-
def test_list_team_members(client: GGClient, get_team: Callable[[], Team]):
1689+
def test_list_team_members(client: GGClient):
16931690
"""
16941691
GIVEN a client
16951692
WHEN calling GET /teams/{id}/members endpoint
@@ -1704,7 +1701,7 @@ def test_list_team_members(client: GGClient, get_team: Callable[[], Team]):
17041701

17051702

17061703
@my_vcr.use_cassette("test_search_team_members.yaml", ignore_localhost=False)
1707-
def test_search_team_members(client: GGClient, get_team: Callable[[], Team]):
1704+
def test_search_team_members(client: GGClient):
17081705
"""
17091706
GIVEN a client
17101707
WHEN calling GET /teams/{id}/members endpoint
@@ -1725,7 +1722,7 @@ def test_search_team_members(client: GGClient, get_team: Callable[[], Team]):
17251722

17261723

17271724
@my_vcr.use_cassette("test_create_team_member.yaml", ignore_localhost=False)
1728-
def test_create_team_member(client: GGClient, get_team: Callable[[], Team]):
1725+
def test_create_team_member(client: GGClient):
17291726
"""
17301727
GIVEN a client
17311728
WHEN calling POST /teams/{id}/members endpoint
@@ -1762,9 +1759,7 @@ def test_create_team_member(client: GGClient, get_team: Callable[[], Team]):
17621759

17631760

17641761
@my_vcr.use_cassette("test_create_team_member_parameters.yaml", ignore_localhost=False)
1765-
def test_create_team_member_without_mail(
1766-
client: GGClient, get_team: Callable[[], Team]
1767-
):
1762+
def test_create_team_member_without_mail(client: GGClient):
17681763
"""
17691764
GIVEN a client
17701765
WHEN calling POST /teams/{id}/members endpoint
@@ -1799,7 +1794,7 @@ def test_create_team_member_without_mail(
17991794

18001795

18011796
@my_vcr.use_cassette("test_delete_team_member.yaml", ignore_localhost=False)
1802-
def test_delete_team_member(client: GGClient, get_team: Callable[[], Team]):
1797+
def test_delete_team_member(client: GGClient):
18031798
"""
18041799
GIVEN a client
18051800
WHEN calling DELETE /teams/{id}/members/{id} endpoint
@@ -1854,7 +1849,7 @@ def test_search_sources(client: GGClient):
18541849

18551850

18561851
@my_vcr.use_cassette("test_list_teams_sources.yaml", ignore_localhost=False)
1857-
def test_list_team_sources(client: GGClient, get_team: Callable[[], Team]):
1852+
def test_list_team_sources(client: GGClient):
18581853
"""
18591854
GIVEN a client
18601855
WHEN calling GET /sources endpoint
@@ -1869,7 +1864,7 @@ def test_list_team_sources(client: GGClient, get_team: Callable[[], Team]):
18691864

18701865

18711866
@my_vcr.use_cassette("test_search_teams_sources.yaml", ignore_localhost=False)
1872-
def test_search_team_sources(client: GGClient, get_team: Callable[[], Team]):
1867+
def test_search_team_sources(client: GGClient):
18731868
"""
18741869
GIVEN a client
18751870
WHEN calling GET /sources endpoint
@@ -1886,7 +1881,7 @@ def test_search_team_sources(client: GGClient, get_team: Callable[[], Team]):
18861881

18871882

18881883
@my_vcr.use_cassette("test_delete_team_sources.yaml", ignore_localhost=False)
1889-
def test_delete_team_sources(client: GGClient, get_team: Callable[[], Team]):
1884+
def test_delete_team_sources(client: GGClient):
18901885
"""
18911886
GIVEN a client
18921887
WHEN calling POST /teams/{id}/sources endpoint
@@ -1911,9 +1906,7 @@ def test_delete_team_sources(client: GGClient, get_team: Callable[[], Team]):
19111906

19121907

19131908
@my_vcr.use_cassette("test_add_team_sources.yaml", ignore_localhost=False)
1914-
def test_add_team_sources(
1915-
client: GGClient, get_team: Callable[[], Team], get_source: Callable[[], Source]
1916-
):
1909+
def test_add_team_sources(client: GGClient):
19171910
"""
19181911
GIVEN a client
19191912
WHEN calling POST /teams/{id}/sources endpoint

tests/utils.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from pygitguardian.models import Invitation, Source, Team, TeamsParameter
2+
from pygitguardian.models_utils import CursorPaginatedResponse
3+
4+
from .conftest import create_client
5+
6+
7+
def get_source() -> Source:
8+
"""
9+
Return the first source available in the account,
10+
not all accounts have a source so testing from scratch
11+
will require installing a source first
12+
"""
13+
14+
client = create_client()
15+
paginated_sources = client.list_sources()
16+
assert isinstance(
17+
paginated_sources, CursorPaginatedResponse
18+
), "Could not fetch sources from GitGuardian"
19+
return paginated_sources.data[0]
20+
21+
22+
def get_invitation() -> Invitation:
23+
"""
24+
Return the first invitation available in the account,
25+
there is no invitation by default, one should be
26+
created to setup the test
27+
"""
28+
29+
client = create_client()
30+
paginated_teams = client.list_invitations()
31+
assert isinstance(
32+
paginated_teams, CursorPaginatedResponse
33+
), "Could not fetch members from GitGuardian"
34+
35+
return paginated_teams.data[0]
36+
37+
38+
def get_team() -> Team:
39+
"""
40+
Return the first team available in the account,
41+
every account should have at least one team (all incidents)
42+
but we skip it since we cannot add sources to it
43+
"""
44+
45+
client = create_client()
46+
47+
paginated_teams = client.list_teams(TeamsParameter(is_global=False))
48+
assert isinstance(
49+
paginated_teams, CursorPaginatedResponse
50+
), "Could not fetch teams from GitGuardian"
51+
52+
return paginated_teams.data[0]

0 commit comments

Comments
 (0)