Mocks and Testing¶
Known Issues¶
Note
There is a known pandas issue that logs a warning about _timelex, and it will show as a warning until it is fixed in pandas. Please ignore this warning for now.
DeprecationWarning: _timelex is a private class and may break without warning, it will be moved and or renamed in future versions.
Run All Tests¶
py.test --maxfail=1
Mock S3 Boto Utilities¶
These are testing utilities for mocking S3 functionality without having an s3 endpoint running.
Mock boto3 s3 objects
-
class
analysis_engine.mocks.mock_boto3_s3.
MockBotoS3
(name='mock_s3', endpoint_url=None, aws_access_key_id=None, aws_secret_access_key=None, region_name=None, config=None)[source]¶
-
analysis_engine.mocks.mock_boto3_s3.
build_boto3_resource
(name='mock_s3', endpoint_url=None, aws_access_key_id=None, aws_secret_access_key=None, region_name=None, config=None)[source]¶ Parameters: - name – name of client
- endpoint_url – endpoint url
- aws_access_key_id – aws access key
- aws_secret_access_key – aws secret key
- region_name – region name
- config – config object
-
analysis_engine.mocks.mock_boto3_s3.
mock_s3_read_contents_from_key_ev
(s3, s3_bucket_name, s3_key, encoding, convert_as_json)[source]¶ mock_s3_read_contents_from_key
Parameters: - s3 – s3 client
- s3_bucket_name – bucket name
- s3_key – key
- encoding – utf-8
- convert_as_json – convert to json
-
analysis_engine.mocks.mock_boto3_s3.
mock_publish_from_s3_to_redis
(work_dict)[source]¶ Parameters: work_dict – dictionary for driving the task
Mock Redis Utilities¶
These are testing utilities for mocking Redis’s functionality without having a Redis server running.
Mock redis objects
Mock Yahoo Utilities¶
These are testing utilities for mocking Yahoo’s functionality without having internet connectivity to fetch data from Yahoo.
Mock Pinance Object for unittests
-
analysis_engine.mocks.mock_pinance.
mock_get_options
(ticker=None, contract_type=None, exp_date_str=None, strike=None)[source]¶ Parameters: - ticker – ticker to lookup
- exp_date_str –
YYYY-MM-DD
expiration date format - strike – optional strike price,
None
returns all option chains - contract_type –
C
calls orP
for puts, ifstrike=None
then thecontract_type
is ignored
-
class
analysis_engine.mocks.mock_pinance.
MockPinance
(symbol='SPY')[source]¶ -
-
get_options
(ticker=None, contract_type=None, exp_date_str=None, strike=None)[source]¶ Parameters: - ticker – ticker to lookup
- exp_date_str –
YYYY-MM-DD
expiration date format - strike – optional strike price,
None
returns all option chains - contract_type –
C
calls orP
for puts, ifstrike=None
then thecontract_type
is ignored
-
Mock IEX Utilities¶
These are testing utilities for mocking IEX functionality without having internet connectivity to fetch data from IEX.
Mocking data fetch api calls
-
analysis_engine.mocks.mock_iex.
mock_minute
(url, token=None, version=None, verbose=False)[source]¶ mock minute history for a chart
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
-
analysis_engine.mocks.mock_iex.
mock_quote
(url, token=None, version=None, verbose=False)[source]¶ mock quote
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
-
analysis_engine.mocks.mock_iex.
mock_stats
(url, token=None, version=None, verbose=False)[source]¶ mock stats
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
-
analysis_engine.mocks.mock_iex.
mock_peers
(url, token=None, version=None, verbose=False)[source]¶ mock peers
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
-
analysis_engine.mocks.mock_iex.
mock_news
(url, token=None, version=None, verbose=False)[source]¶ mock news
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
-
analysis_engine.mocks.mock_iex.
mock_financials
(url, token=None, version=None, verbose=False)[source]¶ mock financials
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
-
analysis_engine.mocks.mock_iex.
mock_earnings
(url, token=None, version=None, verbose=False)[source]¶ mock earnings
Parameters: - url – IEX resource url
- token – optional - string token for your user’s account
- version – optional - version string
- verbose – optional - boolean debug logging
Mock TA Lib¶
These are mock talib functions to help test indicators using talib.
Mock TA-Lib objects
-
analysis_engine.mocks.mock_talib.
MockWILLRBuy
(high=None, low=None, close=None, timeperiod=None)[source]¶ build a mock wiliams r object that will report an
buy
value to test indicators without having talib installedParameters: - high – list of highs
- low – list of lows
- close – list of closes
- timeperiod – integer number of values
in
high
,low
andclose
-
analysis_engine.mocks.mock_talib.
MockWILLRSell
(high=None, low=None, close=None, timeperiod=None)[source]¶ build a mock wiliams r object that will report an
sell
value to test indicators without having talib installedParameters: - high – list of highs
- low – list of lows
- close – list of closes
- timeperiod – integer number of values
in
high
,low
andclose
-
analysis_engine.mocks.mock_talib.
MockWILLRIgnore
(high=None, low=None, close=None, timeperiod=None)[source]¶ build a mock wiliams r object that will report an
ignore
value to test indicators without having talib installedParameters: - high – list of highs
- low – list of lows
- close – list of closes
- timeperiod – integer number of values
in
high
,low
andclose
Mock Trading Tools for Developing Algorithms and Indicators¶
These are mock helper functions for patching the BaseAlgo
object to simulate various test cases
Mock Algorithm Methods for unittesting things like previously-owned shares or sell-side indicators without owning shares
Support mocking owned shares to test indicator selling
If you can modify your algorithm
config_dict
you can also set apositions
dictionary like:algo_config_dict = { # other values omitted for docs 'positions': { 'SPY': { 'shares': 10000, 'buys': [], 'sells': [] } } }
Use with your custom algorithm unittests:
import mock import analysis_engine.mocks.mock_algo_trading as mock_trading @mock.patch( ('analysis_engine.algo.BaseAlgo.get_ticker_positions'), new=mock_trading.mock_algo_owns_shares_in_ticker_before_starting)
Parameters: - obj – algorithm object
- ticker – ticker symbol