Skip to content

Commit

Permalink
change loader logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yliuuuu committed Nov 4, 2023
1 parent 505a399 commit d6adc32
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import kotlin.test.assertNotNull
import kotlin.test.assertTrue

class PartiQLSchemaInferencerTests {
private val provider = PartiQLTestProvider()

init {
// load test inputs
Expand Down Expand Up @@ -130,9 +131,7 @@ class PartiQLSchemaInferencerTests {
fun testCaseWhens(tc: TestCase) = runTest(tc)

companion object {
private val provider = PartiQLTestProvider

private val root = PartiQLTestProvider.javaClass.getResource("/catalogs/default")!!.toURI().toPath().pathString
private val root = PartiQLTestProvider().javaClass.getResource("/catalogs/default")!!.toURI().toPath().pathString

private val PLUGINS = listOf(LocalPlugin())

Expand Down
35 changes: 35 additions & 0 deletions partiql-planner/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.dokka.utilities.relativeTo

/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
Expand Down Expand Up @@ -34,6 +36,39 @@ dependencies {
testFixturesImplementation(project(":partiql-spi"))
}

tasks.register("generateResourcePath") {
doLast {
val resourceDir = file("src/testFixtures/resources")
val outDir = File("$buildDir/resources/testFixtures")
val fileName = "resource_path.txt"
val pathFile = File(outDir, fileName)
println(pathFile.absolutePath)
if (pathFile.exists()) {
println("exists")
pathFile.writeText("") // clean up existing text
}
println("if statement completed")
resourceDir.walk().forEach { file ->
println(file)
if (!file.isDirectory) {
if (file.extension == "ion" || file.extension == "sql") {
val toAppend = file.toURI().relativeTo(resourceDir.toURI())
pathFile.appendText("$toAppend\n")
}
}
}

sourceSets {
testFixtures {
resources {
this.srcDirs += pathFile
}
}
}
}
}

tasks.processTestResources {
dependsOn("generateResourcePath")
from("src/testFixtures/resources")
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,48 @@
package org.partiql.planner.test

import java.io.File
import java.io.InputStream
import java.nio.file.Path
import kotlin.io.path.toPath

/**
* The PartiQLTestProvider is a simple utility for indexing SQL statements within files for re-use across library tests.
*/
object PartiQLTestProvider {
class PartiQLTestProvider {

/**
* Backing map for test input lookup.
*/
private val map: MutableMap<PartiQLTest.Key, PartiQLTest> = mutableMapOf()

/**
* Default database of test inputs.
* Retrieve of path information.
* This is primarily for Non-IDE build, i.e., GITHUB build. Where resource are loading from jar.
* It is very tricky to retrieve files from jar.
*/
private val default = this::class.java.getResource("/inputs")!!.toURI().toPath()
private val default = { this::class.java.getResourceAsStream("/resource_path.txt")!! }

/**
* Load test groups from a directory.
*/
public fun load(root: Path? = null) {
val dir = (root ?: default).toFile()
dir.listFiles { f -> f.isDirectory }!!.map {
for (test in load(it)) {
map[test.key] = test
if (root != null) {
val dir = root.toFile()
dir.listFiles { f -> f.isDirectory }!!.map {
for (test in load(it)) {
map[test.key] = test
}
}
} else {
default().reader().forEachLine { path ->
val pathSteps = path.split("/")
val outMostDir = pathSteps.first()
if (outMostDir == "inputs") {
val group = pathSteps[pathSteps.size - 2]
val resource = this::class.java.getResourceAsStream("/$path")!!
for (test in load(group, resource)) {
map[test.key] = test
}
}
}
}
}
Expand All @@ -66,11 +82,13 @@ object PartiQLTestProvider {
private fun load(dir: File) = dir.listFiles()!!.flatMap { load(dir.name, it) }

// load all tests in a file
private fun load(group: String, file: File): List<PartiQLTest> {
private fun load(group: String, file: File): List<PartiQLTest> = load(group, file.inputStream())

private fun load(group: String, inputStream: InputStream): List<PartiQLTest> {
val tests = mutableListOf<PartiQLTest>()
var name = ""
val statement = StringBuilder()
for (line in file.readLines()) {
for (line in inputStream.reader().readLines()) {

// start of test
if (line.startsWith("--#[") and line.endsWith("]")) {
Expand Down

0 comments on commit d6adc32

Please sign in to comment.