Skip to content

Commit

Permalink
prep for reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
areed1192 committed Aug 8, 2020
1 parent 133a5eb commit ddb2a48
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,4 @@ certs/
td/td_state.json
tests/api_pipeline.py
tests/api_streaming.py
td/certs_old.py
td/certs.py
9 changes: 0 additions & 9 deletions tests/test_default.py

This file was deleted.

70 changes: 43 additions & 27 deletions tests/unit/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from td.client import TDClient
from td.stream import TDStreamerClient

SAVE_FLAG = False


class TDSession(TestCase):

Expand Down Expand Up @@ -71,8 +73,9 @@ def test_single_get_quotes(self):
self.assertIn('MSFT', quotes)

# Save the data.
with open(r'samples\responses\sample_single_quotes.jsonc', 'w+') as data_file:
json.dump(obj=quotes, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_single_quotes.jsonc', 'w+') as data_file:
json.dump(obj=quotes, fp=data_file, indent=3)

def test_get_quotes(self):
"""Test Getting Multiple Quotes."""
Expand All @@ -84,8 +87,9 @@ def test_get_quotes(self):
self.assertTrue(set(['MSFT', 'AAPL']).issuperset(set(quotes.keys())))

# Save the data.
with open(r'samples\responses\sample_multiple_quotes.jsonc', 'w+') as data_file:
json.dump(obj=quotes, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_multiple_quotes.jsonc', 'w+') as data_file:
json.dump(obj=quotes, fp=data_file, indent=3)

def test_get_accounts(self):
"""Test Get Accounts."""
Expand All @@ -100,8 +104,9 @@ def test_get_accounts(self):
# self.assertIn('orderStrategies', accounts[0]['securitiesAccount'])

# Save the data.
with open(r'samples\responses\sample_accounts.jsonc', 'w+') as data_file:
json.dump(obj=accounts, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_accounts.jsonc', 'w+') as data_file:
json.dump(obj=accounts, fp=data_file, indent=3)

def test_create_stream_session(self):
"""Test Creating a new streaming session."""
Expand All @@ -123,8 +128,9 @@ def test_get_transactions(self):
self.assertIn('type', transaction_data_multi[0])

# Save the data.
with open(r'samples\responses\sample_transaction_data.jsonc', 'w+') as data_file:
json.dump(obj=transaction_data_multi, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_transaction_data.jsonc', 'w+') as data_file:
json.dump(obj=transaction_data_multi, fp=data_file, indent=3)

def test_get_market_hours(self):
"""Test get market hours."""
Expand All @@ -148,8 +154,9 @@ def test_get_market_hours(self):
self.assertIn('isOpen', market_hours_multi['equity']['EQ'])

# Save the data.
with open(r'samples\responses\sample_market_hours.jsonc', 'w+') as data_file:
json.dump(obj=market_hours_multi, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_market_hours.jsonc', 'w+') as data_file:
json.dump(obj=market_hours_multi, fp=data_file, indent=3)

def test_get_instrument(self):
"""Test getting an instrument."""
Expand All @@ -164,8 +171,9 @@ def test_get_instrument(self):
self.assertIn('cusip', get_instrument[0])

# Save the data.
with open(r'samples\responses\sample_instrument.jsonc', 'w+') as data_file:
json.dump(obj=get_instrument, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_instrument.jsonc', 'w+') as data_file:
json.dump(obj=get_instrument, fp=data_file, indent=3)

def test_chart_history(self):
"""Test getting historical prices."""
Expand Down Expand Up @@ -222,8 +230,9 @@ def test_chart_history(self):
self.assertFalse(historical_prices['empty'])

# Save the data.
with open(r'samples\responses\sample_historical_prices.jsonc', 'w+') as data_file:
json.dump(obj=historical_prices, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_historical_prices.jsonc', 'w+') as data_file:
json.dump(obj=historical_prices, fp=data_file, indent=3)

def test_custom_historical_prices(self):
"""Test getting historical prices for a custom date range."""
Expand Down Expand Up @@ -265,8 +274,9 @@ def test_custom_historical_prices(self):
self.assertFalse(historical_custom['empty'])

# Save the data.
with open(r'samples\responses\sample_historical_prices.jsonc', 'w+') as data_file:
json.dump(obj=historical_custom, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_historical_prices.jsonc', 'w+') as data_file:
json.dump(obj=historical_custom, fp=data_file, indent=3)

def test_search_instruments(self):
"""Test Searching for Instruments."""
Expand All @@ -281,8 +291,9 @@ def test_search_instruments(self):
self.assertIn('MSFT', instrument_search_data)

# Save the data.
with open(r'samples\responses\sample_search_instrument.jsonc', 'w+') as data_file:
json.dump(obj=instrument_search_data, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_search_instrument.jsonc', 'w+') as data_file:
json.dump(obj=instrument_search_data, fp=data_file, indent=3)

def test_get_movers(self):
"""Test getting Market movers."""
Expand All @@ -302,8 +313,9 @@ def test_get_movers(self):
self.assertIn('symbol', movers_data[0])

# Save the data.
with open(r'samples\responses\sample_movers.jsonc', 'w+') as data_file:
json.dump(obj=movers_data, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_movers.jsonc', 'w+') as data_file:
json.dump(obj=movers_data, fp=data_file, indent=3)

def test_get_user_preferences(self):
"""Test getting user preferences."""
Expand All @@ -317,8 +329,9 @@ def test_get_user_preferences(self):
self.assertIn('expressTrading', preference_data)

# Save the data.
with open(r'samples\responses\sample_account_preferences.jsonc', 'w+') as data_file:
json.dump(obj=preference_data, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_account_preferences.jsonc', 'w+') as data_file:
json.dump(obj=preference_data, fp=data_file, indent=3)

def test_get_user_principals(self):
"""Test getting user principals."""
Expand All @@ -331,22 +344,25 @@ def test_get_user_principals(self):
self.assertIn('authToken', user_principals)

# Save the data.
with open(r'samples\responses\sample_user_principals.jsonc', 'w+') as data_file:
json.dump(obj=user_principals, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_user_principals.jsonc', 'w+') as data_file:
json.dump(obj=user_principals, fp=data_file, indent=3)

def test_get_streamer_keys(self):
"""Test getting user preferences."""

# `get_subscription_keys` endpoint. Should not return an error
streamer_keys = self.td_session.get_streamer_subscription_keys(
accounts=[self.td_session.account_number])
accounts=[self.td_session.account_number]
)

self.assertIsInstance(streamer_keys, dict)
self.assertIn('keys', streamer_keys)

# Save the data.
with open(r'samples\responses\sample_streamer_keys.jsonc', 'w+') as data_file:
json.dump(obj=streamer_keys, fp=data_file, indent=3)
if SAVE_FLAG:
with open(r'samples\responses\sample_streamer_keys.jsonc', 'w+') as data_file:
json.dump(obj=streamer_keys, fp=data_file, indent=3)

def tearDown(self) -> None:
"""Teardown the Robot."""
Expand Down
192 changes: 192 additions & 0 deletions tests/unit/test_statepath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
import pathlib
import unittest

from unittest import TestCase
from td.utils import StatePath


class StatePathSession(TestCase):

"""Will perform a unit test for the StatePath Object."""

def setUp(self) -> None:
"""Set up the StatePath Instance."""

self.state_path = StatePath()

def test_creates_instance_of_session(self) -> None:
"""Create an instance and make sure it's a StatePath Object."""

# Make sure it's a state path.
self.assertIsInstance(self.state_path, StatePath)

# make sure our default name matches.
self.assertEqual(
self.state_path.credenitals_file_name, 'td_state.json'
)

# Make sure the Credentials File is a Windows Path.
self.assertIsInstance(
self.state_path.credentials_file,
pathlib.WindowsPath
)

def test_home_path(self) -> None:
"""Tests creating a homepath."""

truth = pathlib.Path.home()
self.assertEqual(truth, self.state_path.path_home())

def test_home_directory(self) -> None:
"""Tests grabbing the home directory."""

truth = pathlib.Path(__file__).parents[2].joinpath('td')
self.assertEqual(truth, self.state_path.library_directory)

def test_settings_directory(self) -> None:
"""Tests grabbing the settings directory."""

truth = pathlib.Path().home().joinpath('.td_python_library')
self.assertEqual(truth, self.state_path.settings_directory)

def test_library_directory(self) -> None:
"""Tests grabbing the home directory."""

truth = pathlib.Path.home()
self.assertEqual(truth, self.state_path.home_directory)

def test_json_library_path(self) -> None:
"""Test grabbing the Library JSON file path."""

truth = pathlib.Path(__file__).parents[2].joinpath(
'td/td_state.json'
)
self.assertEqual(truth, self.state_path.json_library_path())

def test_json_setting_path(self) -> None:
"""Test grabbing the Setting JSON file path."""

truth = pathlib.Path().home().joinpath('.td_python_library/td_state.json')

self.assertEqual(truth, self.state_path.json_settings_path())

def test_write_to_settings(self) -> None:
"""Test writing the credentials to Settings Folder."""

# Set the fake name.
self.state_path.credenitals_file_name = 'fake_td_state.json'

# Determine our base.
truth = pathlib.Path().home().joinpath('.td_python_library/fake_td_state.json')

# Get the JSON settings paht.
json_settings = self.state_path.json_settings_path()

# Write the credentials.
check = self.state_path.write_credentials(
file_path=json_settings,
state={'value': 'settings'}
)

# Make sure they are equal.
self.assertEqual(truth, check)

def test_write_to_library(self) -> None:
"""Test writing the credentials to Library Folder."""

# Set the fake name.
self.state_path.credenitals_file_name = 'fake_td_state.json'

# Determine our base.
truth = pathlib.Path(__file__).parents[2].joinpath(
'td/fake_td_state.json'
)

# Get the JSON settings paht.
json_settings = self.state_path.json_library_path()

# Write the credentials.
check = self.state_path.write_credentials(
file_path=json_settings,
state={'value': 'library'}
)

# Make sure they are equal.
self.assertEqual(truth, check)

def test_write_to_custom(self) -> None:
"""Test writing to a User Provided Path."""

# Define the file path.
file_path = r'config\td_state_custom.json'

# Define the truth.
truth = pathlib.Path(__file__).parents[2].joinpath(file_path)

# Write and check.
self.assertEqual(truth, self.state_path.write_credentials(
file_path=file_path,
state={'value': 'custom'}
)
)

def test_read_from_settings(self) -> None:
"""Test writing the credentials to Settings Folder."""

# Set the fake name.
self.state_path.credenitals_file_name = 'fake_td_state.json'

truth = {'value': 'settings'}

file_path = self.state_path.json_settings_path()
check = self.state_path.read_credentials(file_path=file_path)

# Make sure they are equal.
self.assertEqual(truth, check)

def test_read_from_library(self) -> None:
"""Test writing the credentials to Library Folder."""

# Set the fake name.
self.state_path.credenitals_file_name = 'fake_td_state.json'

truth = {'value': 'library'}

file_path = self.state_path.json_library_path()

check = self.state_path.read_credentials(file_path=file_path)

self.assertEqual(truth, check)

def test_read_from_custom(self) -> None:
"""Test writing to a User Provided Path."""

truth = {'value': 'custom'}

file_path = pathlib.Path(r'config\td_state_custom.json')

check = self.state_path.read_credentials(file_path=file_path)

# Make sure they are equal.
self.assertEqual(truth, check)

def test_read_from_non_exist(self) -> None:
"""Test writing to a User Provided Path."""

truth = 'Credentials File does not exist.'
file_path = pathlib.Path(r'config\no\td_state_custom.json')

with self.assertRaises(FileNotFoundError) as context:
self.state_path.read_credentials(file_path=file_path)

# Make sure they are equal.
self.assertEqual(truth, str(context.exception))

def tearDown(self) -> None:
"""Teardown the StatePath Object."""

self.state_path = None


if __name__ == '__main__':
unittest.main()

0 comments on commit ddb2a48

Please sign in to comment.