Skip to content

Commit

Permalink
Add some '(not) exists' tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-dzianishchyts committed Feb 12, 2024
1 parent 4155851 commit 95d9aaf
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
25 changes: 25 additions & 0 deletions agrest-cayenne/src/test/java/io/agrest/cayenne/GET/ExpIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import jakarta.ws.rs.core.Configuration;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.UriInfo;

import java.time.LocalDateTime;

public class ExpIT extends MainDbTest {
Expand Down Expand Up @@ -379,6 +380,30 @@ public void like_SingleChar_Pattern_Escape() {
.wasOk().bodyEquals(1, "{\"id\":4}");
}

@Test
public void exists_Path_Relationship() {

tester.e2().insertColumns("id_", "name")
.values(1, "qwe")
.values(2, "try")
.exec();

tester.e3().insertColumns("id_", "name", "e2_id")
.values(1, "xxx", 1)
.values(2, "yxy", 2)
.values(3, "y_y", 2)
.values(4, "y_ay", null)
.exec();

tester.target("/e3")
.queryParam("include", "id")
.queryParam("exp", "exists e2")
.queryParam("sort", "id")
.get()
.wasOk()
.bodyEquals(3, "{\"id\":1}", "{\"id\":2}", "{\"id\":3}");
}

@Test
public void like_MultiChar_Pattern_Escape() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.agrest.runtime.processor.select.SelectContext;
import org.apache.cayenne.di.Injector;
import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.exp.ExpressionException;
import org.apache.cayenne.exp.ExpressionFactory;
import org.apache.cayenne.query.ObjectSelect;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -129,6 +130,23 @@ public void createRootQuery_Qualifier_Exists() {
assertEquals(expectedQ1.andExp(ExpressionFactory.notExists(ObjectSelect.query(E3.class).column(E3.E5))), q2.getWhere());
}

@Test
public void createRootQuery_Qualifier_Exists_Invalid_Condition() {
RootResourceEntity<E3> entity = getResourceEntity(E3.class);

SelectContext<E3> c = new SelectContext<>(
E3.class,
new RequestSchema(mock(AgSchema.class)),
mock(AgRequestBuilder.class),
PathChecker.ofDefault(),
mock(Injector.class));

c.setEntity(entity);

entity.andExp(Exp.exists("name = 'test1'"));
assertThrows(ExpressionException.class, () -> queryAssembler.createRootQuery(c));
}

@Test
public void createRootQuery_ById() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public void parsedToString(String expString, String expected) {
}

@ParameterizedTest
@ValueSource(strings = {"exists", "exists()"})
@ValueSource(strings = {"exists", "exists()", "exists (name == 'test')"})
public void parseInvalidGrammar(String expString) {
assertThrows(AgException.class, () -> Exp.parse(expString));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void parsedToString(String expString, String expected) {
}

@ParameterizedTest
@ValueSource(strings = {"not exists", "not exists()"})
@ValueSource(strings = {"not exists", "not exists()", "not exists (name == 'test')"})
public void parseInvalidGrammar(String expString) {
assertThrows(AgException.class, () -> Exp.parse(expString));
}
Expand Down

0 comments on commit 95d9aaf

Please sign in to comment.