Skip to content

Commit 42a10da

Browse files
committed
test: convert test_decision.py to the pytest format
1 parent d530da6 commit 42a10da

File tree

1 file changed

+64
-69
lines changed

1 file changed

+64
-69
lines changed

test/test_decision.py

Lines changed: 64 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
import shutil
99
import tempfile
10-
import unittest
1110
from pathlib import Path
1211

1312
import pytest
@@ -18,70 +17,66 @@
1817
FAKE_GRAPH_CONFIG = {"product-dir": "browser", "taskgraph": {}}
1918

2019

21-
class TestDecision(unittest.TestCase):
22-
def test_write_artifact_json(self):
23-
data = [{"some": "data"}]
24-
tmpdir = tempfile.mkdtemp()
25-
try:
26-
decision.ARTIFACTS_DIR = Path(tmpdir) / "artifacts"
27-
decision.write_artifact("artifact.json", data)
28-
with open(os.path.join(decision.ARTIFACTS_DIR, "artifact.json")) as f:
29-
self.assertEqual(json.load(f), data)
30-
finally:
31-
if os.path.exists(tmpdir):
32-
shutil.rmtree(tmpdir)
33-
decision.ARTIFACTS_DIR = Path("artifacts")
34-
35-
def test_write_artifact_yml(self):
36-
data = [{"some": "data"}]
37-
tmpdir = tempfile.mkdtemp()
38-
try:
39-
decision.ARTIFACTS_DIR = Path(tmpdir) / "artifacts"
40-
decision.write_artifact("artifact.yml", data)
41-
self.assertEqual(load_yaml(decision.ARTIFACTS_DIR, "artifact.yml"), data)
42-
finally:
43-
if os.path.exists(tmpdir):
44-
shutil.rmtree(tmpdir)
45-
decision.ARTIFACTS_DIR = Path("artifacts")
46-
47-
48-
class TestGetDecisionParameters(unittest.TestCase):
49-
def setUp(self):
50-
self.options = {
51-
"base_repository": "https://hg.mozilla.org/mozilla-unified",
52-
"head_repository": "https://hg.mozilla.org/mozilla-central",
53-
"head_rev": "abcd",
54-
"head_ref": "default",
55-
"head_tag": "v0.0.1",
56-
"project": "mozilla-central",
57-
"pushlog_id": "143",
58-
"pushdate": 1503691511,
59-
"repository_type": "hg",
60-
"owner": "[email protected]",
61-
"tasks_for": "hg-push",
62-
"level": "3",
63-
}
64-
self.old_determine_more_accurate_base_rev = (
65-
decision._determine_more_accurate_base_rev
66-
)
67-
decision._determine_more_accurate_base_rev = lambda *_, **__: "abcd"
68-
69-
def tearDown(self):
70-
decision._determine_more_accurate_base_rev = (
71-
self.old_determine_more_accurate_base_rev
72-
)
73-
74-
def test_simple_options(self):
75-
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
76-
self.assertEqual(params["build_date"], 1503691511)
77-
self.assertEqual(params["head_tag"], "v0.0.1")
78-
self.assertEqual(params["pushlog_id"], "143")
79-
self.assertEqual(params["moz_build_date"], "20170825200511")
80-
81-
def test_no_email_owner(self):
82-
self.options["owner"] = "ffxbld"
83-
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, self.options)
84-
self.assertEqual(params["owner"], "[email protected]")
20+
def test_write_artifact_json():
21+
data = [{"some": "data"}]
22+
tmpdir = tempfile.mkdtemp()
23+
try:
24+
decision.ARTIFACTS_DIR = Path(tmpdir) / "artifacts"
25+
decision.write_artifact("artifact.json", data)
26+
with open(os.path.join(decision.ARTIFACTS_DIR, "artifact.json")) as f:
27+
assert json.load(f) == data
28+
finally:
29+
if os.path.exists(tmpdir):
30+
shutil.rmtree(tmpdir)
31+
decision.ARTIFACTS_DIR = Path("artifacts")
32+
33+
34+
def test_write_artifact_yml():
35+
data = [{"some": "data"}]
36+
tmpdir = tempfile.mkdtemp()
37+
try:
38+
decision.ARTIFACTS_DIR = Path(tmpdir) / "artifacts"
39+
decision.write_artifact("artifact.yml", data)
40+
assert load_yaml(decision.ARTIFACTS_DIR, "artifact.yml") == data
41+
finally:
42+
if os.path.exists(tmpdir):
43+
shutil.rmtree(tmpdir)
44+
decision.ARTIFACTS_DIR = Path("artifacts")
45+
46+
47+
@pytest.fixture
48+
def options(monkeypatch):
49+
monkeypatch.setattr(
50+
decision, "_determine_more_accurate_base_rev", lambda *_, **__: "abcd"
51+
)
52+
return {
53+
"base_repository": "https://hg.mozilla.org/mozilla-unified",
54+
"head_repository": "https://hg.mozilla.org/mozilla-central",
55+
"head_rev": "abcd",
56+
"head_ref": "default",
57+
"head_tag": "v0.0.1",
58+
"project": "mozilla-central",
59+
"pushlog_id": "143",
60+
"pushdate": 1503691511,
61+
"repository_type": "hg",
62+
"owner": "[email protected]",
63+
"tasks_for": "hg-push",
64+
"level": "3",
65+
}
66+
67+
68+
def test_simple_options(options):
69+
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, options)
70+
assert params["build_date"] == 1503691511
71+
assert params["head_tag"] == "v0.0.1"
72+
assert params["pushlog_id"] == "143"
73+
assert params["moz_build_date"] == "20170825200511"
74+
75+
76+
def test_no_email_owner(options):
77+
options["owner"] = "ffxbld"
78+
params = decision.get_decision_parameters(FAKE_GRAPH_CONFIG, options)
79+
assert params["owner"] == "[email protected]"
8580

8681

8782
@pytest.mark.parametrize(
@@ -93,9 +88,9 @@ def test_no_email_owner(self):
9388
),
9489
)
9590
def test_determine_more_accurate_base_ref(
96-
candidate_base_ref, base_rev, expected_base_ref
91+
candidate_base_ref, base_rev, expected_base_ref, mocker
9792
):
98-
repo_mock = unittest.mock.MagicMock()
93+
repo_mock = mocker.MagicMock()
9994
repo_mock.default_branch = "default-branch"
10095

10196
assert (
@@ -121,9 +116,9 @@ def test_determine_more_accurate_base_ref(
121116
),
122117
)
123118
def test_determine_more_accurate_base_rev(
124-
common_rev, candidate_base_rev, expected_base_ref_or_rev, expected_base_rev
119+
common_rev, candidate_base_rev, expected_base_ref_or_rev, expected_base_rev, mocker
125120
):
126-
repo_mock = unittest.mock.MagicMock()
121+
repo_mock = mocker.MagicMock()
127122
repo_mock.find_latest_common_revision.return_value = common_rev
128123
repo_mock.does_revision_exist_locally = lambda rev: rev == "existing-rev"
129124

0 commit comments

Comments
 (0)