-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add Dataset access support #1046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ba3109d
Add 'Dataset.access_grants' property.
tseaver e2fa8e4
.coveragerc already disables '__repr__'.
tseaver cc746f5
Fix testcase class name after rename.
tseaver 786288d
Fix backing attribute name after rename.
tseaver e72948c
Fix copy-pasta in docstring summary line.
tseaver 9c5d6dc
Don't assume back-end will return only documented entity types.
tseaver f7fdc6f
Py3k / lint fixes.
tseaver 09c1d14
Work around hypothetical case parsing 'dataset.access_grants':
tseaver 6720b35
Defend against hash randomization non-determanism.
tseaver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -15,6 +15,22 @@ | |
| import unittest2 | ||
|
|
||
|
|
||
| class TestAccessGrant(unittest2.TestCase): | ||
|
|
||
| def _getTargetClass(self): | ||
| from gcloud.bigquery.dataset import AccessGrant | ||
| return AccessGrant | ||
|
|
||
| def _makeOne(self, *args, **kw): | ||
| return self._getTargetClass()(*args, **kw) | ||
|
|
||
| def test_ctor_defaults(self): | ||
| grant = self._makeOne('OWNER', 'userByEmail', '[email protected]') | ||
| self.assertEqual(grant.role, 'OWNER') | ||
| self.assertEqual(grant.entity_type, 'userByEmail') | ||
| self.assertEqual(grant.entity_id, '[email protected]') | ||
|
|
||
|
|
||
| class TestDataset(unittest2.TestCase): | ||
| PROJECT = 'project' | ||
| DS_NAME = 'dataset-name' | ||
|
|
@@ -39,6 +55,8 @@ def _setUpConstants(self): | |
|
|
||
| def _makeResource(self): | ||
| self._setUpConstants() | ||
| USER_EMAIL = '[email protected]' | ||
| GROUP_EMAIL = '[email protected]' | ||
| return { | ||
| 'creationTime': self.WHEN_TS * 1000, | ||
| 'datasetReference': | ||
|
|
@@ -48,10 +66,32 @@ def _makeResource(self): | |
| 'lastModifiedTime': self.WHEN_TS * 1000, | ||
| 'location': 'US', | ||
| 'selfLink': self.RESOURCE_URL, | ||
| 'access': [ | ||
| {'role': 'OWNER', 'userByEmail': USER_EMAIL}, | ||
| {'role': 'OWNER', 'groupByEmail': GROUP_EMAIL}, | ||
| {'role': 'WRITER', 'specialGroup': 'projectWriters'}, | ||
| {'role': 'READER', 'specialGroup': 'projectReaders'}], | ||
| } | ||
|
|
||
| def _verifyResourceProperties(self, dataset, resource): | ||
| def _verifyAccessGrants(self, access_grants, resource): | ||
| r_grants = [] | ||
| for r_grant in resource['access']: | ||
| role = r_grant.pop('role') | ||
| for entity_type, entity_id in sorted(r_grant.items()): | ||
| r_grants.append({'role': role, | ||
| 'entity_type': entity_type, | ||
| 'entity_id': entity_id}) | ||
|
|
||
| self.assertEqual(len(access_grants), len(r_grants)) | ||
| for a_grant, r_grant in zip(access_grants, r_grants): | ||
| self.assertEqual(a_grant.role, r_grant['role']) | ||
| self.assertEqual(a_grant.entity_type, r_grant['entity_type']) | ||
| self.assertEqual(a_grant.entity_id, r_grant['entity_id']) | ||
|
|
||
| def _verifyReadonlyResourceProperties(self, dataset, resource): | ||
|
|
||
| self.assertEqual(dataset.dataset_id, self.DS_ID) | ||
|
|
||
| if 'creationTime' in resource: | ||
| self.assertEqual(dataset.created, self.WHEN) | ||
| else: | ||
|
|
@@ -69,12 +109,21 @@ def _verifyResourceProperties(self, dataset, resource): | |
| else: | ||
| self.assertEqual(dataset.self_link, None) | ||
|
|
||
| def _verifyResourceProperties(self, dataset, resource): | ||
|
|
||
| self._verifyReadonlyResourceProperties(dataset, resource) | ||
|
|
||
| self.assertEqual(dataset.default_table_expiration_ms, | ||
| resource.get('defaultTableExpirationMs')) | ||
| self.assertEqual(dataset.description, resource.get('description')) | ||
| self.assertEqual(dataset.friendly_name, resource.get('friendlyName')) | ||
| self.assertEqual(dataset.location, resource.get('location')) | ||
|
|
||
| if 'access' in resource: | ||
| self._verifyAccessGrants(dataset.access_grants, resource) | ||
| else: | ||
| self.assertEqual(dataset.access_grants, []) | ||
|
|
||
| def test_ctor(self): | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client) | ||
|
|
@@ -84,6 +133,7 @@ def test_ctor(self): | |
| self.assertEqual( | ||
| dataset.path, | ||
| '/projects/%s/datasets/%s' % (self.PROJECT, self.DS_NAME)) | ||
| self.assertEqual(dataset.access_grants, []) | ||
|
|
||
| self.assertEqual(dataset.created, None) | ||
| self.assertEqual(dataset.dataset_id, None) | ||
|
|
@@ -96,6 +146,29 @@ def test_ctor(self): | |
| self.assertEqual(dataset.friendly_name, None) | ||
| self.assertEqual(dataset.location, None) | ||
|
|
||
| def test_access_roles_setter_non_list(self): | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client) | ||
| with self.assertRaises(TypeError): | ||
| dataset.access_grants = object() | ||
|
|
||
| def test_access_roles_setter_invalid_field(self): | ||
| from gcloud.bigquery.dataset import AccessGrant | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client) | ||
| phred = AccessGrant('OWNER', 'userByEmail', '[email protected]') | ||
| with self.assertRaises(ValueError): | ||
| dataset.access_grants = [phred, object()] | ||
|
|
||
| def test_access_roles_setter(self): | ||
| from gcloud.bigquery.dataset import AccessGrant | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client) | ||
| phred = AccessGrant('OWNER', 'userByEmail', '[email protected]') | ||
| bharney = AccessGrant('OWNER', 'userByEmail', '[email protected]') | ||
| dataset.access_grants = [phred, bharney] | ||
| self.assertEqual(dataset.access_grants, [phred, bharney]) | ||
|
|
||
| def test_default_table_expiration_ms_setter_bad_value(self): | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client) | ||
|
|
@@ -175,6 +248,41 @@ def test_from_api_repr_w_properties(self): | |
| self.assertTrue(dataset._client is client) | ||
| self._verifyResourceProperties(dataset, RESOURCE) | ||
|
|
||
| def test__parse_access_grants_w_unknown_entity_type(self): | ||
| USER_EMAIL = '[email protected]' | ||
| GROUP_EMAIL = '[email protected]' | ||
| RESOURCE = { | ||
| 'access': [ | ||
| {'role': 'OWNER', 'userByEmail': USER_EMAIL}, | ||
| {'role': 'WRITER', 'groupByEmail': GROUP_EMAIL}, | ||
| {'role': 'READER', 'specialGroup': 'projectReaders'}, | ||
| {'role': 'READER', 'unknown': 'UNKNOWN'}] | ||
| } | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client=client) | ||
| grants = dataset._parse_access_grants(RESOURCE['access']) | ||
| self._verifyAccessGrants(grants, RESOURCE) | ||
|
|
||
| def test__parse_access_grants_w_multiple_entity_types(self): | ||
| # Hypothetical case: we don't know that the back-end will ever | ||
| # return such structures, but they are logical. See: | ||
| # https://github.com/GoogleCloudPlatform/gcloud-python/pull/1046#discussion_r36687769 | ||
| USER_EMAIL = '[email protected]' | ||
| OTHER_EMAIL = '[email protected]' | ||
| GROUP_EMAIL = '[email protected]' | ||
| RESOURCE = { | ||
| 'access': [ | ||
| {'role': 'OWNER', 'userByEmail': USER_EMAIL}, | ||
| {'role': 'WRITER', 'groupByEmail': GROUP_EMAIL}, | ||
| {'role': 'READER', | ||
| 'specialGroup': 'projectReaders', | ||
| 'userByEmail': OTHER_EMAIL}] | ||
| } | ||
| client = _Client(self.PROJECT) | ||
| dataset = self._makeOne(self.DS_NAME, client=client) | ||
| grants = dataset._parse_access_grants(RESOURCE['access']) | ||
| self._verifyAccessGrants(grants, RESOURCE) | ||
|
|
||
| def test_create_w_bound_client(self): | ||
| PATH = 'projects/%s/datasets' % self.PROJECT | ||
| RESOURCE = self._makeResource() | ||
|
|
@@ -196,7 +304,10 @@ def test_create_w_bound_client(self): | |
| self._verifyResourceProperties(dataset, RESOURCE) | ||
|
|
||
| def test_create_w_alternate_client(self): | ||
| from gcloud.bigquery.dataset import AccessGrant | ||
| PATH = 'projects/%s/datasets' % self.PROJECT | ||
| USER_EMAIL = '[email protected]' | ||
| GROUP_EMAIL = '[email protected]' | ||
| DESCRIPTION = 'DESCRIPTION' | ||
| TITLE = 'TITLE' | ||
| RESOURCE = self._makeResource() | ||
|
|
@@ -209,6 +320,11 @@ def test_create_w_alternate_client(self): | |
| dataset = self._makeOne(self.DS_NAME, client=CLIENT1) | ||
| dataset.friendly_name = TITLE | ||
| dataset.description = DESCRIPTION | ||
| dataset.access_grants = [ | ||
| AccessGrant('OWNER', 'userByEmail', USER_EMAIL), | ||
| AccessGrant('OWNER', 'groupByEmail', GROUP_EMAIL), | ||
| AccessGrant('READER', 'specialGroup', 'projectReaders'), | ||
| AccessGrant('WRITER', 'specialGroup', 'projectWriters')] | ||
|
|
||
| dataset.create(client=CLIENT2) | ||
|
|
||
|
|
@@ -222,6 +338,11 @@ def test_create_w_alternate_client(self): | |
| {'projectId': self.PROJECT, 'datasetId': self.DS_NAME}, | ||
| 'description': DESCRIPTION, | ||
| 'friendlyName': TITLE, | ||
| 'access': [ | ||
| {'role': 'OWNER', 'userByEmail': USER_EMAIL}, | ||
| {'role': 'OWNER', 'groupByEmail': GROUP_EMAIL}, | ||
| {'role': 'READER', 'specialGroup': 'projectReaders'}, | ||
| {'role': 'WRITER', 'specialGroup': 'projectWriters'}], | ||
| } | ||
| self.assertEqual(req['data'], SENT) | ||
| self._verifyResourceProperties(dataset, RESOURCE) | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.