Skip to content

Commit 08a00b4

Browse files
committed
minor test code cleanup
1 parent a1abba1 commit 08a00b4

File tree

8 files changed

+28
-30
lines changed

8 files changed

+28
-30
lines changed

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/InMemoryUCClient.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ object InMemoryUCClient {
101101
}
102102
}
103103
}
104+
105+
object TableData {
106+
def afterCreate(): TableData = new TableData(0, ArrayBuffer.empty[Commit])
107+
}
104108
}
105109

106110
/**
@@ -184,9 +188,12 @@ class InMemoryUCClient(ucMetastoreId: String) extends UCClient {
184188
/** Visible for testing. Can be overridden to force an exception in commit method. */
185189
protected def forceThrowInCommitMethod(): Unit = {}
186190

187-
private[unitycatalog] def createTableIfNotExistsOrThrow(
188-
ucTableId: String,
189-
tableData: TableData): Unit = {
191+
private[unitycatalog] def insertTableDataAfterCreate(ucTableId: String): Unit = {
192+
Option(tables.putIfAbsent(ucTableId, TableData.afterCreate()))
193+
.foreach(_ => throw new IllegalArgumentException(s"Table $ucTableId already exists"))
194+
}
195+
196+
private[unitycatalog] def insertTableData(ucTableId: String, tableData: TableData): Unit = {
190197
Option(tables.putIfAbsent(ucTableId, tableData))
191198
.foreach(_ => throw new IllegalArgumentException(s"Table $ucTableId already exists"))
192199
}
@@ -203,6 +210,6 @@ class InMemoryUCClient(ucMetastoreId: String) extends UCClient {
203210

204211
/** Retrieves the table data for the given table ID, creating it if it does not exist. */
205212
private def getOrCreateTableIfNotExists(tableId: String): TableData = {
206-
tables.computeIfAbsent(tableId, _ => new TableData(0, ArrayBuffer.empty))
213+
tables.computeIfAbsent(tableId, _ => TableData.afterCreate())
207214
}
208215
}

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/InMemoryUCClientSuite.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class InMemoryUCClientSuite extends AnyFunSuite with UCCatalogManagedTestUtils {
4444
}
4545

4646
test("TableData::appendCommit handles commit version 1 (since CREATE does not go through UC)") {
47-
val tableData = new InMemoryUCClient.TableData(0, ArrayBuffer.empty[Commit])
47+
val tableData = InMemoryUCClient.TableData.afterCreate()
4848
assert(tableData.getMaxRatifiedVersion == 0L)
4949

5050
tableData.appendCommit(createCommit(1L))
@@ -55,7 +55,7 @@ class InMemoryUCClientSuite extends AnyFunSuite with UCCatalogManagedTestUtils {
5555
}
5656

5757
test("TableData::appendCommit throws if commit version is not maxRatifiedVersion + 1") {
58-
val tableData = new InMemoryUCClient.TableData(0, ArrayBuffer.empty[Commit])
58+
val tableData = InMemoryUCClient.TableData.afterCreate()
5959
tableData.appendCommit(createCommit(1L))
6060

6161
val exMsg = intercept[CommitFailedException] {
@@ -66,7 +66,7 @@ class InMemoryUCClientSuite extends AnyFunSuite with UCCatalogManagedTestUtils {
6666
}
6767

6868
test("TableData::appendCommit appends the commit and updates the maxRatifiedVersion") {
69-
val tableData = new InMemoryUCClient.TableData(0, ArrayBuffer.empty[Commit])
69+
val tableData = InMemoryUCClient.TableData.afterCreate()
7070
tableData.appendCommit(createCommit(1L))
7171

7272
assert(tableData.getMaxRatifiedVersion == 1L)

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/UCCatalogManagedCommitterSuite.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,7 @@ class UCCatalogManagedCommitterSuite
210210
withTempDirAndAllDeltaSubDirs { case (tablePath, logPath) =>
211211
// ===== GIVEN =====
212212
val ucClient = new InMemoryUCClient("ucMetastoreId")
213-
ucClient
214-
.createTableIfNotExistsOrThrow(testUcTableId, new TableData(0, ArrayBuffer[Commit]()))
213+
ucClient.insertTableDataAfterCreate(testUcTableId)
215214
val committer = new UCCatalogManagedCommitter(ucClient, testUcTableId, tablePath)
216215

217216
// ===== WHEN =====
@@ -245,8 +244,7 @@ class UCCatalogManagedCommitterSuite
245244
withTempDirAndAllDeltaSubDirs { case (tablePath, logPath) =>
246245
// ===== GIVEN =====
247246
val ucClient = new InMemoryUCClient("ucMetastoreId")
248-
val tableData = new TableData(0, ArrayBuffer.empty[Commit])
249-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData)
247+
ucClient.insertTableDataAfterCreate(testUcTableId)
250248

251249
val testValue = "TEST_COMMIT_DATA_12345"
252250
val actionsIterator = getSingleElementRowIter(testValue)
@@ -302,7 +300,7 @@ class UCCatalogManagedCommitterSuite
302300

303301
val ucClient = new InMemoryUCClient("ucMetastoreId")
304302
val tableData = new TableData(maxRatifiedVersion = 1, commits = ArrayBuffer.empty[Commit])
305-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData)
303+
ucClient.insertTableData(testUcTableId, tableData)
306304
val committer = new UCCatalogManagedCommitter(ucClient, testUcTableId, tablePath)
307305
val commitMetadata = catalogManagedWriteCommitMetadata(2, logPath = logPath)
308306

@@ -329,7 +327,7 @@ class UCCatalogManagedCommitterSuite
329327
null)
330328
}
331329
val tableData = new TableData(maxRatifiedVersion = 1, commits = ArrayBuffer.empty[Commit])
332-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData)
330+
ucClient.insertTableData(testUcTableId, tableData)
333331
val committer = new UCCatalogManagedCommitter(ucClient, testUcTableId, tablePath)
334332
val commitMetadata = catalogManagedWriteCommitMetadata(2, logPath = logPath)
335333
// ===== WHEN =====
@@ -350,7 +348,7 @@ class UCCatalogManagedCommitterSuite
350348
override def forceThrowInCommitMethod(): Unit = throw new IOException("UC network error")
351349
}
352350
val tableData = new TableData(maxRatifiedVersion = 1, commits = ArrayBuffer.empty[Commit])
353-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData)
351+
ucClient.insertTableData(testUcTableId, tableData)
354352
val committer = new UCCatalogManagedCommitter(ucClient, testUcTableId, tablePath)
355353
val commitMetadata = catalogManagedWriteCommitMetadata(2, logPath = logPath)
356354

