@@ -30,6 +30,31 @@ def test_ctor_defaults(self):
3030 self .assertEqual (grant .entity_type , 'userByEmail' )
3131 self .assertEqual (grant .entity_id , 'phred@example.com' )
3232
33+ def test_ctor_bad_entity_type (self ):
34+ with self .assertRaises (ValueError ):
35+ self ._makeOne (None , 'unknown' , None )
36+
37+ def test_ctor_view_with_role (self ):
38+ role = 'READER'
39+ entity_type = 'view'
40+ with self .assertRaises (ValueError ):
41+ self ._makeOne (role , entity_type , None )
42+
43+ def test_ctor_view_success (self ):
44+ role = None
45+ entity_type = 'view'
46+ entity_id = object ()
47+ grant = self ._makeOne (role , entity_type , entity_id )
48+ self .assertEqual (grant .role , role )
49+ self .assertEqual (grant .entity_type , entity_type )
50+ self .assertEqual (grant .entity_id , entity_id )
51+
52+ def test_ctor_nonview_without_role (self ):
53+ role = None
54+ entity_type = 'userByEmail'
55+ with self .assertRaises (ValueError ):
56+ self ._makeOne (role , entity_type , None )
57+
3358
3459class TestDataset (unittest2 .TestCase ):
3560 PROJECT = 'project'
@@ -249,39 +274,27 @@ def test_from_api_repr_w_properties(self):
249274 self ._verifyResourceProperties (dataset , RESOURCE )
250275
251276 def test__parse_access_grants_w_unknown_entity_type (self ):
252- USER_EMAIL = 'phred@example.com'
253- GROUP_EMAIL = 'group-name@lists.example.com'
254- RESOURCE = {
255- 'access' : [
256- {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
257- {'role' : 'WRITER' , 'groupByEmail' : GROUP_EMAIL },
258- {'role' : 'READER' , 'specialGroup' : 'projectReaders' },
259- {'role' : 'READER' , 'unknown' : 'UNKNOWN' }]
260- }
277+ ACCESS = [
278+ {'role' : 'READER' , 'unknown' : 'UNKNOWN' },
279+ ]
261280 client = _Client (self .PROJECT )
262281 dataset = self ._makeOne (self .DS_NAME , client = client )
263- grants = dataset . _parse_access_grants ( RESOURCE [ 'access' ])
264- self . _verifyAccessGrants ( grants , RESOURCE )
282+ with self . assertRaises ( ValueError ):
283+ dataset . _parse_access_grants ( ACCESS )
265284
266- def test__parse_access_grants_w_multiple_entity_types (self ):
267- # Hypothetical case: we don't know that the back-end will ever
268- # return such structures, but they are logical. See:
269- # https://github.com/GoogleCloudPlatform/gcloud-python/pull/1046#discussion_r36687769
285+ def test__parse_access_grants_w_extra_keys (self ):
270286 USER_EMAIL = 'phred@example.com'
271- OTHER_EMAIL = 'bharney@example.com'
272- GROUP_EMAIL = 'group-name@lists.example.com'
273- RESOURCE = {
274- 'access' : [
275- {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
276- {'role' : 'WRITER' , 'groupByEmail' : GROUP_EMAIL },
277- {'role' : 'READER' ,
278- 'specialGroup' : 'projectReaders' ,
279- 'userByEmail' : OTHER_EMAIL }]
280- }
287+ ACCESS = [
288+ {
289+ 'role' : 'READER' ,
290+ 'specialGroup' : 'projectReaders' ,
291+ 'userByEmail' : USER_EMAIL ,
292+ },
293+ ]
281294 client = _Client (self .PROJECT )
282295 dataset = self ._makeOne (self .DS_NAME , client = client )
283- grants = dataset . _parse_access_grants ( RESOURCE [ 'access' ])
284- self . _verifyAccessGrants ( grants , RESOURCE )
296+ with self . assertRaises ( ValueError ):
297+ dataset . _parse_access_grants ( ACCESS )
285298
286299 def test_create_w_bound_client (self ):
287300 PATH = 'projects/%s/datasets' % self .PROJECT
@@ -320,11 +333,19 @@ def test_create_w_alternate_client(self):
320333 dataset = self ._makeOne (self .DS_NAME , client = CLIENT1 )
321334 dataset .friendly_name = TITLE
322335 dataset .description = DESCRIPTION
336+ VIEW = {
337+ 'projectId' : 'my-proj' ,
338+ 'datasetId' : 'starry-skies' ,
339+ 'tableId' : 'northern-hemisphere' ,
340+ }
323341 dataset .access_grants = [
324342 AccessGrant ('OWNER' , 'userByEmail' , USER_EMAIL ),
325343 AccessGrant ('OWNER' , 'groupByEmail' , GROUP_EMAIL ),
344+ AccessGrant ('READER' , 'domain' , 'foo.com' ),
326345 AccessGrant ('READER' , 'specialGroup' , 'projectReaders' ),
327- AccessGrant ('WRITER' , 'specialGroup' , 'projectWriters' )]
346+ AccessGrant ('WRITER' , 'specialGroup' , 'projectWriters' ),
347+ AccessGrant (None , 'view' , VIEW ),
348+ ]
328349
329350 dataset .create (client = CLIENT2 )
330351
@@ -334,15 +355,20 @@ def test_create_w_alternate_client(self):
334355 self .assertEqual (req ['method' ], 'POST' )
335356 self .assertEqual (req ['path' ], '/%s' % PATH )
336357 SENT = {
337- 'datasetReference' :
338- {'projectId' : self .PROJECT , 'datasetId' : self .DS_NAME },
358+ 'datasetReference' : {
359+ 'projectId' : self .PROJECT ,
360+ 'datasetId' : self .DS_NAME ,
361+ },
339362 'description' : DESCRIPTION ,
340363 'friendlyName' : TITLE ,
341364 'access' : [
342365 {'role' : 'OWNER' , 'userByEmail' : USER_EMAIL },
343366 {'role' : 'OWNER' , 'groupByEmail' : GROUP_EMAIL },
367+ {'role' : 'READER' , 'domain' : 'foo.com' },
344368 {'role' : 'READER' , 'specialGroup' : 'projectReaders' },
345- {'role' : 'WRITER' , 'specialGroup' : 'projectWriters' }],
369+ {'role' : 'WRITER' , 'specialGroup' : 'projectWriters' },
370+ {'view' : VIEW },
371+ ],
346372 }
347373 self .assertEqual (req ['data' ], SENT )
348374 self ._verifyResourceProperties (dataset , RESOURCE )
0 commit comments