Skip to content

Commit 444adac

Browse files
committed
add test_process_teams
1 parent 16c9364 commit 444adac

1 file changed

Lines changed: 56 additions & 31 deletions

File tree

tests/scripts/test_create_teams.py

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,36 @@
1+
from collections import OrderedDict
12
from json.decoder import JSONDecodeError
23
from unittest.mock import patch
34

45
import pytest
6+
from faker import Faker
7+
from src.scripts.create_teams import parse_input_file, update_assets
8+
from src.scripts.create_teams import process_teams
9+
from src.shared.models import Organization
10+
from src.shared.models import TeamStructure
11+
from tests.factories import AssetFactory, TeamAttributesFactory, TeamStructureFactory
12+
from tests.factories import OrganizationFactory
513

6-
from src.scripts.create_teams import parse_input_file, process_teams, update_assets
7-
from src.shared.models import Organization, TeamStructure, TeamAttributes
14+
locales = OrderedDict([
15+
('en-US', 1),
16+
])
17+
Faker.seed(10)
18+
fake = Faker(locales)
819

920

1021
@pytest.fixture
1122
def organization():
12-
return Organization(
13-
teams=[
14-
TeamStructure(name="team1", members=[], resources=[]),
15-
TeamStructure(name="team2", members=[], resources=[]),
16-
]
17-
)
23+
return OrganizationFactory.batch(3)
1824

1925

2026
@pytest.mark.parametrize(
2127
"json_data, expected_teams",
2228
[
2329
('{"teams": [{"name": "team1", "members": [], "resources": []}]}', 1),
2430
(
25-
'{"teams": [{"name": "team1", "members": [], "resources": []}, '
26-
'{"name": "team2", "members": [], "resources": []}]}',
27-
2,
31+
'{"teams": [{"name": "team1", "members": [], "resources": []}, '
32+
'{"name": "team2", "members": [], "resources": []}]}',
33+
2,
2834
),
2935
],
3036
)
@@ -43,20 +49,20 @@ def test_parse_input_file(json_data, expected_teams):
4349
("", "", False, ""), # No file provided
4450
("test_input.txt", "some text", False, ""), # Wrong file type provided
4551
(
46-
"test_input.json",
47-
'{"teams": [{"name": "team1", "members": [], "resources": []}',
48-
True,
49-
JSONDecodeError,
52+
"test_input.json",
53+
'{"teams": [{"name": "team1", "members": [], "resources": []}',
54+
True,
55+
JSONDecodeError,
5056
), # Malformed JSON data
5157
(
52-
"test_input.json",
53-
'{"name": "team1", "members": [], "resources": []}',
54-
True,
55-
KeyError,
58+
"test_input.json",
59+
'{"name": "team1", "members": [], "resources": []}',
60+
False,
61+
"",
5662
), # not an organization data
5763
],
5864
)
59-
def test_parse_input_file_with_invalid_json(invalid_file, json_data, should_raise, expected_exception):
65+
def test_parse_input_file__with_invalid_json(invalid_file, json_data, should_raise, expected_exception):
6066
if invalid_file:
6167
with open(invalid_file, "w") as file:
6268
file.write(json_data)
@@ -68,26 +74,45 @@ def test_parse_input_file_with_invalid_json(invalid_file, json_data, should_rais
6874
assert isinstance(exc_info.value, expected_exception)
6975
else:
7076
with pytest.raises(SystemExit) as exc_info:
71-
result = parse_input_file()
77+
parse_input_file()
7278
assert exc_info.value.code == 1
7379

7480

81+
@pytest.fixture
82+
def data():
83+
num_objects = 10
84+
organization = Organization(teams=TeamStructureFactory.batch(num_objects))
85+
assets = AssetFactory.batch(num_objects, asset_name=fake.word())
86+
teams = TeamAttributesFactory.batch(num_objects)
87+
88+
for i in range(num_objects):
89+
assets[i].asset_name = organization.teams[i].resources[0].name
90+
for i in range(num_objects):
91+
teams[i].name = [t.name for t in organization.teams][i]
92+
return organization, assets, teams
93+
94+
7595
@pytest.mark.parametrize(
76-
"existing_teams, expected_teams_to_create, expected_teams_to_delete",
96+
"label, existing_teams_indexes, asset_indexes, len_expected_teams_to_delete",
7797
[
78-
([], ["team1", "team2"], []),
79-
(["team1", "team2"], [], []),
80-
([], [], ["team1", "team2"]),
81-
(["team1", "team2"], ["team3"], ["team1", "team2"]),
82-
],
98+
("No teams to create and no teams to delete", [], [], 0),
99+
("No teams to create and no teams to delete", "all", "all", 0),
100+
("No teams to create and teams to delete", "all", [0, 5], 8),
101+
("Teams to create and no teams to delete", [0, 5], "all", 0),
102+
]
83103
)
84-
def test_process_teams(organization, existing_teams, expected_teams_to_create, expected_teams_to_delete):
85-
existing_teams = [TeamAttributes(name=team, attribute1=None, attribute2=None) for team in existing_teams]
104+
def test_process_teams(label, existing_teams_indexes, asset_indexes, data, len_expected_teams_to_delete):
105+
organization, assets, existing_teams = data
106+
existing_teams = [existing_teams[i] for i in
107+
existing_teams_indexes] if existing_teams_indexes != "all" else existing_teams
108+
assets = [assets[i] for i in
109+
asset_indexes] if asset_indexes != "all" else assets
110+
86111
with patch("src.scripts.create_teams.get_existing_teams") as mock_get_existing_teams:
87112
with patch("src.scripts.create_teams.create_teams") as mock_create_teams:
88113
mock_get_existing_teams.return_value = existing_teams
89-
teams_to_delete = process_teams("token", organization)
90-
assert teams_to_delete == expected_teams_to_delete
114+
teams_to_delete = process_teams("token", organization, assets)
115+
assert len(teams_to_delete) == len_expected_teams_to_delete
91116

92117

93118
def test_update_assets(organization):

0 commit comments

Comments
 (0)