3333
3434import org .junit .After ;
3535import org .junit .Before ;
36+ import org .junit .Rule ;
3637import org .junit .Test ;
38+ import org .junit .rules .ExpectedException ;
3739
3840import java .util .Iterator ;
41+ import java .util .List ;
3942
4043public class DatasetTest {
4144
@@ -48,7 +51,12 @@ public class DatasetTest {
4851 ExternalTableInfo .builder (TableId .of ("dataset" , "table2" ),
4952 ExternalDataConfiguration .of (ImmutableList .of ("URI" ), Schema .of (), FormatOptions .csv ()))
5053 .build ());
54+ private static final UserDefinedFunction FUNCTION1 = UserDefinedFunction .inline ("inline" );
55+ private static final UserDefinedFunction FUNCTION2 = UserDefinedFunction .inline ("gs://b/f" );
56+ private static final List <UserDefinedFunction > FUNCTIONS = ImmutableList .of (FUNCTION1 , FUNCTION2 );
5157
58+ @ Rule
59+ public ExpectedException thrown = ExpectedException .none ();
5260 private BigQuery bigquery ;
5361 private Dataset dataset ;
5462
@@ -69,6 +77,12 @@ public void testInfo() throws Exception {
6977 replay (bigquery );
7078 }
7179
80+ @ Test
81+ public void testBigQuery () throws Exception {
82+ assertSame (bigquery , dataset .bigquery ());
83+ replay (bigquery );
84+ }
85+
7286 @ Test
7387 public void testExists_True () throws Exception {
7488 BigQuery .DatasetOption [] expectedOptions = {BigQuery .DatasetOption .fields ()};
@@ -119,7 +133,28 @@ public void testUpdate() throws Exception {
119133 expect (bigquery .update (updatedInfo )).andReturn (updatedInfo );
120134 replay (bigquery );
121135 Dataset updatedDataset = dataset .update (updatedInfo );
122- assertSame (bigquery , dataset .bigquery ());
136+ assertSame (bigquery , updatedDataset .bigquery ());
137+ assertEquals (updatedInfo , updatedDataset .info ());
138+ }
139+
140+ @ Test
141+ public void testUpdateWithDifferentId () throws Exception {
142+ DatasetInfo updatedInfo = DATASET_INFO .toBuilder ()
143+ .datasetId (DatasetId .of ("dataset2" ))
144+ .description ("Description" )
145+ .build ();
146+ replay (bigquery );
147+ thrown .expect (IllegalArgumentException .class );
148+ dataset .update (updatedInfo );
149+ }
150+
151+ @ Test
152+ public void testUpdateWithOptions () throws Exception {
153+ DatasetInfo updatedInfo = DATASET_INFO .toBuilder ().description ("Description" ).build ();
154+ expect (bigquery .update (updatedInfo , BigQuery .DatasetOption .fields ())).andReturn (updatedInfo );
155+ replay (bigquery );
156+ Dataset updatedDataset = dataset .update (updatedInfo , BigQuery .DatasetOption .fields ());
157+ assertSame (bigquery , updatedDataset .bigquery ());
123158 assertEquals (updatedInfo , updatedDataset .info ());
124159 }
125160
@@ -150,6 +185,27 @@ public void testList() throws Exception {
150185 verify (bigqueryOptions );
151186 }
152187
188+ @ Test
189+ public void testListWithOptions () throws Exception {
190+ BigQueryOptions bigqueryOptions = createStrictMock (BigQueryOptions .class );
191+ PageImpl <BaseTableInfo > tableInfoPage = new PageImpl <>(null , "c" , TABLE_INFO_RESULTS );
192+ expect (bigquery .listTables (DATASET_INFO .datasetId (), BigQuery .TableListOption .maxResults (10L )))
193+ .andReturn (tableInfoPage );
194+ expect (bigquery .options ()).andReturn (bigqueryOptions );
195+ expect (bigqueryOptions .service ()).andReturn (bigquery );
196+ replay (bigquery , bigqueryOptions );
197+ Page <Table > tablePage = dataset .list (BigQuery .TableListOption .maxResults (10L ));
198+ Iterator <BaseTableInfo > tableInfoIterator = tableInfoPage .values ().iterator ();
199+ Iterator <Table > tableIterator = tablePage .values ().iterator ();
200+ while (tableInfoIterator .hasNext () && tableIterator .hasNext ()) {
201+ assertEquals (tableInfoIterator .next (), tableIterator .next ().info ());
202+ }
203+ assertFalse (tableInfoIterator .hasNext ());
204+ assertFalse (tableIterator .hasNext ());
205+ assertEquals (tableInfoPage .nextPageCursor (), tablePage .nextPageCursor ());
206+ verify (bigqueryOptions );
207+ }
208+
153209 @ Test
154210 public void testGet () throws Exception {
155211 BaseTableInfo info = TableInfo .builder (TableId .of ("dataset" , "table1" ), Schema .of ()).build ();
@@ -167,6 +223,17 @@ public void testGetNull() throws Exception {
167223 assertNull (dataset .get ("table1" ));
168224 }
169225
226+ @ Test
227+ public void testGetWithOptions () throws Exception {
228+ BaseTableInfo info = TableInfo .builder (TableId .of ("dataset" , "table1" ), Schema .of ()).build ();
229+ expect (bigquery .getTable (TableId .of ("dataset" , "table1" ), BigQuery .TableOption .fields ()))
230+ .andReturn (info );
231+ replay (bigquery );
232+ Table table = dataset .get ("table1" , BigQuery .TableOption .fields ());
233+ assertNotNull (table );
234+ assertEquals (info , table .info ());
235+ }
236+
170237 @ Test
171238 public void testCreateTable () throws Exception {
172239 TableInfo info = TableInfo .builder (TableId .of ("dataset" , "table1" ), Schema .of (FIELD )).build ();
@@ -176,6 +243,15 @@ public void testCreateTable() throws Exception {
176243 assertEquals (info , table .info ());
177244 }
178245
246+ @ Test
247+ public void testCreateTableWithOptions () throws Exception {
248+ TableInfo info = TableInfo .builder (TableId .of ("dataset" , "table1" ), Schema .of (FIELD )).build ();
249+ expect (bigquery .create (info , BigQuery .TableOption .fields ())).andReturn (info );
250+ replay (bigquery );
251+ Table table = dataset .create ("table1" , Schema .of (FIELD ), BigQuery .TableOption .fields ());
252+ assertEquals (info , table .info ());
253+ }
254+
179255 @ Test
180256 public void testCreateView () throws Exception {
181257 ViewInfo info = ViewInfo .builder (TableId .of ("dataset" , "table2" ), "QUERY" ).build ();
@@ -185,6 +261,24 @@ public void testCreateView() throws Exception {
185261 assertEquals (info , table .info ());
186262 }
187263
264+ @ Test
265+ public void testCreateViewWithUserDefinedFunctions () throws Exception {
266+ ViewInfo info = ViewInfo .builder (TableId .of ("dataset" , "table2" ), "QUERY" , FUNCTIONS ).build ();
267+ expect (bigquery .create (info )).andReturn (info );
268+ replay (bigquery );
269+ Table table = dataset .create ("table2" , "QUERY" , FUNCTIONS );
270+ assertEquals (info , table .info ());
271+ }
272+
273+ @ Test
274+ public void testCreateViewWithOptions () throws Exception {
275+ ViewInfo info = ViewInfo .builder (TableId .of ("dataset" , "table2" ), "QUERY" ).build ();
276+ expect (bigquery .create (info , BigQuery .TableOption .fields ())).andReturn (info );
277+ replay (bigquery );
278+ Table table = dataset .create ("table2" , "QUERY" , BigQuery .TableOption .fields ());
279+ assertEquals (info , table .info ());
280+ }
281+
188282 @ Test
189283 public void testCreateExternalTable () throws Exception {
190284 ExternalTableInfo info = ExternalTableInfo .builder (TableId .of ("dataset" , "table3" ),
@@ -197,6 +291,18 @@ public void testCreateExternalTable() throws Exception {
197291 assertEquals (info , table .info ());
198292 }
199293
294+ @ Test
295+ public void testCreateExternalTableWithOptions () throws Exception {
296+ ExternalTableInfo info = ExternalTableInfo .builder (TableId .of ("dataset" , "table3" ),
297+ ExternalDataConfiguration .of (ImmutableList .of ("URI" ), Schema .of (), FormatOptions .csv ()))
298+ .build ();
299+ expect (bigquery .create (info , BigQuery .TableOption .fields ())).andReturn (info );
300+ replay (bigquery );
301+ Table table = dataset .create ("table3" , ExternalDataConfiguration .of (
302+ ImmutableList .of ("URI" ), Schema .of (), FormatOptions .csv ()), BigQuery .TableOption .fields ());
303+ assertEquals (info , table .info ());
304+ }
305+
200306 @ Test
201307 public void testLoad () throws Exception {
202308 expect (bigquery .getDataset (DATASET_INFO .datasetId ().dataset ())).andReturn (DATASET_INFO );
0 commit comments