<commit_before>"""Test for instrument properties in the pysat Instrument object and methods.

Note
----
Base class stored here, but tests inherited by test_instrument.py

"""

import datetime as dt
from importlib import reload
import logging
import numpy as np

import pandas as pds
import pytest

import pysat
from pysat.utils.time import filter_datetime_input


class InstPropertyTests(object):
    """Basic tests for `pysat.Instrument` properties.

    Note
    ----
    Inherited by classes in test_instrument.py.  Setup and teardown methods are
    specified there.

    See Also
    ---------
    `pysat.tests.test_instrument`

    """

    def test_basic_instrument_bad_keyword_init(self):
        """Check for error when instantiating with bad load keywords on init."""

        # Test that the correct error is raised
        with pytest.raises(ValueError) as verr:
            pysat.Instrument(platform=self.testInst.platform,
                             name=self.testInst.name, num_samples=10,
                             clean_level='clean',
                             unsupported_keyword_yeah=True)

        # Evaluate error message
        assert str(verr).find("unknown keyword supplied") > 0
        return

    @pytest.mark.parametrize('kwarg', ['supported_tags', 'start', 'stop',
                                       'freq', 'date_array', 'data_path'])
    def test_basic_instrument_reserved_keyword(self, kwarg):
        """Check for error when instantiating with reserved keywords.

        Parameters
        ----------
        kwarg : str
            Name of reserved keyword.

        """

        # Check that the correct error is raised
        with pytest.raises(ValueError) as err:
            pysat.Instrument(platform=self.testInst.platform,
                             name=self.testInst.name, num_samples=10,
                             clean_level='clean',
                             **{kwarg: '1s'})

        # Check that the error message is correct
        estr = ''.join(('Reserved keyword "', kwarg, '" is not ',
                        'allowed at instantiation.'))
        assert str(err).find(estr) >= 0
        return

    @pytest.mark.parametrize('attr', ['_test_download', '_test_download_ci',
                                      '_password_req'])
    @pytest.mark.parametrize('setting', [True, False])
    def test_basic_instrument_download_kwargs(self, attr, setting):
        """Check that download flags are appropriately set.

        Parameters
        ----------
        attr : str
            Name of test attribute to check.
        setting : bool
            Setting to test for attr.

        """

        inst_module = getattr(pysat.instruments,
                              '_'.join((self.testInst.platform,
                                        self.testInst.name)))
        # Update settings for this test
        setattr(inst_module, attr, {'': {'': setting}})
        self.testInst = pysat.Instrument(inst_module=inst_module)

        assert getattr(self.testInst, attr) is setting
        return

    def test_list_files(self):
        """Test that `inst.files.files` returns a pandas series."""

        files = self.testInst.files.files
        assert isinstance(files, pds.Series)
        return

    @pytest.mark.parametrize("remote_func,num", [('remote_file_list', 31),
                                                 ('remote_date_range', 2)])
    def test_remote_functions(self, remote_func, num):
        """Test simulated remote functions for valid list of files.

        Parameters
        ----------
        remote_func : str
            Name of remote function to test.
        num : int
            Number of filenames that should be retrieved by remote_func.
        """

        stop = self.ref_time + dt.timedelta(days=30)
        self.out = getattr(self.testInst, remote_func)(start=self.ref_time,
                                                       stop=stop)
        assert len(self.out) == num

        # Get index if a pds.Series is returned.
        if isinstance(self.out, pds.Series):
            self.out = self.out.index
        assert filter_datetime_input(self.out[0]) == self.ref_time
        assert filter_datetime_input(self.out[-1]) == stop
        return

    @pytest.mark.parametrize("file_bounds, non_default",
                             [(False, False), (True, False), (False, True),
                              (True, True)])
    def test_download_updated_files(self, caplog, file_bounds, non_default):
        """Test `download_updated_files` and default bounds are updated.<commit_msg>Apply suggestions from code review<commit_after>"""Test for instrument properties in the pysat Instrument object and methods.

