Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ static FunctionCall concat(FunctionArgument... arguments) {
* @return a FunctionCall for SUBSTRING
*/
static FunctionCall substring(FunctionArgument string, int start, int length) {
return FunctionCall.of(Function.SUBSTRING, string, Literal.of(start), Literal.of(length));
return FunctionCall.of(Function.SUBSTRING, string, lit(start), lit(length));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public static class QueryBuilder {
@SuppressWarnings({"java:S1068", "java:S1450"})
private Sort[] sort = new Sort[0];
private List<QueryPreprocessor> preprocessors = new ArrayList<>();
@SuppressWarnings("java:S1068")
private Select select;

public QueryBuilder withPreprocessor(QueryPreprocessor preprocessor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* Represents a selection of properties or expressions for projection queries.
Expand Down Expand Up @@ -121,8 +120,8 @@ public List<PropertyExpression> getEffectiveExpressions() {
}
if (propertyNames != null) {
return propertyNames.stream()
.map(PropertyReference::of)
.collect(Collectors.toList());
.map(name -> (PropertyExpression) PropertyReference.of(name))
.toList();
}
return new ArrayList<>();
}
Expand All @@ -139,7 +138,7 @@ public List<String> getAliasNames() {
if (hasExpressions()) {
return expressions.stream()
.map(this::getExpressionAlias)
.collect(Collectors.toList());
.toList();
}
return propertyNames != null ? new ArrayList<>(propertyNames) : new ArrayList<>();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,55 +172,35 @@ void givenFieldReferenceWithLesserThanEquals_whenBuildSimpleCondition_thenReturn
void givenFieldReferenceWithStartsWith_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(STARTS_WITH)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(STARTS_WITH, fieldRef));
}

@Test
void givenFieldReferenceWithEndsWith_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(ENDS_WITH)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(ENDS_WITH, fieldRef));
}

@Test
void givenFieldReferenceWithContains_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(CONTAINS)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(CONTAINS, fieldRef));
}

@Test
void givenFieldReferenceWithIn_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(IN)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(IN, fieldRef));
}

@Test
void givenFieldReferenceWithNotIn_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(NOT_IN)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(NOT_IN, fieldRef));
}

@Test
Expand All @@ -237,8 +217,9 @@ void givenFieldReference_whenEquals_thenCompareByFieldName() {
FieldReference ref2 = field("endDate");
FieldReference ref3 = field("startDate");

assertThat(ref1).isEqualTo(ref2);
assertThat(ref1).isNotEqualTo(ref3);
assertThat(ref1)
.isEqualTo(ref2)
.isNotEqualTo(ref3);
}

@Test
Expand Down Expand Up @@ -313,35 +294,22 @@ void givenFunctionCallAsLeftExpression_whenGetEffectiveLeftExpression_thenReturn
@Test
void givenBothPropertyNameAndLeftExpression_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("lastName")
.leftExpression(PropertyReference.of("firstName"))
.operator(EQUALS)
.value("test")
.build(),
ConditionTests::buildConditionWithPropertyNameAndLeftExpression,
"Cannot set both propertyName and leftExpression");
}

@Test
void givenFieldReferenceWithIsNull_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(IS_NULL)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(IS_NULL, fieldRef));
}

@Test
void givenFieldReferenceWithIsNotNull_whenBuildSimpleCondition_thenThrowIllegalArgumentException() {
FieldReference fieldRef = field("otherField");
assertThrows(IllegalArgumentException.class,
() -> SimpleCondition.builder()
.propertyName("someField")
.operator(IS_NOT_NULL)
.value(fieldRef)
.build());
() -> buildConditionWithFieldReference(IS_NOT_NULL, fieldRef));
}

@Test
Expand All @@ -368,16 +336,17 @@ void givenSimpleCondition_whenHashCode_thenReturnSameHashCodeForEqualConditions(
SimpleCondition condition1 = filterBy("lastName", EQUALS, "Skywalker");
SimpleCondition condition2 = filterBy("lastName", EQUALS, "Skywalker");

assertThat(condition1.hashCode()).isEqualTo(condition2.hashCode());
assertThat(condition1).hasSameHashCodeAs(condition2);
}

