99from td .client import TDClient
1010from td .stream import TDStreamerClient
1111
12+ SAVE_FLAG = False
13+
1214
1315class 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."""
0 commit comments