Skip to content

Commit

Permalink
Merge pull request #11 from ergon/feature/dope-167-boolean-comparator…
Browse files Browse the repository at this point in the history
…-with-brackets

DOPE-167 added brackets to all AND and OR expressions, added and edited tests accordingly
  • Loading branch information
martinagallati-ergon authored May 13, 2024
2 parents 9bf582f + c2401d3 commit 5c8d35f
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ fun formatToQueryString(left: String, symbol: String, right: String) = "$left $s
fun formatToQueryString(left: Resolvable, symbol: String, vararg right: Resolvable) =
"${left.toQueryString()} $symbol ${right.joinToString { it.toQueryString() }}"
fun formatToQueryString(symbol: String, vararg argument: Resolvable) =
"$symbol ${argument.joinToString(separator = ", ") { it.toQueryString() }}"
formatToQueryString(symbol, separator = " ", *argument)

fun formatToQueryString(symbol: String, separator: String, vararg argument: Resolvable) =
"$symbol$separator${argument.joinToString(separator = ", ") { it.toQueryString() }}"

fun formatToQueryStringWithBrackets(left: String, symbol: String, right: String) = "($left $symbol $right)"
fun formatToQueryStringWithBrackets(symbol: String, vararg argument: Resolvable) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ch.ergon.dope.validtype.NumberType
class NegationExpression(
numberExpression: TypeExpression<NumberType>,
) : TypeExpression<NumberType>, PrefixOperator("-", numberExpression) {
override fun toQueryString(): String = toPrefixQueryString()
override fun toQueryString(): String = toPrefixQueryString(separator = "")
}

fun neg(numberExpression: TypeExpression<NumberType>): NegationExpression =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class LogicalInfixExpression(
symbol: String,
right: TypeExpression<BooleanType>,
) : TypeExpression<BooleanType>, InfixOperator(left, symbol, right) {
override fun toQueryString(): String = toInfixQueryString()
override fun toQueryString(): String = toInfixQueryString(useBrackets = true)
}

fun TypeExpression<BooleanType>.or(booleanExpression: TypeExpression<BooleanType>): LogicalInfixExpression =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ch.ergon.dope.validtype.BooleanType
class NotExpression(
expression: TypeExpression<BooleanType>,
) : TypeExpression<BooleanType>, PrefixOperator("NOT", expression) {
override fun toQueryString(): String = toPrefixQueryString()
override fun toQueryString(): String = toPrefixQueryString(separator = " ")
}

fun not(expression: TypeExpression<BooleanType>) = NotExpression(expression)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package ch.ergon.dope.resolvable.operator

import ch.ergon.dope.resolvable.expression.TypeExpression
import ch.ergon.dope.resolvable.formatToQueryString
import ch.ergon.dope.resolvable.formatToQueryStringWithBrackets
import ch.ergon.dope.validtype.ValidType