@Test
void givenSimpleCondition_whenToString_thenReturnReadableString() {
SimpleCondition condition = filterBy("lastName", EQUALS, "Skywalker");

assertThat(condition.toString()).contains("lastName");
assertThat(condition.toString()).contains("EQUALS");
assertThat(condition.toString()).contains("Skywalker");
assertThat(condition.toString())
.contains("lastName")
.contains("EQUALS")
.contains("Skywalker");
}

@Test
Expand All @@ -391,4 +360,21 @@ void givenLeftExpressionCondition_whenGetPropertyName_thenReturnNull() {
assertThat(condition.getPropertyName()).isNull();
assertThat(condition.getLeftExpression()).isNotNull();
}

private static void buildConditionWithFieldReference(Operator operator, FieldReference fieldRef) {
SimpleCondition.builder()
.propertyName("someField")
.operator(operator)
.value(fieldRef)
.build();
}

private static void buildConditionWithPropertyNameAndLeftExpression() {
SimpleCondition.builder()
.propertyName("lastName")
.leftExpression(PropertyReference.of("firstName"))
.operator(EQUALS)
.value("test")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@ void givenTwoEqualFunctionCalls_whenEquals_thenReturnTrue() {
FunctionCall fc1 = FunctionCall.of(Function.UPPER, PropertyReference.of("name"));
FunctionCall fc2 = FunctionCall.of(Function.UPPER, PropertyReference.of("name"));

assertThat(fc1).isEqualTo(fc2);
assertThat(fc1.hashCode()).isEqualTo(fc2.hashCode());
assertThat(fc1)
.isEqualTo(fc2)
.hasSameHashCodeAs(fc2);
}

@Test
Expand Down Expand Up @@ -133,17 +134,19 @@ void givenNullFunction_whenBuild_thenThrowException() {

@Test
void givenNullaryFunctionWithArguments_whenBuild_thenThrowException() {
PropertyReference ref = PropertyReference.of("date");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> FunctionCall.of(Function.CURRENT_DATE, PropertyReference.of("date")));
() -> FunctionCall.of(Function.CURRENT_DATE, ref));

assertThat(exception.getMessage()).contains("does not accept any arguments");
}

@Test
void givenFunctionWithWrongArgumentCount_whenBuild_thenThrowException() {
// MOD requires exactly 2 arguments
PropertyReference ref = PropertyReference.of("value");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> FunctionCall.of(Function.MOD, PropertyReference.of("value")));
() -> FunctionCall.of(Function.MOD, ref));

assertThat(exception.getMessage()).contains("requires 2 argument(s)");
}
Expand All @@ -160,8 +163,9 @@ void givenVariadicFunctionWithInsufficientArguments_whenBuild_thenThrowException
@Test
void givenConcatWithOneArgument_whenBuild_thenThrowException() {
// CONCAT requires at least 2 arguments
PropertyReference ref = PropertyReference.of("name");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> FunctionCall.of(Function.CONCAT, PropertyReference.of("name")));
() -> FunctionCall.of(Function.CONCAT, ref));

