# Path: paperbroker/adapters/quotes/GoogleFinanceQuoteAdapter.py
# class GoogleFinanceQuoteAdapter(QuoteAdapter):
# 
#     def __init__(self):
#         self._cache = {}
#     def _set_cache(self, quote):
#         self._cache[quote.asset.symbol] = quote
#         return quote
# 
#     def get_quote(self, asset):
# 
#         asset = asset_factory(asset)
#         if self._cache.get(asset.symbol) is not None:
#             return self._cache.get(asset.symbol)
# 
#         if isinstance(asset, Option):
# 
#             options = self.get_options(asset.underlying, asset.expiration_date)
#             matches = [_ for _ in options if _.asset == asset]
#             if len(matches) == 0:
#                 raise Exception("GoogleFinanceAdapter.get_quote: No quote found for {}".format(asset.symbol))
#             return matches[0]
# 
#         else:
# 
#             google_quotes = getQuotes(asset.symbol)
# 
#             if google_quotes is None or len(google_quotes) == 0:
#                 raise Exception("GoogleFinanceAdapter.get_quote: No quote found for {}".format(asset.symbol))
# 
#             last_trade = google_quotes[0].get('LastTradeWithCurrency', None)
#             if last_trade is None or last_trade == '' or last_trade == '-':
#                 raise Exception("GoogleFinanceAdapter.get_quote: No quote found for {}".format(asset.symbol))
# 
#             return Quote(quote_date=arrow.now().format('YYYY-MM-DD'), asset=asset, bid=float(last_trade)-0.01, ask=float(last_trade)+0.01)
# 
#     def get_expiration_dates(self, underlying_asset=None):
#         oc = OptionChain('NASDAQ:' + asset_factory(underlying_asset).symbol)
#         return sorted(list(set([asset_factory(_['s']).expiration_date for _ in (oc.calls + oc.puts)])))
# 
#     def get_options(self, underlying_asset=None, expiration_date=None):
#         oc = OptionChain('NASDAQ:' + asset_factory(underlying_asset).symbol)
#         underlying_quote = self.get_quote(underlying_asset)
# 
#         out = []
#         for option in (oc.calls + oc.puts):
#             if arrow.get(expiration_date).format('YYMMDD') in option['s']:
#                 quote = OptionQuote(quote_date=arrow.now().format('YYYY-MM-DD'),
#                                 asset=option['s'],
#                                 bid=float(option['b']) if option['b'] != '-' else None,
#                                 ask=float(option['a']) if option['a'] != '-' else None,
#                                 underlying_price = underlying_quote.price)
#                 self._set_cache(quote)
#                 out.append(quote)
# 
#         return out


import unittest
from ..adapters.quotes import GoogleFinanceQuoteAdapter


class TestGoogleFinanceAdapter(unittest.TestCase):

    #    def __init__(self):

    #        super(TestGetQuotes, self).__init__()

    def test_get_quote(self):
        quote_adapter = GoogleFinanceQuoteAdapter()
        quote = quote_adapter.get_quote('GOOG')
        self.assertGreater(quote.price, 0)

    def test_get_expirations(self):

# Path: paperbroker/adapters/quotes/GoogleFinanceQuoteAdapter.py
# class GoogleFinanceQuoteAdapter(QuoteAdapter):
# 
#     def __init__(self):
#         self._cache = {}
#     def _set_cache(self, quote):
#         self._cache[quote.asset.symbol] = quote
#         return quote
# 
#     def get_quote(self, asset):
# 
#         asset = asset_factory(asset)
#         if self._cache.get(asset.symbol) is not None:
#             return self._cache.get(asset.symbol)
# 
#         if isinstance(asset, Option):
# 
#             options = self.get_options(asset.underlying, asset.expiration_date)
#             matches = [_ for _ in options if _.asset == asset]
#             if len(matches) == 0:
#                 raise Exception("GoogleFinanceAdapter.get_quote: No quote found for {}".format(asset.symbol))
#             return matches[0]
# 
#         else:
# 
#             google_quotes = getQuotes(asset.symbol)
# 
#             if google_quotes is None or len(google_quotes) == 0:
#                 raise Exception("GoogleFinanceAdapter.get_quote: No quote found for {}".format(asset.symbol))
# 
#             last_trade = google_quotes[0].get('LastTradeWithCurrency', None)
#             if last_trade is None or last_trade == '' or last_trade == '-':
#                 raise Exception("GoogleFinanceAdapter.get_quote: No quote found for {}".format(asset.symbol))
# 
#             return Quote(quote_date=arrow.now().format('YYYY-MM-DD'), asset=asset, bid=float(last_trade)-0.01, ask=float(last_trade)+0.01)
# 
#     def get_expiration_dates(self, underlying_asset=None):
#         oc = OptionChain('NASDAQ:' + asset_factory(underlying_asset).symbol)
#         return sorted(list(set([asset_factory(_['s']).expiration_date for _ in (oc.calls + oc.puts)])))
# 
#     def get_options(self, underlying_asset=None, expiration_date=None):
#         oc = OptionChain('NASDAQ:' + asset_factory(underlying_asset).symbol)
#         underlying_quote = self.get_quote(underlying_asset)
# 
#         out = []
#         for option in (oc.calls + oc.puts):
#             if arrow.get(expiration_date).format('YYMMDD') in option['s']:
#                 quote = OptionQuote(quote_date=arrow.now().format('YYYY-MM-DD'),
#                                 asset=option['s'],
#                                 bid=float(option['b']) if option['b'] != '-' else None,
#                                 ask=float(option['a']) if option['a'] != '-' else None,
#                                 underlying_price = underlying_quote.price)
#                 self._set_cache(quote)
#                 out.append(quote)
# 
#         return out


# Path: paperbroker/tests/test_google_finance_adapter.py
import unittest
from ..adapters.quotes import GoogleFinanceQuoteAdapter



class TestGoogleFinanceAdapter(unittest.TestCase):

    #    def __init__(self):

    #        super(TestGetQuotes, self).__init__()

    def test_get_quote(self):
        quote_adapter = GoogleFinanceQuoteAdapter()
        quote = quote_adapter.get_quote('GOOG')
        self.assertGreater(quote.price, 0)

    def test_get_expirations(self):

        quote_adapter = GoogleFinanceQuoteAdapter()