|
1 | 1 | import pytest |
| 2 | +import json |
2 | 3 | from io import BytesIO |
| 4 | +from unittest.mock import Mock |
3 | 5 | from urllib.request import Request |
4 | 6 | from kiota_abstractions.request_information import RequestInformation |
| 7 | +from kiota_abstractions.serialization.serialization_writer import SerializationWriter |
5 | 8 | from kiota_abstractions.method import Method |
6 | 9 | from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders |
| 10 | +from kiota_serialization_json.json_serialization_writer_factory import JsonSerializationWriterFactory |
7 | 11 | from msgraph_core.requests.batch_request_item import BatchRequestItem |
8 | 12 |
|
9 | 13 | base_url = "https://graph.microsoft.com/v1.0/me" |
10 | 14 |
|
11 | 15 |
|
12 | 16 | @pytest.fixture |
13 | 17 | def request_info(): |
14 | | - request_info = RequestInformation() |
| 18 | + request_info = RequestInformation() |
15 | 19 | request_info.http_method = "GET" |
16 | | - request_info.url = "f{base_url}/me" |
| 20 | + request_info.url = base_url |
17 | 21 | request_info.headers = RequestHeaders() |
18 | | - request_info.content = BytesIO(b'{"key": "value"}') |
| 22 | + request_info.headers.add("Content-Type", "application/json") |
| 23 | + request_info.content = b'{"key": "value"}' |
19 | 24 | return request_info |
20 | 25 |
|
21 | 26 |
|
22 | 27 | @pytest.fixture |
23 | 28 | def batch_request_item(request_info): |
24 | | - return BatchRequestItem(request_information=request_info) |
| 29 | + return BatchRequestItem(request_information=request_info, id="123") |
25 | 30 |
|
26 | 31 |
|
27 | 32 | def test_initialization(batch_request_item, request_info): |
| 33 | + assert batch_request_item.id == "123" |
28 | 34 | assert batch_request_item.method == "GET" |
29 | | - assert batch_request_item.url == "f{base_url}/me" |
30 | | - assert batch_request_item.body.read() == b'{"key": "value"}' |
| 35 | + assert batch_request_item.url == base_url |
| 36 | + assert batch_request_item.headers == {"content-type": "application/json"} |
| 37 | + assert batch_request_item.body == b'{"key": "value"}' |
31 | 38 |
|
32 | 39 |
|
33 | 40 | def test_create_with_urllib_request(): |
@@ -123,3 +130,15 @@ def test_batch_request_item_method_enum(): |
123 | 130 | def test_depends_on_property(batch_request_item): |
124 | 131 | batch_request_item.set_depends_on(["request1", "request2"]) |
125 | 132 | assert batch_request_item.depends_on == ["request1", "request2"] |
| 133 | + |
| 134 | + |
| 135 | +def test_serialize_json(batch_request_item): |
| 136 | + writer = JsonSerializationWriterFactory().get_serialization_writer('application/json') |
| 137 | + batch_request_item.serialize(writer) |
| 138 | + content = json.loads(writer.get_serialized_content()) |
| 139 | + assert content["id"] == "123" |
| 140 | + assert content["method"] == "GET" |
| 141 | + assert content["url"] == base_url |
| 142 | + assert content["headers"] == {"content-type": "application/json"} |
| 143 | + assert content["body"] == {"key": "value"} |
| 144 | + |
0 commit comments