# Path: hgvs/dataproviders/seqfetcher.py
# class SeqFetcher(object):
#     """This class is intended primarily as a mixin for HGVS data providers
#     that doen't otherwise have access to sequence data.  It uses the
#     fetch_seq() function in this module to fetch sequences from
#     several sources; see that function for details.
# 
#     >> sf = SeqFetcher()
# 
#     >> sf.fetch_seq('NP_056374.2',0,10)
#     'MESRETLSSS'
# 
#     """
# 
#     def __init__(self):
#         # If HGVS_SEQREPO_DIR is defined, we use seqrepo for *all* sequences.
#         # If HGVS_SEQREPO_URL is defined, use instance of seqrepo-rest-service for *all* sequences.
#         #   (see https://github.com/biocommons/seqrepo-rest-service for more info)
#         # Otherwise, we fall back to remote sequence fetching
#         seqrepo_dir = os.environ.get("HGVS_SEQREPO_DIR")
#         seqrepo_url = os.environ.get("HGVS_SEQREPO_URL")
#         if seqrepo_dir:
#             from biocommons.seqrepo import SeqRepo
#             self.sr = SeqRepo(seqrepo_dir)
# 
#             def _fetch_seq_seqrepo(ac, start_i=None, end_i=None):
#                 return self.sr.fetch(ac, start_i, end_i)
# 
#             self.fetcher = _fetch_seq_seqrepo
#             self.source = "SeqRepo ({})".format(seqrepo_dir)
#         elif seqrepo_url:
#             from biocommons.seqrepo.dataproxy import SeqRepoRESTDataProxy
#             self.sr = SeqRepoRESTDataProxy(seqrepo_url)
#             self.fetcher = lambda ac, start_i=None, end_i=None: self.sr.get_sequence(ac, start_i, end_i)
#             self.source = f"SeqRepo REST ({seqrepo_url})"
#         else:
#             self.sr = None
#             self.fetcher = bioutils.seqfetcher.fetch_seq
#             self.source = "bioutils.seqfetcher (network fetching)"
#         _logger.info("Fetching sequences with " + self.source)
# 
#     def fetch_seq(self, ac, start_i=None, end_i=None):
#         try:
#             return self.fetcher(ac, start_i, end_i)
#         except Exception as ex:
#             raise HGVSDataNotAvailableError("Failed to fetch {ac} from {self.source} ({ex})".format(
#                 ac=ac, ex=ex, self=self))


from os import path
from unittest.mock import patch
from biocommons.seqrepo.dataproxy import SeqRepoRESTDataProxy
from biocommons.seqrepo.seqrepo import SeqRepo
from bioutils import seqfetcher
from hgvs.dataproviders.seqfetcher import SeqFetcher




@patch.object(path, "exists")
def test_seqfetcher_initialized_with_seqrepo_dir(mock_path_exists, monkeypatch):
    mock_path_exists.return_value = True
    monkeypatch.setenv("HGVS_SEQREPO_DIR", "/tmp/my-seqrepo-location")
    with patch("sqlite3.connect") as mock_sqlite_connect:
        sf = SeqFetcher()
    mock_path_exists.assert_called_once_with("/tmp/my-seqrepo-location")
    assert mock_sqlite_connect.call_args[0][0] == "/tmp/my-seqrepo-location/aliases.sqlite3"
    assert isinstance(sf.sr, SeqRepo)
    assert sf.source == "SeqRepo (/tmp/my-seqrepo-location)"


def test_seqfetcher_initialized_with_seqrepo_url(monkeypatch):
    monkeypatch.setenv("HGVS_SEQREPO_URL", "http://localhost:5000/seqrepo")
    sf = SeqFetcher()
    assert isinstance(sf.sr, SeqRepoRESTDataProxy)
    # the URL should be versioned automatically for us
    assert sf.sr.base_url == "http://localhost:5000/seqrepo/1/"
    assert sf.source == "SeqRepo REST (http://localhost:5000/seqrepo)"


def test_seqfetcher_initialized_with_public_seqrepo_sources():

