Skip to content

Commit ccab21e

Browse files
authored
Merge pull request #162 from bbeale/master
Unit tests for Watchlists
2 parents 92f1dd9 + beb0c42 commit ccab21e

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

tests/test_rest.py

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,211 @@ def test_data(reqmock):
332332
assert barset['AAPL'].df.index[0].day == 23
333333

334334

335+
def test_watchlists(reqmock):
336+
api = tradeapi.REST('key-id', 'secret-key', api_version='v1')
337+
# get watchlists
338+
reqmock.get('https://api.alpaca.markets/v1/watchlists',
339+
text='''[
340+
{
341+
"id": "900e20b1-46eb-492b-a505-2ea67386b5fd",
342+
"account_id": "1f893862-13b5-4603-b3ca-513980c00c6e",
343+
"created_at": "2019-10-31T01:45:41.308091Z",
344+
"updated_at": "2019-12-09T17:50:57.151693Z",
345+
"name": "Primary Watchlist"
346+
},
347+
{
348+
"id": "e65f2f2d-b596-4db6-bd68-1b7ceb77cccc",
349+
"account_id": "1f893862-13b5-4603-b3ca-513980c00c6e",
350+
"created_at": "2020-01-23T00:52:07.049138Z",
351+
"updated_at": "2020-01-23T00:57:27.063889Z",
352+
"name": "dev"
353+
},
354+
{
355+
"id": "e7574813-a853-4536-a52b-b47cc25def14",
356+
"account_id": "1f893862-13b5-4603-b3ca-513980c00c6e",
357+
"created_at": "2020-01-23T01:36:25.807997Z",
358+
"updated_at": "2020-01-23T01:36:25.807997Z",
359+
"name": "prod"
360+
}
361+
]''')
362+
watchlists = api.get_watchlists()
363+
assert watchlists[0].name == 'Primary Watchlist'
364+
assert watchlists[1].id == 'e65f2f2d-b596-4db6-bd68-1b7ceb77cccc'
365+
assert watchlists[2].account_id == '1f893862-13b5-4603-b3ca-513980c00c6e'
366+
367+
# get a watchlist by watchlist_id
368+
watchlist_id = "e65f2f2d-b596-4db6-bd68-1b7ceb77cccc"
369+
symbol = "AMD"
370+
reqmock.get('https://api.alpaca.markets/v1/watchlists/{}'.format(watchlist_id),
371+
text='''{
372+
"id": "e65f2f2d-b596-4db6-bd68-1b7ceb77cccc",
373+
"account_id": "1f893862-13b5-4603-b3ca-513980c00c6e",
374+
"created_at": "2020-01-23T00:52:07.049138Z",
375+
"updated_at": "2020-01-23T00:57:27.063889Z",
376+
"name": "Primary Watchlist",
377+
"assets": [
378+
{
379+
"id": "03fb07bb-5db1-4077-8dea-5d711b272625",
380+
"class": "us_equity",
381+
"exchange": "NASDAQ",
382+
"symbol": "AMD",
383+
"name": "",
384+
"status": "active",
385+
"tradable": true,
386+
"marginable": true,
387+
"shortable": true,
388+
"easy_to_borrow": true
389+
},
390+
{
391+
"id": "4ce9353c-66d1-46c2-898f-fce867ab0247",
392+
"class": "us_equity",
393+
"exchange": "NASDAQ",
394+
"symbol": "NVDA",
395+
"name": "",
396+
"status": "active",
397+
"tradable": true,
398+
"marginable": true,
399+
"shortable": true,
400+
"easy_to_borrow": true
401+
},
402+
{
403+
"id": "bb2a26c0-4c77-4801-8afc-82e8142ac7b8",
404+
"class": "us_equity",
405+
"exchange": "NASDAQ",
406+
"symbol": "NFLX",
407+
"name": "",
408+
"status": "active",
409+
"tradable": true,
410+
"marginable": true,
411+
"shortable": true,
412+
"easy_to_borrow": true
413+
},
414+
{
415+
"id": "24cbba8c-831b-44e2-8503-dd0c2ed7af8f",
416+
"class": "us_equity",
417+
"exchange": "NASDAQ",
418+
"symbol": "PYPL",
419+
"name": "",
420+
"status": "active",
421+
"tradable": true,
422+
"marginable": true,
423+
"shortable": true,
424+
"easy_to_borrow": true
425+
}
426+
]
427+
}''')
428+
watchlist = api.get_watchlist(watchlist_id)
429+
assert watchlist.name == 'Primary Watchlist'
430+
assert len(watchlist.assets) == 4
431+
assert watchlist.assets[0]["id"] == "03fb07bb-5db1-4077-8dea-5d711b272625"
432+
assert watchlist.assets[0]["class"] == "us_equity"
433+
assert watchlist.assets[0]["exchange"] == "NASDAQ"
434+
assert watchlist.assets[0]["symbol"] == "AMD"
435+
assert watchlist.assets[0]["name"] == ""
436+
assert watchlist.assets[0]["status"] == "active"
437+
assert watchlist.assets[0]["tradable"]
438+
assert watchlist.assets[0]["marginable"]
439+
assert watchlist.assets[0]["shortable"]
440+
assert watchlist.assets[0]["easy_to_borrow"]
441+
442+
# add an asset to a watchlist
443+
reqmock.post('https://api.alpaca.markets/v1/watchlists/{}'.format(watchlist_id),
444+
text='''{
445+
"id": "e65f2f2d-b596-4db6-bd68-1b7ceb77cccc",
446+
"account_id": "1f893862-13b5-4603-b3ca-513980c00c6e",
447+
"created_at": "2020-01-23T00:52:07.049138Z",
448+
"updated_at": "2020-01-23T00:57:27.063889Z",
449+
"name": "Primary Watchlist",
450+
"assets": [
451+
{
452+
"id": "03fb07bb-5db1-4077-8dea-5d711b272625",
453+
"class": "us_equity",
454+
"exchange": "NASDAQ",
455+
"symbol": "AMD",
456+
"name": "",
457+
"status": "active",
458+
"tradable": true,
459+
"marginable": true,
460+
"shortable": true,
461+
"easy_to_borrow": true
462+
}
463+
]
464+
}''')
465+
watchlist = api.add_to_watchlist(watchlist_id, symbol="AMD")
466+
assert watchlist.name == 'Primary Watchlist'
467+
assert len(watchlist.assets) == 1
468+
assert watchlist.assets[0]["symbol"] == "AMD"
469+
470+
# remove an item from a watchlist
471+
reqmock.delete('https://api.alpaca.markets/v1/watchlists/{}/{}'.format(watchlist_id, symbol),
472+
text='''{
473+
"id": "e65f2f2d-b596-4db6-bd68-1b7ceb77cccc",
474+
"account_id": "1f893862-13b5-4603-b3ca-513980c00c6e",
475+
"created_at": "2020-01-23T00:52:07.049138Z",
476+
"updated_at": "2020-01-23T00:57:27.063889Z",
477+
"name": "dev",
478+
"assets": [
479+
{
480+
"id": "03fb07bb-5db1-4077-8dea-5d711b272625",
481+
"class": "us_equity",
482+
"exchange": "NASDAQ",
483+
"symbol": "AMD",
484+
"name": "",
485+
"status": "active",
486+
"tradable": true,
487+
"marginable": true,
488+
"shortable": true,
489+
"easy_to_borrow": true
490+
},
491+
{
492+
"id": "4ce9353c-66d1-46c2-898f-fce867ab0247",
493+
"class": "us_equity",
494+
"exchange": "NASDAQ",
495+
"symbol": "NVDA",
496+
"name": "",
497+
"status": "active",
498+
"tradable": true,
499+
"marginable": true,
500+
"shortable": true,
501+
"easy_to_borrow": true
502+
},
503+
{
504+
"id": "bb2a26c0-4c77-4801-8afc-82e8142ac7b8",
505+
"class": "us_equity",
506+
"exchange": "NASDAQ",
507+
"symbol": "NFLX",
508+
"name": "",
509+
"status": "active",
510+
"tradable": true,
511+
"marginable": true,
512+
"shortable": true,
513+
"easy_to_borrow": true
514+
},
515+
{
516+
"id": "24cbba8c-831b-44e2-8503-dd0c2ed7af8f",
517+
"class": "us_equity",
518+
"exchange": "NASDAQ",
519+
"symbol": "PYPL",
520+
"name": "",
521+
"status": "active",
522+
"tradable": true,
523+
"marginable": true,
524+
"shortable": true,
525+
"easy_to_borrow": true
526+
}
527+
]
528+
}''')
529+
api.delete_from_watchlist(watchlist_id, symbol)
530+
531+
# delete a watchlist
532+
reqmock.delete(
533+
'https://api.alpaca.markets/v1/watchlists/{}'.format(watchlist_id),
534+
text='',
535+
status_code=204,
536+
)
537+
api.delete_watchlist(watchlist_id)
538+
539+
335540
def test_errors(reqmock):
336541
api = tradeapi.REST('key-id', 'secret-key', api_version='v1')
337542

0 commit comments

Comments
 (0)