Skip to content

Commit

Permalink
Skip Nasdaq Data Link test if token not provided (backport #199) (#200)
Browse files Browse the repository at this point in the history
* Skip Nasdaq Data Link test if token not provided (#199)

(cherry picked from commit 71c190c)

* fix spaces

Co-authored-by: Manoel Marques <manoelmrqs@gmail.com>
Co-authored-by: Manoel Marques <manoel.marques@ibm.com>
  • Loading branch information
3 people authored Aug 29, 2022
1 parent 44bb936 commit ac84db7
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 17 deletions.
3 changes: 3 additions & 0 deletions qiskit_finance/data_providers/exchange_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ def run(self) -> None:
except nasdaqdatalink.AuthenticationError as ex:
logger.debug(ex, exc_info=True)
raise QiskitFinanceError("Nasdaq Data Link invalid token.") from ex
except nasdaqdatalink.LimitExceededError as ex:
logger.debug(ex, exc_info=True)
raise QiskitFinanceError("Nasdaq Data Link limit exceeded.") from ex
except nasdaqdatalink.NotFoundError as ex:
logger.debug(ex, exc_info=True)
stocks_notfound.append(name)
Expand Down
3 changes: 3 additions & 0 deletions qiskit_finance/data_providers/wikipedia_data_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def run(self) -> None:
except nasdaqdatalink.AuthenticationError as ex:
logger.debug(ex, exc_info=True)
raise QiskitFinanceError("Nasdaq Data Link invalid token.") from ex
except nasdaqdatalink.LimitExceededError as ex:
logger.debug(ex, exc_info=True)
raise QiskitFinanceError("Nasdaq Data Link limit exceeded.") from ex
except nasdaqdatalink.NotFoundError as ex:
logger.debug(ex, exc_info=True)
stocks_notfound.append(name)
Expand Down
6 changes: 4 additions & 2 deletions test/circuit/test_european_call_delta_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from test import QiskitFinanceTestCase

import numpy as np
import qiskit
from qiskit.circuit.library import IntegerComparator
from qiskit.quantum_info import Operator
from qiskit.utils import QuantumInstance, optionals
Expand Down Expand Up @@ -95,8 +94,11 @@ def test_application(self):
)

# run amplitude estimation
import importlib

aer = importlib.import_module("qiskit.providers.aer")
q_i = QuantumInstance(
qiskit.providers.aer.Aer.get_backend("aer_simulator"),
aer.Aer.get_backend("aer_simulator"),
seed_simulator=125,
seed_transpiler=80,
)
Expand Down
6 changes: 4 additions & 2 deletions test/circuit/test_european_call_pricing_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
from test import QiskitFinanceTestCase

import numpy as np
import qiskit
from qiskit.utils import algorithm_globals, QuantumInstance, optionals
from qiskit.algorithms import IterativeAmplitudeEstimation, EstimationProblem
from qiskit.circuit.library import LinearAmplitudeFunction, TwoLocal
Expand Down Expand Up @@ -102,8 +101,11 @@ def test_application(self):
post_processing=european_call.post_processing,
)

import importlib

aer = importlib.import_module("qiskit.providers.aer")
q_i = QuantumInstance(
qiskit.providers.aer.Aer.get_backend("aer_simulator"),
aer.Aer.get_backend("aer_simulator"),
seed_simulator=125,
seed_transpiler=80,
)
Expand Down
6 changes: 4 additions & 2 deletions test/circuit/test_fixed_income_pricing_objective.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import numpy as np

import qiskit
from qiskit import QuantumCircuit
from qiskit.utils import QuantumInstance, optionals
from qiskit.algorithms import IterativeAmplitudeEstimation, EstimationProblem
Expand Down Expand Up @@ -89,8 +88,11 @@ def test_application(self):
)

# run simulation
import importlib

