From 52d9f76364c53088b942f9d4e3cfe0132ba346e2 Mon Sep 17 00:00:00 2001
From: abdelmegahed <amegahed94@gmail.com>
Date: Mon, 17 Apr 2023 16:11:27 -0400
Subject: [PATCH 1/4] hotfix: handle case when expirationMs is None

---
 google/cloud/bigquery/table.py | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/google/cloud/bigquery/table.py b/google/cloud/bigquery/table.py
index 93b0da67f..5c195e6b3 100644
--- a/google/cloud/bigquery/table.py
+++ b/google/cloud/bigquery/table.py
@@ -687,7 +687,11 @@ def partition_expiration(self, value):
 
         if self.time_partitioning is None:
             self._properties[api_field] = {"type": TimePartitioningType.DAY}
-        self._properties[api_field]["expirationMs"] = str(value)
+
+        if value is None:
+            self._properties[api_field]["expirationMs"] = None
+        else:
+            self._properties[api_field]["expirationMs"] = str(value)
 
     @property
     def clustering_fields(self):

From 52d12e65bdc5848d0f381245d8d7737e593414ac Mon Sep 17 00:00:00 2001
From: abdelmegahed <amegahed94@gmail.com>
Date: Tue, 18 Apr 2023 17:47:12 -0400
Subject: [PATCH 2/4] Add test for unsetting table exp

---
 tests/unit/test_table.py | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/tests/unit/test_table.py b/tests/unit/test_table.py
index 9bdd7b596..2adaf5a74 100644
--- a/tests/unit/test_table.py
+++ b/tests/unit/test_table.py
@@ -1189,6 +1189,20 @@ def test_to_api_repr_w_custom_field(self):
         }
         self.assertEqual(resource, exp_resource)
 
+    def test_to_api_repr_w_unsetting_expiration(self):
+        dataset = DatasetReference(self.PROJECT, self.DS_ID)
+        table_ref = dataset.table(self.TABLE_NAME)
+        table = self._make_one(table_ref)
+        table._properties["timePartitioning"] = {"expirationMs": None}
+        resource = table.to_api_repr()
+
+        exp_resource = {
+            "tableReference": table_ref.to_api_repr(),
+            "labels": {},
+            "timePartitioning": {"expirationMs": None},
+        }
+        self.assertEqual(resource, exp_resource)
+
     def test__build_resource_w_custom_field(self):
         dataset = DatasetReference(self.PROJECT, self.DS_ID)
         table_ref = dataset.table(self.TABLE_NAME)

From ec3b22d4ec6f1f7a2144b28819a428f3626651e7 Mon Sep 17 00:00:00 2001
From: Tim Swast <swast@google.com>
Date: Tue, 18 Apr 2023 18:14:55 -0500
Subject: [PATCH 3/4] Update tests/unit/test_table.py

---
 tests/unit/test_table.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tests/unit/test_table.py b/tests/unit/test_table.py
index 2adaf5a74..baac86992 100644
--- a/tests/unit/test_table.py
+++ b/tests/unit/test_table.py
@@ -1193,7 +1193,7 @@ def test_to_api_repr_w_unsetting_expiration(self):
         dataset = DatasetReference(self.PROJECT, self.DS_ID)
         table_ref = dataset.table(self.TABLE_NAME)
         table = self._make_one(table_ref)
-        table._properties["timePartitioning"] = {"expirationMs": None}
+        table.partition_expiration = None
         resource = table.to_api_repr()
 
         exp_resource = {

From 361c1dd33fe217470f91851a92d23be5ce0e9602 Mon Sep 17 00:00:00 2001
From: abdelmegahed <amegahed94@gmail.com>
Date: Tue, 18 Apr 2023 19:40:44 -0400
Subject: [PATCH 4/4] Update exp_resource for the unsetting_exp test

---
 tests/unit/test_table.py | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/tests/unit/test_table.py b/tests/unit/test_table.py
index baac86992..cef90a5aa 100644
--- a/tests/unit/test_table.py
+++ b/tests/unit/test_table.py
@@ -1190,6 +1190,8 @@ def test_to_api_repr_w_custom_field(self):
         self.assertEqual(resource, exp_resource)
 
     def test_to_api_repr_w_unsetting_expiration(self):
+        from google.cloud.bigquery.table import TimePartitioningType
+
         dataset = DatasetReference(self.PROJECT, self.DS_ID)
         table_ref = dataset.table(self.TABLE_NAME)
         table = self._make_one(table_ref)
@@ -1199,7 +1201,10 @@ def test_to_api_repr_w_unsetting_expiration(self):
         exp_resource = {
             "tableReference": table_ref.to_api_repr(),
             "labels": {},
-            "timePartitioning": {"expirationMs": None},
+            "timePartitioning": {
+                "expirationMs": None,
+                "type": TimePartitioningType.DAY,
+            },
         }
         self.assertEqual(resource, exp_resource)
 
