From 6b892e3dcb18d0f43da6020b08fd4ba891da3670 Mon Sep 17 00:00:00 2001 From: Nejc Habjan Date: Sat, 27 Nov 2021 20:28:15 +0100 Subject: test(cli): improve basic CLI coverage --- tests/unit/conftest.py | 5 +++ tests/unit/data/todo.json | 75 ---------------------------------------- tests/unit/fixtures/todo.json | 75 ++++++++++++++++++++++++++++++++++++++++ tests/unit/objects/test_todos.py | 12 ++++--- tests/unit/test_cli.py | 14 ++++++-- tests/unit/test_config.py | 2 +- 6 files changed, 99 insertions(+), 84 deletions(-) delete mode 100644 tests/unit/data/todo.json create mode 100644 tests/unit/fixtures/todo.json (limited to 'tests/unit') diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index f58c77a..929be1a 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -3,6 +3,11 @@ import pytest import gitlab +@pytest.fixture(scope="session") +def fixture_dir(test_dir): + return test_dir / "unit" / "fixtures" + + @pytest.fixture def gl(): return gitlab.Gitlab( diff --git a/tests/unit/data/todo.json b/tests/unit/data/todo.json deleted file mode 100644 index 93b2151..0000000 --- a/tests/unit/data/todo.json +++ /dev/null @@ -1,75 +0,0 @@ -[ - { - "id": 102, - "project": { - "id": 2, - "name": "Gitlab Ce", - "name_with_namespace": "Gitlab Org / Gitlab Ce", - "path": "gitlab-ce", - "path_with_namespace": "gitlab-org/gitlab-ce" - }, - "author": { - "name": "Administrator", - "username": "root", - "id": 1, - "state": "active", - "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", - "web_url": "https://gitlab.example.com/root" - }, - "action_name": "marked", - "target_type": "MergeRequest", - "target": { - "id": 34, - "iid": 7, - "project_id": 2, - "title": "Dolores in voluptatem tenetur praesentium omnis repellendus voluptatem quaerat.", - "description": "Et ea et omnis illum cupiditate. Dolor aspernatur tenetur ducimus facilis est nihil. Quo esse cupiditate molestiae illo corrupti qui quidem dolor.", - "state": "opened", - "created_at": "2016-06-17T07:49:24.419Z", - "updated_at": "2016-06-17T07:52:43.484Z", - "target_branch": "tutorials_git_tricks", - "source_branch": "DNSBL_docs", - "upvotes": 0, - "downvotes": 0, - "author": { - "name": "Maxie Medhurst", - "username": "craig_rutherford", - "id": 12, - "state": "active", - "avatar_url": "http://www.gravatar.com/avatar/a0d477b3ea21970ce6ffcbb817b0b435?s=80&d=identicon", - "web_url": "https://gitlab.example.com/craig_rutherford" - }, - "assignee": { - "name": "Administrator", - "username": "root", - "id": 1, - "state": "active", - "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", - "web_url": "https://gitlab.example.com/root" - }, - "source_project_id": 2, - "target_project_id": 2, - "labels": [], - "work_in_progress": false, - "milestone": { - "id": 32, - "iid": 2, - "project_id": 2, - "title": "v1.0", - "description": "Assumenda placeat ea voluptatem voluptate qui.", - "state": "active", - "created_at": "2016-06-17T07:47:34.163Z", - "updated_at": "2016-06-17T07:47:34.163Z", - "due_date": null - }, - "merge_when_pipeline_succeeds": false, - "merge_status": "cannot_be_merged", - "subscribed": true, - "user_notes_count": 7 - }, - "target_url": "https://gitlab.example.com/gitlab-org/gitlab-ce/merge_requests/7", - "body": "Dolores in voluptatem tenetur praesentium omnis repellendus voluptatem quaerat.", - "state": "pending", - "created_at": "2016-06-17T07:52:35.225Z" - } -] diff --git a/tests/unit/fixtures/todo.json b/tests/unit/fixtures/todo.json new file mode 100644 index 0000000..93b2151 --- /dev/null +++ b/tests/unit/fixtures/todo.json @@ -0,0 +1,75 @@ +[ + { + "id": 102, + "project": { + "id": 2, + "name": "Gitlab Ce", + "name_with_namespace": "Gitlab Org / Gitlab Ce", + "path": "gitlab-ce", + "path_with_namespace": "gitlab-org/gitlab-ce" + }, + "author": { + "name": "Administrator", + "username": "root", + "id": 1, + "state": "active", + "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + "web_url": "https://gitlab.example.com/root" + }, + "action_name": "marked", + "target_type": "MergeRequest", + "target": { + "id": 34, + "iid": 7, + "project_id": 2, + "title": "Dolores in voluptatem tenetur praesentium omnis repellendus voluptatem quaerat.", + "description": "Et ea et omnis illum cupiditate. Dolor aspernatur tenetur ducimus facilis est nihil. Quo esse cupiditate molestiae illo corrupti qui quidem dolor.", + "state": "opened", + "created_at": "2016-06-17T07:49:24.419Z", + "updated_at": "2016-06-17T07:52:43.484Z", + "target_branch": "tutorials_git_tricks", + "source_branch": "DNSBL_docs", + "upvotes": 0, + "downvotes": 0, + "author": { + "name": "Maxie Medhurst", + "username": "craig_rutherford", + "id": 12, + "state": "active", + "avatar_url": "http://www.gravatar.com/avatar/a0d477b3ea21970ce6ffcbb817b0b435?s=80&d=identicon", + "web_url": "https://gitlab.example.com/craig_rutherford" + }, + "assignee": { + "name": "Administrator", + "username": "root", + "id": 1, + "state": "active", + "avatar_url": "http://www.gravatar.com/avatar/e64c7d89f26bd1972efa854d13d7dd61?s=80&d=identicon", + "web_url": "https://gitlab.example.com/root" + }, + "source_project_id": 2, + "target_project_id": 2, + "labels": [], + "work_in_progress": false, + "milestone": { + "id": 32, + "iid": 2, + "project_id": 2, + "title": "v1.0", + "description": "Assumenda placeat ea voluptatem voluptate qui.", + "state": "active", + "created_at": "2016-06-17T07:47:34.163Z", + "updated_at": "2016-06-17T07:47:34.163Z", + "due_date": null + }, + "merge_when_pipeline_succeeds": false, + "merge_status": "cannot_be_merged", + "subscribed": true, + "user_notes_count": 7 + }, + "target_url": "https://gitlab.example.com/gitlab-org/gitlab-ce/merge_requests/7", + "body": "Dolores in voluptatem tenetur praesentium omnis repellendus voluptatem quaerat.", + "state": "pending", + "created_at": "2016-06-17T07:52:35.225Z" + } +] diff --git a/tests/unit/objects/test_todos.py b/tests/unit/objects/test_todos.py index 9d6b6b4..ded6cf9 100644 --- a/tests/unit/objects/test_todos.py +++ b/tests/unit/objects/test_todos.py @@ -3,20 +3,22 @@ GitLab API: https://docs.gitlab.com/ce/api/todos.html """ import json -import os import pytest import responses from gitlab.v4.objects import Todo -with open(f"{os.path.dirname(__file__)}/../data/todo.json", "r") as json_file: - todo_content = json_file.read() - json_content = json.loads(todo_content) + +@pytest.fixture() +def json_content(fixture_dir): + with open(fixture_dir / "todo.json", "r") as json_file: + todo_content = json_file.read() + return json.loads(todo_content) @pytest.fixture -def resp_todo(): +def resp_todo(json_content): with responses.RequestsMock(assert_all_requests_are_fired=False) as rsps: rsps.add( method=responses.GET, diff --git a/tests/unit/test_cli.py b/tests/unit/test_cli.py index d5afe69..2ada1c3 100644 --- a/tests/unit/test_cli.py +++ b/tests/unit/test_cli.py @@ -25,6 +25,7 @@ from contextlib import redirect_stderr # noqa: H302 import pytest from gitlab import cli +from gitlab.exceptions import GitlabError @pytest.mark.parametrize( @@ -66,12 +67,19 @@ def test_cls_to_what(class_name, expected_what): assert cli.cls_to_what(TestClass) == expected_what -def test_die(): +@pytest.mark.parametrize( + "message,error,expected", + [ + ("foobar", None, "foobar\n"), + ("foo", GitlabError("bar"), "foo (bar)\n"), + ], +) +def test_die(message, error, expected): fl = io.StringIO() with redirect_stderr(fl): with pytest.raises(SystemExit) as test: - cli.die("foobar") - assert fl.getvalue() == "foobar\n" + cli.die(message, error) + assert fl.getvalue() == expected assert test.value.code == 1 diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index f7fffb2..82b9714 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -18,8 +18,8 @@ import io import os from textwrap import dedent +from unittest import mock -import mock import pytest from gitlab import config, USER_AGENT -- cgit v1.2.1