|
1 | 1 | import pytest
|
2 | 2 | from tests import MockResponse
|
| 3 | +from unittest.mock import MagicMock |
3 | 4 | from deluge_web_client import DelugeWebClientError
|
4 | 5 |
|
5 | 6 |
|
@@ -106,3 +107,48 @@ def test_add_label_raises_error(client_mock):
|
106 | 107 |
|
107 | 108 | with pytest.raises(DelugeWebClientError, match="Error adding label:\nRandom error"):
|
108 | 109 | client.add_label("movies")
|
| 110 | + |
| 111 | + |
| 112 | +def test_apply_label(client_mock): |
| 113 | + client, _ = client_mock |
| 114 | + |
| 115 | + # mock the add_label and set_label methods using side_effect to simulate real behavior |
| 116 | + client.add_label = MagicMock( |
| 117 | + side_effect=( |
| 118 | + MockResponse( |
| 119 | + {"result": None, "error": None, "id": 0}, |
| 120 | + ok=True, |
| 121 | + status_code=200, |
| 122 | + reason="test", |
| 123 | + ), |
| 124 | + ) |
| 125 | + ) |
| 126 | + client.set_label = MagicMock( |
| 127 | + side_effect=( |
| 128 | + MockResponse( |
| 129 | + {"result": None, "error": None, "id": 1}, |
| 130 | + ok=True, |
| 131 | + status_code=200, |
| 132 | + reason="test", |
| 133 | + ), |
| 134 | + ) |
| 135 | + ) |
| 136 | + |
| 137 | + info_hash = "mocked_info_hash" |
| 138 | + label = "movies" |
| 139 | + timeout = 30 |
| 140 | + |
| 141 | + # call the helper method |
| 142 | + response_add_label, response_set_label = client._apply_label( |
| 143 | + info_hash, label, timeout |
| 144 | + ) |
| 145 | + |
| 146 | + # assert that add_label and set_label were called with correct arguments |
| 147 | + client.add_label.assert_called_once_with(label, timeout) |
| 148 | + client.set_label.assert_called_once_with(info_hash, label, timeout) |
| 149 | + |
| 150 | + # assert that the responses are as expected |
| 151 | + assert response_add_label.json().get("result") is None |
| 152 | + assert response_add_label.json().get("id") == 0 |
| 153 | + assert response_set_label.json().get("result") is None |
| 154 | + assert response_set_label.json().get("id") == 1 |
0 commit comments