Skip to content

Commit 406549d

Browse files
author
clickingbuttons
authored
Add unittest (#116)
* test harness * add httpretty * add poetry run to Makefile * add empty key * add comments * pass empty api key
1 parent 6b4ee83 commit 406549d

File tree

7 files changed

+115
-3
lines changed

7 files changed

+115
-3
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
- name: Install pypi deps
2828
run: poetry install
2929
- name: Style lint
30-
run: poetry run black --check polygon
30+
run: make style
3131
- name: Static lint
32-
run: poetry run mypy polygon
32+
run: make static
3333
if: always()

.github/workflows/test.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: unittest
2+
on:
3+
push:
4+
tags:
5+
- v*
6+
branches:
7+
- v1
8+
pull_request:
9+
permissions:
10+
contents: read
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
python-version: ['3.7', '3.8', '3.9', '3.10']
18+
name: Lint ${{ matrix.python-version }}
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: Setup Python
22+
uses: actions/setup-python@v3
23+
with:
24+
python-version: ${{ matrix.python-version }}
25+
- name: Setup Poetry
26+
uses: abatilo/[email protected]
27+
- name: Install pypi deps
28+
run: poetry install
29+
- name: Unit tests
30+
run: make test

Makefile

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
.DEFAULT_GOAL := help
2+
TARGET_MAX_CHAR_NUM := 20
3+
4+
GREEN := $(shell tput -Txterm setaf 2)
5+
YELLOW := $(shell tput -Txterm setaf 3)
6+
WHITE := $(shell tput -Txterm setaf 7)
7+
RESET := $(shell tput -Txterm sgr0)
8+
9+
.PHONY: help lint style static test
10+
11+
## Show help
12+
help:
13+
@awk '/^[a-zA-Z\-_0-9]+:/ { \
14+
helpMessage = match(lastLine, /^## (.*)/); \
15+
if (helpMessage) { \
16+
helpCommand = substr($$1, 0, index($$1, ":")-1); \
17+
helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \
18+
printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \
19+
} \
20+
} \
21+
{ lastLine = $$0 }' $(MAKEFILE_LIST)
22+
23+
## Check code style
24+
style:
25+
poetry run black polygon
26+
27+
## Check static types
28+
static:
29+
poetry run mypy polygon
30+
31+
## Check code style and static types
32+
lint: style static
33+
34+
## Run unit tests
35+
test:
36+
poetry run python -m unittest discover -s tests
37+

poetry.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ urllib3 = "^1.26.9"
2828
black = "^22.3.0"
2929
mypy = "^0.942"
3030
types-urllib3 = "^1.26.13"
31+
httpretty = "^1.1.4"
3132

3233
[build-system]
3334
requires = ["poetry-core>=1.0.0"]

tests/mocks.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from polygon import RESTClient
2+
import unittest
3+
import httpretty
4+
5+
mocks = [
6+
(
7+
"/v2/aggs/ticker/AAPL/range/1/day/2005-04-01/2005-04-04",
8+
'{"ticker":"AAPL","queryCount":2,"resultsCount":2,"adjusted":true,"results":[{"v":6.42646396e+08,"vw":1.469,"o":1.5032,"c":1.4604,"h":1.5064,"l":1.4489,"t":1112331600000,"n":82132},{"v":5.78172308e+08,"vw":1.4589,"o":1.4639,"c":1.4675,"h":1.4754,"l":1.4343,"t":1112587200000,"n":65543}],"status":"OK","request_id":"12afda77aab3b1936c5fb6ef4241ae42","count":2}'
9+
)
10+
]
11+
12+
class BaseTest(unittest.TestCase):
13+
setup = False
14+
def setUp(self):
15+
if self.setup:
16+
return
17+
httpretty.enable(verbose=True, allow_net_connect=False)
18+
c = RESTClient("")
19+
for m in mocks:
20+
httpretty.register_uri(httpretty.GET, c.BASE + m[0], m[1])
21+
self.setup = True
22+

tests/test_aggs.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from polygon import RESTClient
2+
from polygon.rest.models import Agg
3+
from mocks import BaseTest
4+
5+
class AggsTest(BaseTest):
6+
def test_get_aggs(self):
7+
c = RESTClient("")
8+
aggs = c.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04")
9+
expected = [Agg(open=1.5032, high=1.5064, low=1.4489, close=1.4604, volume=642646396.0, vwap=1.469, timestamp=1112331600000, transactions=82132), Agg(open=1.4639, high=1.4754, low=1.4343, close=1.4675, volume=578172308.0, vwap=1.4589, timestamp=1112587200000, transactions=65543)]
10+
self.assertEqual(aggs, expected)
11+

0 commit comments

Comments
 (0)