# Path: hgvs/dataproviders/seqfetcher.py
# class SeqFetcher(object):
#     """This class is intended primarily as a mixin for HGVS data providers
#     that doen't otherwise have access to sequence data.  It uses the
#     fetch_seq() function in this module to fetch sequences from
#     several sources; see that function for details.
# 
#     >> sf = SeqFetcher()
# 
#     >> sf.fetch_seq('NP_056374.2',0,10)
#     'MESRETLSSS'
# 
#     """
# 
#     def __init__(self):
#         # If HGVS_SEQREPO_DIR is defined, we use seqrepo for *all* sequences.
#         # If HGVS_SEQREPO_URL is defined, use instance of seqrepo-rest-service for *all* sequences.
#         #   (see https://github.com/biocommons/seqrepo-rest-service for more info)
#         # Otherwise, we fall back to remote sequence fetching
#         seqrepo_dir = os.environ.get("HGVS_SEQREPO_DIR")
#         seqrepo_url = os.environ.get("HGVS_SEQREPO_URL")
#         if seqrepo_dir:
#             from biocommons.seqrepo import SeqRepo
#             self.sr = SeqRepo(seqrepo_dir)
# 
#             def _fetch_seq_seqrepo(ac, start_i=None, end_i=None):
#                 return self.sr.fetch(ac, start_i, end_i)
# 
#             self.fetcher = _fetch_seq_seqrepo
#             self.source = "SeqRepo ({})".format(seqrepo_dir)
#         elif seqrepo_url:
#             from biocommons.seqrepo.dataproxy import SeqRepoRESTDataProxy
#             self.sr = SeqRepoRESTDataProxy(seqrepo_url)
#             self.fetcher = lambda ac, start_i=None, end_i=None: self.sr.get_sequence(ac, start_i, end_i)
#             self.source = f"SeqRepo REST ({seqrepo_url})"
#         else:
#             self.sr = None
#             self.fetcher = bioutils.seqfetcher.fetch_seq
#             self.source = "bioutils.seqfetcher (network fetching)"
#         _logger.info("Fetching sequences with " + self.source)
# 
#     def fetch_seq(self, ac, start_i=None, end_i=None):
#         try:
#             return self.fetcher(ac, start_i, end_i)
#         except Exception as ex:
#             raise HGVSDataNotAvailableError("Failed to fetch {ac} from {self.source} ({ex})".format(
#                 ac=ac, ex=ex, self=self))


# Path: tests/test_seqfetcher_initialization.py
from os import path
from unittest.mock import patch
from biocommons.seqrepo.dataproxy import SeqRepoRESTDataProxy
from biocommons.seqrepo.seqrepo import SeqRepo
from bioutils import seqfetcher
from hgvs.dataproviders.seqfetcher import SeqFetcher





@patch.object(path, "exists")
def test_seqfetcher_initialized_with_seqrepo_dir(mock_path_exists, monkeypatch):
    mock_path_exists.return_value = True
    monkeypatch.setenv("HGVS_SEQREPO_DIR", "/tmp/my-seqrepo-location")
    with patch("sqlite3.connect") as mock_sqlite_connect:
        sf = SeqFetcher()
    mock_path_exists.assert_called_once_with("/tmp/my-seqrepo-location")
    assert mock_sqlite_connect.call_args[0][0] == "/tmp/my-seqrepo-location/aliases.sqlite3"
    assert isinstance(sf.sr, SeqRepo)
    assert sf.source == "SeqRepo (/tmp/my-seqrepo-location)"


def test_seqfetcher_initialized_with_seqrepo_url(monkeypatch):
    monkeypatch.setenv("HGVS_SEQREPO_URL", "http://localhost:5000/seqrepo")
    sf = SeqFetcher()
    assert isinstance(sf.sr, SeqRepoRESTDataProxy)
    # the URL should be versioned automatically for us
    assert sf.sr.base_url == "http://localhost:5000/seqrepo/1/"
    assert sf.source == "SeqRepo REST (http://localhost:5000/seqrepo)"


def test_seqfetcher_initialized_with_public_seqrepo_sources():

    sf = SeqFetcher()