@@ -1424,6 +1424,77 @@ def test_table_create_success(self):
14241424 self .mock_tables .insert .return_value .execute .assert_called_with ()
14251425
14261426
1427+ class TestCreateView (unittest .TestCase ):
1428+
1429+ def setUp (self ):
1430+ self .mock_bq_service = mock .Mock ()
1431+ self .mock_tables = mock .Mock ()
1432+ self .mock_bq_service .tables .return_value = self .mock_tables
1433+ self .table = 'table'
1434+ self .project = 'project'
1435+ self .dataset = 'dataset'
1436+ self .query = 'SELECT "foo" bar'
1437+ self .client = client .BigQueryClient (self .mock_bq_service , self .project )
1438+ self .body = {
1439+ 'view' : {'query' : self .query },
1440+ 'tableReference' : {
1441+ 'tableId' : self .table , 'projectId' : self .project ,
1442+ 'datasetId' : self .dataset }
1443+ }
1444+
1445+ def test_view_create_failed (self ):
1446+ """Ensure that if creating the table fails, False is returned,
1447+ or if swallow_results is False an empty dict is returned."""
1448+
1449+ self .mock_tables .insert .return_value .execute .side_effect = (
1450+ HttpError (HttpResponse (404 ), 'There was an error' ))
1451+
1452+ actual = self .client .create_view (self .dataset , self .table ,
1453+ self .query )
1454+
1455+ self .assertFalse (actual )
1456+
1457+ self .client .swallow_results = False
1458+
1459+ actual = self .client .create_view (self .dataset , self .table ,
1460+ self .query )
1461+
1462+ self .assertEqual (actual , {})
1463+
1464+ self .client .swallow_results = True
1465+
1466+ self .mock_tables .insert .assert_called_with (
1467+ projectId = self .project , datasetId = self .dataset , body = self .body )
1468+
1469+ self .mock_tables .insert .return_value .execute .assert_called_with ()
1470+
1471+ def test_view_create_success (self ):
1472+ """Ensure that if creating the table succeeds, True is returned,
1473+ or if swallow_results is False the actual response is returned."""
1474+
1475+ self .mock_tables .insert .return_value .execute .side_effect = [
1476+ {'status' : 'bar' }]
1477+
1478+ actual = self .client .create_view (self .dataset , self .table ,
1479+ self .query )
1480+
1481+ self .assertTrue (actual )
1482+
1483+ self .client .swallow_results = False
1484+
1485+ actual = self .client .create_view (self .dataset , self .table ,
1486+ self .query )
1487+
1488+ self .assertEqual (actual , {'status' : 'bar' })
1489+
1490+ self .client .swallow_results = True
1491+
1492+ self .mock_tables .insert .assert_called_with (
1493+ projectId = self .project , datasetId = self .dataset , body = self .body )
1494+
1495+ self .mock_tables .insert .return_value .execute .assert_called_with ()
1496+
1497+
14271498class TestDeleteTable (unittest .TestCase ):
14281499
14291500 def setUp (self ):
0 commit comments