|
| 1 | +""" |
| 2 | +Unit tests for Google Forms MCP tools |
| 3 | +
|
| 4 | +Tests the batch_update_form tool with mocked API responses |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +from unittest.mock import Mock |
| 9 | +import sys |
| 10 | +import os |
| 11 | + |
| 12 | +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../.."))) |
| 13 | + |
| 14 | +# Import the internal implementation function (not the decorated one) |
| 15 | +from gforms.forms_tools import _batch_update_form_impl |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.asyncio |
| 19 | +async def test_batch_update_form_multiple_requests(): |
| 20 | + """Test batch update with multiple requests returns formatted results""" |
| 21 | + mock_service = Mock() |
| 22 | + mock_response = { |
| 23 | + "replies": [ |
| 24 | + {"createItem": {"itemId": "item001", "questionId": ["q001"]}}, |
| 25 | + {"createItem": {"itemId": "item002", "questionId": ["q002"]}}, |
| 26 | + ], |
| 27 | + "writeControl": {"requiredRevisionId": "rev123"}, |
| 28 | + } |
| 29 | + |
| 30 | + mock_service.forms().batchUpdate().execute.return_value = mock_response |
| 31 | + |
| 32 | + requests = [ |
| 33 | + { |
| 34 | + "createItem": { |
| 35 | + "item": { |
| 36 | + "title": "What is your name?", |
| 37 | + "questionItem": { |
| 38 | + "question": {"textQuestion": {"paragraph": False}} |
| 39 | + }, |
| 40 | + }, |
| 41 | + "location": {"index": 0}, |
| 42 | + } |
| 43 | + }, |
| 44 | + { |
| 45 | + "createItem": { |
| 46 | + "item": { |
| 47 | + "title": "What is your email?", |
| 48 | + "questionItem": { |
| 49 | + "question": {"textQuestion": {"paragraph": False}} |
| 50 | + }, |
| 51 | + }, |
| 52 | + "location": {"index": 1}, |
| 53 | + } |
| 54 | + }, |
| 55 | + ] |
| 56 | + |
| 57 | + result = await _batch_update_form_impl( |
| 58 | + service=mock_service, |
| 59 | + form_id="test_form_123", |
| 60 | + requests=requests, |
| 61 | + ) |
| 62 | + |
| 63 | + assert "Batch Update Completed" in result |
| 64 | + assert "test_form_123" in result |
| 65 | + assert "Requests Applied: 2" in result |
| 66 | + assert "Replies Received: 2" in result |
| 67 | + assert "item001" in result |
| 68 | + assert "item002" in result |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.asyncio |
| 72 | +async def test_batch_update_form_single_request(): |
| 73 | + """Test batch update with a single request""" |
| 74 | + mock_service = Mock() |
| 75 | + mock_response = { |
| 76 | + "replies": [ |
| 77 | + {"createItem": {"itemId": "item001", "questionId": ["q001"]}}, |
| 78 | + ], |
| 79 | + } |
| 80 | + |
| 81 | + mock_service.forms().batchUpdate().execute.return_value = mock_response |
| 82 | + |
| 83 | + requests = [ |
| 84 | + { |
| 85 | + "createItem": { |
| 86 | + "item": { |
| 87 | + "title": "Favourite colour?", |
| 88 | + "questionItem": { |
| 89 | + "question": { |
| 90 | + "choiceQuestion": { |
| 91 | + "type": "RADIO", |
| 92 | + "options": [ |
| 93 | + {"value": "Red"}, |
| 94 | + {"value": "Blue"}, |
| 95 | + ], |
| 96 | + } |
| 97 | + } |
| 98 | + }, |
| 99 | + }, |
| 100 | + "location": {"index": 0}, |
| 101 | + } |
| 102 | + }, |
| 103 | + ] |
| 104 | + |
| 105 | + result = await _batch_update_form_impl( |
| 106 | + service=mock_service, |
| 107 | + form_id="single_form_456", |
| 108 | + requests=requests, |
| 109 | + ) |
| 110 | + |
| 111 | + assert "single_form_456" in result |
| 112 | + assert "Requests Applied: 1" in result |
| 113 | + assert "Replies Received: 1" in result |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.asyncio |
| 117 | +async def test_batch_update_form_empty_replies(): |
| 118 | + """Test batch update when API returns no replies""" |
| 119 | + mock_service = Mock() |
| 120 | + mock_response = { |
| 121 | + "replies": [], |
| 122 | + } |
| 123 | + |
| 124 | + mock_service.forms().batchUpdate().execute.return_value = mock_response |
| 125 | + |
| 126 | + requests = [ |
| 127 | + { |
| 128 | + "updateFormInfo": { |
| 129 | + "info": {"description": "Updated description"}, |
| 130 | + "updateMask": "description", |
| 131 | + } |
| 132 | + }, |
| 133 | + ] |
| 134 | + |
| 135 | + result = await _batch_update_form_impl( |
| 136 | + service=mock_service, |
| 137 | + form_id="info_form_789", |
| 138 | + requests=requests, |
| 139 | + ) |
| 140 | + |
| 141 | + assert "info_form_789" in result |
| 142 | + assert "Requests Applied: 1" in result |
| 143 | + assert "Replies Received: 0" in result |
| 144 | + |
| 145 | + |
| 146 | +@pytest.mark.asyncio |
| 147 | +async def test_batch_update_form_no_replies_key(): |
| 148 | + """Test batch update when API response lacks replies key""" |
| 149 | + mock_service = Mock() |
| 150 | + mock_response = {} |
| 151 | + |
| 152 | + mock_service.forms().batchUpdate().execute.return_value = mock_response |
| 153 | + |
| 154 | + requests = [ |
| 155 | + { |
| 156 | + "updateSettings": { |
| 157 | + "settings": {"quizSettings": {"isQuiz": True}}, |
| 158 | + "updateMask": "quizSettings.isQuiz", |
| 159 | + } |
| 160 | + }, |
| 161 | + ] |
| 162 | + |
| 163 | + result = await _batch_update_form_impl( |
| 164 | + service=mock_service, |
| 165 | + form_id="quiz_form_000", |
| 166 | + requests=requests, |
| 167 | + ) |
| 168 | + |
| 169 | + assert "quiz_form_000" in result |
| 170 | + assert "Requests Applied: 1" in result |
| 171 | + assert "Replies Received: 0" in result |
| 172 | + |
| 173 | + |
| 174 | +@pytest.mark.asyncio |
| 175 | +async def test_batch_update_form_url_in_response(): |
| 176 | + """Test that the edit URL is included in the response""" |
| 177 | + mock_service = Mock() |
| 178 | + mock_response = { |
| 179 | + "replies": [{}], |
| 180 | + } |
| 181 | + |
| 182 | + mock_service.forms().batchUpdate().execute.return_value = mock_response |
| 183 | + |
| 184 | + requests = [ |
| 185 | + {"updateFormInfo": {"info": {"title": "New Title"}, "updateMask": "title"}} |
| 186 | + ] |
| 187 | + |
| 188 | + result = await _batch_update_form_impl( |
| 189 | + service=mock_service, |
| 190 | + form_id="url_form_abc", |
| 191 | + requests=requests, |
| 192 | + ) |
| 193 | + |
| 194 | + assert "https://docs.google.com/forms/d/url_form_abc/edit" in result |
| 195 | + |
| 196 | + |
| 197 | +@pytest.mark.asyncio |
| 198 | +async def test_batch_update_form_mixed_reply_types(): |
| 199 | + """Test batch update with createItem replies containing different fields""" |
| 200 | + mock_service = Mock() |
| 201 | + mock_response = { |
| 202 | + "replies": [ |
| 203 | + {"createItem": {"itemId": "item_a", "questionId": ["qa"]}}, |
| 204 | + {}, |
| 205 | + {"createItem": {"itemId": "item_c"}}, |
| 206 | + ], |
| 207 | + } |
| 208 | + |
| 209 | + mock_service.forms().batchUpdate().execute.return_value = mock_response |
| 210 | + |
| 211 | + requests = [ |
| 212 | + {"createItem": {"item": {"title": "Q1"}, "location": {"index": 0}}}, |
| 213 | + { |
| 214 | + "updateFormInfo": { |
| 215 | + "info": {"description": "Desc"}, |
| 216 | + "updateMask": "description", |
| 217 | + } |
| 218 | + }, |
| 219 | + {"createItem": {"item": {"title": "Q2"}, "location": {"index": 1}}}, |
| 220 | + ] |
| 221 | + |
| 222 | + result = await _batch_update_form_impl( |
| 223 | + service=mock_service, |
| 224 | + form_id="mixed_form_xyz", |
| 225 | + requests=requests, |
| 226 | + ) |
| 227 | + |
| 228 | + assert "Requests Applied: 3" in result |
| 229 | + assert "Replies Received: 3" in result |
| 230 | + assert "item_a" in result |
| 231 | + assert "item_c" in result |
0 commit comments