@@ -1663,6 +1663,81 @@ def test_table_update_success(self):
16631663 self .mock_tables .update .return_value .execute .assert_called_with ()
16641664
16651665
1666+ class TestPatchTable (unittest .TestCase ):
1667+
1668+ def setUp (self ):
1669+ self .mock_bq_service = mock .Mock ()
1670+ self .mock_tables = mock .Mock ()
1671+ self .mock_bq_service .tables .return_value = self .mock_tables
1672+ self .table = 'table'
1673+ self .schema = [
1674+ {'name' : 'foo' , 'type' : 'STRING' , 'mode' : 'nullable' },
1675+ {'name' : 'bar' , 'type' : 'FLOAT' , 'mode' : 'nullable' }
1676+ ]
1677+ self .project = 'project'
1678+ self .dataset = 'dataset'
1679+ self .client = client .BigQueryClient (self .mock_bq_service , self .project )
1680+ self .body = {
1681+ 'schema' : {'fields' : self .schema },
1682+ 'tableReference' : {
1683+ 'tableId' : self .table , 'projectId' : self .project ,
1684+ 'datasetId' : self .dataset }
1685+ }
1686+ self .expiration_time = 1437513693000
1687+
1688+ def test_table_patch_failed (self ):
1689+ """Ensure that if patching the table fails, False is returned,
1690+ or if swallow_results is False an empty dict is returned."""
1691+
1692+ self .mock_tables .patch .return_value .execute .side_effect = (
1693+ HttpError (HttpResponse (404 ), 'There was an error' .encode ('utf8' )))
1694+
1695+ actual = self .client .patch_table (self .dataset , self .table ,
1696+ self .schema )
1697+
1698+ self .assertFalse (actual )
1699+
1700+ self .client .swallow_results = False
1701+
1702+ actual = self .client .patch_table (self .dataset , self .table ,
1703+ self .schema )
1704+
1705+ self .assertEqual (actual , {})
1706+
1707+ self .client .swallow_results = True
1708+
1709+ self .mock_tables .patch .assert_called_with (
1710+ projectId = self .project , datasetId = self .dataset , body = self .body )
1711+
1712+ self .mock_tables .patch .return_value .execute .assert_called_with ()
1713+
1714+ def test_table_patch_success (self ):
1715+ """Ensure that if patching the table succeeds, True is returned,
1716+ or if swallow_results is False the actual response is returned."""
1717+
1718+ self .mock_tables .patch .return_value .execute .side_effect = [{
1719+ 'status' : 'foo' }, {'status' : 'bar' }]
1720+
1721+ actual = self .client .patch_table (self .dataset , self .table ,
1722+ self .schema )
1723+
1724+ self .assertTrue (actual )
1725+
1726+ self .client .swallow_results = False
1727+
1728+ actual = self .client .patch_table (self .dataset , self .table ,
1729+ self .schema )
1730+
1731+ self .assertEqual (actual , {'status' : 'bar' })
1732+
1733+ self .client .swallow_results = True
1734+
1735+ self .mock_tables .patch .assert_called_with (
1736+ projectId = self .project , datasetId = self .dataset , body = self .body )
1737+
1738+ self .mock_tables .patch .return_value .execute .assert_called_with ()
1739+
1740+
16661741class TestCreateView (unittest .TestCase ):
16671742
16681743 def setUp (self ):
0 commit comments