From d7c5cffbffc081c4ee3e6888cbcad3243747480a Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Mon, 15 Mar 2021 23:04:57 +0200 Subject: [PATCH 01/11] Add long description --- setup.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/setup.py b/setup.py index 0df5772..fb322cb 100644 --- a/setup.py +++ b/setup.py @@ -3,11 +3,16 @@ from setuptools.depends import get_module_constant from setuptools import setup +with open("README.md", "r", encoding="utf-8") as fh: + long_description = fh.read() + setup( name = 'detectlanguage', packages = ['detectlanguage'], version = get_module_constant('detectlanguage', '__version__'), description = 'Language Detection API Client', + long_description=long_description, + long_description_content_type="text/markdown", author = 'Laurynas Butkus', author_email = 'info@detectlanguage.com', url = 'https://github.com/detectlanguage/detectlanguage-python', From 3a3b2b40191bda7acebf084aece8e6f5ed000db4 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Fri, 19 Mar 2021 19:16:09 +0200 Subject: [PATCH 02/11] Fix long description loading on python 2.7 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index fb322cb..feb7ae1 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools.depends import get_module_constant from setuptools import setup -with open("README.md", "r", encoding="utf-8") as fh: +with open("README.md", "r") as fh: long_description = fh.read() setup( From 171c5ff6a74ec01cb8a88100f3f6c46b9718d200 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 16:51:18 +0300 Subject: [PATCH 03/11] Add github actions --- .github/workflows/main.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 .github/workflows/main.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..6132509 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,26 @@ +name: Build +on: [push,pull_request] +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + python-version: + - 2.7 + - 3.4 + - 3.5 + - 3.6 + - 3.7 + - 3.8 + name: Python ${{ matrix.python-version }} sample + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v3 + with: + python-version: ${{ matrix.python-version }} + - run: pip install -r requirements.txt + - run: pip install -r test-requirements.txt + - run: python setup.py develop + - env: + DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }} + run: nosetests From 8d2c291491fb233cba1ee6af69cde533d5b2aea7 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 16:58:22 +0300 Subject: [PATCH 04/11] Adjust github actions --- .github/workflows/main.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 6132509..4e73990 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -18,9 +18,12 @@ jobs: - uses: actions/setup-python@v3 with: python-version: ${{ matrix.python-version }} - - run: pip install -r requirements.txt - - run: pip install -r test-requirements.txt - - run: python setup.py develop + cache: 'pip' + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + pip install -r test-requirements.txt - env: DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }} run: nosetests From 6992fdeca1786e8ace91531b4cf3cf8b6fe6f605 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 17:02:15 +0300 Subject: [PATCH 05/11] Adjust github actions --- .github/workflows/main.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 4e73990..762cb41 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,11 +7,9 @@ jobs: matrix: python-version: - 2.7 - - 3.4 - - 3.5 - 3.6 - 3.7 - - 3.8 + - 3.9 name: Python ${{ matrix.python-version }} sample steps: - uses: actions/checkout@v3 From fc628baa23cae5cdff43567c4b8af03484a09e5f Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 17:03:23 +0300 Subject: [PATCH 06/11] Add python 3.10 --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 762cb41..eda85d0 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,6 +10,7 @@ jobs: - 3.6 - 3.7 - 3.9 + - 3.10 name: Python ${{ matrix.python-version }} sample steps: - uses: actions/checkout@v3 From 5144253bb0d40634d453df270101e3e81a40c257 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 17:04:23 +0300 Subject: [PATCH 07/11] Adjust github actions --- .github/workflows/main.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index eda85d0..a231198 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,11 +6,11 @@ jobs: strategy: matrix: python-version: - - 2.7 - - 3.6 - - 3.7 - - 3.9 - - 3.10 + - '2.7' + - '3.6' + - '3.7' + - '3.9' + - '3.10' name: Python ${{ matrix.python-version }} sample steps: - uses: actions/checkout@v3 From ecf1de140a51aadeaa9407695986578184cdbd2d Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 17:05:32 +0300 Subject: [PATCH 08/11] Remove python 3.10 --- .github/workflows/main.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index a231198..bdbda65 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,7 +10,6 @@ jobs: - '3.6' - '3.7' - '3.9' - - '3.10' name: Python ${{ matrix.python-version }} sample steps: - uses: actions/checkout@v3 From 37badc1ddad3caa49415b64c90ae3661b09613fc Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 17:07:40 +0300 Subject: [PATCH 09/11] Update build badge --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a98e796..43ace25 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ Detect Language API Python Client ======== [![PyPI version](https://badge.fury.io/py/detectlanguage.svg)](https://badge.fury.io/py/detectlanguage) -[![Build Status](https://api.travis-ci.org/detectlanguage/detectlanguage-python.svg)](http://travis-ci.org/detectlanguage/detectlanguage-python) +[![Build Status](https://github.com/detectlanguage/detectlanguage-python/actions/workflows/main.yml/badge.svg)](https://github.com/detectlanguage/detectlanguage-python/actions) Detects language of given text. Returns detected language codes and scores. From 093d8a83b411a6ba5c92b7d4ea7819ec35c5e2b9 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 30 Apr 2022 17:07:58 +0300 Subject: [PATCH 10/11] Remove travis --- .travis.yml | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 3b637d0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: python -python: -- 2.7 -- 3.4 -- 3.5 -- 3.6 -- 3.7 -- 3.8 -install: -- pip install -r requirements.txt -- pip install -r test-requirements.txt -- python setup.py develop -script: -- nosetests -env: - global: - secure: OEf+n4gbaDOfj7kqVjA3sjkIYtIsfEv6K2/5RejYxMoaAHEOR5r79HatWa4D2gmJxNZa+Biro6aep17opbBhMY3FAB/RK9dqW966YMpiOIgI5CF6lQeleOik61ByFYgJgp6S4pNS0wD9hOSN42sK0AX4f67m7KlsTAyFkV7ZgQo= From 83010d555bf454f2a2e0c7dbb55d1d580fe803e3 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 20 Aug 2025 19:45:01 +0300 Subject: [PATCH 11/11] V2 SDK (#10) * Add mise * Switch to pytest * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Adjust github test action * Switch to v3 api * Add proxy support * Update readme * Adjust readme * Adjust readme * Add changelog * Bump version * Add publish action * Adjust build * Add deprecations --- .github/workflows/main.yml | 19 ++++++----- .github/workflows/publish.yml | 53 +++++++++++++++++++++++++++++ .gitignore | 3 ++ CHANGELOG.md | 25 ++++++++++++++ README.md | 48 ++++++++++++-------------- detectlanguage/__init__.py | 7 ++-- detectlanguage/api.py | 59 +++++++++++++++++++++++++++++--- detectlanguage/client.py | 9 ++--- detectlanguage/configuration.py | 4 +-- mise.toml | 13 +++++++ pytest.ini | 12 +++++++ setup.cfg | 3 +- test-requirements.txt | 6 ++-- tests/test_api.py | 60 ++++++++++++++++++--------------- tests/test_detectlanguage.py | 12 +++---- tests/test_proxy.py | 47 ++++++++++++++++++++++++++ tests/utils.py | 4 --- 17 files changed, 291 insertions(+), 93 deletions(-) create mode 100644 .github/workflows/publish.yml create mode 100644 CHANGELOG.md create mode 100644 mise.toml create mode 100644 pytest.ini create mode 100644 tests/test_proxy.py delete mode 100644 tests/utils.py diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bdbda65..6517b0e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -6,22 +6,25 @@ jobs: strategy: matrix: python-version: - - '2.7' - - '3.6' - - '3.7' + - '3.8' - '3.9' + - '3.10' + - '3.11' + - '3.12' + - '3.13' + - '3.14.0-beta.4' name: Python ${{ matrix.python-version }} sample steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} cache: 'pip' - name: Install dependencies run: | - python -m pip install --upgrade pip pip install -r requirements.txt pip install -r test-requirements.txt - - env: + - name: Run tests + env: DETECTLANGUAGE_API_KEY: ${{ secrets.DETECTLANGUAGE_API_KEY }} - run: nosetests + run: pytest diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..3056e77 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,53 @@ +name: Publish Python 🐍 distribution 📦 to PyPI + +on: + release: + types: [published] + +jobs: + build: + name: Build distribution 📦 + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.x" + - name: Install pypa/build + run: >- + python3 -m + pip install + build + --user + - name: Build a binary wheel and a source tarball + run: python3 -m build + - name: Store the distribution packages + uses: actions/upload-artifact@v4 + with: + name: python-package-distributions + path: dist/ + + publish-to-pypi: + name: >- + Publish Python 🐍 distribution 📦 to PyPI + needs: + - build + runs-on: ubuntu-latest + environment: + name: pypi + url: https://pypi.org/p/detectlanguage + permissions: + id-token: write # IMPORTANT: mandatory for trusted publishing + + steps: + - name: Download all the dists + uses: actions/download-artifact@v4 + with: + name: python-package-distributions + path: dist/ + - name: Publish distribution 📦 to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/.gitignore b/.gitignore index 76f2a46..a7b60b6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.env.local +.venv + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..1a40650 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,25 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + + +## v2.0.0 + +### Added +- `detect_batch` method for batch detections +- Proxy support + +### Changed +- Switched to v3 API which uses updated language detection model +- ⚠️ `detect` method result fields are `language` and `score` + +### Deprecated +- Calling `detect()` with list argument. Use `detect_batch` instead. +- `simple_detect()` - Use `detect_code()` instead. Will be removed in a future version. +- `user_status()` - Use `account_status()` instead. Will be removed in a future version. + +### Removed +- Secure mode configuration. HTTPS is used by default. diff --git a/README.md b/README.md index 43ace25..17de63b 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,10 @@ You can get it by signing up at https://detectlanguage.com pip install detectlanguage ``` +### Upgrading + +When upgrading please check [changelog](CHANGELOG.md) for breaking changes. + ### Configuration ```python @@ -22,46 +26,45 @@ import detectlanguage detectlanguage.configuration.api_key = "YOUR API KEY" -# Enable secure mode (SSL) if you are passing sensitive data -# detectlanguage.configuration.secure = True +# You can use proxy if needed +# detectlanguage.configuration.proxies = {'https': 'https://user:pass@proxy:8080'} ``` ## Usage -### Language detection +### Detect language ```python -detectlanguage.detect("Buenos dias señor") +detectlanguage.detect("Dolce far niente") ``` #### Result ```python -[{'isReliable': True, 'confidence': 12.04, 'language': 'es'}] +[{'language': 'it', 'score': 0.5074}] ``` -### Simple language detection +### Detect single code -If you need just a language code you can use `simple_detect`. It returns just the language code. +If you need just a language code you can use `detect_code`. ```python -detectlanguage.simple_detect("Buenos dias señor") +detectlanguage.detect_code("Dolce far niente") ``` #### Result ```python -'es' +'it' ``` ### Batch detection It is possible to detect language of several texts with one request. This method is faster than doing one request per text. -To use batch detection just pass array of texts to `detect` method. ```python -detectlanguage.detect(["Buenos dias señor", "Labas rytas"]) +detectlanguage.detect_batch(["Dolce far niente", "Hello world"]) ``` #### Result @@ -69,14 +72,13 @@ detectlanguage.detect(["Buenos dias señor", "Labas rytas"]) Result is array of detections in the same order as the texts were passed. ```python -[ [ {'isReliable': True, 'confidence': 12.04, 'language': 'es'} ], - [ {'isReliable': True, 'confidence': 9.38, 'language': 'lt'} ] ] +[[{'language': 'it', 'score': 0.5074}], [{'language': 'en', 'score': 0.9098}]] ``` -### Getting your account status +### Get your account status ```python -detectlanguage.user_status() +detectlanguage.account_status() ``` #### Result @@ -87,7 +89,7 @@ detectlanguage.user_status() 'plan_expires': None } ``` -### Getting list detectable languages +### Get list of supported languages ```python detectlanguage.languages() @@ -95,17 +97,9 @@ detectlanguage.languages() #### Result -Array of language codes and names. - -## Contribution - -You are welcome to patch and send GitHub pull requests. - -### Testing - - pip install -r requirements.txt - pip install -r test-requirements.txt - nosetests +```python +[{'code': 'aa', 'name': 'Afar'}, {'code': 'ab', 'name': 'Abkhazian'}, ...] +``` ## License diff --git a/detectlanguage/__init__.py b/detectlanguage/__init__.py index bbec5ef..ca8c3fc 100644 --- a/detectlanguage/__init__.py +++ b/detectlanguage/__init__.py @@ -1,9 +1,12 @@ -__version__ = '1.5.0' +__version__ = '2.0.0' from .exceptions import * from .configuration import Configuration from .client import Client -from .api import simple_detect, detect, user_status, languages +from .api import detect, detect_code, detect_batch, account_status, languages + +# deprecated functions +from .api import simple_detect, user_status configuration = Configuration() client = Client(configuration) diff --git a/detectlanguage/api.py b/detectlanguage/api.py index c4defa5..37a7681 100644 --- a/detectlanguage/api.py +++ b/detectlanguage/api.py @@ -1,15 +1,64 @@ import detectlanguage +import warnings def detect(data): - result = detectlanguage.client.post('detect', { 'q': data }) - return result['data']['detections'] + if isinstance(data, list): + _warn_deprecated('use detect_batch instead for multiple texts') + return detect_batch(data) -def simple_detect(data): + return detectlanguage.client.post('detect', { 'q': data }) + +def detect_code(data): result = detect(data) return result[0]['language'] -def user_status(): - return detectlanguage.client.get('user/status') +def detect_batch(data): + return detectlanguage.client.post('detect-batch', { 'q': data }) + +def account_status(): + return detectlanguage.client.get('account/status') def languages(): return detectlanguage.client.get('languages') + + +### DEPRECATED + +def simple_detect(data): + """ + DEPRECATED: This function is deprecated and will be removed in a future version. + Use detect_code() instead. + + Args: + data: Text to detect language for + + Returns: + str: Language code of the detected language + """ + _warn_deprecated( + "simple_detect() is deprecated and will be removed in a future version. " + "Use detect_code() instead." + ) + return detect_code(data) + +def user_status(): + """ + DEPRECATED: This function is deprecated and will be removed in a future version. + Use account_status() instead. + + Returns: + dict: Account status information + """ + _warn_deprecated( + "user_status() is deprecated and will be removed in a future version. " + "Use account_status() instead." + ) + return account_status() + +def _warn_deprecated(message): + """Internal utility function to emit deprecation warnings.""" + warnings.warn( + message, + DeprecationWarning, + stacklevel=2 + ) diff --git a/detectlanguage/client.py b/detectlanguage/client.py index 999ca45..6f6eae5 100644 --- a/detectlanguage/client.py +++ b/detectlanguage/client.py @@ -13,11 +13,11 @@ def __init__(self, configuration): self.configuration = configuration def get(self, path, payload = {}): - r = requests.get(self.url(path), params=payload, headers = self.headers(), timeout = self.configuration.timeout) + r = requests.get(self.url(path), params=payload, headers = self.headers(), timeout = self.configuration.timeout, proxies = self.configuration.proxies) return self.handle_response(r) def post(self, path, payload): - r = requests.post(self.url(path), json=payload, headers = self.headers(), timeout = self.configuration.timeout) + r = requests.post(self.url(path), json=payload, headers = self.headers(), timeout = self.configuration.timeout, proxies = self.configuration.proxies) return self.handle_response(r) def handle_response(self, r): @@ -42,10 +42,7 @@ def handle_http_error(self, r, err): raise DetectLanguageError(err) def url(self, path): - return "%s://%s/%s/%s" % (self.protocol(), self.configuration.host, self.configuration.api_version, path) - - def protocol(self): - return 'https' if self.configuration.secure else 'http' + return "https://%s/%s/%s" % (self.configuration.host, self.configuration.api_version, path) def headers(self): return { diff --git a/detectlanguage/configuration.py b/detectlanguage/configuration.py index 2fb0482..173ddee 100644 --- a/detectlanguage/configuration.py +++ b/detectlanguage/configuration.py @@ -2,8 +2,8 @@ class Configuration: api_key = None - api_version = '0.2' + api_version = 'v3' host = 'ws.detectlanguage.com' user_agent = 'Detect Language API Python Client ' + detectlanguage.__version__ - secure = False timeout = 5 + proxies = None # e.g., {'https': 'https://proxy.example.com:8080'} diff --git a/mise.toml b/mise.toml new file mode 100644 index 0000000..7902ecf --- /dev/null +++ b/mise.toml @@ -0,0 +1,13 @@ +[tools] + +[env] +_.file = ".env.local" +_.python.venv = { path = ".venv", create = true } + +[tasks] +install-dev = "pip install -r requirements.txt && pip install -r test-requirements.txt && pip install build" +test = "pytest" +test-cov = "pytest --cov=detectlanguage --cov-report=term-missing" +test-verbose = "pytest -v" +console = "python -c \"import detectlanguage; import code; code.interact(local=locals())\"" +build = "python3 -m build" diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..5146207 --- /dev/null +++ b/pytest.ini @@ -0,0 +1,12 @@ +[tool:pytest] +testpaths = tests +python_files = test_*.py +python_classes = Test* +python_functions = test_* +addopts = + --verbose + --tb=short + --strict-markers +markers = + slow: marks tests as slow (deselect with '-m "not slow"') + integration: marks tests as integration tests \ No newline at end of file diff --git a/setup.cfg b/setup.cfg index 8c28267..08aedd7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,2 @@ [metadata] -description-file = README.md - +description_file = README.md diff --git a/test-requirements.txt b/test-requirements.txt index 8a35f17..d184a00 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,3 +1,3 @@ -nose -nose-setenv -mock +pytest>=7.0.0 +pytest-mock>=3.10.0 +pytest-cov>=4.0.0 diff --git a/tests/test_api.py b/tests/test_api.py index 0016f57..189f67a 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -1,48 +1,54 @@ # -*- coding: utf-8 -*- -from . import utils -from nose.tools import * +import pytest import detectlanguage import os -class TestApi(utils.TestCase): - def setUp(self): +class TestApi: + def setup_method(self): detectlanguage.configuration.api_key = os.environ['DETECTLANGUAGE_API_KEY'] - - def test_simple_detect(self): - result = detectlanguage.simple_detect("Hello world") - eq_('en', result) + + def test_detect_code(self): + result = detectlanguage.detect_code("Hello world") + assert result == 'en' def test_detect(self): result = detectlanguage.detect("Hello world") - eq_('en', result[0]['language']) + assert result[0]['language'] == 'en' + + def test_detect_with_array(self): + with pytest.warns(DeprecationWarning, match="use detect_batch"): + detectlanguage.detect(["Hello world", "Ėjo ežiukas"]) def test_detect_unicode(self): result = detectlanguage.detect("Ėjo ežiukas") - eq_('lt', result[0]['language']) + assert result[0]['language'] == 'lt' - def test_detect_array(self): - result = detectlanguage.detect(["Hello world", "Ėjo ežiukas"]) - eq_('en', result[0][0]['language']) - eq_('lt', result[1][0]['language']) + def test_detect_batch(self): + result = detectlanguage.detect_batch(["Hello world", "Ėjo ežiukas"]) + assert result[0][0]['language'] == 'en' + assert result[1][0]['language'] == 'lt' - def test_user_status(self): - result = detectlanguage.user_status() - eq_('ACTIVE', result['status']) + def test_account_status(self): + result = detectlanguage.account_status() + assert result['status'] == 'ACTIVE' def test_languages(self): result = detectlanguage.languages() - assert { 'code': 'en', 'name': 'ENGLISH' } in result + assert { 'code': 'en', 'name': 'English' } in result - def test_secure(self): - detectlanguage.configuration.secure = True - result = detectlanguage.detect("Hello world") - eq_('en', result[0]['language']) - detectlanguage.configuration.secure = False + def test_simple_detect(self): + with pytest.warns(DeprecationWarning, match="simple_detect.*deprecated"): + result = detectlanguage.simple_detect("Hello world") + assert result == 'en' + + def test_user_status(self): + with pytest.warns(DeprecationWarning, match="user_status.*deprecated"): + result = detectlanguage.user_status() + assert result['status'] == 'ACTIVE' -class TestApiErrors(utils.TestCase): - @raises(detectlanguage.DetectLanguageError) +class TestApiErrors: def test_invalid_key(self): detectlanguage.configuration.api_key = 'invalid' - detectlanguage.detect("Hello world") - \ No newline at end of file + with pytest.raises(detectlanguage.DetectLanguageError): + detectlanguage.detect("Hello world") diff --git a/tests/test_detectlanguage.py b/tests/test_detectlanguage.py index 4835a1e..c91be46 100644 --- a/tests/test_detectlanguage.py +++ b/tests/test_detectlanguage.py @@ -1,13 +1,11 @@ -from . import utils -from nose.tools import eq_ +import pytest import detectlanguage -class TestDetectlanguage(utils.TestCase): +class TestDetectlanguage: def testDefaults(self): - eq_('0.2', detectlanguage.configuration.api_version) - eq_('ws.detectlanguage.com', detectlanguage.configuration.host) - eq_(False, detectlanguage.configuration.secure) + assert detectlanguage.configuration.api_version == 'v3' + assert detectlanguage.configuration.host == 'ws.detectlanguage.com' def testConfiguration(self): detectlanguage.configuration.api_key = 'TEST' - eq_('TEST', detectlanguage.client.configuration.api_key) + assert detectlanguage.client.configuration.api_key == 'TEST' diff --git a/tests/test_proxy.py b/tests/test_proxy.py new file mode 100644 index 0000000..fd7f936 --- /dev/null +++ b/tests/test_proxy.py @@ -0,0 +1,47 @@ +import unittest +from unittest.mock import patch, MagicMock +import detectlanguage + + +class TestProxyConfiguration(unittest.TestCase): + def setUp(self): + detectlanguage.configuration.proxies = None + + def test_proxy_configuration(self): + """Test proxy configuration""" + detectlanguage.configuration.proxies = {'https': 'https://proxy.example.com:8080'} + self.assertEqual(detectlanguage.configuration.proxies, {'https': 'https://proxy.example.com:8080'}) + + @patch('requests.get') + def test_client_uses_proxy(self, mock_get): + """Test that client uses configured proxy""" + detectlanguage.configuration.proxies = {'https': 'https://proxy.example.com:8080'} + + mock_response = MagicMock() + mock_response.json.return_value = {'test': 'data'} + mock_response.raise_for_status.return_value = None + mock_get.return_value = mock_response + + detectlanguage.account_status() + + mock_get.assert_called_once() + self.assertEqual(mock_get.call_args[1]['proxies'], {'https': 'https://proxy.example.com:8080'}) + + @patch('requests.get') + def test_client_no_proxy_when_disabled(self, mock_get): + """Test that client doesn't use proxy when disabled""" + detectlanguage.configuration.proxies = None + + mock_response = MagicMock() + mock_response.json.return_value = {'test': 'data'} + mock_response.raise_for_status.return_value = None + mock_get.return_value = mock_response + + detectlanguage.account_status() + + mock_get.assert_called_once() + self.assertIsNone(mock_get.call_args[1]['proxies']) + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file diff --git a/tests/utils.py b/tests/utils.py deleted file mode 100644 index 9918cd5..0000000 --- a/tests/utils.py +++ /dev/null @@ -1,4 +0,0 @@ -import unittest - -class TestCase(unittest.TestCase): - pass \ No newline at end of file