assertThat(exception.getMessage()).contains("requires at least 2 argument(s)");
}
Expand Down Expand Up @@ -201,7 +205,8 @@ void givenFunctionCall_whenToString_thenReturnReadableOutput() {

String str = fc.toString();

assertThat(str).contains("UPPER");
assertThat(str).contains("name");
assertThat(str)
.contains("UPPER")
.contains("name");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void givenNullValue_whenOf_thenThrowException() {

@Test
void givenUnsupportedType_whenOf_thenThrowException() {
assertThatThrownBy(() -> Literal.of(new Object()))
Object invalid = new Object();
assertThatThrownBy(() -> Literal.of(invalid))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("must be String, Number, or Boolean");
}
Expand All @@ -53,30 +54,31 @@ void givenUnsupportedType_whenOf_thenThrowException() {
void givenStringLiteral_whenToString_thenReturnQuotedValue() {
Literal lit = Literal.of("test");

assertThat(lit.toString()).isEqualTo("\"test\"");
assertThat(lit).hasToString("\"test\"");
}

@Test
void givenNumberLiteral_whenToString_thenReturnValue() {
Literal lit = Literal.of(123);

assertThat(lit.toString()).isEqualTo("123");
assertThat(lit).hasToString("123");
}

@Test
void givenBooleanLiteral_whenToString_thenReturnValue() {
Literal lit = Literal.of(false);

assertThat(lit.toString()).isEqualTo("false");
assertThat(lit).hasToString("false");
}

@Test
void givenTwoEqualLiterals_whenEquals_thenReturnTrue() {
Literal lit1 = Literal.of("value");
Literal lit2 = Literal.of("value");

assertThat(lit1).isEqualTo(lit2);
assertThat(lit1.hashCode()).isEqualTo(lit2.hashCode());
assertThat(lit1)
.isEqualTo(lit2)
.hasSameHashCodeAs(lit2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.junit.jupiter.api.Test;

import java.util.List;

import static io.github.queritylib.querity.api.Querity.selectByNative;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -36,8 +35,9 @@ void givenTwoEqualNativeSelectWrappers_whenEquals_thenReturnTrue() {
NativeSelectWrapper<String> wrapper1 = selectByNative("field1", "field2");
NativeSelectWrapper<String> wrapper2 = selectByNative("field1", "field2");

assertThat(wrapper1).isEqualTo(wrapper2);
assertThat(wrapper1.hashCode()).isEqualTo(wrapper2.hashCode());
assertThat(wrapper1)
.isEqualTo(wrapper2)
.hasSameHashCodeAs(wrapper2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ void givenTwoEqualPropertyReferences_whenEquals_thenReturnTrue() {
PropertyReference ref1 = PropertyReference.of("name");
PropertyReference ref2 = PropertyReference.of("name");

assertThat(ref1).isEqualTo(ref2);
assertThat(ref1.hashCode()).isEqualTo(ref2.hashCode());
assertThat(ref1)
.isEqualTo(ref2)
.hasSameHashCodeAs(ref2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ void givenTwoEqualSimpleSelects_whenEquals_thenReturnTrue() {
SimpleSelect select1 = SimpleSelect.of("id", "name");
SimpleSelect select2 = SimpleSelect.of("id", "name");

assertThat(select1).isEqualTo(select2);
assertThat(select1.hashCode()).isEqualTo(select2.hashCode());
assertThat(select1)
.isEqualTo(select2)
.hasSameHashCodeAs(select2);
}

@Test
Expand All @@ -147,9 +148,10 @@ void givenSimpleSelect_whenToString_thenReturnReadableOutput() {

String str = select.toString();

assertThat(str).contains("id");
assertThat(str).contains("firstName");
assertThat(str).contains("lastName");
assertThat(str)
.contains("id")
.contains("firstName")
.contains("lastName");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ void givenTwoSelectsWithSameProperties_whenEquals_thenReturnTrue() {
SimpleSelect select1 = selectBy("id", "name");
SimpleSelect select2 = selectBy("id", "name");

assertThat(select1).isEqualTo(select2);
assertThat(select1.hashCode()).isEqualTo(select2.hashCode());
assertThat(select1)
.isEqualTo(select2)
.hasSameHashCodeAs(select2);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@ void givenTwoEqualSimpleSorts_whenEquals_thenReturnTrue() {
SimpleSort sort1 = SimpleSort.builder().propertyName("name").direction(ASC).build();
SimpleSort sort2 = SimpleSort.builder().propertyName("name").direction(ASC).build();

assertThat(sort1).isEqualTo(sort2);
assertThat(sort1.hashCode()).isEqualTo(sort2.hashCode());
assertThat(sort1)
.isEqualTo(sort2)
.hasSameHashCodeAs(sort2);
}

@Test
Expand All @@ -134,7 +135,8 @@ void givenSimpleSort_whenToString_thenReturnReadableOutput() {

String str = sort.toString();

assertThat(str).contains("lastName");
assertThat(str).contains("DESC");
assertThat(str)
.contains("lastName")
.contains("DESC");
}
}
Loading
Loading