open class InfixOperator(
private val left: TypeExpression<out ValidType>,
private val symbol: String,
private val right: TypeExpression<out ValidType>,
) {
fun toInfixQueryString() = formatToQueryString(left.toQueryString(), symbol, right.toQueryString())
fun toInfixQueryString(useBrackets: Boolean = false) =
if (useBrackets) {
formatToQueryStringWithBrackets(left.toQueryString(), symbol, right.toQueryString())
} else {
formatToQueryString(left.toQueryString(), symbol, right.toQueryString())
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ch.ergon.dope.resolvable.operator

import ch.ergon.dope.resolvable.Resolvable
import ch.ergon.dope.resolvable.formatToQueryStringWithBrackets
import ch.ergon.dope.resolvable.formatToQueryString

open class PrefixOperator(private val symbol: String, private val argument: Resolvable) {
fun toPrefixQueryString() = formatToQueryStringWithBrackets(symbol, argument)
fun toPrefixQueryString(separator: String) = formatToQueryString(symbol, separator = separator, argument)
}
51 changes: 50 additions & 1 deletion core/src/test/kotlin/ch/ergon/dope/BooleanComparatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ch.ergon.dope.helper.someNumberField
import ch.ergon.dope.helper.someStringField
import ch.ergon.dope.helper.unifyString
import ch.ergon.dope.resolvable.expression.unaliased.type.logical.and
import ch.ergon.dope.resolvable.expression.unaliased.type.logical.or
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isEqualTo
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isGreaterOrEqualThan
import ch.ergon.dope.resolvable.expression.unaliased.type.relational.isGreaterThan
Expand Down Expand Up @@ -767,7 +768,7 @@ class BooleanComparatorTest {

@Test
fun `should Support Where With Like Chained`() {
val expected = "SELECT stringField, numberField FROM someBucket WHERE email LIKE \"%@gmail.com\" AND numberField = 46"
val expected = "SELECT stringField, numberField FROM someBucket WHERE (email LIKE \"%@gmail.com\" AND numberField = 46)"

val actual: String = create.select(
someStringField(),
Expand Down Expand Up @@ -871,4 +872,52 @@ class BooleanComparatorTest {

kotlin.test.assertEquals(expected, actual)
}

@Test
fun `should add brackets to one Boolean comparator`() {
val expected = "SELECT * FROM someBucket WHERE (TRUE AND TRUE)"
val actual: String = create
.selectAsterisk()
.from(
someBucket(),
)
.where(
true.toBooleanType().and(true),
)
.build()

assertEquals(expected, actual)
}

@Test
fun `should add first bracket pair to two Boolean comparators`() {
val expected = "SELECT * FROM someBucket WHERE ((TRUE AND TRUE) OR FALSE)"
val actual: String = create
.selectAsterisk()
.from(
someBucket(),
)
.where(
true.toBooleanType().and(true).or(false),
)
.build()

assertEquals(expected, actual)
}

@Test
fun `should add second bracket pair to two Boolean comparators`() {
val expected = "SELECT * FROM someBucket WHERE (TRUE AND (TRUE OR FALSE))"
val actual: String = create
.selectAsterisk()
.from(
someBucket(),
)
.where(
true.toBooleanType().and(true.toBooleanType().or(false)),
)
.build()

assertEquals(expected, actual)
}
}
2 changes: 1 addition & 1 deletion core/src/test/kotlin/ch/ergon/dope/DeleteTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class DeleteTest {

@Test
fun `should support delete`() {
val expected = "DELETE FROM someBucket WHERE someBucket.age = 2 AND TRUE LIMIT 7 OFFSET 10 RETURNING stringField"
val expected = "DELETE FROM someBucket WHERE (someBucket.age = 2 AND TRUE) LIMIT 7 OFFSET 10 RETURNING stringField"

val actual: String = create
.deleteFrom(someBucket())
Expand Down
14 changes: 7 additions & 7 deletions core/src/test/kotlin/ch/ergon/dope/JoinClauseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ class JoinClauseTest {
val expected =
"SELECT DISTINCT MIN(aport.airportname) AS Airport__Name, " +
"MIN(aport.tz) AS Airport__Time, MIN(lmark.name) AS Landmark_Name " +
"FROM airport AS aport LEFT JOIN landmark AS lmark ON aport.city = " +
"lmark.city AND lmark.country = \"United States\" GROUP BY " +
"FROM airport AS aport LEFT JOIN landmark AS lmark ON (aport.city = " +
"lmark.city AND lmark.country = \"United States\") GROUP BY " +
"aport.airportname ORDER BY aport.airportname LIMIT 4"

val actual = create.selectDistinct(
Expand Down Expand Up @@ -185,8 +185,8 @@ class JoinClauseTest {
val expected =
"SELECT DISTINCT MIN(aport.airportname) AS Airport__Name, " +
"MIN(aport.tz) AS Airport__Time, MIN(lmark.name) AS Landmark_Name " +
"FROM airport AS aport RIGHT JOIN landmark AS lmark ON aport.city = " +
"lmark.city AND lmark.country = \"United States\" GROUP BY " +
"FROM airport AS aport RIGHT JOIN landmark AS lmark ON (aport.city = " +
"lmark.city AND lmark.country = \"United States\") GROUP BY " +
"aport.airportname ORDER BY aport.airportname LIMIT 4"

val actual = create.selectDistinct(
Expand Down Expand Up @@ -245,7 +245,7 @@ class JoinClauseTest {
val expected = "SELECT DISTINCT route.destinationairport, route.stops, " +
"route.airline, airline.name, airline.callsign " +
"FROM route JOIN airline ON KEYS route.airlineid " +
"WHERE route.sourceairport = \"SFO\" AND route.stops = 0 LIMIT 4"
"WHERE (route.sourceairport = \"SFO\" AND route.stops = 0) LIMIT 4"

val actual = create.selectDistinct(
someStringField("destinationairport", route),
Expand Down Expand Up @@ -277,8 +277,8 @@ class JoinClauseTest {
fun `Left Outer Lookup Join`() {
val expected = "SELECT route.airline, route.sourceairport, " +
"route.destinationairport, airline.callsign FROM route LEFT JOIN airline " +
"ON KEYS route.airlineid WHERE route.destinationairport = " +
"\"ATL\" AND route.sourceairport = \"SEA\""
"ON KEYS route.airlineid WHERE (route.destinationairport = " +
"\"ATL\" AND route.sourceairport = \"SEA\")"

val actual = create.select(
someStringField("airline", route),
Expand Down
12 changes: 6 additions & 6 deletions core/src/test/kotlin/ch/ergon/dope/OperatorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ class OperatorTest {

@Test
fun `should support negation with a NumberField`() {
val expected = "SELECT -(numberField) FROM someBucket"
val expected = "SELECT -numberField FROM someBucket"

val actual: String = create
.select(
Expand All @@ -517,7 +517,7 @@ class OperatorTest {

@Test
fun `should support negation on a calculation`() {
val expected = "SELECT -((numberField * 12)) FROM someBucket"
val expected = "SELECT -(numberField * 12) FROM someBucket"

val actual: String = create
.select(
Expand All @@ -531,7 +531,7 @@ class OperatorTest {

@Test
fun `should support negation with different calculation`() {
val expected = "SELECT (-(numberField) + 6) FROM someBucket"
val expected = "SELECT (-numberField + 6) FROM someBucket"

val actual: String = create
.select(
Expand All @@ -545,7 +545,7 @@ class OperatorTest {

@Test
fun `should support negation with calculations`() {
val expected = "SELECT (12 + (-(numberField) % ((12 * -((numberField - -(12)))) + 6))) AS calculation FROM someBucket"
val expected = "SELECT (12 + (-numberField % ((12 * -(numberField - -12)) + 6))) AS calculation FROM someBucket"

val actual: String = create
.select(
Expand Down Expand Up @@ -593,7 +593,7 @@ class OperatorTest {

@Test
fun `should support negation with a number type`() {
val expected = "SELECT -(6) FROM someBucket"
val expected = "SELECT -6 FROM someBucket"

val actual: String = create
.select(
Expand All @@ -607,7 +607,7 @@ class OperatorTest {

@Test
fun `should support negation with a number`() {
val expected = "SELECT -(6) FROM someBucket"
val expected = "SELECT -6 FROM someBucket"

val actual: String = create
.select(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class ParameterizedQueriesTest {

@Test
fun `should Support Mixed Parameters 2`() {
val expected = "SELECT $1, \$name OR $2 AND \$MagicNumber AS one"
val expected = "SELECT $1, (\$name OR ($2 AND \$MagicNumber)) AS one"

val actual: String = create
.select(
Expand All @@ -119,7 +119,7 @@ class ParameterizedQueriesTest {

@Test
fun `should Support Mixed Parameters 3`() {
val expected = "SELECT $1, \$name OR $2 AND \$MagicNumber, CONCAT(\$superMagic, $3) AS one"
val expected = "SELECT $1, (\$name OR ($2 AND \$MagicNumber)), CONCAT(\$superMagic, $3) AS one"

val actual: String = create
.select(
Expand All @@ -138,7 +138,7 @@ class ParameterizedQueriesTest {

@Test
fun `should Support Mixed Parameters 4`() {
val expected = "SELECT $1, \$name OR $2 AND \$MagicNumber AS one, CONCAT(\$superMagic, \$3)"
val expected = "SELECT $1, (\$name OR ($2 AND \$MagicNumber)) AS one, CONCAT(\$superMagic, \$3)"

val actual: String = create
.select(
Expand Down
Loading

0 comments on commit 5c8d35f

Please sign in to comment.