Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iscai-msft committed Aug 30, 2021
1 parent 9a00a7f commit d7e39c7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ async def test_iter_bytes(client):
assert response.is_closed
assert raw == b"Hello, world!"

@pytest.mark.asyncio
async def test_iter_text(client):
request = HttpRequest("GET", "/basic/string")
# @pytest.mark.asyncio
# async def test_iter_text(client):
# request = HttpRequest("GET", "/basic/string")

async with client.send_request(request, stream=True) as response:
content = ""
async for part in response.iter_text():
content += part
assert content == "Hello, world!"
# async with client.send_request(request, stream=True) as response:
# content = ""
# async for part in response.iter_text():
# content += part
# assert content == "Hello, world!"

@pytest.mark.asyncio
async def test_iter_lines(client):
request = HttpRequest("GET", "/basic/lines")
# @pytest.mark.asyncio
# async def test_iter_lines(client):
# request = HttpRequest("GET", "/basic/lines")

async with client.send_request(request, stream=True) as response:
content = []
async for line in response.iter_lines():
content.append(line)
assert content == ["Hello,\n", "world!"]
# async with client.send_request(request, stream=True) as response:
# content = []
# async for line in response.iter_lines():
# content.append(line)
# assert content == ["Hello,\n", "world!"]


@pytest.mark.asyncio
Expand Down Expand Up @@ -161,10 +161,10 @@ async def test_iter_read_back_and_forth(client):
# the reason why the code flow is like this, is because the 'iter_x' functions don't
# actually read the contents into the response, the output them. Once they're yielded,
# the stream is closed, so you have to catch the output when you iterate through it
request = HttpRequest("GET", "/basic/lines")
request = HttpRequest("GET", "/basic/string")

async with client.send_request(request, stream=True) as response:
async for line in response.iter_lines():
async for line in response.iter_bytes():
assert line
with pytest.raises(ResponseNotReadError):
response.text()
Expand All @@ -175,16 +175,16 @@ async def test_iter_read_back_and_forth(client):

@pytest.mark.asyncio
async def test_stream_with_return_pipeline_response(client):
request = HttpRequest("GET", "/basic/lines")
request = HttpRequest("GET", "/basic/string")
pipeline_response = await client.send_request(request, stream=True, _return_pipeline_response=True)
assert hasattr(pipeline_response, "http_request")
assert hasattr(pipeline_response.http_request, "content")
assert hasattr(pipeline_response, "http_response")
assert hasattr(pipeline_response, "context")
parts = []
async for line in pipeline_response.http_response.iter_lines():
async for line in pipeline_response.http_response.iter_bytes():
parts.append(line)
assert parts == ['Hello,\n', 'world!']
assert parts == [b'Hello, world!']
await client.close()

@pytest.mark.asyncio
Expand Down
46 changes: 23 additions & 23 deletions sdk/core/azure-core/tests/test_rest_stream_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,23 +79,23 @@ def test_iter_bytes(client):
assert response.is_stream_consumed
assert raw == b"Hello, world!"

def test_iter_text(client):
request = HttpRequest("GET", "/basic/string")
# def test_iter_text(client):
# request = HttpRequest("GET", "/basic/string")

with client.send_request(request, stream=True) as response:
content = ""
for part in response.iter_text():
content += part
assert content == "Hello, world!"
# with client.send_request(request, stream=True) as response:
# content = ""
# for part in response.iter_text():
# content += part
# assert content == "Hello, world!"

def test_iter_lines(client):
request = HttpRequest("GET", "/basic/lines")
# def test_iter_lines(client):
# request = HttpRequest("GET", "/basic/lines")

with client.send_request(request, stream=True) as response:
content = []
for line in response.iter_lines():
content.append(line)
assert content == ["Hello,\n", "world!"]
# with client.send_request(request, stream=True) as response:
# content = []
# for line in response.iter_lines():
# content.append(line)
# assert content == ["Hello,\n", "world!"]

def test_sync_streaming_response(client):
request = HttpRequest("GET", "/streams/basic")
Expand Down Expand Up @@ -175,16 +175,16 @@ def test_decompress_compressed_header(client):
url = "https://{}.blob.core.windows.net/tests/test_with_header.tar.gz".format(account_name)
request = HttpRequest("GET", url)
response = client.send_request(request, stream=True)
iter = response.iter_text()
data = "".join(list(iter))
assert data == "test"
iter = response.iter_bytes()
data = b"".join(list(iter))
assert data == b"test"

def test_iter_read(client):
# thanks to McCoy Patiño for this test!
request = HttpRequest("GET", "/basic/lines")
request = HttpRequest("GET", "/basic/string")
response = client.send_request(request, stream=True)
response.read()
iterator = response.iter_lines()
iterator = response.iter_bytes()
for line in iterator:
assert line
assert response.text()
Expand All @@ -196,9 +196,9 @@ def test_iter_read_back_and_forth(client):
# the reason why the code flow is like this, is because the 'iter_x' functions don't
# actually read the contents into the response, the output them. Once they're yielded,
# the stream is closed, so you have to catch the output when you iterate through it
request = HttpRequest("GET", "/basic/lines")
request = HttpRequest("GET", "/basic/string")
response = client.send_request(request, stream=True)
iterator = response.iter_lines()
iterator = response.iter_bytes()
for line in iterator:
assert line
with pytest.raises(ResponseNotReadError):
Expand All @@ -209,12 +209,12 @@ def test_iter_read_back_and_forth(client):
response.text()

def test_stream_with_return_pipeline_response(client):
request = HttpRequest("GET", "/basic/lines")
request = HttpRequest("GET", "/basic/string")
pipeline_response = client.send_request(request, stream=True, _return_pipeline_response=True)
assert hasattr(pipeline_response, "http_request")
assert hasattr(pipeline_response, "http_response")
assert hasattr(pipeline_response, "context")
assert list(pipeline_response.http_response.iter_lines()) == ['Hello,\n', 'world!']
assert list(pipeline_response.http_response.iter_bytes()) == [b'Hello, world!']

def test_error_reading(client):
request = HttpRequest("GET", "/errors/403")
Expand Down

0 comments on commit d7e39c7

Please sign in to comment.