PyFlowery/tests/tests.py
cswimr 33b63fecc4
Some checks failed
Actions / lint (push) Failing after 16s
Actions / build (push) Successful in 19s
(1.0.3) retry handlers and more
added automatic retry handlers for 429 and 5xx error codes, as well as custom exceptions for ratelimiting, client errors, and server errors
also added a more advanced user agent string constructor. previously, setting FloweryAPIConfig.user_agent would override the default user_agent. so, if you set user_agent to `foobar`, the user agent string would be `foobar` in requests. now, the user agent string sent by requests would be the following on my development machine:
`'User-Agent': 'PyFlowery/1.0.3 PyFloweryTests (Python 3.12.6 (main, Sep  8 2024, 13:18:56) [GCC 14.2.1 20240805])'`
2024-09-17 23:24:14 -04:00

48 lines
1.6 KiB
Python

import asyncio
import logging
import sys
from pyflowery.models import FloweryAPIConfig
from pyflowery.pyflowery import FloweryAPI
root = logging.getLogger()
root.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
root.addHandler(handler)
api = FloweryAPI(FloweryAPIConfig(user_agent="PyFloweryTests"))
ALEXANDER = "fa3ea565-121f-5efd-b4e9-59895c77df23" # TikTok
JACOB = "38f45366-68e8-5d39-b1ef-3fd4eeb61cdb" # Microsoft Azure
STORMTROOPER = "191c5adc-a092-5eea-b4ff-ce01f66153ae" # TikTok
async def test_get_voices():
"""Test the get_voices method"""
async for voice in api.get_voices():
if 'en' in voice.language.code:
api.config.logger.info(voice)
async def test_get_tts():
"""Test the get_tts method"""
voice = await api.get_voice(voice_id=ALEXANDER)
tts = await api.get_tts(text="Sphinx of black quartz, judge my vow. The quick brown fox jumps over a lazy dog.", voice=voice)
try:
with open('test.mp3', 'wb') as f:
f.write(tts)
except Exception as e: # pylint: disable=broad-except
api.config.logger.error(e, exc_info=True)
long_string = 'a' * 2049
try:
await api.get_tts(text=long_string)
except ValueError as e:
api.config.logger.error("This is expected to fail:\n%s", e, exc_info=True)
if __name__ == '__main__':
api.config.logger.info("testing get_voices")
asyncio.run(test_get_voices())
api.config.logger.info("testing get_tts")
asyncio.run(test_get_tts())