Skip to content

Commit 6331f91

Browse files
[Junie] IO overloads tests
1 parent 299adb8 commit 6331f91

File tree

8 files changed

+429
-0
lines changed

8 files changed

+429
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.jetbrains.kotlinx.dataframe.io
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.DataFrame
5+
import org.junit.Test
6+
import java.nio.file.Files
7+
import java.nio.file.Path
8+
import kotlin.io.path.createTempDirectory
9+
import kotlin.io.path.writeText
10+
11+
class GuessOverloadsTest {
12+
13+
private fun sampleJson(): String = """
14+
[
15+
{"name":"Alice","age":15},
16+
{"name":"Bob","age":20}
17+
]
18+
""".trimIndent()
19+
20+
@Test
21+
fun read_guess_overloads_String_Path_File_produce_same_df() {
22+
val tmp: Path = createTempDirectory("guess_overloads_")
23+
try {
24+
val p = tmp.resolve("people.json")
25+
p.writeText(sampleJson())
26+
27+
val d1 = DataFrame.read(p.toString())
28+
val d2 = DataFrame.read(p)
29+
val d3 = DataFrame.read(p.toFile())
30+
31+
d2.rowsCount() shouldBe d1.rowsCount()
32+
d3.rowsCount() shouldBe d1.rowsCount()
33+
d2.columnNames() shouldBe d1.columnNames()
34+
d3.columnNames() shouldBe d1.columnNames()
35+
d2.toJson() shouldBe d1.toJson()
36+
d3.toJson() shouldBe d1.toJson()
37+
} finally {
38+
Files.walk(tmp).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
39+
}
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jetbrains.kotlinx.dataframe.io
2+
3+
import org.junit.Test
4+
import org.junit.Assert.assertEquals
5+
import java.io.File
6+
import java.nio.file.Files
7+
import java.nio.file.Paths
8+
import java.nio.file.Path
9+
import kotlin.io.path.createTempDirectory
10+
11+
class HtmlOverloadsTest {
12+
13+
@Test
14+
fun writeHtml_overloads_String_Path_File_produce_same_content() {
15+
val html = DataFrameHtmlData(style = "body{color:black;}", body = "<div>hello</div>", script = "console.log('x')")
16+
val expected = html.toString()
17+
18+
val tmpDir: Path = createTempDirectory("html_overloads_")
19+
try {
20+
val pathOut = tmpDir.resolve("out_path.html")
21+
val fileOut = tmpDir.resolve("out_file.html").toFile()
22+
val strOut = tmpDir.resolve("out_str.html").toString()
23+
24+
html.writeHtml(pathOut)
25+
html.writeHtml(fileOut)
26+
html.writeHtml(strOut)
27+
28+
fun readText(p: Path): String = String(Files.readAllBytes(p), Charsets.UTF_8)
29+
assertEquals(expected, readText(pathOut))
30+
assertEquals(expected, readText(fileOut.toPath()))
31+
assertEquals(expected, readText(Paths.get(strOut)))
32+
} finally {
33+
Files.walk(tmpDir).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
34+
}
35+
}
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.jetbrains.kotlinx.dataframe.jupyter
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
import java.nio.file.Files
6+
import java.nio.file.Path
7+
import java.nio.file.Paths
8+
import kotlin.io.path.createTempDirectory
9+
10+
class ImportDataSchemaOverloadsTest {
11+
12+
@Test
13+
fun importDataSchema_constructors_and_functions_produce_same_url() {
14+
val tmp: Path = createTempDirectory("ids_overloads_")
15+
try {
16+
val file = tmp.resolve("schema.json").toFile().also { it.writeText("{}") }
17+
val path = file.toPath()
18+
val str = path.toUri().toString() // String-оверлоад ожидает URL/URI-строку
19+
20+
// constructors
21+
val c1 = ImportDataSchema(str).url
22+
val c2 = ImportDataSchema(path).url
23+
val c3 = ImportDataSchema(file).url
24+
25+
assertEquals(c1, c2)
26+
assertEquals(c1, c3)
27+
28+
// top-level functions
29+
val f1 = importDataSchema(str).url
30+
val f2 = importDataSchema(path).url
31+
val f3 = importDataSchema(file).url
32+
33+
assertEquals(f1, f2)
34+
assertEquals(f1, f3)
35+
} finally {
36+
Files.walk(tmp).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
37+
}
38+
}
39+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package org.jetbrains.kotlinx.dataframe.io
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.DataFrame
5+
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
6+
import org.junit.Test
7+
import java.nio.file.Files
8+
import java.nio.file.Path
9+
import java.nio.file.Paths
10+
import kotlin.io.path.createTempDirectory
11+
12+
class ArrowOverloadsSmokeTest {
13+
14+
@Test
15+
fun feather_overloads_roundtrip() {
16+
val df = dataFrameOf("name", "age")(
17+
"Alice", 15,
18+
"Bob", 20,
19+
)
20+
val tmp: Path = createTempDirectory("arrow_overloads_")
21+
try {
22+
val p = tmp.resolve("people_path.feather")
23+
val f = tmp.resolve("people_file.feather").toFile()
24+
val s = tmp.resolve("people_str.feather").toString()
25+
26+
// write with three overloads
27+
df.writeArrowFeather(p)
28+
df.writeArrowFeather(f)
29+
// no String overload for write; only File/Path are supported
30+
31+
// read back the SAME file using different overloads
32+
val r1 = DataFrame.readArrowFeather(p)
33+
val r2 = DataFrame.readArrowFeather(p.toFile())
34+
val r3 = DataFrame.readArrowFeather(p.toString())
35+
36+
r1.rowsCount() shouldBe df.rowsCount()
37+
r2.rowsCount() shouldBe df.rowsCount()
38+
r3.rowsCount() shouldBe df.rowsCount()
39+
r1.columnNames() shouldBe df.columnNames()
40+
r2.columnNames() shouldBe df.columnNames()
41+
r3.columnNames() shouldBe df.columnNames()
42+
} finally {
43+
Files.walk(tmp).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
44+
}
45+
}
46+
47+
@Test
48+
fun ipc_overloads_write_and_read() {
49+
val df = dataFrameOf("x")(
50+
1,
51+
2,
52+
)
53+
val tmp: Path = createTempDirectory("arrow_overloads_ipc_")
54+
try {
55+
val p = tmp.resolve("data_path.arrow")
56+
val f = tmp.resolve("data_file.arrow").toFile()
57+
val s = tmp.resolve("data_str.arrow").toString()
58+
59+
df.writeArrowIPC(p)
60+
df.writeArrowIPC(f)
61+
// no String overload for write; only File/Path are supported
62+
63+
// read back the SAME file using a different overload (String)
64+
val r = DataFrame.readArrowIPC(p.toString())
65+
r.rowsCount() shouldBe df.rowsCount()
66+
r.columnNames() shouldBe df.columnNames()
67+
} finally {
68+
Files.walk(tmp).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
69+
}
70+
}
71+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.jetbrains.kotlinx.dataframe.io
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
5+
import org.jetbrains.kotlinx.dataframe.DataFrame
6+
import org.junit.Test
7+
import java.io.File
8+
import java.nio.file.Files
9+
import java.nio.file.Path
10+
import kotlin.io.path.createTempDirectory
11+
import kotlin.io.path.createTempFile
12+
import kotlin.io.path.deleteExisting
13+
import kotlin.io.path.writeText
14+
15+
class CsvOverloadsTest {
16+
17+
private fun sampleCsv(): String = buildString {
18+
appendLine("name,age")
19+
appendLine("Alice,15")
20+
appendLine("Bob,20")
21+
}
22+
23+
@Test
24+
fun readCsv_overloads_String_Path_File_produce_same_df() {
25+
val tmpDir: Path = createTempDirectory("csv_overloads_read_")
26+
try {
27+
val csvPath = tmpDir.resolve("people.csv")
28+
csvPath.writeText(sampleCsv())
29+
30+
val dfFromString = DataFrame.readCsv(csvPath.toString())
31+
val dfFromPath = DataFrame.readCsv(csvPath)
32+
val dfFromFile = DataFrame.readCsv(csvPath.toFile())
33+
34+
dfFromPath.rowsCount() shouldBe dfFromString.rowsCount()
35+
dfFromFile.rowsCount() shouldBe dfFromString.rowsCount()
36+
dfFromPath.columnNames() shouldBe dfFromString.columnNames()
37+
dfFromFile.columnNames() shouldBe dfFromString.columnNames()
38+
// compare serialized CSV text to avoid dependency on row order formatters
39+
dfFromPath.toCsvStr() shouldBe dfFromString.toCsvStr()
40+
dfFromFile.toCsvStr() shouldBe dfFromString.toCsvStr()
41+
} finally {
42+
Files.walk(tmpDir)
43+
.sorted(Comparator.reverseOrder())
44+
.forEach { it.toFile().delete() }
45+
}
46+
}
47+
48+
@Test
49+
fun writeDelim_overloads_String_Path_File_roundtrip() {
50+
val df = dataFrameOf("name", "age")(
51+
"Alice", 15,
52+
"Bob", 20,
53+
)
54+
55+
val tmpDir: Path = createTempDirectory("csv_overloads_write_")
56+
try {
57+
val pathOut = tmpDir.resolve("out_path.csv")
58+
val fileOut = tmpDir.resolve("out_file.csv").toFile()
59+
val strOut = tmpDir.resolve("out_str.csv").toString()
60+
61+
// write using three overloads
62+
df.writeDelim(pathOut)
63+
df.writeDelim(fileOut)
64+
df.writeDelim(strOut)
65+
66+
// read back with any overload (use Path for uniformity)
67+
val readPath = DataFrame.readCsv(pathOut)
68+
val readFile = DataFrame.readCsv(fileOut)
69+
val readStr = DataFrame.readCsv(strOut)
70+
71+
readPath.toCsvStr() shouldBe df.toCsvStr()
72+
readFile.toCsvStr() shouldBe df.toCsvStr()
73+
readStr.toCsvStr() shouldBe df.toCsvStr()
74+
} finally {
75+
Files.walk(tmpDir)
76+
.sorted(Comparator.reverseOrder())
77+
.forEach { it.toFile().delete() }
78+
}
79+
}
80+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jetbrains.kotlinx.dataframe.io
2+
3+
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
4+
import org.junit.Test
5+
import org.junit.Assert.assertTrue
6+
import java.nio.file.Files
7+
import java.nio.file.Path
8+
import java.nio.file.Paths
9+
import kotlin.io.path.createTempDirectory
10+
11+
class ExcelOverloadsTest {
12+
13+
@Test
14+
fun writeExcel_overloads_String_Path_File_create_nonempty_files() {
15+
val df = dataFrameOf("name", "age")(
16+
"Alice", 15,
17+
"Bob", 20,
18+
)
19+
20+
val tmpDir: Path = createTempDirectory("excel_overloads_write_")
21+
try {
22+
val pathOut = tmpDir.resolve("out_path.xlsx")
23+
val fileOut = tmpDir.resolve("out_file.xlsx").toFile()
24+
val strOut = tmpDir.resolve("out_str.xlsx").toString()
25+
26+
df.writeExcel(pathOut)
27+
df.writeExcel(fileOut)
28+
df.writeExcel(strOut)
29+
30+
assertTrue(Files.exists(pathOut) && Files.size(pathOut) > 0L)
31+
assertTrue(Files.exists(fileOut.toPath()) && Files.size(fileOut.toPath()) > 0L)
32+
val strOutPath = Paths.get(strOut)
33+
assertTrue(Files.exists(strOutPath) && Files.size(strOutPath) > 0L)
34+
} finally {
35+
Files.walk(tmpDir).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
36+
}
37+
}
38+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.jetbrains.kotlinx.dataframe.io
2+
3+
import io.kotest.matchers.shouldBe
4+
import org.jetbrains.kotlinx.dataframe.AnyRow
5+
import org.jetbrains.kotlinx.dataframe.DataFrame
6+
import org.jetbrains.kotlinx.dataframe.api.dataFrameOf
7+
import kotlin.test.Test
8+
import kotlin.test.assertTrue
9+
import java.io.File
10+
import java.nio.file.Files
11+
import java.nio.file.Path
12+
import kotlin.io.path.createTempDirectory
13+
import kotlin.io.path.writeText
14+
15+
class JsonOverloadsTest {
16+
17+
private fun sampleJson(): String = """
18+
[
19+
{"name":"Alice","age":15},
20+
{"name":"Bob","age":20}
21+
]
22+
""".trimIndent()
23+
24+
@Test
25+
fun readJson_overloads_String_Path_File_produce_same_df() {
26+
val tmpDir: Path = createTempDirectory("json_overloads_read_")
27+
try {
28+
val jsonPath = tmpDir.resolve("people.json")
29+
jsonPath.writeText(sampleJson())
30+
31+
val dfFromString = DataFrame.readJson(jsonPath.toString())
32+
val dfFromPath = DataFrame.readJson(jsonPath)
33+
val dfFromFile = DataFrame.readJson(jsonPath.toFile())
34+
35+
dfFromPath.rowsCount() shouldBe dfFromString.rowsCount()
36+
dfFromFile.rowsCount() shouldBe dfFromString.rowsCount()
37+
dfFromPath.columnNames() shouldBe dfFromString.columnNames()
38+
dfFromFile.columnNames() shouldBe dfFromString.columnNames()
39+
40+
// serialize back to JSON (order-insensitive enough for this sample) and compare sizes
41+
dfFromPath.toJson() shouldBe dfFromString.toJson()
42+
dfFromFile.toJson() shouldBe dfFromString.toJson()
43+
} finally {
44+
Files.walk(tmpDir).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
45+
}
46+
}
47+
48+
@Test
49+
fun writeJson_overloads_String_Path_File_roundtrip_df_and_row() {
50+
val df = dataFrameOf("name", "age")(
51+
"Alice", 15,
52+
"Bob", 20,
53+
)
54+
val row: AnyRow = df[0]
55+
56+
val tmpDir: Path = createTempDirectory("json_overloads_write_")
57+
try {
58+
val pathOut = tmpDir.resolve("out_path.json")
59+
val fileOut = tmpDir.resolve("out_file.json").toFile()
60+
val strOut = tmpDir.resolve("out_str.json").toString()
61+
62+
df.writeJson(pathOut, prettyPrint = true)
63+
df.writeJson(fileOut, prettyPrint = true)
64+
df.writeJson(strOut, prettyPrint = true)
65+
66+
// read back using three overloads
67+
val readP = DataFrame.readJson(pathOut)
68+
val readF = DataFrame.readJson(fileOut)
69+
val readS = DataFrame.readJson(strOut)
70+
71+
readP.toJson() shouldBe df.toJson()
72+
readF.toJson() shouldBe df.toJson()
73+
readS.toJson() shouldBe df.toJson()
74+
75+
// AnyRow writeJson overloads do not read back; verify they produce files
76+
row.writeJson(tmpDir.resolve("row_path.json"))
77+
row.writeJson(tmpDir.resolve("row_file.json").toFile())
78+
row.writeJson(tmpDir.resolve("row_str.json").toString())
79+
80+
assertTrue(Files.size(tmpDir.resolve("row_path.json")) > 0L)
81+
assertTrue(Files.size(tmpDir.resolve("row_file.json")) > 0L)
82+
assertTrue(Files.size(tmpDir.resolve("row_str.json")) > 0L)
83+
} finally {
84+
Files.walk(tmpDir).sorted(Comparator.reverseOrder()).forEach { it.toFile().delete() }
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)