# Path: pyhamtools/qsl.py
# def get_eqsl_users(**kwargs):
#     """Download the latest official list of `EQSL.cc`__ users. The list of users can be found here_.
# 
#         Args:
#             url (str, optional): Download URL
# 
#         Returns:
#             list: List containing the callsigns of EQSL users (unicode)
# 
#         Raises:
#             IOError: When network is unavailable, file can't be downloaded or processed
# 
#         Example:
#            The following example downloads the EQSL user list and checks if DH1TW is a user:
# 
#            >>> from pyhamtools.qsl import get_eqsl_users
#            >>> mylist = get_eqsl_users()
#            >>> try:
#            >>>    mylist.index('DH1TW')
#            >>> except ValueError as e:
#            >>>    print e
#            'DH1TW' is not in list
# 
#     .. _here: http://www.eqsl.cc/QSLCard/DownloadedFiles/AGMemberlist.txt
# 
#     """
# 
#     url = ""
# 
#     eqsl = []
# 
#     try:
#         url = kwargs['url']
#     except KeyError:
#         url = "http://www.eqsl.cc/QSLCard/DownloadedFiles/AGMemberlist.txt"
# 
#     try:
#         result = requests.get(url)
#     except (ConnectionError, HTTPError, Timeout) as e:
#         raise IOError(e)
# 
#     if result.status_code == requests.codes.ok:
#         eqsl = re.sub("^List.+UTC", "", result.text)
#         eqsl = eqsl.upper().split()
#     else:
#         raise IOError("HTTP Error: " + str(result.status_code))
# 
#     return eqsl


from past.builtins import execfile
from pyhamtools.qsl import get_eqsl_users
import os
import sys
import datetime
import pytest



if sys.version_info.major == 3:
    unicode = str

test_dir = os.path.dirname(os.path.abspath(__file__))
fix_dir = os.path.join(test_dir, 'fixtures')
class Test_eqsl_methods:

    def test_check_content_with_mocked_http_server(self, httpserver):
        httpserver.serve_content(open(os.path.join(fix_dir, 'eqsl_data.html'), 'rb').read(), headers={'content-type': 'text/plain; charset=ISO-8859-1'})

        namespace = {}
        execfile(os.path.join(fix_dir,"eqsl_data.py"), namespace)
        assert get_eqsl_users(url=httpserver.url) == namespace['eqsl_fixture']

    def test_download_lotw_list_and_check_types(self):

        data = get_eqsl_users()
        assert isinstance(data, list)
        for el in data:
            assert isinstance(el, unicode)
        assert len(data) > 1000

    def test_with_invalid_url(self):
        with pytest.raises(IOError):

# Path: pyhamtools/qsl.py
# def get_eqsl_users(**kwargs):
#     """Download the latest official list of `EQSL.cc`__ users. The list of users can be found here_.
# 
#         Args:
#             url (str, optional): Download URL
# 
#         Returns:
#             list: List containing the callsigns of EQSL users (unicode)
# 
#         Raises:
#             IOError: When network is unavailable, file can't be downloaded or processed
# 
#         Example:
#            The following example downloads the EQSL user list and checks if DH1TW is a user:
# 
#            >>> from pyhamtools.qsl import get_eqsl_users
#            >>> mylist = get_eqsl_users()
#            >>> try:
#            >>>    mylist.index('DH1TW')
#            >>> except ValueError as e:
#            >>>    print e
#            'DH1TW' is not in list
# 
#     .. _here: http://www.eqsl.cc/QSLCard/DownloadedFiles/AGMemberlist.txt
# 
#     """
# 
#     url = ""
# 
#     eqsl = []
# 
#     try:
#         url = kwargs['url']
#     except KeyError:
#         url = "http://www.eqsl.cc/QSLCard/DownloadedFiles/AGMemberlist.txt"
# 
#     try:
#         result = requests.get(url)
#     except (ConnectionError, HTTPError, Timeout) as e:
#         raise IOError(e)
# 
#     if result.status_code == requests.codes.ok:
#         eqsl = re.sub("^List.+UTC", "", result.text)
#         eqsl = eqsl.upper().split()
#     else:
#         raise IOError("HTTP Error: " + str(result.status_code))
# 
#     return eqsl


# Path: test/test_eqsl.py
from past.builtins import execfile
from pyhamtools.qsl import get_eqsl_users
import os
import sys
import datetime
import pytest




if sys.version_info.major == 3:
    unicode = str

test_dir = os.path.dirname(os.path.abspath(__file__))
fix_dir = os.path.join(test_dir, 'fixtures')
class Test_eqsl_methods:

    def test_check_content_with_mocked_http_server(self, httpserver):
        httpserver.serve_content(open(os.path.join(fix_dir, 'eqsl_data.html'), 'rb').read(), headers={'content-type': 'text/plain; charset=ISO-8859-1'})

        namespace = {}
        execfile(os.path.join(fix_dir,"eqsl_data.py"), namespace)
        assert get_eqsl_users(url=httpserver.url) == namespace['eqsl_fixture']

    def test_download_lotw_list_and_check_types(self):

        data = get_eqsl_users()
        assert isinstance(data, list)
        for el in data:
            assert isinstance(el, unicode)
        assert len(data) > 1000

    def test_with_invalid_url(self):
        with pytest.raises(IOError):

            get_eqsl_users(url="http://www.eqsl.cc/QSLCard/DownloadedFiles/AGMemberlist_my_unit_test.txt")