-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from kxrob/raise_http_error
raise_for_status(), and Session for efficient http
- Loading branch information
Showing
12 changed files
with
221 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- _ci_test | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
## runs-on: windows-2019 | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
python-version: ['3.7', '3.10'] | ||
architecture: ['x64'] | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
|
||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
architecture: ${{ matrix.architecture }} | ||
|
||
- name: Show runner information | ||
run: | | ||
python --version | ||
pip --version | ||
# Minimal pip requirements for test & bdist_wheel | ||
- name: Install requirements | ||
run: make pip-install-build | ||
|
||
- name: Run tests | ||
run: make test | ||
|
||
- name: Build wheels | ||
run: | | ||
python setup.py bdist_wheel | ||
- uses: actions/upload-artifact@v2 | ||
if: ${{ always() }} | ||
with: | ||
name: wheels | ||
path: dist/*.whl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-r requirements.txt | ||
pytest | ||
pytest-cov | ||
vcrpy | ||
wheel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,15 +33,14 @@ def save_version(): | |
mo = re.search(VSRE, content_file, re.M) | ||
current_version = mo.group(1) | ||
|
||
content_file = content_file.replace(current_version, "{}".format(version)) | ||
content_file_new = content_file.replace(current_version, "{}".format(version)) | ||
|
||
if content_file_new == content_file: | ||
return | ||
with open(version_path, 'w') as version_file_write: | ||
version_file_write.write(content_file) | ||
|
||
|
||
save_version() | ||
|
||
|
||
class VersionCommand(Command): | ||
description = 'Show library version' | ||
user_options = [] | ||
|
@@ -57,7 +56,7 @@ def run(self): | |
|
||
|
||
# Get the long description | ||
with codecs.open(os.path.join(here, 'README.rst')) as f: | ||
with codecs.open(os.path.join(here, 'README.rst'), encoding='utf-8') as f: | ||
long_description = '\n{}'.format(f.read()) | ||
|
||
# Get change log | ||
|
@@ -73,7 +72,9 @@ def run(self): | |
tests_requirements = [line.replace('\n', '') for line in f.readlines() if not line == '-r requirements.txt\n'] | ||
|
||
|
||
setup( | ||
def run(): | ||
save_version() | ||
return setup( | ||
author='Terry Yin', | ||
author_email='[email protected]', | ||
classifiers=[ | ||
|
@@ -106,3 +107,6 @@ def run(self): | |
url='https://github.com/terryyin/google-translate-python', | ||
version=version, | ||
) | ||
|
||
if __name__ == "__main__": | ||
run() |
52 changes: 52 additions & 0 deletions
52
tests/fixtures/cassettes/test_translate_with_HTTPError.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
User-Agent: | ||
- Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebit/535.19(KHTML, like | ||
Gecko) Chrome/18.0.1025.168 Safari/535.19 | ||
method: GET | ||
uri: http://api.mymemory.translated.net/get-nonsense?q=hello&langpair=en%7Cde | ||
response: | ||
body: | ||
string: '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> | ||
<html><head> | ||
<title>404 Not Found</title> | ||
</head><body> | ||
<h1>Not Found</h1> | ||
<p>The requested URL was not found on this server.</p> | ||
<hr> | ||
<address>Apache/2.4.29 (Ubuntu) Server at api.mymemory.translated.net Port | ||
80</address> | ||
</body></html> | ||
' | ||
headers: | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '289' | ||
Content-Type: | ||
- text/html; charset=iso-8859-1 | ||
Date: | ||
- Mon, 28 Feb 2022 16:27:12 GMT | ||
Server: | ||
- Apache/2.4.29 (Ubuntu) | ||
status: | ||
code: 404 | ||
message: Not Found | ||
version: 1 |
50 changes: 50 additions & 0 deletions
50
tests/fixtures/cassettes/test_translate_with_status_error.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
interactions: | ||
- request: | ||
body: null | ||
headers: | ||
Accept: | ||
- '*/*' | ||
Accept-Encoding: | ||
- gzip, deflate | ||
Connection: | ||
- keep-alive | ||
User-Agent: | ||
- Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebit/535.19(KHTML, like | ||
Gecko) Chrome/18.0.1025.168 Safari/535.19 | ||
method: GET | ||
uri: http://api.mymemory.translated.net/get?q=hello+again%21&langpair=en%7Cde&de=invalid | ||
response: | ||
body: | ||
string: '{"responseData":{"translatedText":"INVALID EMAIL PROVIDED"},"quotaFinished":null,"mtLangSupported":null,"responseDetails":"INVALID | ||
EMAIL PROVIDED","responseStatus":"403","responderId":null,"exception_code":null,"matches":""}' | ||
headers: | ||
Access-Control-Allow-Origin: | ||
- '*' | ||
Cache-Control: | ||
- no-cache, no-store, max-age=0, must-revalidate | ||
Connection: | ||
- keep-alive | ||
Content-Length: | ||
- '224' | ||
Content-Type: | ||
- application/json; charset=utf-8 | ||
Date: | ||
- Mon, 28 Feb 2022 17:46:15 GMT | ||
Expires: | ||
- Fri, 01 Jan 1990 00:00:00 GMT | ||
Pragma: | ||
- no-cache | ||
Server: | ||
- Apache/2.4.29 (Ubuntu) | ||
X-Backend-Content-Length: | ||
- '10' | ||
X-Embedded-Status: | ||
- '200' | ||
X-Frame-Options: | ||
- SAMEORIGIN | ||
X-XSS-Protection: | ||
- '0' | ||
status: | ||
code: 200 | ||
message: OK | ||
version: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
except Exception: | ||
import mock | ||
|
||
from translate.providers import MyMemoryProvider, MicrosoftProvider, LibreProvider | ||
from translate.providers import MyMemoryProvider, MicrosoftProvider | ||
|
||
|
||
def test_provider_mymemory_languages_attribute(): | ||
|
@@ -21,27 +21,23 @@ def test_provider_mymemory_default_email(): | |
assert provider.email == '' | ||
|
||
|
||
@mock.patch('requests.get') | ||
@mock.patch('requests.Session.get') | ||
def test_provider_mymemory_make_request_with_valid_email(mock_requests): | ||
mock_requests.return_value.json.return_value = {} | ||
valid_email = '[email protected]' | ||
provider = MyMemoryProvider(to_lang='en', headers={}, email=valid_email) | ||
provider._make_request('test') == {} | ||
assert mock_requests.call_args[1]['params']['de'] == valid_email == provider.email | ||
|
||
provider = MyMemoryProvider(to_lang='de', headers={}, email=valid_email) | ||
provider._make_request('test') | ||
assert mock_requests._mock_call_args[1]['params']['de'] == valid_email == provider.email | ||
|
||
@mock.patch('requests.post') | ||
@mock.patch('requests.get') | ||
def xtest_provider_microsoft_make_request(mock_requests_get, mock_requests_post): | ||
class MockRequests: | ||
content = b'token' | ||
|
||
@mock.patch('requests.Session.post') | ||
def test_provider_microsoft_make_request(mock_requests_post): | ||
class MockResponse: | ||
text = '"dummyjson"' | ||
def raise_for_status(self): | ||
return False | ||
|
||
mock_requests_get.return_value.text.return_value = '' | ||
mock_requests_post.return_value = MockRequests() | ||
mock_requests_post.return_value = MockResponse() | ||
provider = MicrosoftProvider(to_lang='en', headers={}, secret_access_key='secret') | ||
provider._make_request('test') | ||
assert mock_requests_get.called | ||
assert mock_requests_post.called |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.