Exemple #1
0
def test_request_json__post_parameters(mock_request):
    mock_request.side_effect = Session().request
    data = {"apple": "pie", "orange": None}
    request_json(url="http://google.com", body=data, parameters=data)
    _, kwargs = mock_request.call_args
    assert kwargs["method"] == "POST"
    assert kwargs["params"] == d2l(clean_dict(data))
Exemple #2
0
def omdb_search(api_key, query, year=None, media_type=None, page=1, cache=True):
    """
    Search for media using the Open Movie Database.

    Online docs: http://www.omdbapi.com/#parameters.
    """
    if media_type and media_type not in OMDB_MEDIA_TYPES:
        raise MapiProviderException(
            "media_type must be one of %s" % ",".join(OMDB_MEDIA_TYPES)
        )
    if 1 > page > 100:
        raise MapiProviderException("page must be between 1 and 100")
    url = "http://www.omdbapi.com"
    parameters = {
        "apikey": api_key,
        "s": query,
        "y": year,
        "type": media_type,
        "page": page,
    }
    parameters = clean_dict(parameters)
    status, content = request_json(url, parameters, cache=cache)
    if status == 401:
        raise MapiProviderException("invalid API key")
    elif content and not content.get("totalResults"):
        raise MapiNotFoundException()
    elif not content or status != 200:  # pragma: no cover
        raise MapiNetworkException("OMDb down or unavailable?")
    return content
Exemple #3
0
def test_clean_dict__str_strip():
    dict_in = {
        "please": ".",
        "fix ": ".",
        " my spacing": ".",
        "  issues  ": ".",
    }
    dict_expect = {"please": ".", "fix": ".", "my spacing": ".", "issues": "."}
    dict_out = clean_dict(dict_in)
    assert dict_expect == dict_out
Exemple #4
0
def test_clean_dict__some_none():
    dict_in = {
        "super": "mario",
        "sonic": "hedgehog",
        "samus": None,
        "princess": "zelda",
        "bowser": None,
    }
    dict_expect = {"super": "mario", "sonic": "hedgehog", "princess": "zelda"}
    dict_out = clean_dict(dict_in)
    assert dict_expect == dict_out
Exemple #5
0
def omdb_title(
    api_key,
    id_imdb=None,
    media_type=None,
    title=None,
    season=None,
    episode=None,
    year=None,
    plot=None,
    cache=True,
):
    """
    Lookup media using the Open Movie Database.

    Online docs: http://www.omdbapi.com/#parameters
    """
    if (not title and not id_imdb) or (title and id_imdb):
        raise MapiProviderException("either id_imdb or title must be specified")
    elif media_type and media_type not in OMDB_MEDIA_TYPES:
        raise MapiProviderException(
            "media_type must be one of %s" % ",".join(OMDB_MEDIA_TYPES)
        )
    elif plot and plot not in OMDB_PLOT_TYPES:
        raise MapiProviderException(
            "plot must be one of %s" % ",".join(OMDB_PLOT_TYPES)
        )
    url = "http://www.omdbapi.com"
    parameters = {
        "apikey": api_key,
        "i": id_imdb,
        "t": title,
        "y": year,
        "season": season,
        "episode": episode,
        "type": media_type,
        "plot": plot,
    }
    parameters = clean_dict(parameters)
    status, content = request_json(url, parameters, cache=cache)
    error = content.get("Error") if isinstance(content, dict) else None
    if status == 401:
        raise MapiProviderException("invalid API key")
    elif status != 200 or not isinstance(content, dict):
        raise MapiNetworkException("OMDb down or unavailable?")
    elif error:
        raise MapiNotFoundException(error)
    return content
Exemple #6
0
def test_clean_dict__whitelist():
    whitelist = {"apple", "raspberry", "pecan"}
    dict_in = {"apple": "pie", "pecan": "pie", "pumpkin": "pie"}
    dict_out = {"apple": "pie", "pecan": "pie"}
    assert clean_dict(dict_in, whitelist) == dict_out
Exemple #7
0
def test_clean_dict__not_a_dict():
    with pytest.raises(AssertionError):
        clean_dict("mama mia pizza pie")
Exemple #8
0
def test_clean_dict__int_values():
    dict_in = {"0": 0, "1": 1, "2": 2, "3": 3, "4": 4}
    dict_expect = {"0": "0", "1": "1", "2": "2", "3": "3", "4": "4"}
    dict_out = clean_dict(dict_in)
    assert dict_expect == dict_out
Exemple #9
0
def test_clean_dict__all_falsy():
    dict_in = {"who": None, "let": 0, "the": False, "dogs": [], "out": ()}
    dict_expect = {"let": "0", "the": "False"}
    dict_out = clean_dict(dict_in)
    assert dict_expect == dict_out
Exemple #10
0
def test_clean_dict__str_values():
    dict_in = {"apple": "pie", "candy": "corn", "bologna": "sandwich"}
    dict_out = clean_dict(dict_in)
    assert dict_in == dict_out