| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f26bf7d commit c764bee
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,61 +20,74 @@ | |||
| 20 | 20 | import warnings | |
| 21 | 21 | ||
| 22 | 22 | import pytest | |
| 23 | - from httmock import HTTMock, response, urlmatch, with_httmock # noqa | ||
| 23 | + import responses | ||
| 24 | 24 | ||
| 25 | 25 | import gitlab | |
| 26 | 26 | ||
| 27 | 27 | localhost = "http://localhost" | |
| 28 | - username = "username" | ||
| 29 | - user_id = 1 | ||
| 30 | 28 | token = "abc123" | |
| 31 | 29 | ||
| 32 | 30 | ||
| 33 | - @urlmatch(scheme="http", netloc="localhost", path="/api/v4/user", method="get") | ||
| 34 | - def resp_get_user(url, request): | ||
| 35 | - headers = {"content-type": "application/json"} | ||
| 36 | - content = f'{{"id": {user_id:d}, "username": "{username:s}"}}'.encode("utf-8") | ||
| 37 | - return response(200, content, headers, None, 5, request) | ||
| 31 | + @pytest.fixture | ||
| 32 | + def resp_get_user(): | ||
| 33 | + return { | ||
| 34 | + "method": responses.GET, | ||
| 35 | + "url": "http://localhost/api/v4/user", | ||
| 36 | + "json": {"id": 1, "username": "username"}, | ||
| 37 | + "content_type": "application/json", | ||
| 38 | + "status": 200, | ||
| 39 | + } | ||
| 38 | 40 | ||
| 39 | 41 | ||
| 40 | - @urlmatch(scheme="http", netloc="localhost", path="/api/v4/tests", method="get") | ||
| 41 | - def resp_page_1(url, request): | ||
| 42 | + @pytest.fixture | ||
| 43 | + def resp_page_1(): | ||
| 42 | 44 | headers = { | |
| 43 | - "content-type": "application/json", | ||
| 44 | - "X-Page": 1, | ||
| 45 | - "X-Next-Page": 2, | ||
| 46 | - "X-Per-Page": 1, | ||
| 47 | - "X-Total-Pages": 2, | ||
| 48 | - "X-Total": 2, | ||
| 45 | + "X-Page": "1", | ||
| 46 | + "X-Next-Page": "2", | ||
| 47 | + "X-Per-Page": "1", | ||
| 48 | + "X-Total-Pages": "2", | ||
| 49 | + "X-Total": "2", | ||
| 49 | 50 | "Link": ("<http://localhost/api/v4/tests?per_page=1&page=2>;" ' rel="next"'), | |
| 50 | 51 | } | |
| 51 | - content = '[{"a": "b"}]' | ||
| 52 | - return response(200, content, headers, None, 5, request) | ||
| 53 | 52 | ||
| 53 | + return { | ||
| 54 | + "method": responses.GET, | ||
| 55 | + "url": "http://localhost/api/v4/tests", | ||
| 56 | + "json": [{"a": "b"}], | ||
| 57 | + "headers": headers, | ||
| 58 | + "content_type": "application/json", | ||
| 59 | + "status": 200, | ||
| 60 | + "match_querystring": True, | ||
| 61 | + } | ||
| 54 | 62 | ||
| 55 | - @urlmatch( | ||
| 56 | - scheme="http", | ||
| 57 | - netloc="localhost", | ||
| 58 | - path="/api/v4/tests", | ||
| 59 | - method="get", | ||
| 60 | - query=r".*page=2", | ||
| 61 | - ) | ||
| 62 | - def resp_page_2(url, request): | ||
| 63 | + | ||
| 64 | + @pytest.fixture | ||
| 65 | + def resp_page_2(): | ||
| 63 | 66 | headers = { | |
| 64 | - "content-type": "application/json", | ||
| 65 | - "X-Page": 2, | ||
| 66 | - "X-Next-Page": 2, | ||
| 67 | - "X-Per-Page": 1, | ||
| 68 | - "X-Total-Pages": 2, | ||
| 69 | - "X-Total": 2, | ||
| 67 | + "X-Page": "2", | ||
| 68 | + "X-Next-Page": "2", | ||
| 69 | + "X-Per-Page": "1", | ||
| 70 | + "X-Total-Pages": "2", | ||
| 71 | + "X-Total": "2", | ||
| 72 | + } | ||
| 73 | + params = {"per_page": "1", "page": "2"} | ||
| 74 | + | ||
| 75 | + return { | ||
| 76 | + "method": responses.GET, | ||
| 77 | + "url": "http://localhost/api/v4/tests", | ||
| 78 | + "json": [{"c": "d"}], | ||
| 79 | + "headers": headers, | ||
| 80 | + "content_type": "application/json", | ||
| 81 | + "status": 200, | ||
| 82 | + "match": [responses.matchers.query_param_matcher(params)], | ||
| 83 | + "match_querystring": False, | ||
| 70 | 84 | } | |
| 71 | - content = '[{"c": "d"}]' | ||
| 72 | - return response(200, content, headers, None, 5, request) | ||
| 73 | 85 | ||
| 74 | 86 | ||
| 75 | - def test_gitlab_build_list(gl): | ||
| 76 | - with HTTMock(resp_page_1): | ||
| 77 | - obj = gl.http_list("/tests", as_list=False) | ||
| 87 | + @responses.activate | ||
| 88 | + def test_gitlab_build_list(gl, resp_page_1, resp_page_2): | ||
| 89 | + responses.add(**resp_page_1) | ||
| 90 | + obj = gl.http_list("/tests", as_list=False) | ||
| 78 | 91 | assert len(obj) == 2 | |
| 79 | 92 | assert obj._next_url == "http://localhost/api/v4/tests?per_page=1&page=2" | |
| 80 | 93 | assert obj.current_page == 1 | |
@@ -84,15 +97,17 @@ def test_gitlab_build_list(gl): | |||
| 84 | 97 | assert obj.total_pages == 2 | |
| 85 | 98 | assert obj.total == 2 | |
| 86 | 99 | ||
| 87 | - with HTTMock(resp_page_2): | ||
| 88 | - test_list = list(obj) | ||
| 100 | + responses.add(**resp_page_2) | ||
| 101 | + test_list = list(obj) | ||
| 89 | 102 | assert len(test_list) == 2 | |
| 90 | 103 | assert test_list[0]["a"] == "b" | |
| 91 | 104 | assert test_list[1]["c"] == "d" | |
| 92 | 105 | ||
| 93 | 106 | ||
| 94 | - @with_httmock(resp_page_1, resp_page_2) | ||
| 95 | - def test_gitlab_all_omitted_when_as_list(gl): | ||
| 107 | + @responses.activate | ||
| 108 | + def test_gitlab_all_omitted_when_as_list(gl, resp_page_1, resp_page_2): | ||
| 109 | + responses.add(**resp_page_1) | ||
| 110 | + responses.add(**resp_page_2) | ||
| 96 | 111 | result = gl.http_list("/tests", as_list=False, all=True) | |
| 97 | 112 | assert isinstance(result, gitlab.GitlabList) | |
| 98 | 113 | ||
@@ -119,11 +134,12 @@ def test_gitlab_pickability(gl): | |||
| 119 | 134 | assert unpickled._objects == original_gl_objects | |
| 120 | 135 | ||
| 121 | 136 | ||
| 122 | - @with_httmock(resp_get_user) | ||
| 123 | - def test_gitlab_token_auth(gl, callback=None): | ||
| 137 | + @responses.activate | ||
| 138 | + def test_gitlab_token_auth(gl, resp_get_user): | ||
| 139 | + responses.add(**resp_get_user) | ||
| 124 | 140 | gl.auth() | |
| 125 | - assert gl.user.username == username | ||
| 126 | - assert gl.user.id == user_id | ||
| 141 | + assert gl.user.username == "username" | ||
| 142 | + assert gl.user.id == 1 | ||
| 127 | 143 | assert isinstance(gl.user, gitlab.v4.objects.CurrentUser) | |
| 128 | 144 | ||
| 129 | 145 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments