Skip to content

Commit

Permalink
added skipif to tests that require api keys instead of returning and …
Browse files Browse the repository at this point in the history
…passing
  • Loading branch information
KariSt1 committed Aug 30, 2023
1 parent 7ec98ae commit ba60bd0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 70 deletions.
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
87 changes: 53 additions & 34 deletions tests/test_speech.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@
import re
import sys
import datetime
import logging
import pytest
from pathlib import Path
from itertools import product

import requests
from speech import text_to_audio_url
from speech.trans import DefaultTranscriber as DT
from utility import read_api_key

# Shenanigans to enable Pytest to discover modules in the
# main workspace directory (the parent of /tests)
Expand All @@ -42,6 +39,20 @@
if mainpath not in sys.path:
sys.path.insert(0, mainpath)

from speech import text_to_audio_url
from speech.trans import DefaultTranscriber as DT
from utility import read_json_api_key

# TODO: remove these tests once icespeak is released


def has_azure_api_key() -> bool:
return read_json_api_key("AzureSpeechServerKey") != {}


def has_aws_api_key() -> bool:
return read_json_api_key("AWSPollyServerKey") != {}


def test_voices_utils():
"""Test utility functions in speech.voices."""
Expand Down Expand Up @@ -72,43 +83,51 @@ def test_voices_utils():
)


def test_speech_synthesis():
"""Test basic speech synthesis functionality."""
@pytest.mark.skipif(not has_aws_api_key(), reason="No AWS Polly API key found")
def test_speech_synthesis_aws():
"""Test basic speech synthesis functionality with AWS Polly."""

_TEXT = "Prufa"
_MIN_AUDIO_SIZE = 1000

# Test AWS Polly
if read_api_key("AWSPollyServerKey.json"):
url = text_to_audio_url(
text=_TEXT,
text_format="text",
audio_format="mp3",
voice_id="Dora",
)
assert url and url.startswith("http")
r = requests.get(url, timeout=10)
assert r.headers.get("Content-Type") == "audio/mpeg", "Expected MP3 audio data"
assert len(r.content) > _MIN_AUDIO_SIZE, "Expected longer audio data"
else:
logging.info("No AWS Polly API key found, skipping test")
url = text_to_audio_url(
text=_TEXT,
text_format="text",
audio_format="mp3",
voice_id="Dora",
)
assert url and url.startswith("http")
r = requests.get(url, timeout=10)
assert r.headers.get("Content-Type") == "audio/mpeg", "Expected MP3 audio data"
assert len(r.content) > _MIN_AUDIO_SIZE, "Expected longer audio data"


@pytest.mark.skipif(
not has_azure_api_key(),
reason="No Azure Speech API key found",
)
def test_speech_synthesis_azure():
"""
Test basic speech synthesis functionality with Azure Cognitive Services.
"""

_TEXT = "Prufa"
_MIN_AUDIO_SIZE = 1000

# Test Azure Cognitive Services
if read_api_key("AzureSpeechServerKey.json"):
url = text_to_audio_url(
text=_TEXT,
text_format="text",
audio_format="mp3",
voice_id="Gudrun",
)
assert url and url.startswith("file://") and url.endswith(".mp3")
path_str = url[7:]
path = Path(path_str)
assert path.is_file(), "Expected audio file to exist"
assert path.stat().st_size > _MIN_AUDIO_SIZE, "Expected longer audio data"
path.unlink()
else:
logging.info("No Azure Speech API key found, skipping test")
url = text_to_audio_url(
text=_TEXT,
text_format="text",
audio_format="mp3",
voice_id="Gudrun",
)
assert url and url.startswith("file://") and url.endswith(".mp3")
path_str = url[7:]
path = Path(path_str)
assert path.is_file(), "Expected audio file to exist"
assert path.stat().st_size > _MIN_AUDIO_SIZE, "Expected longer audio data"
path.unlink()


def test_gssml():
Expand Down

0 comments on commit ba60bd0

Please sign in to comment.