From 6ace3ada73b528dbc3b3da8ac9107c65578292a5 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 25 Apr 2022 15:28:44 -0400 Subject: [PATCH 1/6] test harness --- Makefile | 31 +++++++++++++++++++++++++++++++ tests/test_aggs.py | 19 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 Makefile create mode 100644 tests/test_aggs.py diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..5190136b --- /dev/null +++ b/Makefile @@ -0,0 +1,31 @@ +.DEFAULT_GOAL := help +TARGET_MAX_CHAR_NUM := 20 + +GREEN := $(shell tput -Txterm setaf 2) +YELLOW := $(shell tput -Txterm setaf 3) +WHITE := $(shell tput -Txterm setaf 7) +RESET := $(shell tput -Txterm sgr0) + +.PHONY: help lint test + +## Show help +help: + @awk '/^[a-zA-Z\-_0-9]+:/ { \ + helpMessage = match(lastLine, /^## (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")-1); \ + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ + printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET} ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) + +.PHONY: lint +lint: + poetry run black polygon + poetry run mypy polygon + +.PHONY: test +test: + python -m unittest discover -s tests + diff --git a/tests/test_aggs.py b/tests/test_aggs.py new file mode 100644 index 00000000..6166eace --- /dev/null +++ b/tests/test_aggs.py @@ -0,0 +1,19 @@ +import unittest + +class TestStringMethods(unittest.TestCase): + def test_upper(self): + self.assertEqual('foo'.upper(), 'FOO') + + def test_isupper(self): + self.assertTrue('FOO'.isupper()) + self.assertFalse('Foo'.isupper()) + + def test_split(self): + s = 'hello world' + self.assertEqual(s.split(), ['hello', 'world']) + # check that s.split fails when the separator is not a string + with self.assertRaises(TypeError): + s.split(2) + +if __name__ == '__main__': + unittest.main() From 80f8bd09857e8c24645fe2ec4ffabe10b5f417d2 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 25 Apr 2022 16:32:34 -0400 Subject: [PATCH 2/6] add httpretty --- .github/workflows/lint.yml | 4 ++-- .github/workflows/test.yml | 30 ++++++++++++++++++++++++++++++ Makefile | 8 +++++--- poetry.lock | 13 ++++++++++++- pyproject.toml | 1 + tests/mocks.py | 22 ++++++++++++++++++++++ tests/test_aggs.py | 26 +++++++++----------------- 7 files changed, 81 insertions(+), 23 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 tests/mocks.py diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index a6c5edfc..c9fc2fc4 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -27,7 +27,7 @@ jobs: - name: Install pypi deps run: poetry install - name: Style lint - run: poetry run black --check polygon + run: make style - name: Static lint - run: poetry run mypy polygon + run: make static if: always() diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 00000000..beebd438 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,30 @@ +name: unittest +on: + push: + tags: + - v* + branches: + - v1 + pull_request: +permissions: + contents: read +jobs: + lint: + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + python-version: ['3.7', '3.8', '3.9', '3.10'] + name: Lint ${{ matrix.python-version }} + steps: + - uses: actions/checkout@v3 + - name: Setup Python + uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - name: Setup Poetry + uses: abatilo/actions-poetry@v2.0.0 + - name: Install pypi deps + run: poetry install + - name: Unit tests + run: make test diff --git a/Makefile b/Makefile index 5190136b..82f67f5f 100644 --- a/Makefile +++ b/Makefile @@ -20,12 +20,14 @@ help: } \ { lastLine = $$0 }' $(MAKEFILE_LIST) -.PHONY: lint -lint: +style: poetry run black polygon + +static: poetry run mypy polygon -.PHONY: test +lint: style static + test: python -m unittest discover -s tests diff --git a/poetry.lock b/poetry.lock index 9a99c7f0..7a9b6760 100644 --- a/poetry.lock +++ b/poetry.lock @@ -41,6 +41,14 @@ category = "dev" optional = false python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*" +[[package]] +name = "httpretty" +version = "1.1.4" +description = "HTTP client mock for Python" +category = "dev" +optional = false +python-versions = ">=3" + [[package]] name = "importlib-metadata" version = "4.11.3" @@ -165,7 +173,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = "^3.7" -content-hash = "a62d0ece01b486b99384a3b2e21392ef28d135fd7646ab98cdcfe4cb83ea52a9" +content-hash = "992b579a470d1662ac4e64e78155506ec2f8c31013124a4cbb43cf0625a2931c" [metadata.files] black = [ @@ -201,6 +209,9 @@ colorama = [ {file = "colorama-0.4.4-py2.py3-none-any.whl", hash = "sha256:9f47eda37229f68eee03b24b9748937c7dc3868f906e8ba69fbcbdd3bc5dc3e2"}, {file = "colorama-0.4.4.tar.gz", hash = "sha256:5941b2b48a20143d2267e95b1c2a7603ce057ee39fd88e7329b0c292aa16869b"}, ] +httpretty = [ + {file = "httpretty-1.1.4.tar.gz", hash = "sha256:20de0e5dd5a18292d36d928cc3d6e52f8b2ac73daec40d41eb62dee154933b68"}, +] importlib-metadata = [ {file = "importlib_metadata-4.11.3-py3-none-any.whl", hash = "sha256:1208431ca90a8cca1a6b8af391bb53c1a2db74e5d1cef6ddced95d4b2062edc6"}, {file = "importlib_metadata-4.11.3.tar.gz", hash = "sha256:ea4c597ebf37142f827b8f39299579e31685c31d3a438b59f469406afd0f2539"}, diff --git a/pyproject.toml b/pyproject.toml index c351c6c2..98d3228f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ urllib3 = "^1.26.9" black = "^22.3.0" mypy = "^0.942" types-urllib3 = "^1.26.13" +httpretty = "^1.1.4" [build-system] requires = ["poetry-core>=1.0.0"] diff --git a/tests/mocks.py b/tests/mocks.py new file mode 100644 index 00000000..169d452b --- /dev/null +++ b/tests/mocks.py @@ -0,0 +1,22 @@ +from polygon import RESTClient +import unittest +import httpretty + +mocks = [ + ( + "/v2/aggs/ticker/AAPL/range/1/day/2005-04-01/2005-04-04", + '{"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}' + ) +] + +class BaseTest(unittest.TestCase): + setup = False + def setUp(self): + if self.setup: + return + httpretty.enable(verbose=True, allow_net_connect=False) + c = RESTClient() + for m in mocks: + httpretty.register_uri(httpretty.GET, c.BASE + m[0], m[1]) + self.setup = True + diff --git a/tests/test_aggs.py b/tests/test_aggs.py index 6166eace..7870e8b2 100644 --- a/tests/test_aggs.py +++ b/tests/test_aggs.py @@ -1,19 +1,11 @@ -import unittest +from polygon import RESTClient +from polygon.rest.models import Agg +from mocks import BaseTest -class TestStringMethods(unittest.TestCase): - def test_upper(self): - self.assertEqual('foo'.upper(), 'FOO') +class AggsTest(BaseTest): + def test_get_aggs(self): + c = RESTClient() + aggs = c.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04") + 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)] + self.assertEqual(aggs, expected) - def test_isupper(self): - self.assertTrue('FOO'.isupper()) - self.assertFalse('Foo'.isupper()) - - def test_split(self): - s = 'hello world' - self.assertEqual(s.split(), ['hello', 'world']) - # check that s.split fails when the separator is not a string - with self.assertRaises(TypeError): - s.split(2) - -if __name__ == '__main__': - unittest.main() From 58a095e3dddf9cdd4658eb94a80504e67ef0852a Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 25 Apr 2022 16:35:46 -0400 Subject: [PATCH 3/6] add poetry run to Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 82f67f5f..35aa5693 100644 --- a/Makefile +++ b/Makefile @@ -29,5 +29,5 @@ static: lint: style static test: - python -m unittest discover -s tests + poetry run python -m unittest discover -s tests From cbac84c75168d3d98a74207400bb5ad24c223d7d Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 25 Apr 2022 16:38:30 -0400 Subject: [PATCH 4/6] add empty key --- tests/mocks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/mocks.py b/tests/mocks.py index 169d452b..ce35c718 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -15,7 +15,7 @@ def setUp(self): if self.setup: return httpretty.enable(verbose=True, allow_net_connect=False) - c = RESTClient() + c = RESTClient("") for m in mocks: httpretty.register_uri(httpretty.GET, c.BASE + m[0], m[1]) self.setup = True From 2ea1e767d077c5f0f6536f572f583677a1a82dc0 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 25 Apr 2022 16:40:32 -0400 Subject: [PATCH 5/6] add comments --- .github/workflows/test.yml | 2 +- Makefile | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index beebd438..a1afc865 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,7 @@ on: permissions: contents: read jobs: - lint: + test: runs-on: ubuntu-latest strategy: fail-fast: false diff --git a/Makefile b/Makefile index 35aa5693..f6751664 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ YELLOW := $(shell tput -Txterm setaf 3) WHITE := $(shell tput -Txterm setaf 7) RESET := $(shell tput -Txterm sgr0) -.PHONY: help lint test +.PHONY: help lint style static test ## Show help help: @@ -20,14 +20,18 @@ help: } \ { lastLine = $$0 }' $(MAKEFILE_LIST) +## Check code style style: poetry run black polygon +## Check static types static: poetry run mypy polygon +## Check code style and static types lint: style static +## Run unit tests test: poetry run python -m unittest discover -s tests From 4b910fcf6d8892c4688118c2a47ba2b05eef3c09 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 25 Apr 2022 16:46:52 -0400 Subject: [PATCH 6/6] pass empty api key --- tests/test_aggs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_aggs.py b/tests/test_aggs.py index 7870e8b2..f8c88cca 100644 --- a/tests/test_aggs.py +++ b/tests/test_aggs.py @@ -4,7 +4,7 @@ class AggsTest(BaseTest): def test_get_aggs(self): - c = RESTClient() + c = RESTClient("") aggs = c.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04") 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)] self.assertEqual(aggs, expected)