def test_look_up_product(MockOAuthSession): """test_look_up_product(MockOAuthSession) param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session 3 tests based on resp_format = (None,'xml') test exception raised when resp_format is something different from two choices """ response = {"symbol": "MMM", "description": "3M CO COM", "type": "EQUITY"} XML_response = r"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <LookupResponse> <Data><symbol>MMM</symbol> <description>3M CO COM</description> <type>EQUITY</type></Data> </LookupResponse>""" # Set Mock returns for resp_format=xml MockOAuthSession().get().text = XML_response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=False) # Test Get Quote returning python dict resp = mark.look_up_product("mmm") assert isinstance(resp, dict) assert MockOAuthSession().get.called # Set Mock returns for resp_format=json MockOAuthSession().get().json.return_value = response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=False) # Test Get Quote returning python dict resp = mark.look_up_product("mmm", resp_format="json") assert isinstance(resp, dict) assert MockOAuthSession().get.called
def test_get_option_expire_date(MockOAuthSession): """test_get_optionexpiredate(MockOAuthSession) param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session """ # response = [dt.date(2019, 1, 18), dt.date(2019, 1, 25)] XML_response = (r'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' r"<ExpirationDate></ExpirationDate>") # Set Mock returns for resp_format=None MockOAuthSession().get().text = XML_response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=False) resp = mark.get_option_expire_date("AAPL", resp_format="xml") assert isinstance(resp, dict) assert MockOAuthSession().get.called MockOAuthSession().get().text = XML_response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=True) resp = mark.get_option_expire_date("AAPL", resp_format="xml") assert isinstance(resp, dict) assert MockOAuthSession().get.called
def test_get_option_chains(MockOAuthSession): """test_get_optionexpiredate(MockOAuthSession) param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session """ response = {"timeStamp": 1546546266, "bid": 41.55, "OptionGreeks": {"iv": 0.6716}} XML_response = r"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <OptionChainResponse><OptionPair><Call> <timeStamp>1546546266</timeStamp><bid>41.55</bid> <OptionGreeks><iv>0.435700</iv></OptionGreeks></Call> </OptionPair></OptionChainResponse> """ # Set Mock returns for resp_format=xml MockOAuthSession().get().text = XML_response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=False) resp = mark.get_option_chains( "AAPL", expiry_date=dt.date(2019, 2, 15), resp_format="xml" ) assert isinstance(resp, dict) assert MockOAuthSession().get.called # Set Mock returns for resp_format=xml and dev=True MockOAuthSession().get().text = XML_response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=True) resp = mark.get_option_chains( "AAPL", expiry_date=dt.date(2019, 2, 15), resp_format="xml" ) assert isinstance(resp, dict) assert MockOAuthSession().get.called # Set Mock returns for resp_format=json MockOAuthSession().get().json.return_value = response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=False) resp = mark.get_option_chains( "AAPL", expiry_date=dt.date(2019, 2, 15), resp_format="json" ) assert isinstance(resp, dict) assert MockOAuthSession().get.called # Set Mock returns for resp_format=xml MockOAuthSession().get().json.return_value = response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=True) resp = mark.get_option_chains( "AAPL", expiry_date=dt.date(2019, 2, 15), resp_format="xml" ) assert isinstance(resp, dict) assert MockOAuthSession().get.called
def test_get_option_chain_exceptions(self, MockOAuthSession): '''test_get_quote(MockOAuthSession) -> None param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session''' mark = market.ETradeMarket('abc123', 'xyz123', 'abctoken', 'xyzsecret') # Test exception classes with self.assertRaises(etrade_exception.OptionChainBadMonthException): mark.get_option_chain( 111, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = True ) with self.assertRaises(etrade_exception.OptionChainBadYearException): mark.get_option_chain( 9, 2008, 'GOOG', chain_type = OptionType.BOTH, dev = True ) with self.assertRaises(etrade_exception.OptionChainBadOptionTypeException): mark.get_option_chain( 9, 2018, 'GOOG', chain_type = "Blarg", dev = True ) with self.assertRaises(etrade_exception.OptionChainBadBoolParamException): mark.get_option_chain( 9, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = "Blarg" ) with self.assertRaises(etrade_exception.OptionChainBadBoolParamException): mark.get_option_chain( 9, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = True, keep_skip_adjusted = "Blarg" ) with self.assertRaises(etrade_exception.OptionChainBadRespStringException): mark.get_option_chain( 9, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = True, resp_format = "Blarg" )
def test_look_up_product(self, MockOAuthSession): '''test_look_up_product(MockOAuthSession) -> None param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session''' # Set Mock returns MockOAuthSession().get().json.return_value = "{'BAC': '32.10'}" MockOAuthSession().get().text = r'<xml> returns </xml>' mark = market.ETradeMarket('abc123', 'xyz123', 'abctoken', 'xyzsecret') # Test Dev Get Quote self.assertEqual( mark.look_up_product('Bank Of', 'EQ'), "{'BAC': '32.10'}" ) self.assertTrue(MockOAuthSession().get().json.called) self.assertTrue(MockOAuthSession().get.called) # Test Dev Get Qoute xml self.assertEqual(mark.look_up_product( 'Back Of', 'EQ', resp_format='xml'), r"<xml> returns </xml>" ) self.assertTrue(MockOAuthSession().get.called) # Test Prod Get Qoute self.assertEqual(mark.look_up_product( 'Bank Of', 'EQ', dev=False), "{'BAC': '32.10'}" ) self.assertTrue(MockOAuthSession().get().json.called) self.assertTrue(MockOAuthSession().get.called) # Test prod Get Qoute xml self.assertEqual(mark.look_up_product( 'Back Of', 'EQ', resp_format='xml', dev=False), r"<xml> returns </xml>" ) self.assertTrue(MockOAuthSession().get.called)
def test_look_up_product(self, MockOAuthSession): '''test_look_up_product(MockOAuthSession) -> None param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session ''' response = {'BAC': '32.10'} # Set Mock returns MockOAuthSession().get().json.return_value = response MockOAuthSession().get().text = XML_response mark = market.ETradeMarket('abc123', 'xyz123', 'abctoken', 'xyzsecret', dev=False) # Test Get Quote JSON self.assertEqual(mark.look_up_product('Bank Of', 'EQ'), response) self.assertTrue(MockOAuthSession().get().json.called) self.assertTrue(MockOAuthSession().get.called) # Test Get Quote xml self.assertEqual( mark.look_up_product('Back Of', 'EQ', resp_format='xml'), XML_response) self.assertTrue(MockOAuthSession().get.called)
def test_get_option_chain(self, MockOAuthSession): '''test_get_quote(MockOAuthSession) -> None param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session''' # Set Mock returns def f( a, b): pass # expected internal composition of a paylod for GOOG # based on the calling params used in the calls to #get_option_chain below. Change these if you change #the invocation of get_option_chain below. payload = {'chainType': OptionType.BOTH, 'expirationMonth': 1, 'expirationYear': 2018, 'underlier': "GOOG", 'skipAdjusted': True, } MockOAuthSession().get().json.return_value = "{'BAC': '32.10'}" MockOAuthSession().get().text = r'<xml> returns </xml>' mark = market.ETradeMarket('abc123', 'xyz123', 'abctoken', 'xyzsecret') self.assertEqual(mark.get_option_chain(1, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = True), "{'BAC': '32.10'}") self.assertTrue(MockOAuthSession().get().json.called) self.assertTrue(MockOAuthSession().get.called) # Test prod get_option_chain self.assertEqual(mark.get_option_chain(1, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = False), "{'BAC': '32.10'}") self.assertTrue(MockOAuthSession().get().json.called) # MockOAuthSession().get().assert_called_with( # r'https://etws.etrade.com/'\ # r'market/sandbox/rest/optionchains.json', \ # params = payload) self.assertEqual(mark.get_option_chain(1, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = True), "{'BAC': '32.10'}") self.assertTrue(MockOAuthSession().get().json.called) # MockOAuthSession().get.assert_called_with( # r'https://etwssandbox.etrade.com/'\ # r'market/sandbox/rest/optionchains.json', # params = payload) # Test xml self.assertEqual(mark.get_option_chain(1, 2018, 'GOOG', chain_type = OptionType.BOTH, dev = True, resp_format = 'xml'), r"<xml> returns </xml>") self.assertTrue(MockOAuthSession().get.called)
def test_get_quote_exception(self, MockOAuthSession): '''test_get_quote(MockOAuthSession) -> None param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session''' # Generate symboles sym = [''.join(random.choice(string.ascii_uppercase) \ for _ in range(3)) for _ in range(30)] mark = market.ETradeMarket('abc123', 'xyz123', 'abctoken', 'xyzsecret') # Test exception class with self.assertRaises(etrade_exception.MarketQuoteException): mark.get_quote(*sym)
def test_get_quote(MockOAuthSession): """test_get_quote(MockOAuthSession) param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session """ response = { "securityType": "EQ", "symbol": "MMM", "dateTimeUTC": 1546545180, "adjustedFlag": "false", "annualDividend": 0.0, "averageVolume": 3078683.0, } XML_response = r"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <QuoteResponse> <QuoteData> <dateTime>14:53:00 EST 01-03-2019</dateTime> <dateTimeUTC>1546545180</dateTimeUTC> <All> <adjustedFlag>false</adjustedFlag> <annualDividend>0.0</annualDividend> <averageVolume>3078683</averageVolume></All> <Product> <securityType>EQ</securityType> <symbol>MMM</symbol></Product> </QuoteData></QuoteResponse> """ # Set Mock returns for resp_format=None MockOAuthSession().get().text = XML_response MockOAuthSession().get().json.return_value = response mark = market.ETradeMarket("abc123", "xyz123", "abctoken", "xyzsecret", dev=False) resp = mark.get_quote(["MMM"]) assert isinstance(resp, dict) assert MockOAuthSession().get.called resp = mark.get_quote(["MMM"], resp_format="json") assert isinstance(resp, dict) assert MockOAuthSession().get.called
def test_get_quote(self, MockOAuthSession): '''test_get_quote(MockOAuthSession) -> None param: MockOAuthSession type: mock.MagicMock description: MagicMock of OAuth1Session ''' response = {'BAC': '32.10'} # Set Mock returns MockOAuthSession().get().json.return_value = response MockOAuthSession().get().text = XML_response mark = market.ETradeMarket('abc123', 'xyz123', 'abctoken', 'xyzsecret', dev=False) # Test prod Get Qoute self.assertEqual(mark.get_quote(['BAC']), response) self.assertTrue(MockOAuthSession().get().json.called) self.assertTrue(MockOAuthSession().get.called) # Test prod Get Qoute xml self.assertEqual(mark.get_quote(['BAC'], resp_format='xml'), XML_response) self.assertTrue(MockOAuthSession().get.called) # Test log message if more than 25 quotes are requested # Generate 30 symbols; response should only be 25 symbols sym = [ ''.join(random.choice(string.ascii_uppercase) for _ in range(3)) for _ in range(30) ] retn = {x: 32.1 for x in sym[:25]} MockOAuthSession().get().json.return_value = retn self.assertEqual(mark.get_quote(sym), retn) self.assertTrue(MockOAuthSession().get().json.called) self.assertTrue(MockOAuthSession().get.called)