Skip to content

Commit ddb2a48

Browse files
committed
prep for reorg
1 parent 133a5eb commit ddb2a48

File tree

4 files changed

+236
-37
lines changed

4 files changed

+236
-37
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,4 @@ certs/
119119
td/td_state.json
120120
tests/api_pipeline.py
121121
tests/api_streaming.py
122-
td/certs_old.py
122+
td/certs.py

tests/test_default.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/unit/test_session.py

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from td.client import TDClient
1010
from td.stream import TDStreamerClient
1111

12+
SAVE_FLAG = False
13+
1214

1315
class TDSession(TestCase):
1416

@@ -71,8 +73,9 @@ def test_single_get_quotes(self):
7173
self.assertIn('MSFT', quotes)
7274

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

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

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

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

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

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

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

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

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

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

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

170178
def test_chart_history(self):
171179
"""Test getting historical prices."""
@@ -222,8 +230,9 @@ def test_chart_history(self):
222230
self.assertFalse(historical_prices['empty'])
223231

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

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

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

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

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

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

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

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

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

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

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

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

340354
# `get_subscription_keys` endpoint. Should not return an error
341355
streamer_keys = self.td_session.get_streamer_subscription_keys(
342-
accounts=[self.td_session.account_number])
356+
accounts=[self.td_session.account_number]
357+
)
343358

344359
self.assertIsInstance(streamer_keys, dict)
345360
self.assertIn('keys', streamer_keys)
346361

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

351367
def tearDown(self) -> None:
352368
"""Teardown the Robot."""

tests/unit/test_statepath.py

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
import pathlib
2+
import unittest
3+
4+
from unittest import TestCase
5+
from td.utils import StatePath
6+
7+
8+
class StatePathSession(TestCase):
9+
10+
"""Will perform a unit test for the StatePath Object."""
11+
12+
def setUp(self) -> None:
13+
"""Set up the StatePath Instance."""
14+
15+
self.state_path = StatePath()
16+
17+
def test_creates_instance_of_session(self) -> None:
18+
"""Create an instance and make sure it's a StatePath Object."""
19+
20+
# Make sure it's a state path.
21+
self.assertIsInstance(self.state_path, StatePath)
22+
23+
# make sure our default name matches.
24+
self.assertEqual(
25+
self.state_path.credenitals_file_name, 'td_state.json'
26+
)
27+
28+
# Make sure the Credentials File is a Windows Path.
29+
self.assertIsInstance(
30+
self.state_path.credentials_file,
31+
pathlib.WindowsPath
32+
)
33+
34+
def test_home_path(self) -> None:
35+
"""Tests creating a homepath."""
36+
37+
truth = pathlib.Path.home()
38+
self.assertEqual(truth, self.state_path.path_home())
39+
40+
def test_home_directory(self) -> None:
41+
"""Tests grabbing the home directory."""
42+
43+
truth = pathlib.Path(__file__).parents[2].joinpath('td')
44+
self.assertEqual(truth, self.state_path.library_directory)
45+
46+
def test_settings_directory(self) -> None:
47+
"""Tests grabbing the settings directory."""
48+
49+
truth = pathlib.Path().home().joinpath('.td_python_library')
50+
self.assertEqual(truth, self.state_path.settings_directory)
51+
52+
def test_library_directory(self) -> None:
53+
"""Tests grabbing the home directory."""
54+
55+
truth = pathlib.Path.home()
56+
self.assertEqual(truth, self.state_path.home_directory)
57+
58+
def test_json_library_path(self) -> None:
59+
"""Test grabbing the Library JSON file path."""
60+
61+
truth = pathlib.Path(__file__).parents[2].joinpath(
62+
'td/td_state.json'
63+
)
64+
self.assertEqual(truth, self.state_path.json_library_path())
65+
66+
def test_json_setting_path(self) -> None:
67+
"""Test grabbing the Setting JSON file path."""
68+
69+
truth = pathlib.Path().home().joinpath('.td_python_library/td_state.json')
70+
71+
self.assertEqual(truth, self.state_path.json_settings_path())
72+
73+
def test_write_to_settings(self) -> None:
74+
"""Test writing the credentials to Settings Folder."""
75+
76+
# Set the fake name.
77+
self.state_path.credenitals_file_name = 'fake_td_state.json'
78+
79+
# Determine our base.
80+
truth = pathlib.Path().home().joinpath('.td_python_library/fake_td_state.json')
81+
82+
# Get the JSON settings paht.
83+
json_settings = self.state_path.json_settings_path()
84+
85+
# Write the credentials.
86+
check = self.state_path.write_credentials(
87+
file_path=json_settings,
88+
state={'value': 'settings'}
89+
)
90+
91+
# Make sure they are equal.
92+
self.assertEqual(truth, check)
93+
94+
def test_write_to_library(self) -> None:
95+
"""Test writing the credentials to Library Folder."""
96+
97+
# Set the fake name.
98+
self.state_path.credenitals_file_name = 'fake_td_state.json'
99+
100+
# Determine our base.
101+
truth = pathlib.Path(__file__).parents[2].joinpath(
102+
'td/fake_td_state.json'
103+
)
104+
105+
# Get the JSON settings paht.
106+
json_settings = self.state_path.json_library_path()
107+
108+
# Write the credentials.
109+
check = self.state_path.write_credentials(
110+
file_path=json_settings,
111+
state={'value': 'library'}
112+
)
113+
114+
# Make sure they are equal.
115+
self.assertEqual(truth, check)
116+
117+
def test_write_to_custom(self) -> None:
118+
"""Test writing to a User Provided Path."""
119+
120+
# Define the file path.
121+
file_path = r'config\td_state_custom.json'
122+
123+
# Define the truth.
124+
truth = pathlib.Path(__file__).parents[2].joinpath(file_path)
125+
126+
# Write and check.
127+
self.assertEqual(truth, self.state_path.write_credentials(
128+
file_path=file_path,
129+
state={'value': 'custom'}
130+
)
131+
)
132+
133+
def test_read_from_settings(self) -> None:
134+
"""Test writing the credentials to Settings Folder."""
135+
136+
# Set the fake name.
137+
self.state_path.credenitals_file_name = 'fake_td_state.json'
138+
139+
truth = {'value': 'settings'}
140+
141+
file_path = self.state_path.json_settings_path()
142+
check = self.state_path.read_credentials(file_path=file_path)
143+
144+
# Make sure they are equal.
145+
self.assertEqual(truth, check)
146+
147+
def test_read_from_library(self) -> None:
148+
"""Test writing the credentials to Library Folder."""
149+
150+
# Set the fake name.
151+
self.state_path.credenitals_file_name = 'fake_td_state.json'
152+
153+
truth = {'value': 'library'}
154+
155+
file_path = self.state_path.json_library_path()
156+
157+
check = self.state_path.read_credentials(file_path=file_path)
158+
159+
self.assertEqual(truth, check)
160+
161+
def test_read_from_custom(self) -> None:
162+
"""Test writing to a User Provided Path."""
163+
164+
truth = {'value': 'custom'}
165+
166+
file_path = pathlib.Path(r'config\td_state_custom.json')
167+
168+
check = self.state_path.read_credentials(file_path=file_path)
169+
170+
# Make sure they are equal.
171+
self.assertEqual(truth, check)
172+
173+
def test_read_from_non_exist(self) -> None:
174+
"""Test writing to a User Provided Path."""
175+
176+
truth = 'Credentials File does not exist.'
177+
file_path = pathlib.Path(r'config\no\td_state_custom.json')
178+
179+
with self.assertRaises(FileNotFoundError) as context:
180+
self.state_path.read_credentials(file_path=file_path)
181+
182+
# Make sure they are equal.
183+
self.assertEqual(truth, str(context.exception))
184+
185+
def tearDown(self) -> None:
186+
"""Teardown the StatePath Object."""
187+
188+
self.state_path = None
189+
190+
191+
if __name__ == '__main__':
192+
unittest.main()

0 commit comments

Comments
 (0)