Skip to content

Commit 8c03771

Browse files
committed
test: create separate module for commit tests
1 parent 82deb7d commit 8c03771

File tree

2 files changed

+79
-44
lines changed

2 files changed

+79
-44
lines changed

gitlab/tests/objects/test_commits.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from httmock import urlmatch, response, with_httmock
2+
3+
from .test_projects import headers, TestProject
4+
5+
6+
@urlmatch(
7+
scheme="http",
8+
netloc="localhost",
9+
path="/api/v4/projects/1/repository/commits/6b2257ea",
10+
method="get",
11+
)
12+
def resp_get_commit(url, request):
13+
"""Mock for commit GET response."""
14+
content = """{
15+
"id": "6b2257eabcec3db1f59dafbd84935e3caea04235",
16+
"short_id": "6b2257ea",
17+
"title": "Initial commit"
18+
}"""
19+
content = content.encode("utf-8")
20+
return response(200, content, headers, None, 5, request)
21+
22+
23+
@urlmatch(
24+
scheme="http", path="/api/v4/projects/1/repository/commits", method="post",
25+
)
26+
def resp_create_commit(url, request):
27+
"""Mock for commit create POST response."""
28+
content = """{
29+
"id": "ed899a2f4b50b4370feeea94676502b42383c746",
30+
"short_id": "ed899a2f",
31+
"title": "Commit message"
32+
}"""
33+
content = content.encode("utf-8")
34+
return response(200, content, headers, None, 5, request)
35+
36+
37+
@urlmatch(
38+
scheme="http", path="/api/v4/projects/1/repository/commits/6b2257ea", method="post",
39+
)
40+
def resp_revert_commit(url, request):
41+
"""Mock for commit revert POST response."""
42+
content = """{
43+
"id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
44+
"short_id": "8b090c1b",
45+
"title":"Revert \\"Initial commit\\""
46+
}"""
47+
content = content.encode("utf-8")
48+
return response(200, content, headers, None, 5, request)
49+
50+
51+
class TestCommit(TestProject):
52+
"""
53+
Base class for commit tests. Inherits from TestProject,
54+
since currently all commit methods are under projects.
55+
"""
56+
57+
@with_httmock(resp_get_commit)
58+
def test_get_commit(self):
59+
commit = self.project.commits.get("6b2257ea")
60+
self.assertEqual(commit.short_id, "6b2257ea")
61+
self.assertEqual(commit.title, "Initial commit")
62+
63+
@with_httmock(resp_create_commit)
64+
def test_create_commit(self):
65+
data = {
66+
"branch": "master",
67+
"commit_message": "Commit message",
68+
"actions": [{"action": "create", "file_path": "README", "content": "",}],
69+
}
70+
commit = self.project.commits.create(data)
71+
self.assertEqual(commit.short_id, "ed899a2f")
72+
self.assertEqual(commit.title, data["commit_message"])
73+
74+
@with_httmock(resp_revert_commit)
75+
def test_revert_commit(self):
76+
commit = self.project.commits.get("6b2257ea", lazy=True)
77+
revert_commit = commit.revert(branch="master")
78+
self.assertEqual(revert_commit["short_id"], "8b090c1b")
79+
self.assertEqual(revert_commit["title"], 'Revert "Initial commit"')

gitlab/tests/test_gitlab.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -843,50 +843,6 @@ def resp_deactivate(url, request):
843843
self.gl.users.get(1, lazy=True).activate()
844844
self.gl.users.get(1, lazy=True).deactivate()
845845

846-
def test_commit_revert(self):
847-
@urlmatch(
848-
scheme="http",
849-
netloc="localhost",
850-
path="/api/v4/projects/1/repository/commits/6b2257ea",
851-
method="get",
852-
)
853-
def resp_get_commit(url, request):
854-
headers = {"content-type": "application/json"}
855-
content = """{
856-
"id": "6b2257eabcec3db1f59dafbd84935e3caea04235",
857-
"short_id": "6b2257ea",
858-
"title": "Initial commit"
859-
}"""
860-
content = content.encode("utf-8")
861-
return response(200, content, headers, None, 5, request)
862-
863-
@urlmatch(
864-
scheme="http",
865-
netloc="localhost",
866-
path="/api/v4/projects/1/repository/commits/6b2257ea",
867-
method="post",
868-
)
869-
def resp_revert_commit(url, request):
870-
headers = {"content-type": "application/json"}
871-
content = """{
872-
"id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
873-
"short_id": "8b090c1b",
874-
"title":"Revert \\"Initial commit\\""
875-
}"""
876-
content = content.encode("utf-8")
877-
return response(200, content, headers, None, 5, request)
878-
879-
with HTTMock(resp_get_commit):
880-
project = self.gl.projects.get(1, lazy=True)
881-
commit = project.commits.get("6b2257ea")
882-
self.assertEqual(commit.short_id, "6b2257ea")
883-
self.assertEqual(commit.title, "Initial commit")
884-
885-
with HTTMock(resp_revert_commit):
886-
revert_commit = commit.revert(branch="master")
887-
self.assertEqual(revert_commit["short_id"], "8b090c1b")
888-
self.assertEqual(revert_commit["title"], 'Revert "Initial commit"')
889-
890846
def test_update_submodule(self):
891847
@urlmatch(
892848
scheme="http", netloc="localhost", path="/api/v4/projects/1$", method="get"

0 commit comments

Comments
 (0)