Skip to content

Commit 4cc6429

Browse files
authored
Refactor PostgresTest and add PostgresConnectionUrlTest (#798)
Refactored `PostgresTest` for modular, reusable test data setup and introduced `PostgresConnectionUrlTest` to validate different PostgreSQL connection URLs. These changes improve test readability and ensure comprehensive URL configurations handling.
1 parent 53c7c5b commit 4cc6429

File tree

2 files changed

+272
-144
lines changed

2 files changed

+272
-144
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package org.jetbrains.kotlinx.dataframe.io.local
2+
3+
import io.kotest.assertions.throwables.shouldThrow
4+
import io.kotest.matchers.shouldBe
5+
import org.jetbrains.kotlinx.dataframe.DataFrame
6+
import org.jetbrains.kotlinx.dataframe.api.cast
7+
import org.jetbrains.kotlinx.dataframe.api.filter
8+
import org.jetbrains.kotlinx.dataframe.io.DbConnectionConfig
9+
import org.jetbrains.kotlinx.dataframe.io.readDataFrame
10+
import org.jetbrains.kotlinx.dataframe.io.readSqlTable
11+
import org.junit.Ignore
12+
import org.junit.Test
13+
import java.sql.DriverManager
14+
15+
private const val URL_WITH_LOGIN_PASSWORD = "jdbc:postgresql://localhost:5432/test?" +
16+
"user=postgres&password=pass&connectTimeout=10&tcpKeepAlive=true"
17+
18+
private const val URL_NO_LOGIN_PASSWORD = "jdbc:postgresql://localhost:5432/test?connectTimeout=10&tcpKeepAlive=true"
19+
20+
private const val URL_WITH_PASSWORD =
21+
"jdbc:postgresql://localhost:5432/test?password=pass&connectTimeout=10&tcpKeepAlive=true"
22+
23+
private const val URL_WITH_LOGIN =
24+
"jdbc:postgresql://localhost:5432/test?user=postgres&connectTimeout=10&tcpKeepAlive=true"
25+
26+
private const val TABLE_NAME = "table1"
27+
28+
@Ignore
29+
class PostgresConnectionUrlTest {
30+
@Test
31+
fun `read from table with login and password in connection URL`() {
32+
DriverManager.getConnection(URL_WITH_LOGIN_PASSWORD).use { connection ->
33+
createTestData(connection)
34+
35+
val df1 = DataFrame.readSqlTable(connection, TABLE_NAME).cast<Table1>()
36+
val result1 = df1.filter { it[Table1::id] == 1 }
37+
38+
result1[0][2] shouldBe 11
39+
40+
val df2 = connection.readDataFrame(TABLE_NAME).cast<Table1>()
41+
val result2 = df2.filter { it[Table1::id] == 1 }
42+
43+
result2[0][2] shouldBe 11
44+
45+
clearTestData(connection)
46+
}
47+
}
48+
49+
@Test
50+
fun `read from table with login and password in connection URL for DBConfig`() {
51+
DriverManager.getConnection(URL_WITH_LOGIN_PASSWORD).use { connection ->
52+
createTestData(connection)
53+
54+
val dbConfig = DbConnectionConfig(URL_WITH_LOGIN_PASSWORD)
55+
val df1 = DataFrame.readSqlTable(dbConfig = dbConfig, TABLE_NAME).cast<Table1>()
56+
val result1 = df1.filter { it[Table1::id] == 1 }
57+
58+
result1[0][2] shouldBe 11
59+
60+
val df2 = dbConfig.readDataFrame(TABLE_NAME).cast<Table1>()
61+
val result2 = df2.filter { it[Table1::id] == 1 }
62+
63+
result2[0][2] shouldBe 11
64+
65+
clearTestData(connection)
66+
}
67+
}
68+
69+
@Test
70+
fun `read from table without login and password`() {
71+
val dbConfig = DbConnectionConfig(URL_NO_LOGIN_PASSWORD)
72+
73+
shouldThrow<org.postgresql.util.PSQLException> {
74+
testReadFromTable(dbConfig)
75+
}
76+
}
77+
78+
@Test
79+
fun `read from table with password only`() {
80+
val dbConfig = DbConnectionConfig(URL_WITH_PASSWORD)
81+
82+
shouldThrow<org.postgresql.util.PSQLException> {
83+
testReadFromTable(dbConfig)
84+
}
85+
}
86+
87+
@Test
88+
fun `read from table with login only`() {
89+
val dbConfig = DbConnectionConfig(URL_WITH_LOGIN)
90+
91+
shouldThrow<org.postgresql.util.PSQLException> {
92+
testReadFromTable(dbConfig)
93+
}
94+
}
95+
96+
private fun testReadFromTable(dbConfig: DbConnectionConfig) {
97+
DriverManager.getConnection(URL_WITH_LOGIN_PASSWORD).use { connection ->
98+
createTestData(connection)
99+
100+
val df2 = dbConfig.readDataFrame(TABLE_NAME).cast<Table1>()
101+
val result2 = df2.filter { it[Table1::id] == 1 }
102+
103+
result2[0][2] shouldBe 11
104+
105+
clearTestData(connection)
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)