import nickmab.async_util.web as w
from _utils import expect_specific_err as _expect_specific_err

'''To be run by executing py.test in the parent dir'''

def test_json_query_pool():
    q = { 
        'ip': 'http://ip.jsontest.com/',
        'headers': 'http://headers.jsontest.com/',
    }
    p = w.JSONQueryPool(1, q)
    assert not p.is_finished
    p.run()
    assert p.is_finished
    assert not p.has_exception
    assert isinstance(p.result['ip'], dict)
    assert isinstance(p.result['headers'], dict)

def test_json_query_pool_with_exception():
    q = {
        'nil': 'dslkfjlsdkfjlksd'
    }
    p = w.JSONQueryPool(1, q)
    p.run()
    assert p.has_exception

def test_json_query_pool_no_target():
    _expect_specific_err(w.JSONQueryPool, ValueError)    
    
def test_json_query_pool_wrong_type():
    _expect_specific_err(w.JSONQueryPool, TypeError, 
        kwargs={ 'num_worker_threads': 'fun', 'queries': { 's': 's' } })
    _expect_specific_err(w.JSONQueryPool, TypeError,
        kwargs={ 'queries': { 1: 'fun' } })
    _expect_specific_err(w.JSONQueryPool, TypeError,
        kwargs={ 'queries': { '1': 2 } })
    _expect_specific_err(w.JSONQueryPool, TypeError, kwargs={ 'queries': 123 })
