Skip to content

Commit

Permalink
Merge pull request #6 from DavideGalilei/dev
Browse files Browse the repository at this point in the history
❇️ Fixes #5
  • Loading branch information
alsoGAMER authored May 18, 2021
2 parents 90af4d8 + a2dfc83 commit d1c1448
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 64 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Tests

on:
push:
branches: [ "dev", "master" ]
pull_request:
branches: [ "dev", "master" ]

jobs:
tests:
name: "Python ${{ matrix.version }} compatibility"
runs-on: "ubuntu-latest"

strategy:
matrix:
version: [ "3.6", "3.7", "3.8", "3.9" ]

steps:
- uses: "actions/checkout@v2"
- uses: "actions/setup-python@v2"
with:
python-version: "${{ matrix.version }}"
- name: "Install Gpytranslate"
run: python setup.py install
- name: "Install pytest"
run: python -m pip install pytest-asyncio
- name: "Run tests"
run: python -m pytest tests
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
- name: Install Gpytranslate
run: |
python setup.py install
- name: "Install pytest"
run: python -m pip install pytest-asyncio

- name: Run tests
run: |
Expand Down
8 changes: 5 additions & 3 deletions gpytranslate/gpytranslate.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from collections.abc import Mapping
from typing import Union, Literal, Dict, List, Any, Final
from typing import Union, Dict, List, Any

import httpx

from .types import TranslatedObject


class Translator:
__version__: Final[str] = "1.0.2"
__version__: str = "1.0.3"

def __init__(
self,
Expand All @@ -24,7 +24,9 @@ async def translate(
sourcelang: str = "auto",
targetlang: str = "en",
client: str = "gtx",
dt: Literal["t", "at", "rm", "bd", "md", "ss", "ex", "rw", "dj"] = "t",
dt: str = "t",
# Literal["t", "at", "rm", "bd", "md", "ss", "ex", "rw", "dj"] = "t",
# broken compatibility with python <3.8 :(
dj: int = 1,
**extra
) -> Union[TranslatedObject, List[TranslatedObject], Dict[str, TranslatedObject]]:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="gpytranslate",
version="1.0.2",
version="1.0.3",
author="Davide Galilei",
author_email="[email protected]",
description="A Python3 library for translating text using Google Translate API.",
Expand Down
19 changes: 7 additions & 12 deletions tests/test_detect.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import unittest
import pytest


from gpytranslate import Translator


class Test(unittest.IsolatedAsyncioTestCase):
async def test_detect(self):
translator = Translator()
language: str = await translator.detect(
text="Ciao Mondo."
)
@pytest.mark.asyncio
async def test_detect():
translator = Translator()
language: str = await translator.detect(text="Ciao Mondo.")

self.assertEqual(
language,
"it",
"Translations are not equal.",
)
assert language == "it", "Translations are not equal."
93 changes: 45 additions & 48 deletions tests/test_translate.py
Original file line number Diff line number Diff line change
@@ -1,53 +1,50 @@
import unittest
import pytest

from typing import List, Dict, Any

from gpytranslate import Translator, TranslatedObject


class Test(unittest.IsolatedAsyncioTestCase):
async def test_translate_auto(self):
translator = Translator()
translation: TranslatedObject = await translator.translate(
"Ciao Mondo.", targetlang="en"
)
self.assertEqual(
translation.text,
"Hello World.",
"Translations are not equal.",
)

async def test_translate_source(self):
translator = Translator()
translation: TranslatedObject = await translator.translate(
"Ciao.", sourcelang="it", targetlang="en"
)

self.assertEqual(
translation.text,
"Hello.",
"Translations are not equal.",
)

async def test_translate_list(self):
translator = Translator()
translations: List[TranslatedObject] = await translator.translate(
["Ciao Mondo.", "Come stai?"], targetlang="en"
)

self.assertEqual(
[translation.text for translation in translations],
["Hello World.", "How are you?"],
"Translations are not equal.",
)

async def test_translate_dict(self):
translator = Translator()
translations: Dict[Any, TranslatedObject] = await translator.translate(
{1: "Ciao Mondo.", 2: "Come stai?"}, targetlang="en"
)

self.assertEqual(
{k: v.text for k, v in translations.items()},
{1: "Hello World.", 2: "How are you?"},
"Translations are not equal.",
)
@pytest.mark.asyncio
async def test_translate_auto():
translator = Translator()
translation: TranslatedObject = await translator.translate(
"Ciao Mondo.", targetlang="en"
)
assert translation.text == "Hello World.", "Translations are not equal."


@pytest.mark.asyncio
async def test_translate_source():
translator = Translator()
translation: TranslatedObject = await translator.translate(
"Ciao.", sourcelang="it", targetlang="en"
)

assert translation.text == "Hello.", "Translations are not equal."


@pytest.mark.asyncio
async def test_translate_list():
translator = Translator()
translations: List[TranslatedObject] = await translator.translate(
["Ciao Mondo.", "Come stai?"], targetlang="en"
)

assert [translation.text for translation in translations] == [
"Hello World.",
"How are you?",
], "Translations are not equal."


@pytest.mark.asyncio
async def test_translate_dict():
translator = Translator()
translations: Dict[Any, TranslatedObject] = await translator.translate(
{1: "Ciao Mondo.", 2: "Come stai?"}, targetlang="en"
)

assert {k: v.text for k, v in translations.items()} == {
1: "Hello World.",
2: "How are you?",
}, "Translations are not equal."

0 comments on commit d1c1448

Please sign in to comment.