@@ -375,7 +373,7 @@ class UCCatalogManagedCommitterSuite
375373
}
376374
}
377375
val tableData = new TableData(maxRatifiedVersion = 1, commits = ArrayBuffer.empty[Commit])
378-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData)
376+
ucClient.insertTableData(testUcTableId, tableData)
379377
val committer = new UCCatalogManagedCommitter(ucClient, "unknownTableId", tablePath)
380378
val commitMetadata = catalogManagedWriteCommitMetadata(2, logPath = logPath)
381379

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/UCCatalogManagedTestUtils.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,14 @@ trait UCCatalogManagedTestUtils
179179
fileStatus.getModificationTime)
180180
}
181181
val tableData = new TableData(maxRatifiedVersion, ArrayBuffer(catalogCommits: _*))
182-
ucClient.createTableIfNotExistsOrThrow("testUcTableId", tableData)
182+
ucClient.insertTableData("testUcTableId", tableData)
183183
textFx(ucClient, tablePath, maxRatifiedVersion)
184184
}
185185

186186
def createUCCatalogManagedClientForTableAfterCreate(
187187
ucTableId: String = "testUcTableId"): UCCatalogManagedClient = {
188188
val ucClient = new InMemoryUCClient("ucMetastoreId")
189-
val tableData = new TableData(0, ArrayBuffer.empty[Commit])
190-
ucClient.createTableIfNotExistsOrThrow(ucTableId, tableData)
189+
ucClient.insertTableDataAfterCreate(ucTableId)
191190
new UCCatalogManagedClient(ucClient)
192191
}
193192

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/UCE2ESuite.scala

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ class UCE2ESuite extends AnyFunSuite with UCCatalogManagedTestUtils {
6464
.buildCreateTableTransaction(testUcTableId, tablePath, testSchema, "test-engine")
6565
.build(engine)
6666
.commit(engine, CloseableIterable.emptyIterable() /* dataActions */ )
67-
val tableData0 = new TableData(0, ArrayBuffer[Commit]())
68-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData0)
67+
ucClient.insertTableDataAfterCreate(testUcTableId)
6968
result0.getPostCommitSnapshot.get().publish(engine) // Should be no-op!
7069

