summaryrefslogtreecommitdiff
path: root/gitlab/tests
diff options
context:
space:
mode:
authorNejc Habjan <hab.nejc@gmail.com>2020-03-22 06:03:53 -0400
committerNejc Habjan <hab.nejc@gmail.com>2020-03-22 08:50:05 -0400
commit8c037712a53c1c54e46298fbb93441d9b7a7144a (patch)
treec4795b86ce7ae1afc0e5e6af0457e0ef76be326f /gitlab/tests
parent82deb7dbe261c4b42a9c45a5b85a2c767f3a8218 (diff)
downloadgitlab-8c037712a53c1c54e46298fbb93441d9b7a7144a.tar.gz
test: create separate module for commit tests
Diffstat (limited to 'gitlab/tests')
-rw-r--r--gitlab/tests/objects/test_commits.py79
-rw-r--r--gitlab/tests/test_gitlab.py44
2 files changed, 79 insertions, 44 deletions
diff --git a/gitlab/tests/objects/test_commits.py b/gitlab/tests/objects/test_commits.py
new file mode 100644
index 0000000..3247d60
--- /dev/null
+++ b/gitlab/tests/objects/test_commits.py
@@ -0,0 +1,79 @@
+from httmock import urlmatch, response, with_httmock
+
+from .test_projects import headers, TestProject
+
+
+@urlmatch(
+ scheme="http",
+ netloc="localhost",
+ path="/api/v4/projects/1/repository/commits/6b2257ea",
+ method="get",
+)
+def resp_get_commit(url, request):
+ """Mock for commit GET response."""
+ content = """{
+ "id": "6b2257eabcec3db1f59dafbd84935e3caea04235",
+ "short_id": "6b2257ea",
+ "title": "Initial commit"
+ }"""
+ content = content.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+
+@urlmatch(
+ scheme="http", path="/api/v4/projects/1/repository/commits", method="post",
+)
+def resp_create_commit(url, request):
+ """Mock for commit create POST response."""
+ content = """{
+ "id": "ed899a2f4b50b4370feeea94676502b42383c746",
+ "short_id": "ed899a2f",
+ "title": "Commit message"
+ }"""
+ content = content.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+
+@urlmatch(
+ scheme="http", path="/api/v4/projects/1/repository/commits/6b2257ea", method="post",
+)
+def resp_revert_commit(url, request):
+ """Mock for commit revert POST response."""
+ content = """{
+ "id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
+ "short_id": "8b090c1b",
+ "title":"Revert \\"Initial commit\\""
+ }"""
+ content = content.encode("utf-8")
+ return response(200, content, headers, None, 5, request)
+
+
+class TestCommit(TestProject):
+ """
+ Base class for commit tests. Inherits from TestProject,
+ since currently all commit methods are under projects.
+ """
+
+ @with_httmock(resp_get_commit)
+ def test_get_commit(self):
+ commit = self.project.commits.get("6b2257ea")
+ self.assertEqual(commit.short_id, "6b2257ea")
+ self.assertEqual(commit.title, "Initial commit")
+
+ @with_httmock(resp_create_commit)
+ def test_create_commit(self):
+ data = {
+ "branch": "master",
+ "commit_message": "Commit message",
+ "actions": [{"action": "create", "file_path": "README", "content": "",}],
+ }
+ commit = self.project.commits.create(data)
+ self.assertEqual(commit.short_id, "ed899a2f")
+ self.assertEqual(commit.title, data["commit_message"])
+
+ @with_httmock(resp_revert_commit)
+ def test_revert_commit(self):
+ commit = self.project.commits.get("6b2257ea", lazy=True)
+ revert_commit = commit.revert(branch="master")
+ self.assertEqual(revert_commit["short_id"], "8b090c1b")
+ self.assertEqual(revert_commit["title"], 'Revert "Initial commit"')
diff --git a/gitlab/tests/test_gitlab.py b/gitlab/tests/test_gitlab.py
index 591f166..d104c7d 100644
--- a/gitlab/tests/test_gitlab.py
+++ b/gitlab/tests/test_gitlab.py
@@ -843,50 +843,6 @@ class TestGitlab(unittest.TestCase):
self.gl.users.get(1, lazy=True).activate()
self.gl.users.get(1, lazy=True).deactivate()
- def test_commit_revert(self):
- @urlmatch(
- scheme="http",
- netloc="localhost",
- path="/api/v4/projects/1/repository/commits/6b2257ea",
- method="get",
- )
- def resp_get_commit(url, request):
- headers = {"content-type": "application/json"}
- content = """{
- "id": "6b2257eabcec3db1f59dafbd84935e3caea04235",
- "short_id": "6b2257ea",
- "title": "Initial commit"
- }"""
- content = content.encode("utf-8")
- return response(200, content, headers, None, 5, request)
-
- @urlmatch(
- scheme="http",
- netloc="localhost",
- path="/api/v4/projects/1/repository/commits/6b2257ea",
- method="post",
- )
- def resp_revert_commit(url, request):
- headers = {"content-type": "application/json"}
- content = """{
- "id": "8b090c1b79a14f2bd9e8a738f717824ff53aebad",
- "short_id": "8b090c1b",
- "title":"Revert \\"Initial commit\\""
- }"""
- content = content.encode("utf-8")
- return response(200, content, headers, None, 5, request)
-
- with HTTMock(resp_get_commit):
- project = self.gl.projects.get(1, lazy=True)
- commit = project.commits.get("6b2257ea")
- self.assertEqual(commit.short_id, "6b2257ea")
- self.assertEqual(commit.title, "Initial commit")
-
- with HTTMock(resp_revert_commit):
- revert_commit = commit.revert(branch="master")
- self.assertEqual(revert_commit["short_id"], "8b090c1b")
- self.assertEqual(revert_commit["title"], 'Revert "Initial commit"')
-
def test_update_submodule(self):
@urlmatch(
scheme="http", netloc="localhost", path="/api/v4/projects/1$", method="get"