Skip to content
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

add Long strategy #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/main/kotlin/com/sipios/springsearch/strategies/LongStrategy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sipios.springsearch.strategies

import com.sipios.springsearch.SearchOperation
import javax.persistence.criteria.CriteriaBuilder
import javax.persistence.criteria.Path
import javax.persistence.criteria.Predicate
import kotlin.reflect.KClass

class LongStrategy : ParsingStrategy {
override fun buildPredicate(
builder: CriteriaBuilder,
path: Path<*>,
fieldName: String,
ops: SearchOperation?,
value: Any?
): Predicate? {
return when (ops) {
SearchOperation.GREATER_THAN -> builder.greaterThan(path.get(fieldName), value as Long)
SearchOperation.LESS_THAN -> builder.lessThan(path.get(fieldName), value as Long)
else -> super.buildPredicate(builder, path, fieldName, ops, value)
}
}

override fun parse(value: String?, fieldClass: KClass<out Any>): Any? {
return value?.toLong()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface ParsingStrategy {
fieldClass == Double::class -> DoubleStrategy()
fieldClass == Float::class -> FloatStrategy()
fieldClass == Int::class -> IntStrategy()
fieldClass == Long::class -> LongStrategy()
fieldClass.isSubclassOf(Enum::class) -> EnumStrategy()
fieldClass == Duration::class -> DurationStrategy()
fieldClass == LocalDate::class -> LocalDateStrategy()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,60 @@ class SpringSearchApplicationTest {
Assertions.assertTrue(setOf(userWith1ChildId, userWith5ChildrenId, userWith6ChildrenId) == specificationUsers.map { user -> user.userId }.toSet())
}

@Test
fun canGetUserWithHugeDebt() {
val userWith1BDebtId = userRepository.save(Users(userDebt = 4000000000)).userId
val userWith2BDebtId = userRepository.save(Users(userDebt = 5000000000)).userId
val userWith3BDebtId = userRepository.save(Users(userDebt = 6000000000)).userId
userRepository.save(Users(userDebt = 0))
userRepository.save(Users(userDebt = 1000))
userRepository.save(Users(userDebt = 20000))
userRepository.save(Users(userDebt = 300000))

val specification = SpecificationsBuilder<Users>(SearchSpec::class.constructors.first().call("", true)).withSearch("userDebt>1000000000").build()
val specificationUsers = userRepository.findAll(specification)
Assertions.assertTrue(setOf(userWith1BDebtId, userWith2BDebtId, userWith3BDebtId) == specificationUsers.map { user -> user.userId }.toSet())
}

@Test
fun canGetUserWithSmallDebt() {
userRepository.save(Users(userDebt = 4000000000))
userRepository.save(Users(userDebt = 5000000000))
userRepository.save(Users(userDebt = 6000000000))
val userWith1KDebtId = userRepository.save(Users(userDebt = 1000)).userId
val userWith2KDebtId = userRepository.save(Users(userDebt = 2000)).userId

val specification = SpecificationsBuilder<Users>(SearchSpec::class.constructors.first().call("", true)).withSearch("userDebt>0 AND userDebt<1000000000").build()
val specificationUsers = userRepository.findAll(specification)
Assertions.assertTrue(setOf(userWith1KDebtId, userWith2KDebtId) == specificationUsers.map { user -> user.userId }.toSet())
}

@Test
fun canGetUserWithNoDebt() {
val user1WithNoDebtId = userRepository.save(Users(userDebt = 0)).userId
val user2WithNoDebtId = userRepository.save(Users(userDebt = 0)).userId
val user3WithNoDebtId = userRepository.save(Users(userDebt = 0)).userId
userRepository.save(Users(userDebt = 1000))
userRepository.save(Users(userDebt = 2000))
userRepository.save(Users(userDebt = 3000))

val specification = SpecificationsBuilder<Users>(SearchSpec::class.constructors.first().call("", true)).withSearch("userDebt:0").build()
val specificationUsers = userRepository.findAll(specification)
Assertions.assertTrue(setOf(user1WithNoDebtId, user2WithNoDebtId, user3WithNoDebtId) == specificationUsers.map { user -> user.userId }.toSet())
}

@Test
fun canGetUserWithDebt() {
val user1WithNoDebtId = userRepository.save(Users(userDebt = 1000)).userId
val user2WithNoDebtId = userRepository.save(Users(userDebt = 2000)).userId
userRepository.save(Users(userDebt = 0))
userRepository.save(Users(userDebt = 0))

val specification = SpecificationsBuilder<Users>(SearchSpec::class.constructors.first().call("", true)).withSearch("userDebt!0").build()
val specificationUsers = userRepository.findAll(specification)
Assertions.assertTrue(setOf(user1WithNoDebtId, user2WithNoDebtId) == specificationUsers.map { user -> user.userId }.toSet())
}

@Test
fun canGetUserWithSmallerSalary() {
val smallerSalaryUserId = userRepository.save(Users(userSalary = 2223.3F)).userId
Expand Down
3 changes: 3 additions & 0 deletions src/test/kotlin/com/sipios/springsearch/Users.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ data class Users(
@Column(name = "NumberOfChildren")
var userChildrenNumber: Int = 3,

@Column(name = "Debt")
var userDebt: Long = 3000000000L,

@Column(name = "Salary")
var userSalary: Float = 3000.0F,

Expand Down