aer = importlib.import_module("qiskit.providers.aer")
q_i = QuantumInstance(
qiskit.providers.aer.Aer.get_backend("aer_simulator"),
aer.Aer.get_backend("aer_simulator"),
seed_simulator=2,
seed_transpiler=2,
)
Expand Down
35 changes: 24 additions & 11 deletions test/data_providers/test_data_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import datetime
from test import QiskitFinanceTestCase
from ddt import ddt, data, unpack
import nasdaqdatalink
import numpy as np
from qiskit_finance import QiskitFinanceError
from qiskit_finance.data_providers import (
Expand All @@ -39,9 +40,11 @@ class TestDataProviders(QiskitFinanceTestCase):
def setUp(self):
super().setUp()
self._nasdaq_data_link_api_key = (
os.getenv("NASDAQ_DATA_LINK_API_KEY") if os.getenv("NASDAQ_DATA_LINK_API_KEY") else ""
os.getenv("NASDAQ_DATA_LINK_API_KEY") if os.getenv("NASDAQ_DATA_LINK_API_KEY") else None
)
self._on_demand_token = (
os.getenv("ON_DEMAND_TOKEN") if os.getenv("ON_DEMAND_TOKEN") else None
)
self._on_demand_token = os.getenv("ON_DEMAND_TOKEN") if os.getenv("ON_DEMAND_TOKEN") else ""
warnings.filterwarnings(action="ignore", message="unclosed", category=ResourceWarning)
warnings.filterwarnings(action="ignore", module="urllib3", category=DeprecationWarning)

Expand Down Expand Up @@ -140,15 +143,19 @@ def test_wikipedia(self):
wiki.get_similarity_matrix(), similarity, decimal=3
)
except QiskitFinanceError as ex:
self.skipTest(f"Test of WikipediaDataProvider skipped: {str(ex)}")
# The trouble for automating testing is that after 50 tries
# from one IP address within a day
# Nasdaq Data Link complains about the free usage tier limits.
# This gets "dressed" as QiskitFinanceError.
# This also introduces a couple of seconds of a delay.
if isinstance(ex.__cause__, nasdaqdatalink.LimitExceededError):
self.skipTest(f"Test of WikipediaDataProvider skipped: {str(ex)}")
else:
self.fail(f"Test of WikipediaDataProvider failed: {str(ex)}")

def test_nasdaq(self):
"""nasdaq test"""
if self._on_demand_token is None:
self.skipTest(
"Skipped NASDAQ On Demand test since token was not provided in "
"env. variable ON_DEMAND_TOKEN"
)
return
try:
nasdaq = DataOnDemandProvider(
token=self._on_demand_token,
Expand All @@ -158,10 +165,16 @@ def test_nasdaq(self):
)
nasdaq.run()
except QiskitFinanceError as ex:
self.skipTest(f"Test of DataOnDemandProvider skipped {str(ex)}")
self.fail(f"Test of DataOnDemandProvider failed: {str(ex)}")

def test_exchangedata(self):
"""exchange data test"""
if self._nasdaq_data_link_api_key is None:
self.skipTest(
"Skipped NASDAQ Data Link test since token was not provided in "
"env. variable NASDAQ_DATA_LINK_API_KEY"
)
return
try:
lse = ExchangeDataProvider(
token=self._nasdaq_data_link_api_key,
Expand All @@ -184,7 +197,7 @@ def test_exchangedata(self):
lse.get_similarity_matrix(), similarity, decimal=3
)
except QiskitFinanceError as ex:
self.skipTest(f"Test of ExchangeDataProvider skipped {str(ex)}")
self.fail(f"Test of ExchangeDataProvider failed: {str(ex)}")

@data(
[["AEO", "AEP"], [[7.0, 1.0], [1.0, 15.0]], [[1.0e00, 9.2e-05], [9.2e-05, 1.0e00]]],
Expand All @@ -209,7 +222,7 @@ def test_yahoo(self, tickers, covariance, similarity):
yahoo.get_similarity_matrix(), np.array(similarity), decimal=1
)
except QiskitFinanceError as ex:
self.skipTest(f"Test of YahooDataProvider skipped {str(ex)}")
self.fail(f"Test of YahooDataProvider failed: {str(ex)}")


if __name__ == "__main__":
Expand Down

0 comments on commit ac84db7

Please sign in to comment.