Techdebt: Replace sure with regular assertions in Polly (#6679)

This commit is contained in:
kbalk 2023-08-16 17:56:23 -04:00 committed by GitHub
parent 0b4889ad37
commit bbecbd2acd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 265 additions and 274 deletions

View File

@ -1,267 +1,263 @@
from botocore.exceptions import ClientError import boto3
import boto3 from botocore.exceptions import ClientError
import sure # noqa # pylint: disable=unused-import
from moto import mock_polly from moto import mock_polly
# Polly only available in a few regions # Polly only available in a few regions
DEFAULT_REGION = "eu-west-1" DEFAULT_REGION = "eu-west-1"
LEXICON_XML = """<?xml version="1.0" encoding="UTF-8"?> LEXICON_XML = """<?xml version="1.0" encoding="UTF-8"?>
<lexicon version="1.0" <lexicon version="1.0"
xmlns="http://www.w3.org/2005/01/pronunciation-lexicon" xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd" http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
alphabet="ipa" alphabet="ipa"
xml:lang="en-US"> xml:lang="en-US">
<lexeme> <lexeme>
<grapheme>W3C</grapheme> <grapheme>W3C</grapheme>
<alias>World Wide Web Consortium</alias> <alias>World Wide Web Consortium</alias>
</lexeme> </lexeme>
</lexicon>""" </lexicon>"""
@mock_polly @mock_polly
def test_describe_voices(): def test_describe_voices():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
resp = client.describe_voices() resp = client.describe_voices()
len(resp["Voices"]).should.be.greater_than(1) assert len(resp["Voices"]) >= 1
resp = client.describe_voices(LanguageCode="en-GB") resp = client.describe_voices(LanguageCode="en-GB")
len(resp["Voices"]).should.equal(4) assert len(resp["Voices"]) == 4
try: try:
client.describe_voices(LanguageCode="SOME_LANGUAGE") client.describe_voices(LanguageCode="SOME_LANGUAGE")
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("400") assert err.response["Error"]["Code"] == "400"
else: else:
raise RuntimeError("Should have raised an exception") raise RuntimeError("Should have raised an exception")
@mock_polly @mock_polly
def test_put_list_lexicon(): def test_put_list_lexicon():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
# Return nothing # Return nothing
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
resp = client.list_lexicons() resp = client.list_lexicons()
len(resp["Lexicons"]).should.equal(1) assert len(resp["Lexicons"]) == 1
@mock_polly @mock_polly
def test_put_get_lexicon(): def test_put_get_lexicon():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
# Return nothing # Return nothing
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
resp = client.get_lexicon(Name="test") resp = client.get_lexicon(Name="test")
resp.should.contain("Lexicon") assert "Lexicon" in resp
resp.should.contain("LexiconAttributes") assert "LexiconAttributes" in resp
@mock_polly @mock_polly
def test_put_lexicon_bad_name(): def test_put_lexicon_bad_name():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
try: try:
client.put_lexicon(Name="test-invalid", Content=LEXICON_XML) client.put_lexicon(Name="test-invalid", Content=LEXICON_XML)
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("InvalidParameterValue") assert err.response["Error"]["Code"] == "InvalidParameterValue"
else: else:
raise RuntimeError("Should have raised an exception") raise RuntimeError("Should have raised an exception")
@mock_polly @mock_polly
def test_synthesize_speech(): def test_synthesize_speech():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
# Return nothing # Return nothing
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
tests = (("pcm", "audio/pcm"), ("mp3", "audio/mpeg"), ("ogg_vorbis", "audio/ogg")) tests = (("pcm", "audio/pcm"), ("mp3", "audio/mpeg"), ("ogg_vorbis", "audio/ogg"))
for output_format, content_type in tests: for output_format, content_type in tests:
resp = client.synthesize_speech( resp = client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat=output_format, OutputFormat=output_format,
SampleRate="16000", SampleRate="16000",
Text="test1234", Text="test1234",
TextType="text", TextType="text",
VoiceId="Astrid", VoiceId="Astrid",
) )
resp["ContentType"].should.equal(content_type) assert resp["ContentType"] == content_type
@mock_polly @mock_polly
def test_synthesize_speech_bad_lexicon(): def test_synthesize_speech_bad_lexicon():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test2"], LexiconNames=["test2"],
OutputFormat="pcm", OutputFormat="pcm",
SampleRate="16000", SampleRate="16000",
Text="test1234", Text="test1234",
TextType="text", TextType="text",
VoiceId="Astrid", VoiceId="Astrid",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("LexiconNotFoundException") assert err.response["Error"]["Code"] == "LexiconNotFoundException"
else: else:
raise RuntimeError("Should have raised LexiconNotFoundException") raise RuntimeError("Should have raised LexiconNotFoundException")
@mock_polly @mock_polly
def test_synthesize_speech_bad_output_format(): def test_synthesize_speech_bad_output_format():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat="invalid", OutputFormat="invalid",
SampleRate="16000", SampleRate="16000",
Text="test1234", Text="test1234",
TextType="text", TextType="text",
VoiceId="Astrid", VoiceId="Astrid",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("InvalidParameterValue") assert err.response["Error"]["Code"] == "InvalidParameterValue"
else: else:
raise RuntimeError("Should have raised ") raise RuntimeError("Should have raised ")
@mock_polly @mock_polly
def test_synthesize_speech_bad_sample_rate(): def test_synthesize_speech_bad_sample_rate():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat="pcm", OutputFormat="pcm",
SampleRate="18000", SampleRate="18000",
Text="test1234", Text="test1234",
TextType="text", TextType="text",
VoiceId="Astrid", VoiceId="Astrid",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("InvalidSampleRateException") assert err.response["Error"]["Code"] == "InvalidSampleRateException"
else: else:
raise RuntimeError("Should have raised ") raise RuntimeError("Should have raised ")
@mock_polly @mock_polly
def test_synthesize_speech_bad_text_type(): def test_synthesize_speech_bad_text_type():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat="pcm", OutputFormat="pcm",
SampleRate="16000", SampleRate="16000",
Text="test1234", Text="test1234",
TextType="invalid", TextType="invalid",
VoiceId="Astrid", VoiceId="Astrid",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("InvalidParameterValue") assert err.response["Error"]["Code"] == "InvalidParameterValue"
else: else:
raise RuntimeError("Should have raised ") raise RuntimeError("Should have raised ")
@mock_polly @mock_polly
def test_synthesize_speech_bad_voice_id(): def test_synthesize_speech_bad_voice_id():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat="pcm", OutputFormat="pcm",
SampleRate="16000", SampleRate="16000",
Text="test1234", Text="test1234",
TextType="text", TextType="text",
VoiceId="Luke", VoiceId="Luke",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("InvalidParameterValue") assert err.response["Error"]["Code"] == "InvalidParameterValue"
else: else:
raise RuntimeError("Should have raised ") raise RuntimeError("Should have raised ")
@mock_polly @mock_polly
def test_synthesize_speech_text_too_long(): def test_synthesize_speech_text_too_long():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat="pcm", OutputFormat="pcm",
SampleRate="16000", SampleRate="16000",
Text="test1234" * 376, # = 3008 characters Text="test1234" * 376, # = 3008 characters
TextType="text", TextType="text",
VoiceId="Astrid", VoiceId="Astrid",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal("TextLengthExceededException") assert err.response["Error"]["Code"] == "TextLengthExceededException"
else: else:
raise RuntimeError("Should have raised ") raise RuntimeError("Should have raised ")
@mock_polly @mock_polly
def test_synthesize_speech_bad_speech_marks1(): def test_synthesize_speech_bad_speech_marks1():
client = boto3.client("polly", region_name=DEFAULT_REGION) client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) client.put_lexicon(Name="test", Content=LEXICON_XML)
try: try:
client.synthesize_speech( client.synthesize_speech(
LexiconNames=["test"], LexiconNames=["test"],
OutputFormat="pcm", OutputFormat="pcm",
SampleRate="16000", SampleRate="16000",
Text="test1234", Text="test1234",
TextType="text", TextType="text",
SpeechMarkTypes=["word"], SpeechMarkTypes=["word"],
VoiceId="Astrid", VoiceId="Astrid",
) )
except ClientError as err: except ClientError as err:
err.response["Error"]["Code"].should.equal( assert err.response["Error"]["Code"] == "MarksNotSupportedForFormatException"
"MarksNotSupportedForFormatException" else:
) raise RuntimeError("Should have raised ")
else:
raise RuntimeError("Should have raised ")
@mock_polly
def test_synthesize_speech_bad_speech_marks2():
@mock_polly client = boto3.client("polly", region_name=DEFAULT_REGION)
def test_synthesize_speech_bad_speech_marks2(): client.put_lexicon(Name="test", Content=LEXICON_XML)
client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML) try:
client.synthesize_speech(
try: LexiconNames=["test"],
client.synthesize_speech( OutputFormat="pcm",
LexiconNames=["test"], SampleRate="16000",
OutputFormat="pcm", Text="test1234",
SampleRate="16000", TextType="ssml",
Text="test1234", SpeechMarkTypes=["word"],
TextType="ssml", VoiceId="Astrid",
SpeechMarkTypes=["word"], )
VoiceId="Astrid", except ClientError as err:
) assert err.response["Error"]["Code"] == "MarksNotSupportedForFormatException"
except ClientError as err: else:
err.response["Error"]["Code"].should.equal( raise RuntimeError("Should have raised ")
"MarksNotSupportedForFormatException"
)
else: @mock_polly
raise RuntimeError("Should have raised ") def test_update_lexicon():
client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML)
@mock_polly client.put_lexicon(Name="test", Content=LEXICON_XML)
def test_update_lexicon():
client = boto3.client("polly", region_name=DEFAULT_REGION)
client.put_lexicon(Name="test", Content=LEXICON_XML)
client.put_lexicon(Name="test", Content=LEXICON_XML)

View File

@ -1,12 +1,7 @@
import sure # noqa # pylint: disable=unused-import """Test the different server responses."""
import moto.server as server import moto.server as server
from moto import mock_polly from moto import mock_polly
"""
Test the different server responses
"""
@mock_polly @mock_polly
def test_polly_list(): def test_polly_list():
@ -14,4 +9,4 @@ def test_polly_list():
test_client = backend.test_client() test_client = backend.test_client()
res = test_client.get("/v1/lexicons") res = test_client.get("/v1/lexicons")
res.status_code.should.equal(200) assert res.status_code == 200