Note
----
Base class stored here, but tests inherited by test_instrument.py

"""

import datetime as dt
from importlib import reload
import logging
import numpy as np

import pandas as pds
import pytest

import pysat
from pysat.utils.time import filter_datetime_input


class InstPropertyTests(object):
    """Basic tests for `pysat.Instrument` properties.

    Note
    ----
    Inherited by classes in test_instrument.py.  Setup and teardown methods are
    specified there.

    See Also
    ---------
    `pysat.tests.test_instrument`

    """

    def test_basic_instrument_bad_keyword_init(self):
        """Check for error when instantiating with bad load keywords on init."""

        # Test that the correct error is raised
        with pytest.raises(ValueError) as verr:
            pysat.Instrument(platform=self.testInst.platform,
                             name=self.testInst.name, num_samples=10,
                             clean_level='clean',
                             unsupported_keyword_yeah=True)

        # Evaluate error message
        assert str(verr).find("unknown keyword supplied") > 0
        return

    @pytest.mark.parametrize('kwarg', ['supported_tags', 'start', 'stop',
                                       'freq', 'date_array', 'data_path'])
    def test_basic_instrument_reserved_keyword(self, kwarg):
        """Check for error when instantiating with reserved keywords.

        Parameters
        ----------
        kwarg : str
            Name of reserved keyword.

        """

        # Check that the correct error is raised
        with pytest.raises(ValueError) as err:
            pysat.Instrument(platform=self.testInst.platform,
                             name=self.testInst.name, num_samples=10,
                             clean_level='clean',
                             **{kwarg: '1s'})

        # Check that the error message is correct
        estr = ''.join(('Reserved keyword "', kwarg, '" is not ',
                        'allowed at instantiation.'))
        assert str(err).find(estr) >= 0
        return

    @pytest.mark.parametrize('attr', ['_test_download', '_test_download_ci',
                                      '_password_req'])
    @pytest.mark.parametrize('setting', [True, False])
    def test_basic_instrument_download_kwargs(self, attr, setting):
        """Check that download flags are appropriately set.

        Parameters
        ----------
        attr : str
            Name of test attribute to check.
        setting : bool
            Setting to test for attr.

        """

        inst_module = getattr(pysat.instruments,
                              '_'.join((self.testInst.platform,
                                        self.testInst.name)))
        # Update settings for this test
        setattr(inst_module, attr, {'': {'': setting}})
        self.testInst = pysat.Instrument(inst_module=inst_module)

        assert getattr(self.testInst, attr) is setting
        return

    def test_list_files(self):
        """Test that `inst.files.files` returns a pandas series."""

        files = self.testInst.files.files
        assert isinstance(files, pds.Series)
        return

    @pytest.mark.parametrize("remote_func,num", [('remote_file_list', 31),
                                                 ('remote_date_range', 2)])
    def test_remote_functions(self, remote_func, num):
        """Test simulated remote functions for valid list of files.

        Parameters
        ----------
        remote_func : str
            Name of remote function to test.
        num : int
            Number of filenames that should be retrieved by remote_func.

        """

        stop = self.ref_time + dt.timedelta(days=30)
        self.out = getattr(self.testInst, remote_func)(start=self.ref_time,
                                                       stop=stop)
        assert len(self.out) == num

        # Get index if a pds.Series is returned.
        if isinstance(self.out, pds.Series):
            self.out = self.out.index
        assert filter_datetime_input(self.out[0]) == self.ref_time
        assert filter_datetime_input(self.out[-1]) == stop
        return

    @pytest.mark.parametrize("file_bounds, non_default",
                             [(False, False), (True, False), (False, True),
                              (True, True)])
    def test_download_updated_files(self, caplog, file_bounds, non_default):
        """Test `download_updated_files` and default bounds are updated.
{'fraction_non_alphanumeric': 0.09911274615884008, 'fraction_numerical': 0.003029647262497295, 'mean_word_length': 1.8956766917293233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 26, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1207338', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2405, 'n_words': 659}