Skip to content

Commit

Permalink
🧪 test: add unittest
Browse files Browse the repository at this point in the history
add test case for task utils and sec_synctask

also amend documentation for abstract_quotes_fetcher
  • Loading branch information
aaron yang committed Jul 24, 2022
1 parent 3479e45 commit 30e40b4
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 4 deletions.
12 changes: 9 additions & 3 deletions omega/worker/abstract_quotes_fetcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,20 @@ def get_instance(cls):
return cls._instances[i]

@classmethod
async def get_security_list(cls, date: datetime.date) -> Union[None, np.ndarray]:
"""按如下格式返回证券列表。
async def get_security_list(
cls, date: datetime.date = None
) -> Optional[np.ndarray]:
"""获取证券列表
按如下格式返回证券列表。
code display_name name start_date end_date type
000001.XSHE 平安银行 PAYH 1991-04-03 2200-01-01 stock
Args:
date: 查询日期,如果为None,则返回最新的证券列表。
Returns:
Union[None, np.ndarray]: [description]
`date`日对应的证券列表。
"""
securities = await cls.get_instance().get_security_list(date)
if securities is None or len(securities) == 0:
Expand Down
Empty file added tests/worker/tasks/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions tests/worker/tasks/test_sec_synctask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from tests import init_test_env
import omicron
from omega.worker.tasks.sec_synctask import sync_xrxd_report_list
import datetime
from omicron import cache
import cfg4py
from omega.worker.abstract_quotes_fetcher import AbstractQuotesFetcher as aq


class SecSyncTaskTest(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
await init_test_env()
await omicron.init()
await self.create_quotes_fetcher()

async def asyncTearDown(self) -> None:
await omicron.close()

async def create_quotes_fetcher(self):
cfg = cfg4py.get_instance()
fetcher_info = cfg.quotes_fetchers[0]
impl = fetcher_info["impl"]
params = fetcher_info["workers"][0]
await aq.create_instance(impl, **params)

async def test_sync_xrxd_report_list(self):
# fixme: 先清除掉influxdb和缓存
await cache.sys.hmset("ut:sec:xrxd:report", "worker_count", "0")
await sync_xrxd_report_list(
{
"end": datetime.date(2022, 6, 30),
"state": "start",
"timeout": 20,
"name": "ut_sync_xrxd",
}
)

# fixme: how to check result? 这里应该检查influxdb和缓存中的数据是否如期望
31 changes: 31 additions & 0 deletions tests/worker/tasks/test_task_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import unittest
from omega.worker.tasks.task_utils import cache_init
from tests import init_test_env
import omicron
from omicron import cache
import cfg4py
from omega.worker.abstract_quotes_fetcher import AbstractQuotesFetcher as aq


class TaskUtilsTest(unittest.IsolatedAsyncioTestCase):
async def asyncSetUp(self) -> None:
await init_test_env()
await omicron.init()
await self.create_quotes_fetcher()

async def create_quotes_fetcher(self):
cfg = cfg4py.get_instance()
fetcher_info = cfg.quotes_fetchers[0]
impl = fetcher_info["impl"]
params = fetcher_info["workers"][0]
await aq.create_instance(impl, **params)

async def asyncTearDown(self) -> None:
await omicron.close()

async def test_cache_init(self):
await cache.security.delete("calendar:1d")
await cache.security.delete("security:all")
await cache_init()
self.assertTrue(cache.security.exists("calendar:1d"))
self.assertTrue(cache.security.exists("security:all"))
2 changes: 1 addition & 1 deletion tests/worker/test_jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def test_load_cron_task(self):
self.assertSetEqual(base, set([job.name for job in scheduler.get_jobs()]))

@mock.patch("omega.master.tasks.synctask.mail_notify")
@mock.patch("omega.master.jobs.TimeFrame.save_calendar")
@mock.patch("omega.master.jobs.tf.save_calendar")
@mock.patch("jqadaptor.fetcher.Fetcher.get_all_trade_days")
async def test_sync_calendar(self, get_all_trade_days, *args):
# all_trade_days.npy
Expand Down

0 comments on commit 30e40b4

Please sign in to comment.