Skip to content

Commit

Permalink
Add project initialization tests.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 710722982
  • Loading branch information
Nate Schmitz authored and Google Earth Engine Authors committed Dec 30, 2024
1 parent 565f49b commit 4e49fe9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
1 change: 0 additions & 1 deletion .github/workflows/ci-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# TODO(user): Fix or skip tests that fail on GitHub
name: ci-tests
on: [
push,
Expand Down
47 changes: 47 additions & 0 deletions python/ee/tests/ee_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,53 @@ def MockAlgorithms():
self.assertEqual(ee.ApiFunction._api, {})
self.assertFalse(ee.Image._initialized)

def testProjectInitialization(self):
"""Verifies that we can fetch the client project from many locations.
This also exercises the logic in data.get_persistent_credentials.
"""

cred_args = dict(refresh_token='rt', quota_project_id='qp1')
google_creds = credentials.Credentials(token=None, quota_project_id='qp2')
expected_project = None

def CheckDataInit(**kwargs):
self.assertEqual(expected_project, kwargs.get('project'))

moc = mock.patch.object
with (moc(ee.oauth, 'get_credentials_arguments', new=lambda: cred_args),
moc(ee.oauth, 'is_valid_credentials', new=lambda _: True),
moc(google.auth, 'default', new=lambda: (google_creds, None)),
moc(ee.data, 'initialize', side_effect=CheckDataInit) as inits):
expected_project = 'qp0'
ee.Initialize(project='qp0')

expected_project = 'qp1'
ee.Initialize()

cred_args['refresh_token'] = None
ee.Initialize()

cred_args['quota_project_id'] = None
expected_project = 'qp2'
ee.Initialize()

google_creds = google_creds.with_quota_project(None)
expected_project = None
ee.Initialize()
self.assertEqual(5, inits.call_count)

msg = 'Earth Engine API has not been used in project 764086051850 before'
with moc(ee.ApiFunction, 'initialize', side_effect=ee.EEException(msg)):
with self.assertRaisesRegex(ee.EEException, '.*no project found..*'):
ee.Initialize()

cred_args['client_id'] = '764086051850-xxx' # dummy usable-auth client
cred_args['refresh_token'] = 'rt'
with self.assertRaisesRegex(ee.EEException, '.*no project found..*'):
ee.Initialize()
self.assertEqual(6, inits.call_count)

def testCallAndApply(self):
"""Verifies library initialization."""

Expand Down

0 comments on commit 4e49fe9

Please sign in to comment.