Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating tests to use skipif #217

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions images.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
from db import Session, SessionContext
from db.models import Link, BlacklistedLink
from settings import Settings
from utility import read_api_key
from utility import read_txt_api_key

# HTTP request timeout
QUERY_TIMEOUT = 4.0
Expand Down Expand Up @@ -138,7 +138,7 @@ def get_image_url(

if not jdoc:
# Not found in cache: prepare to ask Google
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
# No API key: can't ask for an image
logging.warning("No API key for image lookup")
Expand Down Expand Up @@ -310,7 +310,7 @@ def get_staticmap_image(
height: int = 180,
) -> Optional[BytesIO]:
"""Request image from Google Static Maps API, return image data as bytes."""
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
return None

Expand Down
7 changes: 4 additions & 3 deletions queries/atm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from speech.trans.num import number_to_text
from utility import QUERIES_RESOURCES_DIR

# TODO: fetch ATM data from a web service instead of a local file every once in a while
# TODO: Handle multiple ATMs in same location, but with different services
# TODO: "við" er ekki rétt í öllum tilfellum, td ætti að vera "í Norðurturni Smáralindar"

Expand Down Expand Up @@ -459,7 +460,7 @@ def _answ_for_atm_query(location: LatLonTuple, result: Result) -> AnswerTuple:
elif result.qkey == _ATM_FURTHER_INFO_OPENING_HOURS:
ans_start = "Hraðbankinn við "
opening_hours: str = atm_list[0]["opening_hours_text"].get("is", "")
if len(opening_hours) is not 0:
if len(opening_hours) != 0:
opening_hours = opening_hours[0].lower() + opening_hours[1:]
if atm_list[0]["always_open"] is True:
answ_fmt = "{0}{1} er alltaf opinn."
Expand All @@ -471,7 +472,7 @@ def _answ_for_atm_query(location: LatLonTuple, result: Result) -> AnswerTuple:
ans_start,
voice_street_name,
)
elif len(opening_hours) is not 0 and opening_hours.startswith("opnunartím"):
elif len(opening_hours) != 0 and opening_hours.startswith("opnunartím"):
answ_fmt = "{0}{1} fylgir {2}."
answer = answ_fmt.format(
ans_start,
Expand All @@ -483,7 +484,7 @@ def _answ_for_atm_query(location: LatLonTuple, result: Result) -> AnswerTuple:
voice_street_name,
NounPhrase(opening_hours).dative,
)
elif len(opening_hours) is not 0 and opening_hours.startswith("opið"):
elif len(opening_hours) != 0 and opening_hours.startswith("opið"):
answ_fmt = "{0}{1} er {2}."
opening_hours = opening_hours.replace("opið", "opinn")
index = opening_hours.find("daga") + 4
Expand Down
4 changes: 2 additions & 2 deletions queries/ja.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from queries import AnswerTuple, Query, QueryStateDict
from tree import Result, Node
from geo import iceprep_for_street
from utility import read_api_key, icequote
from utility import read_txt_api_key, icequote


# Module priority
Expand Down Expand Up @@ -81,7 +81,7 @@ def QJaPhoneNum4NameQuery(node: Node, params: QueryStateDict, result: Result) ->

def query_ja_api(q: str) -> Optional[Dict[str, Any]]:
"""Send query to ja.is API"""
key = read_api_key("JaServerKey")
key = read_txt_api_key("JaServerKey")
if not key:
# No key, can't query the API
logging.warning("No API key for ja.is")
Expand Down
6 changes: 3 additions & 3 deletions queries/play.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

from queries import Query
from queries.util import gen_answer
from utility import read_api_key
from utility import read_txt_api_key


class VideoIdDict(TypedDict):
Expand Down Expand Up @@ -80,7 +80,7 @@ def yt_api() -> Any:
"""Lazily instantiate YouTube API client."""
global _youtube_api
if not _youtube_api:
_youtube_api = Api(api_key=read_api_key("GoogleServerKey"))
_youtube_api = Api(api_key=read_txt_api_key("GoogleServerKey"))
if not _youtube_api:
logging.error("Unable to instantiate YouTube API client")
return _youtube_api
Expand Down Expand Up @@ -144,7 +144,7 @@ def find_youtube_playlists(q: str, limit: int = 3) -> List[str]:


def rand_yt_playlist_for_genre(
genre_name: str, limit: int = 5, fallback: Optional[str]=None
genre_name: str, limit: int = 5, fallback: Optional[str] = None
) -> Optional[str]:
"""Given a musical genre name, search for YouTube playlists and return a
URL to a randomly selected one, with an (optional) fallback video URL."""
Expand Down
12 changes: 6 additions & 6 deletions queries/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
QUERIES_GRAMMAR_DIR,
QUERIES_JS_DIR,
QUERIES_UTIL_GRAMMAR_DIR,
read_api_key,
read_txt_api_key,
)


