Skip to content

Commit 93f5592

Browse files
彭灿00244106maropu
彭灿00244106
authored andcommitted
[MINOR][SQL] Combine the same codes in test cases
## What changes were proposed in this pull request? In the DDLSuit, there are four test cases have the same codes , writing a function can combine the same code. ## How was this patch tested? existing tests. Closes #23194 from CarolinePeng/Update_temp. Authored-by: 彭灿00244106 <00244106@zte.intra> Signed-off-by: Takeshi Yamamuro <yamamuro@apache.org>
1 parent 2612848 commit 93f5592

File tree

1 file changed

+16
-24
lines changed
  • sql/core/src/test/scala/org/apache/spark/sql/execution/command

1 file changed

+16
-24
lines changed

sql/core/src/test/scala/org/apache/spark/sql/execution/command/DDLSuite.scala

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -377,41 +377,41 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
377377
}
378378
}
379379

380-
test("CTAS a managed table with the existing empty directory") {
381-
val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab1")))
380+
private def withEmptyDirInTablePath(dirName: String)(f : File => Unit): Unit = {
381+
val tableLoc =
382+
new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier(dirName)))
382383
try {
383384
tableLoc.mkdir()
385+
f(tableLoc)
386+
} finally {
387+
waitForTasksToFinish()
388+
Utils.deleteRecursively(tableLoc)
389+
}
390+
}
391+
392+
393+
test("CTAS a managed table with the existing empty directory") {
394+
withEmptyDirInTablePath("tab1") { tableLoc =>
384395
withTable("tab1") {
385396
sql(s"CREATE TABLE tab1 USING ${dataSource} AS SELECT 1, 'a'")
386397
checkAnswer(spark.table("tab1"), Row(1, "a"))
387398
}
388-
} finally {
389-
waitForTasksToFinish()
390-
Utils.deleteRecursively(tableLoc)
391399
}
392400
}
393401

394402
test("create a managed table with the existing empty directory") {
395-
val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab1")))
396-
try {
397-
tableLoc.mkdir()
403+
withEmptyDirInTablePath("tab1") { tableLoc =>
398404
withTable("tab1") {
399405
sql(s"CREATE TABLE tab1 (col1 int, col2 string) USING ${dataSource}")
400406
sql("INSERT INTO tab1 VALUES (1, 'a')")
401407
checkAnswer(spark.table("tab1"), Row(1, "a"))
402408
}
403-
} finally {
404-
waitForTasksToFinish()
405-
Utils.deleteRecursively(tableLoc)
406409
}
407410
}
408411

409412
test("create a managed table with the existing non-empty directory") {
410413
withTable("tab1") {
411-
val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab1")))
412-
try {
413-
// create an empty hidden file
414-
tableLoc.mkdir()
414+
withEmptyDirInTablePath("tab1") { tableLoc =>
415415
val hiddenGarbageFile = new File(tableLoc.getCanonicalPath, ".garbage")
416416
hiddenGarbageFile.createNewFile()
417417
val exMsg = "Can not create the managed table('`tab1`'). The associated location"
@@ -439,28 +439,20 @@ abstract class DDLSuite extends QueryTest with SQLTestUtils {
439439
}.getMessage
440440
assert(ex.contains(exMsgWithDefaultDB))
441441
}
442-
} finally {
443-
waitForTasksToFinish()
444-
Utils.deleteRecursively(tableLoc)
445442
}
446443
}
447444
}
448445

449446
test("rename a managed table with existing empty directory") {
450-
val tableLoc = new File(spark.sessionState.catalog.defaultTablePath(TableIdentifier("tab2")))
451-
try {
447+
withEmptyDirInTablePath("tab2") { tableLoc =>
452448
withTable("tab1") {
453449
sql(s"CREATE TABLE tab1 USING $dataSource AS SELECT 1, 'a'")
454-
tableLoc.mkdir()
455450
val ex = intercept[AnalysisException] {
456451
sql("ALTER TABLE tab1 RENAME TO tab2")
457452
}.getMessage
458453
val expectedMsg = "Can not rename the managed table('`tab1`'). The associated location"
459454
assert(ex.contains(expectedMsg))
460455
}
461-
} finally {
462-
waitForTasksToFinish()
463-
Utils.deleteRecursively(tableLoc)
464456
}
465457
}
466458

0 commit comments

Comments
 (0)