-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2233 from dhruvan2006/fix/tests
Add GitHub Actions workflow and fix failing tests
- Loading branch information
Showing
8 changed files
with
700 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
name: Pytest | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
- main | ||
- dev | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: "3.12" | ||
cache: 'pip' | ||
|
||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements.txt pytest | ||
- name: Run non-cache tests | ||
run: pytest tests/ --ignore tests/test_cache.py --ignore tests/test_price_repair.py | ||
|
||
- name: Run cache tests | ||
run: | | ||
pytest tests/test_cache.py::TestCache | ||
pytest tests/test_cache.py::TestCacheNoPermission |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
""" | ||
Tests for cache | ||
To run all tests in suite from commandline: | ||
python -m unittest tests.cache | ||
Specific test class: | ||
python -m unittest tests.cache.TestCache | ||
""" | ||
from unittest import TestSuite | ||
|
||
from tests.context import yfinance as yf | ||
|
||
import unittest | ||
import tempfile | ||
import os | ||
|
||
|
||
class TestCache(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
cls.tempCacheDir = tempfile.TemporaryDirectory() | ||
yf.set_tz_cache_location(cls.tempCacheDir.name) | ||
|
||
@classmethod | ||
def tearDownClass(cls): | ||
yf.cache._TzDBManager.close_db() | ||
cls.tempCacheDir.cleanup() | ||
|
||
def test_storeTzNoRaise(self): | ||
# storing TZ to cache should never raise exception | ||
tkr = 'AMZN' | ||
tz1 = "America/New_York" | ||
tz2 = "London/Europe" | ||
cache = yf.cache.get_tz_cache() | ||
cache.store(tkr, tz1) | ||
cache.store(tkr, tz2) | ||
|
||
def test_setTzCacheLocation(self): | ||
self.assertEqual(yf.cache._TzDBManager.get_location(), self.tempCacheDir.name) | ||
|
||
tkr = 'AMZN' | ||
tz1 = "America/New_York" | ||
cache = yf.cache.get_tz_cache() | ||
cache.store(tkr, tz1) | ||
|
||
self.assertTrue(os.path.exists(os.path.join(self.tempCacheDir.name, "tkr-tz.db"))) | ||
|
||
|
||
class TestCacheNoPermission(unittest.TestCase): | ||
@classmethod | ||
def setUpClass(cls): | ||
if os.name == "nt": # Windows | ||
cls.cache_path = "C:\\Windows\\System32\\yf-cache" | ||
else: # Unix/Linux/MacOS | ||
# Use a writable directory | ||
cls.cache_path = "/yf-cache" | ||
yf.set_tz_cache_location(cls.cache_path) | ||
|
||
def test_tzCacheRootStore(self): | ||
# Test that if cache path in read-only filesystem, no exception. | ||
tkr = 'AMZN' | ||
tz1 = "America/New_York" | ||
|
||
# During attempt to store, will discover cannot write | ||
yf.cache.get_tz_cache().store(tkr, tz1) | ||
|
||
# Handling the store failure replaces cache with a dummy | ||
cache = yf.cache.get_tz_cache() | ||
self.assertTrue(cache.dummy) | ||
cache.store(tkr, tz1) | ||
|
||
def test_tzCacheRootLookup(self): | ||
# Test that if cache path in read-only filesystem, no exception. | ||
tkr = 'AMZN' | ||
# During attempt to lookup, will discover cannot write | ||
yf.cache.get_tz_cache().lookup(tkr) | ||
|
||
# Handling the lookup failure replaces cache with a dummy | ||
cache = yf.cache.get_tz_cache() | ||
self.assertTrue(cache.dummy) | ||
cache.lookup(tkr) | ||
|
||
def suite(): | ||
ts: TestSuite = unittest.TestSuite() | ||
ts.addTest(TestCache('Test cache')) | ||
ts.addTest(TestCacheNoPermission('Test cache no permission')) | ||
return ts | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.