import logging

import pandas as pd
import pandasdmx as sdmx
import pytest


def test_open_file():
    # Can't infer message type for unknown file extension
    with pytest.raises(RuntimeError):
        sdmx.open_file('foo.badsuffix')


def test_request():
    # Constructor
    r = sdmx.Request(log_level=logging.ERROR)

    # Invalid agency name (replaces former test_request.py)
    with pytest.raises(ValueError):
        sdmx.Request('noagency')

    # Regular methods
    r.clear_cache()

    r.timeout = 300
    assert r.timeout == 300


@pytest.mark.remote_data
def test_request_get():
    req = sdmx.Request('ESTAT')
    req.get('datastructure', params={'foo': 'bar'}, dry_run=True)

    # Test Request._make_key_from_dsd()
    req.data('une_rt_a', key={'GEO': 'EL+ES+IE'},
             params={'startPeriod': '2007'})

    with pytest.warns(UserWarning, match='agency argument is redundant'):
        req.data('une_rt_a', key={'GEO': 'EL+ES+IE'},
                 params={'startPeriod': '2007'},
                 agency='ESTAT')

    # Using an unknown endpoint is an exception
    with pytest.raises(ValueError):
        req.get('badendpoint', 'id')

    # TODO test req.get(obj) with all IdentifiableArtefact subclasses
    # TODO test req.get(..., validate=False)


@pytest.mark.remote_data
def test_request_make_series_key():
    req = sdmx.Request('ECB')  # noqa: F841

    # TODO add a test that exercises _make_key_from_series
    # req.data('une_rt_a', key={'GEO': 'EL+ES+IE'},
    #          params={'startPeriod': '2007'})


@pytest.mark.remote_data
def test_request_url():
    # URL can be queried without instantiating Request
    sdmx.Request.url('http://sdw-wsrest.ecb.int/service/datastructure/ECB/'
                     'ECB_EXR1/latest?references=all')


@pytest.mark.remote_data
def test_request_preview_data():
    req = sdmx.Request('ECB')

    # List of keys can be retrieved
    keys = req.preview_data('EXR')
    assert isinstance(keys, list)

    # Count of keys can be determined
    assert len(keys) == 4291

    # A filter can be provided, resulting in fewer keys
    keys = req.preview_data('EXR', {'CURRENCY': 'CAD+CHF+CNY'})
    assert len(keys) == 24

    # Result can be converted to pandas object
    keys_pd = sdmx.to_pandas(keys)
    assert isinstance(keys_pd, pd.DataFrame)
    assert len(keys_pd) == 24

@@ -583,16 +583,21 @@
 uest_get
+_args
 ():%0A    
@@ -628,210 +628,649 @@
 T')%0A
+%0A
     
-req.get('datastructure', params=%7B'foo': 'bar'%7D, dry_run=True)%0A%0A    # Test Request._make_key_from_dsd()%0A    req.data('une_rt_a', key=%7B'GEO': 'EL+ES+IE'%7D,%0A             params=%7B'startPeriod': '2007'%7D)%0A
+# Request._make_key accepts '+'-separated values%0A    args = dict(resource_id='une_rt_a', key=%7B'GEO': 'EL+ES+IE'%7D,%0A                params=%7B'startPeriod': '2007'%7D, dry_run=True, use_cache=True)%0A    # Store the URL%0A    url = req.data(**args).url%0A%0A    # Using an iterable of key values gives the same URL%0A    args%5B'key'%5D = %7B'GEO': %5B'EL', 'ES', 'IE'%5D%7D%0A    assert req.data(**args).url == url%0A%0A    # Using a direct string for a key gives the same URL%0A    args%5B'key'%5D = '....EL+ES+IE'  # No specified values for first 4 dimensions%0A    assert req.data(**args).url == url%0A%0A    # Giving 'provider' is redundant for a data request, causes a warning
 %0A   
@@ -1311,15 +1311,19 @@
 tch=
-'agency
+%22'provider'
  arg
@@ -1340,17 +1340,17 @@
 edundant
-'
+%22
 ):%0A     
@@ -1464,22 +1464,24 @@
         
-agency
+provider
 ='ESTAT'
@@ -1643,12 +1643,8 @@
 ith 
-all 
 Iden
@@ -1675,319 +1675,8 @@
 ses%0A
-    # TODO test req.get(..., validate=False)%0A%0A%0A@pytest.mark.remote_data%0Adef test_request_make_series_key():%0A    req = sdmx.Request('ECB')  # noqa: F841%0A%0A    # TODO add a test that exercises _make_key_from_series%0A    # req.data('une_rt_a', key=%7B'GEO': 'EL+ES+IE'%7D,%0A    #          params=%7B'startPeriod': '2007'%7D)%0A
 %0A%0A@p

Test Request.get(key=) formats