MCPcopy Create free account
hub / github.com/Kaggle/docker-python / TestCloudNaturalLanguage

Class TestCloudNaturalLanguage

tests/test_natural_language.py:14–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

12 return Mock(spec=google.auth.credentials.Credentials)
13
14class TestCloudNaturalLanguage(unittest.TestCase):
15 class FakeClient:
16 def __init__(self, credentials=None, client_info=None, **kwargs):
17 self.credentials = credentials
18
19 class FakeConnection():
20 def __init__(self, user_agent):
21 self.user_agent = user_agent
22 if (client_info is not None):
23 self._connection = FakeConnection(client_info.user_agent)
24
25 @patch("google.cloud.language.LanguageServiceClient", new=FakeClient)
26 def test_default_credentials(self):
27 env = EnvironmentVarGuard()
28 env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
29 env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI')
30 with env:
31 init_natural_language()
32 client = language.LanguageServiceClient()
33 self.assertIsNotNone(client.credentials)
34 self.assertIsInstance(client.credentials, KaggleKernelCredentials)
35
36 @patch("google.cloud.language.LanguageServiceClient", new=FakeClient)
37 def test_user_provided_credentials(self):
38 credentials = _make_credentials()
39 env = EnvironmentVarGuard()
40 env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
41 env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI')
42 with env:
43 init_natural_language()
44 client = language.LanguageServiceClient(credentials=credentials)
45 self.assertIsNotNone(client.credentials)
46 self.assertNotIsInstance(client.credentials, KaggleKernelCredentials)
47
48
49 def test_monkeypatching_succeed(self):
50 env = EnvironmentVarGuard()
51 env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
52 env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI')
53 with env:
54 init_natural_language()
55 client = language.LanguageServiceClient.__init__
56 self.assertTrue("kaggle_gcp" in inspect.getsourcefile(client))
57
58 def test_monkeypatching_idempotent(self):
59 env = EnvironmentVarGuard()
60 env.set('KAGGLE_USER_SECRETS_TOKEN', 'foobar')
61 env.set('KAGGLE_KERNEL_INTEGRATIONS', 'CLOUDAI')
62 with env:
63 init_natural_language()
64 client1 = language.LanguageServiceClient.__init__
65 init_natural_language()
66 client2 = language.LanguageServiceClient.__init__
67 self.assertEqual(client1, client2)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected