Skip to content

Commit

Permalink
feat(test): Improve test coverage (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahoo-Wang authored Mar 5, 2024
1 parent 492f0d1 commit 4d5214b
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 22 deletions.
10 changes: 5 additions & 5 deletions compensation/wow-compensation-dashboard/src/app/api/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ export enum Operator {
LT = "LT",
GTE = "GTE",
LTE = "LTE",
LIKE = "LIKE",
CONTAINS = "CONTAINS",
IN = "IN",
NOT_IN = "NOT_IN",
BETWEEN = "BETWEEN",
ALL_IN = "ALL_IN",
STATS_WITH = "STATS_WITH",
STARTS_WITH = "STARTS_WITH",
ELEM_MATCH = "ELEM_MATCH",
NULL = "NULL",
NOT_NULL = "NOT_NULL",
Expand Down Expand Up @@ -91,8 +91,8 @@ export class Conditions {
return {field, operator: Operator.LTE, value, children: [], not: false}
}

static like(field: string, value: any): Condition {
return {field, operator: Operator.LIKE, value, children: [], not: false}
static contains(field: string, value: any): Condition {
return {field, operator: Operator.CONTAINS, value, children: [], not: false}
}

static in(field: string, value: any): Condition {
Expand All @@ -112,7 +112,7 @@ export class Conditions {
}

static startsWith(field: string, value: any): Condition {
return {field, operator: Operator.STATS_WITH, value, children: [], not: false}
return {field, operator: Operator.STARTS_WITH, value, children: [], not: false}
}

static elemMatch(field: string, value: Condition): Condition {
Expand Down
13 changes: 9 additions & 4 deletions documentation/docs/guide/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ enum class Operator {
/**
* 匹配给定字段的值包含指定值的所有文档
*/
LIKE,
CONTAINS,

/**
* 匹配字段值等于指定值列表中的任何值的所有文档
Expand All @@ -87,7 +87,12 @@ enum class Operator {
/**
* 匹配字段值以指定字符串开头的文档
*/
STATS_WITH,
STARTS_WITH,

/**
* 匹配字段值以指定字符串结尾的文档
*/
ENDS_WITH,

/**
* 条件与包含数组字段的所有文档相匹配,其中数组中至少有一个成员与给定的条件匹配。
Expand Down Expand Up @@ -130,7 +135,7 @@ condition {
"field4" lt 1
"field5" gte 1
"field6" lte 1
"field7" like "value7"
"field7" contains "value7"
"field8" isIn listOf("value8")
"field9" notIn listOf("value9")
"field10" between (1 to 2)
Expand Down Expand Up @@ -209,7 +214,7 @@ pagedQuery {
"field4" lt 1
"field5" gte 1
"field6" lte 1
"field7" like "value7"
"field7" contains "value7"
"field8" isIn listOf("value8")
"field9" notIn listOf("value9")
"field10" between (1 to 2)
Expand Down
4 changes: 2 additions & 2 deletions wow-api/src/main/kotlin/me/ahoo/wow/api/query/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ enum class Operator {
/**
* 匹配给定字段的值包含指定值的所有文档
*/
LIKE,
CONTAINS,

/**
* 匹配字段值等于指定值列表中的任何值的所有文档
Expand Down Expand Up @@ -166,7 +166,7 @@ data class Condition(
fun lt(field: String, value: Any) = Condition(field, Operator.LT, value)
fun gte(field: String, value: Any) = Condition(field, Operator.GTE, value)
fun lte(field: String, value: Any) = Condition(field, Operator.LTE, value)
fun like(field: String, value: Any) = Condition(field, Operator.LIKE, value)
fun contains(field: String, value: Any) = Condition(field, Operator.CONTAINS, value)
fun isIn(field: String, value: List<Any>) = Condition(field, Operator.IN, value)
fun notIn(field: String, value: List<Any>) = Condition(field, Operator.NOT_IN, value)
fun <V> between(field: String, start: V, end: V) = Condition(field, Operator.BETWEEN, listOf(start, end))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ object MongoFilterConverter {
Operator.LT -> Filters.lt(field, value)
Operator.GTE -> Filters.gte(field, value)
Operator.LTE -> Filters.lte(field, value)
Operator.LIKE -> Filters.regex(field, value as String)
Operator.CONTAINS -> Filters.regex(field, value as String)
Operator.IN -> Filters.`in`(field, value as List<*>)
Operator.NOT_IN -> Filters.nin(field, value as List<*>)
Operator.TRUE -> Filters.eq(field, true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class MongoFilterConverterTest {
Arguments.of(Condition.lt("id", 1), Filters.lt("id", 1)),
Arguments.of(Condition.gte("id", 1), Filters.gte("id", 1)),
Arguments.of(Condition.lte("id", 1), Filters.lte("id", 1)),
Arguments.of(Condition.like("id", "value"), Filters.regex("id", "value")),
Arguments.of(Condition.contains("id", "value"), Filters.regex("id", "value")),
Arguments.of(Condition.isIn("id", listOf("value")), Filters.`in`("id", listOf("value"))),
Arguments.of(Condition.notIn("id", listOf("value")), Filters.nin("id", listOf("value"))),
Arguments.of(
Expand All @@ -84,6 +84,7 @@ class MongoFilterConverterTest {
Filters.elemMatch("id", Filters.eq("id", "id"))
),
Arguments.of(Condition.startsWith("id", "value"), Filters.regex("id", "^value")),
Arguments.of(Condition.endsWith("id", "value"), Filters.regex("id", "value$")),
Arguments.of(
Condition.and(listOf(Condition("id", Operator.EQ, "id"))),
Filters.and(Filters.eq("id", "id"))
Expand Down
6 changes: 3 additions & 3 deletions wow-query/src/main/kotlin/me/ahoo/wow/query/ConditionDsl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import me.ahoo.wow.api.query.Condition
* "field4" lt 1
* "field5" gte 1
* "field6" lte 1
* "field7" like "value7"
* "field7" contains "value7"
* "field8" isIn listOf("value8")
* "field9" notIn listOf("value9")
* "field10" between (1 to 2)
Expand Down Expand Up @@ -124,8 +124,8 @@ class ConditionDsl {
condition(Condition.lte(this, value))
}

infix fun String.like(value: Any) {
condition(Condition.like(this, value))
infix fun String.contains(value: Any) {
condition(Condition.contains(this, value))
}

infix fun String.isIn(value: List<Any>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ConditionDslTest {
"field4" lt 1
"field5" gte 1
"field6" lte 1
"field7" like "value7"
"field7" contains "value7"
"field8" isIn listOf("value8")
"field9" notIn listOf("value9")
"field10" between (1 to 2)
Expand Down Expand Up @@ -63,7 +63,7 @@ class ConditionDslTest {
Condition.lt("field4", 1),
Condition.gte("field5", 1),
Condition.lte("field6", 1),
Condition.like("field7", "value7"),
Condition.contains("field7", "value7"),
Condition.isIn("field8", listOf("value8")),
Condition.notIn("field9", listOf("value9")),
Condition.between("field10", 1, 2),
Expand Down Expand Up @@ -212,9 +212,9 @@ class ConditionDslTest {
@Test
fun like() {
val condition = condition {
"field1" like "value1"
"field1" contains "value1"
}
assertThat(condition, equalTo(Condition.like("field1", "value1")))
assertThat(condition, equalTo(Condition.contains("field1", "value1")))
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class PagedQueryDslTest {
"field4" lt 1
"field5" gte 1
"field6" lte 1
"field7" like "value7"
"field7" contains "value7"
"field8" isIn listOf("value8")
"field9" notIn listOf("value9")
"field10" between (1 to 2)
Expand Down Expand Up @@ -62,7 +62,7 @@ class PagedQueryDslTest {
Condition.lt("field4", 1),
Condition.gte("field5", 1),
Condition.lte("field6", 1),
Condition.like("field7", "value7"),
Condition.contains("field7", "value7"),
Condition.isIn("field8", listOf("value8")),
Condition.notIn("field9", listOf("value9")),
Condition.between("field10", 1, 2),
Expand Down

0 comments on commit 4d5214b

Please sign in to comment.