Skip to content

Commit 2fadfbb

Browse files
Add tests and handle an "id" edge-case
1 parent 54710a8 commit 2fadfbb

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/main/kotlin/com/github/ivy/explicit/rule/DataClassTypedIDsRule.kt

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ class DataClassTypedIDsRule(config: Config) : Rule(config) {
6767
if (paramType == "UUID") return true
6868

6969
name?.let { paramName ->
70+
if (paramName == "id") return true
71+
7072
val endsLikeID = IdFieldEndings.any {
7173
paramName.endsWith(it, ignoreCase = false)
7274
}

src/test/kotlin/com/github/ivy/explicit/rule/DataClassTypedIDsRuleRuleTest.kt

+81
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,85 @@ internal class DataClassTypedIDsRuleRuleTest(private val env: KotlinCoreEnvironm
2626
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.
2727
""".trimIndent()
2828
}
29+
30+
@Test
31+
fun `reports data class having String as id`() {
32+
val code = """
33+
data class A(
34+
val id: String,
35+
)
36+
"""
37+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
38+
findings shouldHaveSize 1
39+
}
40+
41+
@Test
42+
fun `reports data class having transactionId Int`() {
43+
val code = """
44+
data class A(
45+
val name: String,
46+
val transactionId: Int
47+
)
48+
"""
49+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
50+
findings shouldHaveSize 1
51+
}
52+
53+
@Test
54+
fun `doesn't report data class without ids`() {
55+
val code = """
56+
data class Person(
57+
val firstName: String,
58+
val lastName: String,
59+
)
60+
"""
61+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
62+
findings shouldHaveSize 0
63+
}
64+
65+
@Test
66+
fun `doesn't report Dto classes`() {
67+
val code = """
68+
data class SomeDto(
69+
val id: UUID
70+
)
71+
"""
72+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
73+
findings shouldHaveSize 0
74+
}
75+
76+
@Test
77+
fun `doesn't report Entity classes`() {
78+
val code = """
79+
data class SomeEntity(
80+
val id: UUID
81+
)
82+
"""
83+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
84+
findings shouldHaveSize 0
85+
}
86+
87+
@Test
88+
fun `doesn't report classes annotated with @Entity`() {
89+
val code = """
90+
@Entity(tableName = "budgets")
91+
data class A(
92+
val id: UUID
93+
)
94+
"""
95+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
96+
findings shouldHaveSize 0
97+
}
98+
99+
@Test
100+
fun `doesn't report classes annotated with @Serializable`() {
101+
val code = """
102+
@Serializable
103+
data class A(
104+
val id: UUID
105+
)
106+
"""
107+
val findings = DataClassTypedIDsRule(Config.empty).compileAndLintWithContext(env, code)
108+
findings shouldHaveSize 0
109+
}
29110
}

0 commit comments

Comments
 (0)