Skip to content

Commit

Permalink
Merge pull request #16 from jiak94/add_test
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
jiak94 authored May 16, 2024
2 parents 81db42d + 62d4aec commit c28f7c5
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ if your are using poetry

:white_check_mark: Place Multileg Order

:white_square_button: Place Combo Order
:white_check_mark: Place Combo Order

:white_square_button: Place OTO Order

Expand Down
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ strenum = "^0.4.15"
websockets = "^12.0"

[tool.poetry.group.dev.dependencies]
aioresponses = "^0.7.6"
m2r2 = "^0.3.3.post2"
pre-commit = "^3.6.0"
pytest = "^7.4.3"
pytest-asyncio = "^0.23.2"
pytest-cov = "^4.1.0"
pytest-mock = "^3.12.0"
sphinx = "^7.2.6"
sphinx-rtd-theme = "^2.0.0"
sphinx-autobuild = "^2021.3.14"
sphinx-rtd-theme = "^2.0.0"
sphinxcontrib-apidoc = "^0.4.0"
m2r2 = "^0.3.3.post2"

[tool.poetry-dynamic-versioning]
enable = true
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import pytest

from asynctradier.tradier import TradierClient
from asynctradier.utils.webutils import WebUtil


@pytest.fixture
def tradier_client():
return TradierClient("account_id", "access_token", sandbox=True)


@pytest.fixture
def webutil():
base_url = "http://api.example.com"
token = "your_token"
return WebUtil(base_url, token)
108 changes: 108 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import pytest
from aioresponses import aioresponses

from asynctradier.exceptions import BadRequestException
from asynctradier.utils.common import (
build_option_symbol,
is_valid_datetime,
Expand Down Expand Up @@ -49,3 +53,107 @@ def test_is_valid_datetime():
assert is_valid_datetime(d) is True
d = "2021-01-15T12:00"
assert is_valid_datetime(d) is False


@pytest.mark.asyncio
async def test_get_success(webutil):
path = "/test"
response_data = {"key": "value"}
url = webutil.base_url + path

with aioresponses() as m:
m.get(url, payload=response_data)

response = await webutil.get(path)
assert response == response_data


@pytest.mark.asyncio
async def test_post_success(webutil):
path = "/test"
data = {"key": "value"}
response_data = {"success": True}
url = webutil.base_url + path

with aioresponses() as m:
m.post(url, payload=response_data)

response = await webutil.post(path, data=data)
assert response == response_data


@pytest.mark.asyncio
async def test_delete_success(webutil):
path = "/test"
response_data = {"success": True}
url = webutil.base_url + path

with aioresponses() as m:
m.delete(url, payload=response_data)

response = await webutil.delete(path)
assert response == response_data


@pytest.mark.asyncio
async def test_put_success(webutil):
path = "/test"
data = {"key": "value"}
response_data = {"success": True}
url = webutil.base_url + path

with aioresponses() as m:
m.put(url, payload=response_data)

response = await webutil.put(path, data=data)
assert response == response_data


@pytest.mark.asyncio
async def test_get_bad_request_exception(webutil):
path = "/test"
url = webutil.base_url + path

with aioresponses() as m:
m.get(url, status=400, payload={"errors": {"error": "Bad Request"}})

with pytest.raises(BadRequestException):
await webutil.get(path)


@pytest.mark.asyncio
async def test_post_bad_request_exception(webutil):
path = "/test"
data = {"key": "value"}
url = webutil.base_url + path

with aioresponses() as m:
m.post(url, status=400, payload={"errors": {"error": "Bad Request"}})

with pytest.raises(BadRequestException):
await webutil.post(path, data=data)


@pytest.mark.asyncio
async def test_delete_bad_request_exception(webutil):
path = "/test"
url = webutil.base_url + path

with aioresponses() as m:
m.delete(url, status=400, payload={"errors": {"error": "Bad Request"}})

with pytest.raises(BadRequestException):
await webutil.delete(path)


@pytest.mark.asyncio
async def test_put_bad_request_exception(webutil):
path = "/test"
data = {"key": "value"}
url = webutil.base_url + path

with aioresponses() as m:
m.put(url, status=400, payload={"errors": {"error": "Bad Request"}})

with pytest.raises(BadRequestException):
await webutil.put(path, data=data)

0 comments on commit c28f7c5

Please sign in to comment.