Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
Test json_or_text (#13)
Browse files Browse the repository at this point in the history
* Add custom coverage test

* identified and added an aditional mark

* Revert "identified and added an aditional mark"

This reverts commit 17c4a75.

* Revert "Add custom coverage test"

This reverts commit c8f9434.

* Added test

* Add coverages to report

* solved pr review
  • Loading branch information
YasinKaryagdi authored Jun 14, 2024
1 parent ad31e9c commit 07fbb8f
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ _get and _aget
[commit](https://github.com/tthijm/sep/commit/017ddcd6bcf0fb838a9e5b3fc427ab7a746ddce6)

![](./assets/_get_custom.png)

json_or_text

[commit](https://github.com/tthijm/sep/commit/c8f9434b7caa2fffe7a4b5d92a5af532f82bfb5e) +
[commit](https://github.com/tthijm/sep/commit/17c4a758b69a2f9fed324376e5388e50df7d8089)
Note: the second commmit is the final instrumentation

![](./assets/json_or_text_custom.png)

## Coverage improvement

Expand Down Expand Up @@ -118,6 +126,17 @@ test_get_find

The coverage improvement is 88%.
The coverage has been improved, because the previous test_get_find only considered the sync and asyn get with single values. I extended the test so that it tests these functions with multiple attributes as well, this way it currently reaches more branches and therefore the coverage improved. Additionally the previous test did not consider the case where we loop through the array and we don't find the corresponding item we are looking for, we added this case by giving an empty array as argument, which causes the code to not enter the for loop and thus reach more branches.

test_json_or_text

[commit](https://github.com/tthijm/sep/commit/590afe1584d3e8f2dcc0a806202ddd4f2c4d7a69)

![](./assets/json_or_text_before.png)

![](./assets/json_or_text_after.png)

The coverage improvement is 71%.
The coverage has been improved, because the previously there was no test for json_or_text. I now added a test that considers two cases: one where the data is a json and one where the data is plain text. This way we reach two additional branches and thus the coverage improves.

### Overall

Expand Down
Binary file added assets/json_or_text_after.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/json_or_text_before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/json_or_text_custom.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest
from discord import http


class CustomResponse:
def __init__(self, text_data, content_type):
self._text = text_data
self.headers = {'content-type': content_type}

async def text(self, encoding='utf-8'):
return self._text


@pytest.mark.asyncio
async def test_json_or_text():
# Testing with json data
response = CustomResponse('{"key": "value"}', 'application/json')
result = await http.json_or_text(response)
assert isinstance(result, dict)
assert result == {"key": "value"}

# Testing with plain text data
response = CustomResponse('Hello world!', 'text/plain')
result = await http.json_or_text(response)
assert isinstance(result, str)
assert result == 'Hello world!'

0 comments on commit 07fbb8f

Please sign in to comment.