7170
// Step 2: WRITE -- v1.uuid.json
@@ -131,8 +130,7 @@ class UCE2ESuite extends AnyFunSuite with UCCatalogManagedTestUtils {
131130
.buildCreateTableTransaction(testUcTableId, tablePath, testSchema, "test-engine")
132131
.build(engine)
133132
.commit(engine, CloseableIterable.emptyIterable())
134-
val tableData0 = new TableData(0, ArrayBuffer[Commit]())
135-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData0)
133+
ucClient.insertTableDataAfterCreate(testUcTableId)
136134

137135
var currentSnapshot = result0.getPostCommitSnapshot.get()
138136

@@ -198,8 +196,7 @@ class UCE2ESuite extends AnyFunSuite with UCCatalogManagedTestUtils {
198196
.buildCreateTableTransaction(testUcTableId, tablePath, testSchema, "test-engine")
199197
.build(engine)
200198
.commit(engine, CloseableIterable.emptyIterable())
201-
val tableData0 = new TableData(0, ArrayBuffer[Commit]())
202-
ucClient.createTableIfNotExistsOrThrow(testUcTableId, tableData0)
199+
ucClient.insertTableDataAfterCreate(testUcTableId)
203200

204201
// Step 2: WRITE and commit data up to version 2
205202
val postCommitSnapshot1 = writeDataAndVerify(

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/UcCommitTelemetrySuite.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ class UcCommitTelemetrySuite
5151
.buildCreateTableTransaction("testUcTableId", tablePath, testSchema, "test-engine")
5252
.build(engine)
5353
.commit(engine, CloseableIterable.emptyIterable() /* dataActions */ )
54-
val tableData0 = new TableData(0, ArrayBuffer[Commit]())
55-
ucClient.createTableIfNotExistsOrThrow("testUcTableId", tableData0)
54+
ucClient.insertTableDataAfterCreate("testUcTableId")
5655

5756
// Verify CREATE metrics
5857
assert(reporter.reports.size === 1)

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/UcLoadSnapshotTelemetrySuite.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ class UcLoadSnapshotTelemetrySuite
4545
.build(engine)
4646
.commit(engine, CloseableIterable.emptyIterable())
4747

48-
val tableData = new InMemoryUCClient.TableData(0, scala.collection.mutable.ArrayBuffer())
49-
ucClient.createTableIfNotExistsOrThrow("testUcTableId", tableData)
48+
ucClient.insertTableDataAfterCreate("testUcTableId")
5049

5150
val result1 = result0.getPostCommitSnapshot.get()
5251
.buildUpdateTableTransaction("engineInfo", io.delta.kernel.Operation.MANUAL_UPDATE)

kernel/unitycatalog/src/test/scala/io/delta/kernel/unitycatalog/UcPublishTelemetrySuite.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ class UcPublishTelemetrySuite
4646
.build(engine)
4747
.commit(engine, CloseableIterable.emptyIterable())
4848

49-
val tableData0 = new TableData(0, ArrayBuffer[Commit]())
50-
ucClient.createTableIfNotExistsOrThrow("testUcTableId", tableData0)
49+
ucClient.insertTableDataAfterCreate("testUcTableId")
5150

5251
val resultV1 = result0.getPostCommitSnapshot.get()
5352
.buildUpdateTableTransaction("engineInfo", Operation.MANUAL_UPDATE)

0 commit comments

Comments
 (0)