Skip to content

Commit 9e14ea3

Browse files
GeorgeOGGeorge Garber
and
George Garber
authored
fix: sequencing of batches
* fix batch creation * fix batch creation * fix * tests --------- Co-authored-by: George Garber <[email protected]>
1 parent acb30bc commit 9e14ea3

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/msgraph_core/requests/batch_request_content_collection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def __init__(self) -> None:
1616
1717
"""
1818
self.max_requests_per_batch = BatchRequestContent.MAX_REQUESTS
19-
self.batches: list[BatchRequestContent] = []
2019
self.current_batch: BatchRequestContent = BatchRequestContent()
20+
self.batches: list[BatchRequestContent] = [self.current_batch]
2121

2222
def add_batch_request_item(self, request: BatchRequestItem) -> None:
2323
"""
@@ -26,10 +26,10 @@ def add_batch_request_item(self, request: BatchRequestItem) -> None:
2626
request (BatchRequestItem): The request item to add.
2727
"""
2828
if len(self.current_batch.requests) >= self.max_requests_per_batch:
29-
self.batches.append(self.current_batch.finalize())
29+
self.current_batch.finalize()
3030
self.current_batch = BatchRequestContent()
31+
self.batches.append(self.current_batch)
3132
self.current_batch.add_request(request.id, request)
32-
self.batches.append(self.current_batch)
3333

3434
def remove_batch_request_item(self, request_id: str) -> None:
3535
"""
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import pytest
2+
from io import BytesIO
3+
from kiota_abstractions.request_information import RequestInformation
4+
from msgraph_core.requests.batch_request_item import BatchRequestItem
5+
from msgraph_core.requests.batch_request_content_collection import BatchRequestContentCollection
6+
from kiota_abstractions.headers_collection import HeadersCollection as RequestHeaders
7+
8+
9+
@pytest.fixture
10+
def batch_request_content_collection():
11+
return BatchRequestContentCollection()
12+
13+
14+
@pytest.fixture
15+
def request_info():
16+
request_info = RequestInformation()
17+
request_info.http_method = "GET"
18+
request_info.url = "https://graph.microsoft.com/v1.0/me"
19+
request_info.headers = RequestHeaders()
20+
request_info.headers.add("Content-Type", "application/json")
21+
request_info.content = BytesIO(b'{"key": "value"}')
22+
return request_info
23+
24+
25+
@pytest.fixture
26+
def batch_request_item1(request_info):
27+
return BatchRequestItem(request_information=request_info, id="1")
28+
29+
30+
@pytest.fixture
31+
def batch_request_item2(request_info):
32+
return BatchRequestItem(request_information=request_info, id="2")
33+
34+
35+
def test_init_batches(batch_request_content_collection):
36+
assert len(batch_request_content_collection.batches) == 1
37+
assert batch_request_content_collection.current_batch is not None
38+
39+
40+
def test_add_batch_request_item(batch_request_content_collection, batch_request_item1, batch_request_item2):
41+
batch_request_content_collection.add_batch_request_item(batch_request_item1)
42+
batch_request_content_collection.add_batch_request_item(batch_request_item2)
43+
assert len(batch_request_content_collection.batches) == 1
44+
assert len(batch_request_content_collection.current_batch.requests) == 2

0 commit comments

Comments
 (0)