|
| 1 | +""" |
| 2 | +GitLab API: |
| 3 | +https://docs.gitlab.com/ce/api/topics.html |
| 4 | +""" |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | +from gitlab.v4.objects import Topic |
| 9 | + |
| 10 | +name = "GitLab" |
| 11 | +new_name = "gitlab-test" |
| 12 | +topic_content = { |
| 13 | + "id": 1, |
| 14 | + "name": name, |
| 15 | + "description": "GitLab is an open source end-to-end software development platform.", |
| 16 | + "total_projects_count": 1000, |
| 17 | + "avatar_url": "http://www.gravatar.com/avatar/a0d477b3ea21970ce6ffcbb817b0b435?s=80&d=identicon", |
| 18 | +} |
| 19 | +topics_url = "http://localhost/api/v4/topics" |
| 20 | +topic_url = f"{topics_url}/1" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def resp_list_topics(): |
| 25 | + with responses.RequestsMock() as rsps: |
| 26 | + rsps.add( |
| 27 | + method=responses.GET, |
| 28 | + url=topics_url, |
| 29 | + json=[topic_content], |
| 30 | + content_type="application/json", |
| 31 | + status=200, |
| 32 | + ) |
| 33 | + yield rsps |
| 34 | + |
| 35 | + |
| 36 | +@pytest.fixture |
| 37 | +def resp_get_topic(): |
| 38 | + with responses.RequestsMock() as rsps: |
| 39 | + rsps.add( |
| 40 | + method=responses.GET, |
| 41 | + url=topic_url, |
| 42 | + json=topic_content, |
| 43 | + content_type="application/json", |
| 44 | + status=200, |
| 45 | + ) |
| 46 | + yield rsps |
| 47 | + |
| 48 | + |
| 49 | +@pytest.fixture |
| 50 | +def resp_create_topic(): |
| 51 | + with responses.RequestsMock() as rsps: |
| 52 | + rsps.add( |
| 53 | + method=responses.POST, |
| 54 | + url=topics_url, |
| 55 | + json=topic_content, |
| 56 | + content_type="application/json", |
| 57 | + status=200, |
| 58 | + ) |
| 59 | + yield rsps |
| 60 | + |
| 61 | + |
| 62 | +@pytest.fixture |
| 63 | +def resp_update_topic(): |
| 64 | + updated_content = dict(topic_content) |
| 65 | + updated_content["name"] = new_name |
| 66 | + |
| 67 | + with responses.RequestsMock() as rsps: |
| 68 | + rsps.add( |
| 69 | + method=responses.PUT, |
| 70 | + url=topic_url, |
| 71 | + json=updated_content, |
| 72 | + content_type="application/json", |
| 73 | + status=200, |
| 74 | + ) |
| 75 | + yield rsps |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture |
| 79 | +def resp_delete_topic(no_content): |
| 80 | + with responses.RequestsMock() as rsps: |
| 81 | + rsps.add( |
| 82 | + method=responses.DELETE, |
| 83 | + url=topic_url, |
| 84 | + json=no_content, |
| 85 | + content_type="application/json", |
| 86 | + status=204, |
| 87 | + ) |
| 88 | + yield rsps |
| 89 | + |
| 90 | + |
| 91 | +def test_list_topics(gl, resp_list_topics): |
| 92 | + topics = gl.topics.list() |
| 93 | + assert isinstance(topics, list) |
| 94 | + assert isinstance(topics[0], Topic) |
| 95 | + assert topics[0].name == name |
| 96 | + |
| 97 | + |
| 98 | +def test_get_topic(gl, resp_get_topic): |
| 99 | + topic = gl.topics.get(1) |
| 100 | + assert isinstance(topic, Topic) |
| 101 | + assert topic.name == name |
| 102 | + |
| 103 | + |
| 104 | +def test_create_topic(gl, resp_create_topic): |
| 105 | + topic = gl.topics.create({"name": name}) |
| 106 | + assert isinstance(topic, Topic) |
| 107 | + assert topic.name == name |
| 108 | + |
| 109 | + |
| 110 | +def test_update_topic(gl, resp_update_topic): |
| 111 | + topic = gl.topics.get(1, lazy=True) |
| 112 | + topic.name = new_name |
| 113 | + topic.save() |
| 114 | + assert topic.name == new_name |
| 115 | + |
| 116 | + |
| 117 | +def test_delete_topic(gl, resp_delete_topic): |
| 118 | + topic = gl.topics.get(1, lazy=True) |
| 119 | + topic.delete() |
0 commit comments