Skip to content

Commit 592f1bb

Browse files
tarekbadrshafrittoli
authored andcommitted
add branch obj
1 parent d1ac3fa commit 592f1bb

File tree

4 files changed

+136
-23
lines changed

4 files changed

+136
-23
lines changed

cli/cdevents/cli/branch.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
"""Module for cli branch commands."""
22
from __future__ import annotations
33

4-
import os
54
from typing import List
65

76
import click
87

98
from cdevents.cli.utils import add_disclaimer_text, print_function_args
109
from cdevents.cli.cdevents_command import CDeventsCommand
1110

12-
from cdevents.core.events import Events
13-
from cdevents.core import event_type
11+
from cdevents.core.branch import Branch, BranchType
1412

1513
# pylint: disable=unused-argument
1614
def common_branch_options(function):
@@ -56,11 +54,11 @@ def created(
5654
repoid: str = None,
5755
data: List[str] = None,
5856
):
59-
print_function_args()
60-
e = Events()
61-
new_event = e.create_branch_event(event_type.BranchCreatedEventV1, id, name, repoid, data)
57+
print_function_args()
58+
branch = Branch(branch_type=BranchType.BranchCreatedEventV1, id=id, name=name, repoid=repoid)
59+
branch_event = branch.create_event(data)
6260
cdevents_command = CDeventsCommand()
63-
cdevents_command.run(new_event)
61+
cdevents_command.run(branch_event)
6462

6563

6664
@click.command(help=add_disclaimer_text("Branch Deleted CloudEvent."))
@@ -71,8 +69,8 @@ def deleted(
7169
repoid: str = None,
7270
data: List[str] = None,
7371
):
74-
print_function_args()
75-
e = Events()
76-
new_event = e.create_branch_event(event_type.BranchDeletedEventV1, id, name, repoid, data)
72+
print_function_args()
73+
branch = Branch(branch_type=BranchType.BranchDeletedEventV1, id=id, name=name, repoid=repoid)
74+
branch_event = branch.create_event(data)
7775
cdevents_command = CDeventsCommand()
78-
cdevents_command.run(new_event)
76+
cdevents_command.run(branch_event)

cli/tests/test_branch.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""Unit tests for artifact."""
2+
import pytest
3+
from unittest.mock import patch
4+
from click.testing import CliRunner
5+
6+
from cdevents.cli.branch import created, deleted
7+
from cdevents.cli.cdevents_command import CDeventsCommand
8+
9+
# pylint: disable=missing-function-docstring, protected-access, missing-class-docstring
10+
11+
@pytest.fixture
12+
def runner() -> CliRunner:
13+
return CliRunner()
14+
15+
16+
ID_ARG = "id"
17+
NAME_ARG = "name"
18+
REPOID_ARG = "repoid"
19+
DATA_ARG = "data"
20+
21+
@pytest.mark.unit
22+
def test_created(runner: CliRunner):
23+
"""Test created of a branch."""
24+
25+
expected_id = "task1"
26+
expected_name = "My Task Run"
27+
expected_repoid = "repo1"
28+
expected_data = ["key1", "value1"]
29+
30+
with patch.object(CDeventsCommand, "run", spec=CDeventsCommand):
31+
result = runner.invoke(
32+
created,
33+
[
34+
f"--{ID_ARG}",
35+
expected_id,
36+
f"--{NAME_ARG}",
37+
expected_name,
38+
f"--{REPOID_ARG}",
39+
expected_repoid,
40+
f"--{DATA_ARG}",
41+
*expected_data,
42+
],
43+
)
44+
assert result.exit_code == 0
45+
assert f"{ID_ARG}={expected_id}" in result.stdout
46+
assert f"{NAME_ARG}={expected_name}" in result.stdout
47+
assert f"{REPOID_ARG}={expected_repoid}" in result.stdout
48+
assert f"{DATA_ARG}=({tuple(expected_data)},)" in result.stdout
49+
50+
@pytest.mark.unit
51+
def test_deleted(runner: CliRunner):
52+
"""Test deleted of a branch."""
53+
54+
expected_id = "task1"
55+
expected_name = "My Task Run"
56+
expected_repoid = "repo1"
57+
expected_data = ["key1", "value1"]
58+
59+
with patch.object(CDeventsCommand, "run", spec=CDeventsCommand):
60+
result = runner.invoke(
61+
deleted,
62+
[
63+
f"--{ID_ARG}",
64+
expected_id,
65+
f"--{NAME_ARG}",
66+
expected_name,
67+
f"--{REPOID_ARG}",
68+
expected_repoid,
69+
f"--{DATA_ARG}",
70+
*expected_data,
71+
],
72+
)
73+
assert result.exit_code == 0
74+
assert f"{ID_ARG}={expected_id}" in result.stdout
75+
assert f"{NAME_ARG}={expected_name}" in result.stdout
76+
assert f"{REPOID_ARG}={expected_repoid}" in result.stdout
77+
assert f"{DATA_ARG}=({tuple(expected_data)},)" in result.stdout

core/cdevents/core/branch.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""branch"""
2+
3+
from enum import Enum
4+
from cdevents.core.events import Events
5+
6+
class BranchType(Enum):
7+
BranchCreatedEventV1: str = "cd.repository.branch.created.v1"
8+
BranchDeletedEventV1: str = "cd.repository.branch.deleted.v1"
9+
10+
11+
class Branch(Events):
12+
"""Events."""
13+
14+
def __init__(self, branch_type: BranchType, id: str, name: str, repoid: str):
15+
"""Initializes class.
16+
"""
17+
self._event_type = branch_type
18+
self._id = id
19+
self._name = name
20+
self._repoid = repoid
21+
22+
def create_extensions(self):
23+
"""Create extensions.
24+
"""
25+
extensions = {
26+
"branchid": self._id,
27+
"branchname": self._name,
28+
"branchrepositoryid": self._repoid,
29+
}
30+
return extensions
31+
32+
def create_event(self, data: dict={}):
33+
"""Create branch event.
34+
"""
35+
extensions = self.create_extensions()
36+
event = super().create_event(event_type=self._event_type.value, extensions=extensions, data=data)
37+
return event

core/tests/test_branch.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
from cdevents.core import event_type
21
import pytest
32

4-
from cdevents.core.events import Events
3+
from cdevents.core.branch import Branch, BranchType
54

65
@pytest.mark.unit
76
def test_repository_branch_created():
8-
event = Events().create_branch_event(event_type.BranchCreatedEventV1, id="_id", name="_name", repoid="_repoid", data={"branch": "_branch"})
9-
assert event is not None
10-
assert event._attributes["type"] == event_type.BranchCreatedEventV1
11-
assert event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
12-
assert event.data == {"branch": "_branch"}
7+
branch = Branch(branch_type=BranchType.BranchCreatedEventV1, id="_id", name="_name", repoid="_repoid")
8+
branch_event = branch.create_event(data={"key1": "value1"})
9+
assert branch_event is not None
10+
assert branch_event._attributes["type"] == "cd.repository.branch.created.v1"
11+
assert branch_event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
12+
assert branch_event.data == {"key1": "value1"}
1313

1414
@pytest.mark.unit
1515
def test_repository_branch_deleted():
16-
event = Events().create_branch_event(event_type.BranchDeletedEventV1, id="_id", name="_name", repoid="_repoid", data={"branch": "_branch"})
17-
assert event is not None
18-
assert event._attributes["type"] == event_type.BranchDeletedEventV1
19-
assert event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
20-
assert event.data == {"branch": "_branch"}
16+
branch = Branch(branch_type=BranchType.BranchDeletedEventV1, id="_id", name="_name", repoid="_repoid")
17+
branch_event = branch.create_event(data={"key1": "value1"})
18+
assert branch_event is not None
19+
assert branch_event._attributes["type"] == "cd.repository.branch.deleted.v1"
20+
assert branch_event._attributes["extensions"] == {"branchid": "_id", "branchname": "_name", "branchrepositoryid": "_repoid"}
21+
assert branch_event.data == {"key1": "value1"}

0 commit comments

Comments
 (0)