@@ -87,6 +87,7 @@ describe('BigQuery/Dataset', () => {
8787 } as { } as _root . BigQuery ;
8888 const DATASET_ID = 'kittens' ;
8989 const LOCATION = 'asia-northeast1' ;
90+ const ANOTHER_PROJECT_ID = 'another-test-project' ;
9091
9192 // tslint:disable-next-line variable-name
9293 let Dataset : typeof _root . Dataset ;
@@ -148,6 +149,12 @@ describe('BigQuery/Dataset', () => {
148149 assert . strictEqual ( ds . location , LOCATION ) ;
149150 } ) ;
150151
152+ it ( 'should set the client projectId by default' , ( ) => {
153+ const ds = new Dataset ( BIGQUERY , DATASET_ID ) ;
154+
155+ assert . strictEqual ( ds . projectId , BIGQUERY . projectId ) ;
156+ } ) ;
157+
151158 it ( 'should capture user provided projectId' , ( ) => {
152159 const projectIdOverride = 'octavia' ;
153160 const options = { projectId : projectIdOverride } ;
@@ -171,7 +178,9 @@ describe('BigQuery/Dataset', () => {
171178 } ) ;
172179
173180 it ( 'should call through to BigQuery#createDataset' , done => {
174- const OPTIONS = { } ;
181+ const OPTIONS = {
182+ projectId : BIGQUERY . projectId ,
183+ } ;
175184
176185 bq . createDataset = ( id : string , options : { } , callback : Function ) => {
177186 assert . strictEqual ( id , DATASET_ID ) ;
@@ -249,6 +258,7 @@ describe('BigQuery/Dataset', () => {
249258 json : {
250259 etag : FAKE_ETAG ,
251260 } ,
261+ uri : `/projects/${ BIGQUERY . projectId } /` ,
252262 } ;
253263
254264 const reqOpts = interceptor . request ( fakeReqOpts ) ;
@@ -266,6 +276,7 @@ describe('BigQuery/Dataset', () => {
266276 json : {
267277 etag : FAKE_ETAG ,
268278 } ,
279+ uri : `/projects/${ BIGQUERY . projectId } /` ,
269280 } ;
270281
271282 const expectedHeaders = Object . assign ( { } , fakeReqOpts . headers , {
@@ -284,6 +295,7 @@ describe('BigQuery/Dataset', () => {
284295 json : {
285296 etag : FAKE_ETAG ,
286297 } ,
298+ uri : `/projects/${ BIGQUERY . projectId } /` ,
287299 } ;
288300
289301 const reqOpts = interceptor . request ( fakeReqOpts ) ;
@@ -428,6 +440,7 @@ describe('BigQuery/Dataset', () => {
428440 const API_RESPONSE = {
429441 tableReference : {
430442 tableId : TABLE_ID ,
443+ projectId : BIGQUERY . projectId ,
431444 } ,
432445 } ;
433446
@@ -443,10 +456,7 @@ describe('BigQuery/Dataset', () => {
443456 const body = reqOpts . json ;
444457 assert . deepStrictEqual ( body . schema , SCHEMA_OBJECT ) ;
445458 assert . strictEqual ( body . tableReference . datasetId , DATASET_ID ) ;
446- assert . strictEqual (
447- body . tableReference . projectId ,
448- ds . bigQuery . projectId
449- ) ;
459+ assert . strictEqual ( body . tableReference . projectId , ds . projectId ) ;
450460 assert . strictEqual ( body . tableReference . tableId , TABLE_ID ) ;
451461
452462 done ( ) ;
@@ -455,6 +465,31 @@ describe('BigQuery/Dataset', () => {
455465 ds . createTable ( TABLE_ID , options , assert . ifError ) ;
456466 } ) ;
457467
468+ it ( 'should create a table on a different project' , done => {
469+ const options = {
470+ schema : SCHEMA_OBJECT ,
471+ } ;
472+ const anotherDs = new Dataset ( BIGQUERY , DATASET_ID , {
473+ projectId : ANOTHER_PROJECT_ID ,
474+ } ) as any ;
475+ anotherDs . request = ( reqOpts : DecorateRequestOptions ) => {
476+ assert . strictEqual ( reqOpts . method , 'POST' ) ;
477+ assert . strictEqual ( reqOpts . uri , '/tables' ) ;
478+
479+ const body = reqOpts . json ;
480+ assert . deepStrictEqual ( body . schema , SCHEMA_OBJECT ) ;
481+ assert . strictEqual ( body . tableReference . datasetId , DATASET_ID ) ;
482+ assert . strictEqual ( body . tableReference . projectId , ANOTHER_PROJECT_ID ) ;
483+ assert . strictEqual ( body . tableReference . tableId , TABLE_ID ) ;
484+
485+ done ( ) ;
486+ } ;
487+
488+ // Under the hood dataset.createTable is called
489+ const table = anotherDs . table ( TABLE_ID ) ;
490+ table . create ( options , assert . ifError ) ;
491+ } ) ;
492+
458493 it ( 'should not require options' , done => {
459494 ds . request = ( reqOpts : DecorateRequestOptions , callback : Function ) => {
460495 callback ( null , API_RESPONSE ) ;
@@ -577,6 +612,22 @@ describe('BigQuery/Dataset', () => {
577612 ds . createTable ( TABLE_ID , { schema : SCHEMA_OBJECT } , assert . ifError ) ;
578613 } ) ;
579614
615+ it ( 'should pass the projectId to the Table' , done => {
616+ const response = Object . assign ( { location : LOCATION } , API_RESPONSE ) ;
617+
618+ ds . request = ( reqOpts : DecorateRequestOptions , callback : Function ) => {
619+ callback ( null , response ) ;
620+ } ;
621+
622+ ds . table = ( id : string , options : TableOptions ) => {
623+ assert . strictEqual ( options . location , LOCATION ) ;
624+ setImmediate ( done ) ;
625+ return { } ;
626+ } ;
627+
628+ ds . createTable ( TABLE_ID , { schema : SCHEMA_OBJECT } , assert . ifError ) ;
629+ } ) ;
630+
580631 it ( 'should return an apiResponse' , done => {
581632 const opts = { id : TABLE_ID , schema : SCHEMA_OBJECT } ;
582633
0 commit comments