<commit_before>"""
Helioviewer Client tests
"""
from __future__ import absolute_import

#pylint: disable=C0103,R0904,W0201,W0212,W0232,E1103
import sunpy
import sunpy.map
import pytest
from sunpy.net.helioviewer import HelioviewerClient

from sunpy.tests.helpers import skip_glymur


@pytest.mark.online
class TestHelioviewerClient:
    """Tests the Helioviewer.org API Client class"""

    def setup_class(self):
        self.client = HelioviewerClient()
        if not self.client.is_online():
            self.__SKIP_TESTS__ = True
            print("Skipping Helioviewer.org tests (server inaccessible)")
        else:
            self.__SKIP_TESTS__ = False

        self.sources = self.client.get_data_sources()

    def teardown_class(self):
        self.client = None

    def test_get_datasources(self):
        """Makes sure datasource query returns a valid result and source id
        is casted to an integer"""
        #  we skip these tests like this if the HV api is offline because
        # we can not define __SKIP_TESTS__ in a scope that pytest.mark.skipif will find.
        if self.__SKIP_TESTS__:
            return
        assert type(self.sources['SDO']['AIA']['AIA']['171']['sourceId']) is int

    def test_get_closest_image(self):
        """Tests getClosestImage API method"""
        if self.__SKIP_TESTS__:
            return
        # check basic query
        im1 = self.client.get_closest_image('1994/01/01',
                                              observatory='SOHO',
                                              instrument='EIT',
                                              detector='EIT',
                                              measurement='195')
        assert im1['width'] == im1['height'] == 1024

        # result should be same when using source id to query
        source_id = self.sources['SOHO']['EIT']['EIT']['195']['sourceId']

        im2 = self.client.get_closest_image('1994/01/01', sourceId=source_id)

        assert im1 == im2

    @skip_glymur
    def test_download_jp2(self):
        """Tests getJP2Image API method"""
        if self.__SKIP_TESTS__:
            return
        filepath = self.client.download_jp2('2020/01/01', observatory='SOHO',
                                            instrument='MDI', detector='MDI',
                                            measurement='continuum')
        map_ = sunpy.map.Map(filepath)
        assert isinstance(map_, sunpy.map.GenericMap)<commit_msg>Using fixtures on Helioviewer tests<commit_after>"""
Helioviewer Client tests
"""
from __future__ import absolute_import

#pylint: disable=C0103,R0904,W0201,W0212,W0232,E1103
import sunpy
import sunpy.map
import pytest
from sunpy.net.helioviewer import HelioviewerClient
from sunpy.extern.six.moves import urllib

from sunpy.tests.helpers import skip_glymur


@pytest.fixture(scope="function")
def client():
    """
    Fixture to create a client and skip tests if not available
    """
    try:
        client = HelioviewerClient()
        client.sources = client.get_data_sources()
        return client
    except (urllib.error.HTTPError), e:
        print("There's a HTTP problem {} {}".format(e.code, e.args))
        pytest.skip("HTTP error {}".format(e.code))

@pytest.mark.online
class TestHelioviewerClient:
    """Tests the Helioviewer.org API Client class"""

    def test_get_datasources(self, client):
        """Makes sure datasource query returns a valid result and source id
        is casted to an integer"""
        assert type(client.sources['SDO']['AIA']['AIA']['171']['sourceId']) is int

    def test_get_closest_image(self, client):
        """Tests getClosestImage API method"""
        # check basic query
        im1 = client.get_closest_image('1994/01/01',
                                       observatory='SOHO',
                                       instrument='EIT',
                                       detector='EIT',
                                       measurement='195')
        assert im1['width'] == im1['height'] == 1024

        # result should be same when using source id to query
        source_id = client.sources['SOHO']['EIT']['EIT']['195']['sourceId']

        im2 = client.get_closest_image('1994/01/01', sourceId=source_id)

        assert im1 == im2

    @skip_glymur
    def test_download_jp2(self, client):
        """Tests getJP2Image API method"""
        filepath = client.download_jp2('2020/01/01', observatory='SOHO',
                                       instrument='MDI', detector='MDI',
                                       measurement='continuum')
        map_ = sunpy.map.Map(filepath)
        assert isinstance(map_, sunpy.map.GenericMap)
{'fraction_non_alphanumeric': 0.10515067322077366, 'fraction_numerical': 0.02992092327420389, 'mean_word_length': 2.19672131147541, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 22, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1991611', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1332, 'n_words': 320}