@@ -2,7 +2,7 @@ import { Dataset, Table } from "@google-cloud/bigquery";
22import { expect } from "chai" ;
33import { anything , instance , mock , verify , when } from "ts-mockito" ;
44
5- import { BigQueryDbAdapter } from "df/cli/api/dbadapters/bigquery" ;
5+ import { BigQueryDbAdapter , StaticBigQueryClientProvider } from "df/cli/api/dbadapters/bigquery" ;
66import { dataform } from "df/protos/ts" ;
77import { suite , test } from "df/testing" ;
88
@@ -17,12 +17,12 @@ suite("BigQueryDbAdapter", () => {
1717 const projectId = "project1" ;
1818
1919 const credentials = dataform . BigQuery . create ( { projectId, location : "US" } ) ;
20- const adapter = new BigQueryDbAdapter ( credentials , { bigqueryClient : instance ( mockBigQuery ) } ) ;
20+ const adapter = new BigQueryDbAdapter ( credentials , { clientProvider : new StaticBigQueryClientProvider ( instance ( mockBigQuery ) ) } ) ;
2121
2222 when ( mockBigQuery . dataset ( schemaName ) ) . thenReturn ( instance ( mockDataset ) ) ;
2323 // getTables returns an array where the first element is an array of tables.
2424 // Each table object needs an 'id' property.
25- when ( mockDataset . getTables ( ) ) . thenReturn ( Promise . resolve ( [ [ { id : tableName } ] ] as any ) ) ;
25+ when ( mockDataset . getTables ( anything ( ) ) ) . thenReturn ( Promise . resolve ( [ [ { id : tableName } ] ] as any ) ) ;
2626 when ( mockDataset . table ( tableName ) ) . thenReturn ( instance ( mockTable ) ) ;
2727 when ( mockTable . getMetadata ( ) ) . thenReturn (
2828 Promise . resolve ( [
@@ -54,10 +54,10 @@ suite("BigQueryDbAdapter", () => {
5454 const projectId = "project" ;
5555
5656 const credentials = dataform . BigQuery . create ( { projectId, location : "US" } ) ;
57- const adapter = new BigQueryDbAdapter ( credentials , { bigqueryClient : instance ( mockBigQuery ) } ) ;
57+ const adapter = new BigQueryDbAdapter ( credentials , { clientProvider : new StaticBigQueryClientProvider ( instance ( mockBigQuery ) ) } ) ;
5858
5959 when ( mockBigQuery . dataset ( schemaName ) ) . thenReturn ( instance ( mockDataset ) ) ;
60- when ( mockDataset . getTables ( ) ) . thenReturn ( Promise . resolve ( [ [ { id : tableName } ] ] as any ) ) ;
60+ when ( mockDataset . getTables ( anything ( ) ) ) . thenReturn ( Promise . resolve ( [ [ { id : tableName } ] ] as any ) ) ;
6161 when ( mockDataset . table ( tableName ) ) . thenReturn ( instance ( mockTable ) ) ;
6262 when ( mockTable . getMetadata ( ) ) . thenReturn (
6363 Promise . resolve ( [
@@ -70,7 +70,7 @@ suite("BigQueryDbAdapter", () => {
7070 ] as any )
7171 ) ;
7272
73- when ( mockBigQuery . getDatasets ( ) ) . thenReturn ( Promise . resolve ( [ [ { id : schemaName } ] ] as any ) ) ;
73+ when ( mockBigQuery . getDatasets ( anything ( ) ) ) . thenReturn ( Promise . resolve ( [ [ { id : schemaName } ] ] as any ) ) ;
7474
7575 const result = await adapter . tables ( projectId ) ;
7676
@@ -79,4 +79,67 @@ suite("BigQueryDbAdapter", () => {
7979 expect ( result [ 0 ] . target . schema ) . to . equal ( schemaName ) ;
8080 expect ( result [ 0 ] . target . name ) . to . equal ( tableName ) ;
8181 } ) ;
82+
83+ test ( "setMetadata handles action without columns" , async ( ) => {
84+ // Partial mock for BigQuery client to avoid real network calls
85+ const mockBigQuery : any = {
86+ dataset : ( ) => ( {
87+ table : ( ) => ( {
88+ getMetadata : ( ) => Promise . resolve ( [ { schema : { fields : [ ] } } ] ) ,
89+ setMetadata : ( metadata : any ) => {
90+ expect ( metadata . description ) . to . equal ( "test" ) ;
91+ return Promise . resolve ( [ ] ) ;
92+ }
93+ } )
94+ } )
95+ } ;
96+
97+ const credentials = dataform . BigQuery . create ( { projectId : "p" , location : "US" } ) ;
98+ const adapter = new BigQueryDbAdapter ( credentials , {
99+ concurrencyLimit : 1 ,
100+ clientProvider : { get : ( ) => mockBigQuery }
101+ } ) ;
102+
103+ const action = dataform . ExecutionAction . create ( {
104+ target : { database : "db" , schema : "sch" , name : "tab" } ,
105+ actionDescriptor : { description : "test" }
106+ // columns is missing/null in this action
107+ } ) ;
108+
109+ // This should not throw "cannot read property 'find' of undefined"
110+ await adapter . setMetadata ( action ) ;
111+ } ) ;
112+
113+ test ( "setMetadata correctly maps column descriptions" , async ( ) => {
114+ const mockBigQuery : any = {
115+ dataset : ( ) => ( {
116+ table : ( ) => ( {
117+ getMetadata : ( ) => Promise . resolve ( [ {
118+ schema : {
119+ fields : [ { name : "id" , type : "INTEGER" } ]
120+ }
121+ } ] ) ,
122+ setMetadata : ( metadata : any ) => {
123+ expect ( metadata . schema [ 0 ] . description ) . to . equal ( "id desc" ) ;
124+ return Promise . resolve ( [ ] ) ;
125+ }
126+ } )
127+ } )
128+ } ;
129+
130+ const credentials = dataform . BigQuery . create ( { projectId : "p" , location : "US" } ) ;
131+ const adapter = new BigQueryDbAdapter ( credentials , {
132+ concurrencyLimit : 1 ,
133+ clientProvider : { get : ( ) => mockBigQuery }
134+ } ) ;
135+
136+ const action = dataform . ExecutionAction . create ( {
137+ target : { database : "db" , schema : "sch" , name : "tab" } ,
138+ actionDescriptor : {
139+ columns : [ { path : [ "id" ] , description : "id desc" } ]
140+ }
141+ } ) ;
142+
143+ await adapter . setMetadata ( action ) ;
144+ } ) ;
82145} ) ;
0 commit comments