-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Add YawnComparison enum to yawn-api #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
618f9b1
Add YawnComparison enum to com.faire.yawn.query package
cursoragent 8d7efee
Add tests for YawnComparison and fix value type to F & Any
cursoragent a96746d
Add EQ variant to YawnComparison enum
cursoragent 13640cb
Rewrite YawnComparison tests with realistic multi-column scenario
cursoragent 9e0020f
Improve test readability: assert columns separately, move helper to b…
cursoragent 304fca2
Fix detekt ArgumentListWrapping violations in test
cursoragent 59dcccb
Add database integration tests for YawnComparison
cursoragent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
yawn-api/src/main/kotlin/com/faire/yawn/query/YawnComparison.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.faire.yawn.query | ||
|
|
||
| import com.faire.yawn.YawnDef | ||
|
|
||
| enum class YawnComparison { | ||
| EQ, LT, LE, GT, GE; | ||
|
|
||
| fun <SOURCE : Any, F> compare( | ||
chinhtrung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| column: YawnDef<SOURCE, *>.YawnColumnDef<F>, | ||
| value: F & Any, | ||
| ): YawnQueryCriterion<SOURCE> = when (this) { | ||
| EQ -> YawnRestrictions.eq(column, value) | ||
| LT -> YawnRestrictions.lt(column, value) | ||
| LE -> YawnRestrictions.le(column, value) | ||
| GT -> YawnRestrictions.gt(column, value) | ||
| GE -> YawnRestrictions.ge(column, value) | ||
| } | ||
| } | ||
121 changes: 121 additions & 0 deletions
121
yawn-api/src/test/kotlin/com/faire/yawn/query/YawnComparisonTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| package com.faire.yawn.query | ||
|
|
||
| import com.faire.yawn.YawnTableDef | ||
| import com.faire.yawn.YawnTableDefParent | ||
| import org.assertj.core.api.Assertions.assertThat | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| internal class YawnComparisonTest { | ||
chinhtrung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| private class OrderEntity | ||
|
|
||
| /** | ||
| * Simulates a table with columns of different types — the scenario where | ||
| * you can't pass YawnRestrictions::le as a function parameter because F | ||
| * is invariant and would need to unify across Int, String, and Long. | ||
| */ | ||
| private object OrderDef : YawnTableDef<OrderEntity, OrderEntity>(YawnTableDefParent.RootTableDefParent) { | ||
| val amount: ColumnDef<Int> = ColumnDef("amount") | ||
| val createdAt: ColumnDef<String> = ColumnDef("created_at") | ||
| val itemCount: ColumnDef<Long> = ColumnDef("item_count") | ||
| } | ||
|
|
||
| @Test | ||
| fun `same comparison applies to columns of different types`() { | ||
| val (amountCriterion, createdAtCriterion, itemCountCriterion) = buildOrderCriteria( | ||
| comparison = YawnComparison.LE, | ||
| amount = 100, | ||
| createdAt = "2026-01-01", | ||
| itemCount = 50L, | ||
| ) | ||
|
|
||
| assertThat(amountCriterion.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.LessThanOrEqualTo::class.java) | ||
| assertThat(createdAtCriterion.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.LessThanOrEqualTo::class.java) | ||
| assertThat(itemCountCriterion.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.LessThanOrEqualTo::class.java) | ||
| } | ||
|
|
||
| @Test | ||
| fun `switching comparison changes all generated restrictions`() { | ||
| val (leAmount, leCreatedAt, leItemCount) = buildOrderCriteria( | ||
| comparison = YawnComparison.LE, | ||
| amount = 100, | ||
| createdAt = "2026-01-01", | ||
| itemCount = 50L, | ||
| ) | ||
| val (gtAmount, gtCreatedAt, gtItemCount) = buildOrderCriteria( | ||
| comparison = YawnComparison.GT, | ||
| amount = 100, | ||
| createdAt = "2026-01-01", | ||
| itemCount = 50L, | ||
| ) | ||
|
|
||
| assertThat(leAmount.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.LessThanOrEqualTo::class.java) | ||
| assertThat(leCreatedAt.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.LessThanOrEqualTo::class.java) | ||
| assertThat(leItemCount.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.LessThanOrEqualTo::class.java) | ||
|
|
||
| assertThat(gtAmount.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.GreaterThan::class.java) | ||
| assertThat(gtCreatedAt.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.GreaterThan::class.java) | ||
| assertThat(gtItemCount.yawnRestriction) | ||
| .isInstanceOf(YawnQueryRestriction.GreaterThan::class.java) | ||
| } | ||
|
|
||
| @Test | ||
| fun `each variant produces the expected restriction type`() { | ||
| val expected = mapOf( | ||
| YawnComparison.EQ to YawnQueryRestriction.Equals::class.java, | ||
| YawnComparison.LT to YawnQueryRestriction.LessThan::class.java, | ||
| YawnComparison.LE to YawnQueryRestriction.LessThanOrEqualTo::class.java, | ||
| YawnComparison.GT to YawnQueryRestriction.GreaterThan::class.java, | ||
| YawnComparison.GE to YawnQueryRestriction.GreaterThanOrEqualTo::class.java, | ||
| ) | ||
|
|
||
| for ((comparison, restrictionClass) in expected) { | ||
| val criterion = comparison.compare(OrderDef.amount, 42) | ||
| assertThat(criterion.yawnRestriction) | ||
| .`as`("YawnComparison.%s", comparison.name) | ||
| .isInstanceOf(restrictionClass) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| fun `all comparison variants are present`() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this test is all that valuable, it's subsumed by the restriction type test |
||
| assertThat(YawnComparison.entries).containsExactly( | ||
| YawnComparison.EQ, | ||
| YawnComparison.LT, | ||
| YawnComparison.LE, | ||
| YawnComparison.GT, | ||
| YawnComparison.GE, | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * A helper that applies the same comparison to columns of different types. | ||
| * This is the pattern YawnComparison enables: F is resolved independently | ||
| * at each compare() call site, so a single comparison value works across | ||
| * ColumnDef<Int>, ColumnDef<String>, and ColumnDef<Long>. | ||
| * | ||
| * This cannot be expressed with a Kotlin function type parameter like | ||
| * `(YawnDef<SOURCE, *>.YawnColumnDef<F>, F & Any) -> YawnQueryCriterion<SOURCE>` | ||
| * because F would have to be fixed for the entire function signature. | ||
| */ | ||
| private fun buildOrderCriteria( | ||
| comparison: YawnComparison, | ||
| amount: Int, | ||
| createdAt: String, | ||
| itemCount: Long, | ||
| ): Triple<YawnQueryCriterion<OrderEntity>, YawnQueryCriterion<OrderEntity>, YawnQueryCriterion<OrderEntity>> { | ||
| return Triple( | ||
| comparison.compare(OrderDef.amount, amount), | ||
| comparison.compare(OrderDef.createdAt, createdAt), | ||
| comparison.compare(OrderDef.itemCount, itemCount), | ||
| ) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.