<commit_before>import pytest
from requests.exceptions import HTTPError

from erddapy import ERDDAP
from erddapy.utilities import parse_dates


def _url_to_dict(url):
    return {v.split("=")[0]: v.split("=")[1] for v in url.split("&")[1:]}


@pytest.fixture
@pytest.mark.web
def e():
    yield ERDDAP(
        server="https://upwell.pfeg.noaa.gov/erddap",
        protocol="tabledap",
        response="htmlTable",
    )


@pytest.fixture
def taodata(e):
    e.dataset_id = "pmelTao5dayIso"
    e.constraints = {
        "time>=": "1977-11-10T12:00:00Z",
        "time<=": "2019-01-26T12:00:00Z",
        "latitude>=": 10,
        "latitude<=": 10,
        "longitude>=": 265,
        "longitude<=": 265,
    }
    e.variables = ["ISO_6", "time"]
    yield e


@pytest.mark.web
def test_search_url_bad_request(e):
    """Test if a bad request returns HTTPError."""
    kw = {
        "min_time": "1700-01-01T12:00:00Z",
        "max_time": "1750-01-01T12:00:00Z",
    }
    with pytest.raises(HTTPError):
        e.get_search_url(**kw)


@pytest.mark.web
def test_search_url_valid_request(e):
    """Test if a bad request returns HTTPError."""
    min_time = "1800-01-01T12:00:00Z"
    max_time = "1950-01-01T12:00:00Z"
    kw = {"min_time": min_time, "max_time": max_time}
    url = e.get_search_url(**kw)
    assert url.startswith(f"{e.server}/search/advanced.{e.response}?")
    options = _url_to_dict(url)
    assert options.pop("minTime") == str(parse_dates(min_time))
    assert options.pop("maxTime") == str(parse_dates(max_time))
    assert options.pop("itemsPerPage") == str(1000)
    for k, v in options.items():
        if k == "protocol":
            assert v == e.protocol
        else:
            assert v == "(ANY)"


@pytest.mark.web
def test_info_url(e):
    """Check info URL results."""
    dataset_id = "gtoppAT"
    url = e.get_info_url(dataset_id=dataset_id)
    assert url == f"{e.server}/info/{dataset_id}/index.{e.response}"

    url = e.get_info_url(dataset_id=dataset_id, response="csv")
    assert url == f"{e.server}/info/{dataset_id}/index.csv"


@pytest.mark.web
def test_categorize_url(e):
    """Check categorize URL results."""
    categorize_by = "standard_name"
    url = e.get_categorize_url(categorize_by=categorize_by)
    assert url == f"{e.server}/categorize/{categorize_by}/index.{e.response}"

    url = e.get_categorize_url(categorize_by=categorize_by, response="csv")
    assert url == f"{e.server}/categorize/{categorize_by}/index.csv"


@pytest.mark.web<commit_msg>test new functionality<commit_after>import pytest

from requests.exceptions import HTTPError

from erddapy import ERDDAP
from erddapy.utilities import parse_dates


def _url_to_dict(url):
    return {v.split("=")[0]: v.split("=")[1] for v in url.split("&")[1:]}


@pytest.fixture
@pytest.mark.web
def e():
    yield ERDDAP(
        server="https://upwell.pfeg.noaa.gov/erddap",
        protocol="tabledap",
        response="htmlTable",
    )


@pytest.fixture
def taodata(e):
    e.dataset_id = "pmelTao5dayIso"
    e.constraints = {
        "time>=": "1977-11-10T12:00:00Z",
        "time<=": "2019-01-26T12:00:00Z",
        "latitude>=": 10,
        "latitude<=": 10,
        "longitude>=": 265,
        "longitude<=": 265,
    }
    e.variables = ["ISO_6", "time"]
    yield e


@pytest.mark.web
def test_search_url_bad_request(e):
    """Test if a bad request returns HTTPError."""
    kw = {
        "min_time": "1700-01-01T12:00:00Z",
        "max_time": "1750-01-01T12:00:00Z",
    }
    with pytest.raises(HTTPError):
        e.get_search_url(**kw)


@pytest.mark.web
def test_search_url_valid_request(e):
    """Test if a bad request returns HTTPError."""
    min_time = "1800-01-01T12:00:00Z"
    max_time = "1950-01-01T12:00:00Z"
    kw = {"min_time": min_time, "max_time": max_time}
    url = e.get_search_url(**kw)
    assert url.startswith(f"{e.server}/search/advanced.{e.response}?")
    options = _url_to_dict(url)
    assert options.pop("minTime") == str(parse_dates(min_time))
    assert options.pop("maxTime") == str(parse_dates(max_time))
    assert options.pop("itemsPerPage") == str(1000)
    for k, v in options.items():
        if k == "protocol":
            assert v == e.protocol
        else:
            assert v == "(ANY)"


@pytest.mark.web
def test_search_url_change_protocol(e):
    """Test if we change the protocol it show in the URL."""
    kw = {"search_for": "salinity"}
    url = e.get_search_url(protocol="tabledap", **kw)
    options = _url_to_dict(url)
    assert options.pop("protocol") == "tabledap"

    url = e.get_search_url(protocol="griddap", **kw)
    options = _url_to_dict(url)
    assert options.pop("protocol") == "griddap"

    e.protocol = None
    url = e.get_search_url(**kw)
    options = _url_to_dict(url)
    assert options.pop("protocol") == "(ANY)"


@pytest.mark.web
def test_info_url(e):
    """Check info URL results."""
    dataset_id = "gtoppAT"
    url = e.get_info_url(dataset_id=dataset_id)
    assert url == f"{e.server}/info/{dataset_id}/index.{e.response}"

    url = e.get_info_url(dataset_id=dataset_id, response="csv")
    assert url == f"{e.server}/info/{dataset_id}/index.csv"


@pytest.mark.web
def test_categorize_url(e):
    """Check categorize URL results."""
    categorize_by = "standard_name"
    url = e.get_categorize_url(categorize_by=categorize_by)
    assert url == f"{e.server}/categorize/{categorize_by}/index.{e.response}"

    url = e.get_categorize_url(categorize_by=categorize_by, response="csv")
    assert url == f"{e.server}/categorize/{categorize_by}/index.csv"


@pytest.mark.web
{'fraction_non_alphanumeric': 0.16318141197497765, 'fraction_numerical': 0.036818588025022345, 'mean_word_length': 3.4377478191911184, 'pattern_counts': {'":': 23, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1749488', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2166, 'n_words': 369}