FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
docker-python/tests/test_translation.py at master · Technology-Development/docker-python · GitHub
Technology-Development
/
docker-python
Public
forked from
Kaggle/docker-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
docker-python
/
tests
/
test_translation.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
131 lines (114 loc) · 5.73 KB
Breadcrumbs
docker-python
/
tests
/
test_translation.py
Copy path
File metadata and controls
131 lines (114 loc) · 5.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import
unittest
import
inspect
from
unittest
.
mock
import
Mock
,
patch
from
kaggle_gcp
import
KaggleKernelCredentials
,
KaggleKernelWithProjetCredentials
,
init_translation_v2
,
init_translation_v3
from
test
.
support
import
EnvironmentVarGuard
from
google
.
api_core
import
client_options
from
google
.
cloud
import
translate
,
translate_v2
def
_make_credentials
():
import
google
.
auth
.
credentials
return
Mock
(
spec
=
google
.
auth
.
credentials
.
Credentials
)
class
TestCloudTranslation
(
unittest
.
TestCase
):
class
FakeClient
:
def
__init__
(
self
,
credentials
=
None
,
client_info
=
None
,
client_options
=
None
,
**
kwargs
):
self
.
credentials
=
credentials
self
.
client_options
=
client_options
class
FakeConnection
():
def
__init__
(
self
,
user_agent
):
self
.
user_agent
=
user_agent
if
(
client_info
is
not
None
):
self
.
_connection
=
FakeConnection
(
client_info
.
user_agent
)
@
patch
(
"google.cloud.translate_v2.Client"
,
new
=
FakeClient
)
def
test_default_credentials_v2
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v2
()
client
=
translate_v2
.
Client
()
self
.
assertIsNotNone
(
client
.
credentials
)
self
.
assertIsInstance
(
client
.
credentials
,
KaggleKernelCredentials
)
@
patch
(
"google.cloud.translate_v2.Client"
,
new
=
FakeClient
)
def
test_user_provided_credentials_v2
(
self
):
credentials
=
_make_credentials
()
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v2
()
client
=
translate_v2
.
Client
(
credentials
=
credentials
)
self
.
assertIsNotNone
(
client
.
credentials
)
self
.
assertNotIsInstance
(
client
.
credentials
,
KaggleKernelCredentials
)
@
patch
(
"google.cloud.translate.TranslationServiceClient"
,
new
=
FakeClient
)
def
test_default_credentials_v3
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v3
()
client
=
translate
.
TranslationServiceClient
()
self
.
assertIsNotNone
(
client
.
credentials
)
self
.
assertIsInstance
(
client
.
credentials
,
KaggleKernelCredentials
)
@
patch
(
"google.cloud.translate.TranslationServiceClient"
,
new
=
FakeClient
)
def
test_user_provided_credentials_v3
(
self
):
credentials
=
_make_credentials
()
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v3
()
client
=
translate
.
TranslationServiceClient
(
credentials
=
credentials
)
self
.
assertIsNotNone
(
client
.
credentials
)
self
.
assertNotIsInstance
(
client
.
credentials
,
KaggleKernelCredentials
)
def
test_monkeypatching_succeed
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v2
()
init_translation_v3
()
# Translation V2
client2
=
translate_v2
.
Client
.
__init__
self
.
assertTrue
(
"kaggle_gcp"
in
inspect
.
getsourcefile
(
client2
))
# Translation V3
client3
=
translate
.
TranslationServiceClient
.
__init__
self
.
assertTrue
(
"kaggle_gcp"
in
inspect
.
getsourcefile
(
client3
))
def
test_monkeypatching_idempotent
(
self
):
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v2
()
init_translation_v3
()
# Translation V2
client2_1
=
translate_v2
.
client
.
Client
.
__init__
# Translation V3
client3_1
=
translate
.
TranslationServiceClient
.
__init__
init_translation_v2
()
init_translation_v3
()
client2_2
=
translate_v2
.
Client
.
__init__
client3_2
=
translate
.
TranslationServiceClient
.
__init__
self
.
assertEqual
(
client2_1
,
client2_2
)
self
.
assertEqual
(
client3_1
,
client3_2
)
@
patch
(
"google.cloud.translate.TranslationServiceClient"
,
new
=
FakeClient
)
def
test_client_credential_uniqueness_v3
(
self
):
"""
Client instance must use unique KaggleKernelWithProjetCredentials with quota_project_id
when client_options.quota_project_id provided. (even if quota_project_id is same)
"""
credentials
=
_make_credentials
()
env
=
EnvironmentVarGuard
()
env
.
set
(
'KAGGLE_USER_SECRETS_TOKEN'
,
'foobar'
)
env
.
set
(
'KAGGLE_KERNEL_INTEGRATIONS'
,
'CLOUDAI'
)
with
env
:
init_translation_v3
()
client1
=
translate
.
TranslationServiceClient
()
client2
=
translate
.
TranslationServiceClient
(
client_options
=
client_options
.
ClientOptions
(
quota_project_id
=
"_doesn't_matter"
))
client3
=
translate
.
TranslationServiceClient
(
client_options
=
client_options
.
ClientOptions
(
quota_project_id
=
"_doesn't_matter2"
))
self
.
assertIsNotNone
(
client1
.
credentials
)
self
.
assertIsNotNone
(
client2
.
credentials
)
self
.
assertIsNotNone
(
client3
.
credentials
)
self
.
assertIsInstance
(
client2
.
credentials
,
KaggleKernelWithProjetCredentials
)
self
.
assertIsInstance
(
client3
.
credentials
,
KaggleKernelWithProjetCredentials
)
self
.
assertNotEqual
(
client1
.
credentials
,
client2
.
credentials
)
self
.
assertNotEqual
(
client2
.
credentials
,
client3
.
credentials
)
Back
|
FazBrowse Home
|
New Git URL