Skip to content

Commit 34f5317

Browse files
authored
Improve schema validation. (#96) (#107)
1 parent 2a3ea50 commit 34f5317

5 files changed

Lines changed: 34 additions & 48 deletions

File tree

storm-kotlin/src/main/kotlin/st/orm/template/ORMTemplate.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import st.orm.repository.RepositoryLookup
2424
import st.orm.template.impl.ORMTemplateImpl
2525
import java.sql.Connection
2626
import javax.sql.DataSource
27+
import kotlin.reflect.KClass
2728

2829
/**
2930
* The primary entry point for Storm's ORM functionality in Kotlin, combining SQL template query construction with
@@ -113,7 +114,7 @@ interface ORMTemplate :
113114
* @throws st.orm.PersistenceException if the template does not support schema validation.
114115
* @since 1.9
115116
*/
116-
fun validateSchema(types: Iterable<Class<out Data>>): List<String>
117+
fun validateSchema(vararg types: KClass<out Data>): List<String>
117118

118119
/**
119120
* Validates all discovered types and throws if any errors are found.
@@ -136,7 +137,7 @@ interface ORMTemplate :
136137
* @throws st.orm.PersistenceException if validation fails or the template does not support schema validation.
137138
* @since 1.9
138139
*/
139-
fun validateSchemaOrThrow(types: Iterable<Class<out Data>>)
140+
fun validateSchemaOrThrow(vararg types: KClass<out Data>)
140141

141142
companion object {
142143
/**

storm-kotlin/src/main/kotlin/st/orm/template/impl/ORMTemplateImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ class ORMTemplateImpl(private val core: st.orm.core.template.ORMTemplate) :
8888

8989
override fun validateSchema(): List<String> = core.validateSchema()
9090

91-
override fun validateSchema(types: Iterable<Class<out Data>>): List<String> = core.validateSchema(types)
91+
override fun validateSchema(vararg types: KClass<out Data>): List<String> = core.validateSchema(types.map { it.java })
9292

9393
override fun validateSchemaOrThrow() = core.validateSchemaOrThrow()
9494

95-
override fun validateSchemaOrThrow(types: Iterable<Class<out Data>>) = core.validateSchemaOrThrow(types)
95+
override fun validateSchemaOrThrow(vararg types: KClass<out Data>) = core.validateSchemaOrThrow(types.map { it.java })
9696

9797
/**
9898
* Returns the repository for the given entity type.

storm-kotlin/src/test/kotlin/st/orm/template/ORMTemplateFactoryTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ open class ORMTemplateFactoryTest(
167167

168168
@Test
169169
fun `validateSchema should return list of errors or empty`() {
170-
val errors = orm.validateSchema(listOf(City::class.java))
170+
val errors = orm.validateSchema(City::class)
171171
// City should validate fine against H2
172172
errors shouldHaveSize 0
173173
}

storm-kotlin/src/test/kotlin/st/orm/template/ORMTemplateTest.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,29 @@ open class ORMTemplateTest(
106106

107107
@Test
108108
fun `validateSchema should return empty for valid types`() {
109-
val errors = orm.validateSchema(listOf(City::class.java))
109+
val errors = orm.validateSchema(City::class)
110110
errors shouldHaveSize 0
111111
}
112112

113113
@Test
114114
fun `validateSchema should return errors for invalid types`() {
115-
@st.orm.DbTable("nonexistent_table")
116-
data class NonExistentEntity(@st.orm.PK val id: Int = 0) : Entity<Int>
117-
val errors = orm.validateSchema(listOf(NonExistentEntity::class.java))
115+
@DbTable("nonexistent_table")
116+
data class NonExistentEntity(@PK val id: Int = 0) : Entity<Int>
117+
val errors = orm.validateSchema(NonExistentEntity::class)
118118
errors.size shouldBe 1
119119
}
120120

121121
@Test
122122
fun `validateSchemaOrThrow should not throw for valid types`() {
123-
orm.validateSchemaOrThrow(listOf(City::class.java))
123+
orm.validateSchemaOrThrow(City::class)
124124
}
125125

126126
@Test
127127
fun `validateSchemaOrThrow should throw for invalid types`() {
128-
@st.orm.DbTable("nonexistent_table")
129-
data class NonExistentEntity(@st.orm.PK val id: Int = 0) : Entity<Int>
128+
@DbTable("nonexistent_table")
129+
data class NonExistentEntity(@PK val id: Int = 0) : Entity<Int>
130130
assertThrows<PersistenceException> {
131-
orm.validateSchemaOrThrow(listOf(NonExistentEntity::class.java))
131+
orm.validateSchemaOrThrow(NonExistentEntity::class)
132132
}
133133
}
134134

storm-kotlin/src/test/kotlin/st/orm/template/SchemaValidationTest.kt

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,51 +28,49 @@ open class SchemaValidationTest(
2828

2929
@Test
3030
fun `validateSchema with City should return no errors`() {
31-
val errors = orm.validateSchema(listOf(City::class.java))
31+
val errors = orm.validateSchema(City::class)
3232
errors.shouldBeEmpty()
3333
}
3434

3535
@Test
3636
fun `validateSchema with Owner should return no errors`() {
37-
val errors = orm.validateSchema(listOf(Owner::class.java))
37+
val errors = orm.validateSchema(Owner::class)
3838
errors.shouldBeEmpty()
3939
}
4040

4141
@Test
4242
fun `validateSchema with Pet should return no errors`() {
43-
val errors = orm.validateSchema(listOf(Pet::class.java))
43+
val errors = orm.validateSchema(Pet::class)
4444
errors.shouldBeEmpty()
4545
}
4646

4747
@Test
4848
fun `validateSchema with Vet should return no errors`() {
49-
val errors = orm.validateSchema(listOf(Vet::class.java))
49+
val errors = orm.validateSchema(Vet::class)
5050
errors.shouldBeEmpty()
5151
}
5252

5353
@Test
5454
fun `validateSchema with PetType should return no errors`() {
55-
val errors = orm.validateSchema(listOf(PetType::class.java))
55+
val errors = orm.validateSchema(PetType::class)
5656
errors.shouldBeEmpty()
5757
}
5858

5959
@Test
6060
fun `validateSchema with Visit should return no errors`() {
61-
val errors = orm.validateSchema(listOf(Visit::class.java))
61+
val errors = orm.validateSchema(Visit::class)
6262
errors.shouldBeEmpty()
6363
}
6464

6565
@Test
6666
fun `validateSchema with multiple valid types should return no errors`() {
6767
val errors = orm.validateSchema(
68-
listOf(
69-
City::class.java,
70-
Owner::class.java,
71-
Pet::class.java,
72-
PetType::class.java,
73-
Vet::class.java,
74-
Visit::class.java,
75-
),
68+
City::class,
69+
Owner::class,
70+
Pet::class,
71+
PetType::class,
72+
Vet::class,
73+
Visit::class,
7674
)
7775
errors.shouldBeEmpty()
7876
}
@@ -88,7 +86,7 @@ open class SchemaValidationTest(
8886

8987
@Test
9088
fun `validateSchema with nonexistent table entity should return errors`() {
91-
val errors = orm.validateSchema(listOf(NonExistentEntity::class.java))
89+
val errors = orm.validateSchema(NonExistentEntity::class)
9290
errors.shouldNotBeEmpty()
9391
}
9492

@@ -102,7 +100,7 @@ open class SchemaValidationTest(
102100

103101
@Test
104102
fun `validateSchema with mismatched columns should return errors`() {
105-
val errors = orm.validateSchema(listOf(CityMismatch::class.java))
103+
val errors = orm.validateSchema(CityMismatch::class)
106104
errors.shouldNotBeEmpty()
107105
}
108106

@@ -111,37 +109,24 @@ open class SchemaValidationTest(
111109
@Test
112110
fun `validateSchemaOrThrow with valid types should not throw`() {
113111
// Should complete without exception for valid entity types.
114-
orm.validateSchemaOrThrow(listOf(City::class.java, Vet::class.java))
112+
orm.validateSchemaOrThrow(City::class, Vet::class)
115113
}
116114

117115
@Test
118116
fun `validateSchemaOrThrow with invalid type should throw PersistenceException`() {
119117
assertThrows<PersistenceException> {
120-
orm.validateSchemaOrThrow(listOf(NonExistentEntity::class.java))
118+
orm.validateSchemaOrThrow(NonExistentEntity::class)
121119
}
122120
}
123121

124122
@Test
125123
fun `validateSchemaOrThrow with mixed valid and invalid types should throw`() {
126124
assertThrows<PersistenceException> {
127-
orm.validateSchemaOrThrow(listOf(City::class.java, NonExistentEntity::class.java))
125+
orm.validateSchemaOrThrow(City::class, NonExistentEntity::class)
128126
}
129127
}
130128

131-
// validateSchema with empty list
132-
133-
@Test
134-
fun `validateSchema with empty list should return no errors`() {
135-
val errors = orm.validateSchema(emptyList())
136-
errors.shouldBeEmpty()
137-
}
138-
139-
@Test
140-
fun `validateSchemaOrThrow with empty list should not throw`() {
141-
orm.validateSchemaOrThrow(emptyList())
142-
}
143-
144-
// validateSchema on connection-backed template
129+
// validateSchema with no args (zero varargs)
145130

146131
@Test
147132
fun `validateSchema on connection-backed template should throw PersistenceException`() {
@@ -168,12 +153,12 @@ open class SchemaValidationTest(
168153

169154
@Test
170155
fun `validateSchema with OwnerView projection should return no errors`() {
171-
val errors = orm.validateSchema(listOf(OwnerView::class.java))
156+
val errors = orm.validateSchema(OwnerView::class)
172157
errors.shouldBeEmpty()
173158
}
174159

175160
@Test
176161
fun `validateSchemaOrThrow with OwnerView projection should not throw`() {
177-
orm.validateSchemaOrThrow(listOf(OwnerView::class.java))
162+
orm.validateSchemaOrThrow(OwnerView::class)
178163
}
179164
}

0 commit comments

Comments
 (0)