Skip to content

Commit

Permalink
refactor: generate test data for each cli parameter that we support
Browse files Browse the repository at this point in the history
  • Loading branch information
Joris Conijn committed Feb 5, 2022
1 parent 7565a0f commit bee8914
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 174 deletions.
80 changes: 80 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
COMMIT2 = """commit my-hash-1
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my first commit"""

COMMIT1 = """commit my-hash-2
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my second commit"""

COMMITS = f"{COMMIT1}\n\n Issue #1\n\n{COMMIT2}\n\n Issue #2"
COMMITS_NO_ISSUES = f"{COMMIT1}\n\n{COMMIT2}"

SCENARIOS = [
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-west-1",
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-central-1",
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS_NO_ISSUES,
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-west-1",
"my-other-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS_NO_ISSUES,
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-central-1",
"my-other-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit::eu-west-1://my-repository",
"eu-central-1",
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit::://my-profile@my-repository",
None,
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit::://my-repository",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit://my-profile@my-repository",
None,
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
(
"codecommit://my-repository",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
),
]
180 changes: 21 additions & 159 deletions tests/test_command.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import json
from io import TextIOWrapper, BytesIO
from typing import List
from typing import List, Tuple, Any, Optional
from unittest.mock import patch, MagicMock

import pytest
from click.testing import CliRunner
from pull_request_codecommit import main
from pull_request_codecommit.config import configparser
from pull_request_codecommit.git import Commits
from .test_git_client import COMMITS, COMMITS_NO_ISSUES
from tests import SCENARIOS


def edit_message(message: str) -> str:
return message


def execute(parameters, stdout):
def aws_client_execute_side_effect(parameters, stdout):
assert -1 == stdout
mock_stdout = MagicMock()

Expand All @@ -29,154 +29,24 @@ def execute(parameters, stdout):
return mock_stdout


def generate_invoke_parameters(
cli_parameters,
) -> List[Tuple[str, Optional[str], Optional[str], bytes, str, List[str]]]:
"""
Generate pytest parameters that are used to invoke the test
"""
pytest_parameters = []

for cli_parameter in cli_parameters:
for scenario in SCENARIOS:
pytest_parameters.append(scenario + (cli_parameter,))

return pytest_parameters


@pytest.mark.parametrize(
"remote, region, profile, config, commits, parameters",
[
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-west-1",
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-central-1",
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS_NO_ISSUES,
[],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-west-1",
"my-other-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS_NO_ISSUES,
[],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-central-1",
"my-other-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit::eu-west-1://my-repository",
"eu-central-1",
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit::://my-profile@my-repository",
None,
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit::://my-repository",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit://my-profile@my-repository",
None,
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit://my-repository",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
[],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-west-1",
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-central-1",
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS_NO_ISSUES,
["--auto-merge"],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-west-1",
"my-other-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS_NO_ISSUES,
["--auto-merge"],
),
(
"codecommit::eu-west-1://my-profile@my-repository",
"eu-central-1",
"my-other-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
(
"codecommit::eu-west-1://my-repository",
"eu-central-1",
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
(
"codecommit::://my-profile@my-repository",
None,
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
(
"codecommit::://my-repository",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
(
"codecommit://my-profile@my-repository",
None,
"my-profile",
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
(
"codecommit://my-repository",
None,
None,
b"[default]\nbranch: my-main\n[profile my-profile]\nbranch: my-master",
COMMITS,
["--auto-merge"],
),
],
generate_invoke_parameters([[], ["--auto-merge"]]),
)
@patch("pull_request_codecommit.aws.client.subprocess.run")
@patch("pull_request_codecommit.repository.GitClient")
Expand All @@ -198,7 +68,7 @@ def test_invoke(
mock_git_client.return_value.remote.return_value = remote
mock_git_client.return_value.current_branch.return_value = "feat/my-feature"
configparser.open = MagicMock(return_value=TextIOWrapper(BytesIO(config))) # type: ignore
mock_aws_client.side_effect = execute
mock_aws_client.side_effect = aws_client_execute_side_effect

runner = CliRunner()
result = runner.invoke(main, parameters)
Expand Down Expand Up @@ -269,15 +139,7 @@ def test_invoke_with_path(
)
mock_git_client.return_value.current_branch.return_value = "feat/my-feature"
configparser.open = MagicMock(return_value=TextIOWrapper(BytesIO(config))) # type: ignore

def execute(parameters, stdout):
assert -1 == stdout
mock_stdout = MagicMock()
data = {"pullRequest": {"pullRequestId": 1}}
mock_stdout.stdout = bytes(json.dumps(data), "utf-8")
return mock_stdout

mock_aws_client.side_effect = execute
mock_aws_client.side_effect = aws_client_execute_side_effect

runner = CliRunner()
result = runner.invoke(main, ["--repository-path", "./some/path/to/repo"])
Expand Down
16 changes: 1 addition & 15 deletions tests/test_git_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,7 @@

from pull_request_codecommit.git import Commit
from pull_request_codecommit.git.client import Client

COMMIT2 = """commit my-hash-1
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my first commit"""

COMMIT1 = """commit my-hash-2
Author: John Doe <john@doe.nl>
Date: Fri Jan 21 21:01:00 2022 +0100
feat: my second commit"""

COMMITS = f"{COMMIT1}\n\n Issue #1\n\n{COMMIT2}\n\n Issue #2"
COMMITS_NO_ISSUES = f"{COMMIT1}\n\n{COMMIT2}"
from tests import COMMITS


def test_git_client() -> None:
Expand Down

0 comments on commit bee8914

Please sign in to comment.