|
36 | 36 | except (ImportError, AttributeError): # pragma: NO COVER |
37 | 37 | pyarrow = None |
38 | 38 |
|
| 39 | +import google.api_core.exceptions |
39 | 40 | from google.cloud.bigquery.dataset import DatasetReference |
40 | 41 |
|
41 | 42 |
|
@@ -804,6 +805,61 @@ def test_create_dataset_w_string(self): |
804 | 805 | }, |
805 | 806 | ) |
806 | 807 |
|
| 808 | + def test_create_dataset_alreadyexists_w_exists_ok_false(self): |
| 809 | + creds = _make_credentials() |
| 810 | + client = self._make_one( |
| 811 | + project=self.PROJECT, credentials=creds, location=self.LOCATION |
| 812 | + ) |
| 813 | + client._connection = _make_connection( |
| 814 | + google.api_core.exceptions.AlreadyExists("dataset already exists") |
| 815 | + ) |
| 816 | + |
| 817 | + with pytest.raises(google.api_core.exceptions.AlreadyExists): |
| 818 | + client.create_dataset(self.DS_ID) |
| 819 | + |
| 820 | + def test_create_dataset_alreadyexists_w_exists_ok_true(self): |
| 821 | + post_path = "/projects/{}/datasets".format(self.PROJECT) |
| 822 | + get_path = "/projects/{}/datasets/{}".format(self.PROJECT, self.DS_ID) |
| 823 | + resource = { |
| 824 | + "datasetReference": {"projectId": self.PROJECT, "datasetId": self.DS_ID}, |
| 825 | + "etag": "etag", |
| 826 | + "id": "{}:{}".format(self.PROJECT, self.DS_ID), |
| 827 | + "location": self.LOCATION, |
| 828 | + } |
| 829 | + creds = _make_credentials() |
| 830 | + client = self._make_one( |
| 831 | + project=self.PROJECT, credentials=creds, location=self.LOCATION |
| 832 | + ) |
| 833 | + conn = client._connection = _make_connection( |
| 834 | + google.api_core.exceptions.AlreadyExists("dataset already exists"), resource |
| 835 | + ) |
| 836 | + |
| 837 | + dataset = client.create_dataset(self.DS_ID, exists_ok=True) |
| 838 | + |
| 839 | + self.assertEqual(dataset.dataset_id, self.DS_ID) |
| 840 | + self.assertEqual(dataset.project, self.PROJECT) |
| 841 | + self.assertEqual(dataset.etag, resource["etag"]) |
| 842 | + self.assertEqual(dataset.full_dataset_id, resource["id"]) |
| 843 | + self.assertEqual(dataset.location, self.LOCATION) |
| 844 | + |
| 845 | + conn.api_request.assert_has_calls( |
| 846 | + [ |
| 847 | + mock.call( |
| 848 | + method="POST", |
| 849 | + path=post_path, |
| 850 | + data={ |
| 851 | + "datasetReference": { |
| 852 | + "projectId": self.PROJECT, |
| 853 | + "datasetId": self.DS_ID, |
| 854 | + }, |
| 855 | + "labels": {}, |
| 856 | + "location": self.LOCATION, |
| 857 | + }, |
| 858 | + ), |
| 859 | + mock.call(method="GET", path=get_path), |
| 860 | + ] |
| 861 | + ) |
| 862 | + |
807 | 863 | def test_create_table_w_day_partition(self): |
808 | 864 | from google.cloud.bigquery.table import Table |
809 | 865 | from google.cloud.bigquery.table import TimePartitioning |
@@ -1177,6 +1233,79 @@ def test_create_table_w_string(self): |
1177 | 1233 | ) |
1178 | 1234 | self.assertEqual(got.table_id, self.TABLE_ID) |
1179 | 1235 |
|
| 1236 | + def test_create_table_alreadyexists_w_exists_ok_false(self): |
| 1237 | + post_path = "/projects/{}/datasets/{}/tables".format(self.PROJECT, self.DS_ID) |
| 1238 | + creds = _make_credentials() |
| 1239 | + client = self._make_one( |
| 1240 | + project=self.PROJECT, credentials=creds, location=self.LOCATION |
| 1241 | + ) |
| 1242 | + conn = client._connection = _make_connection( |
| 1243 | + google.api_core.exceptions.AlreadyExists("table already exists") |
| 1244 | + ) |
| 1245 | + |
| 1246 | + with pytest.raises(google.api_core.exceptions.AlreadyExists): |
| 1247 | + client.create_table("{}.{}".format(self.DS_ID, self.TABLE_ID)) |
| 1248 | + |
| 1249 | + conn.api_request.assert_called_once_with( |
| 1250 | + method="POST", |
| 1251 | + path=post_path, |
| 1252 | + data={ |
| 1253 | + "tableReference": { |
| 1254 | + "projectId": self.PROJECT, |
| 1255 | + "datasetId": self.DS_ID, |
| 1256 | + "tableId": self.TABLE_ID, |
| 1257 | + }, |
| 1258 | + "labels": {}, |
| 1259 | + }, |
| 1260 | + ) |
| 1261 | + |
| 1262 | + def test_create_table_alreadyexists_w_exists_ok_true(self): |
| 1263 | + post_path = "/projects/{}/datasets/{}/tables".format(self.PROJECT, self.DS_ID) |
| 1264 | + get_path = "/projects/{}/datasets/{}/tables/{}".format( |
| 1265 | + self.PROJECT, self.DS_ID, self.TABLE_ID |
| 1266 | + ) |
| 1267 | + resource = { |
| 1268 | + "id": "%s:%s:%s" % (self.PROJECT, self.DS_ID, self.TABLE_ID), |
| 1269 | + "tableReference": { |
| 1270 | + "projectId": self.PROJECT, |
| 1271 | + "datasetId": self.DS_ID, |
| 1272 | + "tableId": self.TABLE_ID, |
| 1273 | + }, |
| 1274 | + } |
| 1275 | + creds = _make_credentials() |
| 1276 | + client = self._make_one( |
| 1277 | + project=self.PROJECT, credentials=creds, location=self.LOCATION |
| 1278 | + ) |
| 1279 | + conn = client._connection = _make_connection( |
| 1280 | + google.api_core.exceptions.AlreadyExists("table already exists"), resource |
| 1281 | + ) |
| 1282 | + |
| 1283 | + got = client.create_table( |
| 1284 | + "{}.{}".format(self.DS_ID, self.TABLE_ID), exists_ok=True |
| 1285 | + ) |
| 1286 | + |
| 1287 | + self.assertEqual(got.project, self.PROJECT) |
| 1288 | + self.assertEqual(got.dataset_id, self.DS_ID) |
| 1289 | + self.assertEqual(got.table_id, self.TABLE_ID) |
| 1290 | + |
| 1291 | + conn.api_request.assert_has_calls( |
| 1292 | + [ |
| 1293 | + mock.call( |
| 1294 | + method="POST", |
| 1295 | + path=post_path, |
| 1296 | + data={ |
| 1297 | + "tableReference": { |
| 1298 | + "projectId": self.PROJECT, |
| 1299 | + "datasetId": self.DS_ID, |
| 1300 | + "tableId": self.TABLE_ID, |
| 1301 | + }, |
| 1302 | + "labels": {}, |
| 1303 | + }, |
| 1304 | + ), |
| 1305 | + mock.call(method="GET", path=get_path), |
| 1306 | + ] |
| 1307 | + ) |
| 1308 | + |
1180 | 1309 | def test_get_table(self): |
1181 | 1310 | path = "projects/%s/datasets/%s/tables/%s" % ( |
1182 | 1311 | self.PROJECT, |
@@ -1804,6 +1933,33 @@ def test_delete_dataset_wrong_type(self): |
1804 | 1933 | with self.assertRaises(TypeError): |
1805 | 1934 | client.delete_dataset(client.dataset(self.DS_ID).table("foo")) |
1806 | 1935 |
|
| 1936 | + def test_delete_dataset_w_not_found_ok_false(self): |
| 1937 | + path = "/projects/{}/datasets/{}".format(self.PROJECT, self.DS_ID) |
| 1938 | + creds = _make_credentials() |
| 1939 | + http = object() |
| 1940 | + client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) |
| 1941 | + conn = client._connection = _make_connection( |
| 1942 | + google.api_core.exceptions.NotFound("dataset not found") |
| 1943 | + ) |
| 1944 | + |
| 1945 | + with self.assertRaises(google.api_core.exceptions.NotFound): |
| 1946 | + client.delete_dataset(self.DS_ID) |
| 1947 | + |
| 1948 | + conn.api_request.assert_called_with(method="DELETE", path=path, query_params={}) |
| 1949 | + |
| 1950 | + def test_delete_dataset_w_not_found_ok_true(self): |
| 1951 | + path = "/projects/{}/datasets/{}".format(self.PROJECT, self.DS_ID) |
| 1952 | + creds = _make_credentials() |
| 1953 | + http = object() |
| 1954 | + client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) |
| 1955 | + conn = client._connection = _make_connection( |
| 1956 | + google.api_core.exceptions.NotFound("dataset not found") |
| 1957 | + ) |
| 1958 | + |
| 1959 | + client.delete_dataset(self.DS_ID, not_found_ok=True) |
| 1960 | + |
| 1961 | + conn.api_request.assert_called_with(method="DELETE", path=path, query_params={}) |
| 1962 | + |
1807 | 1963 | def test_delete_table(self): |
1808 | 1964 | from google.cloud.bigquery.table import Table |
1809 | 1965 |
|
@@ -1836,6 +1992,39 @@ def test_delete_table_w_wrong_type(self): |
1836 | 1992 | with self.assertRaises(TypeError): |
1837 | 1993 | client.delete_table(client.dataset(self.DS_ID)) |
1838 | 1994 |
|
| 1995 | + def test_delete_table_w_not_found_ok_false(self): |
| 1996 | + path = "/projects/{}/datasets/{}/tables/{}".format( |
| 1997 | + self.PROJECT, self.DS_ID, self.TABLE_ID |
| 1998 | + ) |
| 1999 | + creds = _make_credentials() |
| 2000 | + http = object() |
| 2001 | + client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) |
| 2002 | + conn = client._connection = _make_connection( |
| 2003 | + google.api_core.exceptions.NotFound("table not found") |
| 2004 | + ) |
| 2005 | + |
| 2006 | + with self.assertRaises(google.api_core.exceptions.NotFound): |
| 2007 | + client.delete_table("{}.{}".format(self.DS_ID, self.TABLE_ID)) |
| 2008 | + |
| 2009 | + conn.api_request.assert_called_with(method="DELETE", path=path) |
| 2010 | + |
| 2011 | + def test_delete_table_w_not_found_ok_true(self): |
| 2012 | + path = "/projects/{}/datasets/{}/tables/{}".format( |
| 2013 | + self.PROJECT, self.DS_ID, self.TABLE_ID |
| 2014 | + ) |
| 2015 | + creds = _make_credentials() |
| 2016 | + http = object() |
| 2017 | + client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) |
| 2018 | + conn = client._connection = _make_connection( |
| 2019 | + google.api_core.exceptions.NotFound("table not found") |
| 2020 | + ) |
| 2021 | + |
| 2022 | + client.delete_table( |
| 2023 | + "{}.{}".format(self.DS_ID, self.TABLE_ID), not_found_ok=True |
| 2024 | + ) |
| 2025 | + |
| 2026 | + conn.api_request.assert_called_with(method="DELETE", path=path) |
| 2027 | + |
1839 | 2028 | def test_job_from_resource_unknown_type(self): |
1840 | 2029 | from google.cloud.bigquery.job import UnknownJob |
1841 | 2030 |
|
|
0 commit comments