Skip to content

Commit

Permalink
Add tests and handle an "id" edge-case
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Jan 30, 2024
1 parent 54710a8 commit 2fadfbb
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ class DataClassTypedIDsRule(config: Config) : Rule(config) {
if (paramType == "UUID") return true

name?.let { paramName ->
if (paramName == "id") return true

val endsLikeID = IdFieldEndings.any {
paramName.endsWith(it, ignoreCase = false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,85 @@ internal class DataClassTypedIDsRuleRuleTest(private val env: KotlinCoreEnvironm
Data class 'A' should use type-safe IDs instead of UUID for property 'id'. Typed-IDs like `value class SomeId(val id: UUID)` provide compile-time safety and prevent mixing IDs of different entities.
""".trimIndent()
}

@Test
fun `reports data class having String as id`() {
val code = """
data class A(
val id: String,
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}

@Test
fun `reports data class having transactionId Int`() {
val code = """
data class A(
val name: String,
val transactionId: Int
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 1
}

@Test
fun `doesn't report data class without ids`() {
val code = """
data class Person(
val firstName: String,
val lastName: String,
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report Dto classes`() {
val code = """
data class SomeDto(
val id: UUID
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report Entity classes`() {
val code = """
data class SomeEntity(
val id: UUID
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report classes annotated with @Entity`() {
val code = """
@Entity(tableName = "budgets")
data class A(
val id: UUID
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}

@Test
fun `doesn't report classes annotated with @Serializable`() {
val code = """
@Serializable
data class A(
val id: UUID
)
"""
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
findings shouldHaveSize 0
}
}

0 comments on commit 2fadfbb

Please sign in to comment.