Expand Down Expand Up @@ -442,7 +442,7 @@ def query_json_api(
def query_geocode_api_coords(lat: float, lon: float) -> Optional[Dict[str, Any]]:
"""Look up coordinates in Google's geocode API."""
# Load API key
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
# No key, can't query Google location API
logging.warning("No API key for coordinates lookup")
Expand All @@ -464,7 +464,7 @@ def query_geocode_api_coords(lat: float, lon: float) -> Optional[Dict[str, Any]]
def query_geocode_api_addr(addr: str) -> Optional[Dict[str, Any]]:
"""Look up address in Google's geocode API."""
# Load API key
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
# No key, can't query the API
logging.warning("No API key for address lookup")
Expand Down Expand Up @@ -498,7 +498,7 @@ def query_traveltime_api(
assert mode in _TRAVEL_MODES

# Load API key
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
# No key, can't query the API
logging.warning("No API key for travel time lookup")
Expand Down Expand Up @@ -534,7 +534,7 @@ def query_places_api(
fields = "place_id,opening_hours,geometry/location,formatted_address"

# Load API key
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
# No key, can't query the API
logging.warning("No API key for Google Places lookup")
Expand Down Expand Up @@ -570,7 +570,7 @@ def query_place_details(
https://developers.google.com/places/web-service/details"""

# Load API key
key = read_api_key("GoogleServerKey")
key = read_txt_api_key("GoogleServerKey")
if not key:
# No key, can't query the API
logging.warning("No API key for Google Place Details lookup")
Expand Down
4 changes: 2 additions & 2 deletions routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
)
from speech.voices import voice_for_locale
from queries.util.openai_gpt import summarize
from utility import read_api_key, icelandic_asciify
from utility import read_txt_api_key, icelandic_asciify

from . import routes, better_jsonify, text_from_request, bool_from_request
from . import MAX_URL_LENGTH, MAX_UUID_LENGTH
Expand Down Expand Up @@ -507,7 +507,7 @@ def _has_valid_api_key(req: Request, allow_query_param: bool = False) -> bool:
key = request.headers.get("Authorization", "")
if not key and allow_query_param:
key = cast(Dict[str, str], request.values).get("api_key", "")
gak = read_api_key("GreynirServerKey") # Cached
gak = read_txt_api_key("GreynirServerKey") # Cached
return all((gak, key, key == gak))


Expand Down
4 changes: 2 additions & 2 deletions routes/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

from . import routes, cache, max_age
from settings import changedlocale
from utility import read_api_key
from utility import read_txt_api_key
from db import SessionContext, Session
from db.sql import (
StatsQuery,
Expand Down Expand Up @@ -354,7 +354,7 @@ def stats_queries() -> Union[Response, str]:

# Accessing this route requires an API key
key = request.args.get("key")
if key is None or key != read_api_key("GreynirServerKey"):
if key is None or key != read_txt_api_key("GreynirServerKey"):
return Response(f"Not authorized", status=401)

days = _DEFAULT_QUERY_STATS_PERIOD
Expand Down
3 changes: 3 additions & 0 deletions tests/files/dummy_json_api_key.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"key": 123456789
}
4 changes: 2 additions & 2 deletions tests/test_greynir.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
sys.path.insert(0, mainpath)

from main import app # noqa
from utility import read_api_key # noqa
from utility import read_txt_api_key # noqa

# pylint: disable=unused-wildcard-import
from geo import * # noqa
Expand All @@ -60,7 +60,7 @@ def in_ci_environment() -> bool:
is a dummy value (set in CI config)."""
global DUMMY_API_KEY
try:
return read_api_key("GreynirServerKey") == DUMMY_API_KEY
return read_txt_api_key("GreynirServerKey") == DUMMY_API_KEY
except Exception:
return False

Expand Down
54 changes: 18 additions & 36 deletions tests/test_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from db import SessionContext
from db.models import Query, QueryClientData # , QueryLog
from queries import ResponseDict
from utility import read_api_key
from utility import read_txt_api_key
from speech.trans import strip_markup
from utility import QUERIES_RESOURCES_DIR

Expand Down Expand Up @@ -148,15 +148,15 @@ def _has_no_numbers(v: str) -> bool:


def has_google_api_key() -> bool:
return read_api_key("GoogleServerKey") != ""
return read_txt_api_key("GoogleServerKey") != ""


def has_ja_api_key() -> bool:
return read_api_key("JaServerKey") != ""
return read_txt_api_key("JaServerKey") != ""


def has_greynir_api_key() -> bool:
return read_api_key("GreynirServerKey") != ""
return read_txt_api_key("GreynirServerKey") != ""


def has_atm_locations_file() -> bool:
Expand Down Expand Up @@ -444,13 +444,12 @@ def test_counting(client: FlaskClient) -> None:
assert _has_no_numbers(json["voice"])


@pytest.mark.skipif(
not has_atm_locations_file(), reason="no ATM locations file found on test server"
)
def test_atm(client: FlaskClient) -> None:
"""ATM module"""

if not has_atm_locations_file():
# NB: No ATM locations file found, skip this test
return

_query_data_cleanup() # Remove any data logged to DB on account of tests

json = qmcall(
Expand Down Expand Up @@ -854,13 +853,10 @@ def test_dictionary(client: FlaskClient) -> None:
assert "skíthæll" in json["answer"].lower()


@pytest.mark.skipif(not has_google_api_key(), reason="no Google API key on test server")
def test_distance(client: FlaskClient) -> None:
"""Distance module."""

if not has_google_api_key():
# NB: No Google API key on test server
return

json = qmcall(
client, {"q": "hvað er ég langt frá perlunni", "voice": True}, "Distance"
)
Expand Down Expand Up @@ -1050,12 +1046,10 @@ def test_geography(client: FlaskClient) -> None:
assert "Noregi" in json["answer"]


@pytest.mark.skipif(not has_ja_api_key(), reason="no Ja.is API key on test server")
def test_ja(client: FlaskClient) -> None:
"""Ja.is module."""

if not has_ja_api_key():
return

json = qmcall(
client,
{"q": "hver er síminn hjá Sveinbirni Þórðarsyni?", "voice": True},
Expand Down Expand Up @@ -1137,13 +1131,10 @@ def test_petrol(client: FlaskClient) -> None:
assert "source" in json and json["source"].startswith("Gasvaktin")


@pytest.mark.skipif(not has_google_api_key(), reason="no Google API key on test server")
def test_pic(client: FlaskClient) -> None:
"""Pic module."""

if not has_google_api_key():
# NB: No Google API key on test server
return

# TODO: Re-add test with "Katrín Jakobsdóttir" when fixed GreynirEngine is released
json = qmcall(client, {"q": "Sýndu mér mynd af Bjarna Benediktssyni"}, "Picture")
assert "image" in json
Expand All @@ -1155,13 +1146,10 @@ def test_pic(client: FlaskClient) -> None:
assert "answer" in json and json["answer"]


@pytest.mark.skipif(not has_google_api_key(), reason="no Google API key on test server")
def test_places(client: FlaskClient) -> None:
"""Places module."""

if not has_google_api_key():
# NB: No Google API key on test server
return

json = qmcall(client, {"q": "Er lokað á Forréttabarnum?", "voice": True}, "Places")
assert (
"answer" in json
Expand All @@ -1186,13 +1174,10 @@ def test_places(client: FlaskClient) -> None:
assert _has_no_numbers(json["voice"])


@pytest.mark.skipif(not has_google_api_key(), reason="no Google API key on test server")
def test_play(client: FlaskClient) -> None:
"""Play module."""

if not has_google_api_key():
# NB: No Google (YouTube) API key on test server
return

json = qmcall(client, {"q": "spilaðu einhverja klassíska tónlist"}, "Play")
assert "open_url" in json

Expand Down Expand Up @@ -1621,13 +1606,10 @@ def test_userinfo(client: FlaskClient) -> None:
# assert json["answer"].startswith("Gaman að kynnast") and "Boutros" in json["answer"]


@pytest.mark.skipif(not has_google_api_key(), reason="no Google API key on test server")
def test_userloc(client: FlaskClient) -> None:
"""User location module."""

if not has_google_api_key():
# NB: No Google API key on test server
return

json = qmcall(client, {"q": "Hvar er ég"}, "UserLocation")
assert "Fiskislóð" in json["answer"]
json = qmcall(
Expand Down Expand Up @@ -1751,13 +1733,13 @@ def test_yulelads(client: FlaskClient) -> None:
)


@pytest.mark.skipif(
not has_greynir_api_key(),
reason="We don't run these tests unless a Greynir API key is present",
)
def test_query_history_api(client: FlaskClient) -> None:
"""Tests for the query history deletion API endpoint."""

if not has_greynir_api_key():
# We don't run these tests unless a Greynir API key is present
return

def _verify_basic(r: Any) -> Dict:
"""Make sure the server response is minimally sane."""
assert r.content_type.startswith(API_CONTENT_TYPE)
Expand All @@ -1784,7 +1766,7 @@ def _num_logged_query_info(client_id: str, model_name: str) -> int:

# Create basic query param dict
qdict: Dict[str, Any] = dict(
api_key=read_api_key("GreynirServerKey"),
api_key=read_txt_api_key("GreynirServerKey"),
action="clear",
client_id=DUMMY_CLIENT_ID,
)
Expand Down
Loading