Skip to content

Commit

Permalink
add test for values
Browse files Browse the repository at this point in the history
  • Loading branch information
mdavis95 committed Jan 2, 2023
1 parent 1f04108 commit 1ed9254
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,37 @@

import io.zulia.message.ZuliaQuery;

public class FilterQuery extends StandardQuery {

private boolean exclude = false;

public FilterQuery(String query) {
super(query);
}

public FilterQuery include() {
exclude = false;
return this;
}

public FilterQuery exclude() {
exclude = true;
return this;
}

@Override
protected void completeQuery(ZuliaQuery.Query.Builder queryBuilder) {
if (exclude) {
queryBuilder.setQueryType(ZuliaQuery.Query.QueryType.FILTER_NOT);
}
else {
queryBuilder.setQueryType(ZuliaQuery.Query.QueryType.FILTER);
}
}
public class FilterQuery extends StandardQuery<FilterQuery> {

private boolean exclude = false;

public FilterQuery(String query) {
super(query);
}

@Override
protected FilterQuery getSelf() {
return this;
}

public FilterQuery include() {
exclude = false;
return this;
}

public FilterQuery exclude() {
exclude = true;
return this;
}

@Override
protected void completeQuery(ZuliaQuery.Query.Builder queryBuilder) {
if (exclude) {
queryBuilder.setQueryType(ZuliaQuery.Query.QueryType.FILTER_NOT);
} else {
queryBuilder.setQueryType(ZuliaQuery.Query.QueryType.FILTER);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@
import io.zulia.message.ZuliaQuery;
import io.zulia.message.ZuliaQuery.Query.QueryType;

public class ScoredQuery extends StandardQuery {
public class ScoredQuery extends StandardQuery<ScoredQuery> {
private boolean must;
private String scoreFunction;

public ScoredQuery(String query) {
this(query, true);
}

@Override
protected ScoredQuery getSelf() {
return this;
}

/**
*
* @param query
* @param must - if must is true than query will be required, otherwise it will be used to as an optional (should clause) to help scoring
* @param must - if must is true then query will be required, otherwise it will be used to as an optional (should clause) to help scoring
*/
public ScoredQuery(String query, boolean must) {
super(query);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.List;

public abstract class StandardQuery implements QueryBuilder {
public abstract class StandardQuery<T extends StandardQuery> implements QueryBuilder {

private final ZuliaQuery.Query.Builder queryBuilder;

Expand All @@ -18,60 +18,62 @@ public StandardQuery(String query) {
}
}

public StandardQuery setQuery(String query) {
public T setQuery(String query) {
queryBuilder.setQ(query);
return this;
return getSelf();
}

protected abstract T getSelf();

public List<String> getQueryFields() {
return queryBuilder.getQfList();
}

public StandardQuery addQueryField(String queryField) {
public T addQueryField(String queryField) {
queryBuilder.addQf(queryField);
return this;
return getSelf();
}

public StandardQuery addQueryFields(String... queryFields) {
public T addQueryFields(String... queryFields) {
queryBuilder.addAllQf(List.of(queryFields));
return this;
return getSelf();
}

public StandardQuery addQueryFields(Iterable<String> queryFields) {
public T addQueryFields(Iterable<String> queryFields) {
queryBuilder.addAllQf(queryFields);
return this;
return getSelf();
}

public StandardQuery clearQueryField() {
public T clearQueryField() {
queryBuilder.clearQf();
return this;
return getSelf();
}

public StandardQuery setQueryFields(@NotNull List<String> queryFields) {
public T setQueryFields(@NotNull List<String> queryFields) {
if (queryFields == null) {
throw new IllegalArgumentException("Query Fields cannot be null");
}
queryBuilder.clearQf();
queryBuilder.addAllQf(queryFields);
return this;
return getSelf();
}

public ZuliaQuery.Query.Operator getDefaultOperator() {
return queryBuilder.getDefaultOp();
}

public StandardQuery setDefaultOperator(ZuliaQuery.Query.Operator defaultOperator) {
public T setDefaultOperator(ZuliaQuery.Query.Operator defaultOperator) {
queryBuilder.setDefaultOp(defaultOperator);
return this;
return getSelf();
}

public int getMinShouldMatch() {
return queryBuilder.getMm();
}

public StandardQuery setMinShouldMatch(int minShouldMatch) {
public T setMinShouldMatch(int minShouldMatch) {
queryBuilder.setMm(minShouldMatch);
return this;
return getSelf();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static Values atLeast(int minimumShouldMatch) {

private Values(Type type, Integer minimumShouldMatch) {
this.type = type;
this.minimumShouldMatch = null;
this.minimumShouldMatch = minimumShouldMatch;
}

public Values of(String... values) {
Expand Down Expand Up @@ -108,8 +108,11 @@ public String asString() {
sb.append("-");
}

COMMA_JOINER.appendTo(sb, fields);
sb.append(":(");
if (fields != null && !fields.isEmpty()) {
COMMA_JOINER.appendTo(sb, fields);
sb.append(":");
}
sb.append("(");

List<String> valuesHandled = values.stream().map(valueHandler).toList();
if (type.equals(Type.ALL)) {
Expand All @@ -122,7 +125,7 @@ public String asString() {
sb.append("~");
sb.append(minimumShouldMatch);
}
return null;
return sb.toString();
}


Expand All @@ -135,28 +138,27 @@ public ScoredQuery asScoredQuery() {
}


public <T extends StandardQuery> T asQuery(Function<String, T> constructor) {
public <T extends StandardQuery<T>> T asQuery(Function<String, T> constructor) {
List<String> valuesHandled = values.stream().map(valueHandler).toList();
String query = SPACE_JOINER.join(valuesHandled);
T tQuery = constructor.apply(query);
tQuery.setDefaultOperator(type.equals(Type.ALL) ? Operator.AND : Operator.OR);
fields.forEach(tQuery::addQueryField);

if (exclude) {
if (tQuery instanceof FilterQuery fq) {
fq.exclude();
} else if (tQuery instanceof ScoredQuery sq) {
throw new IllegalStateException("Exclude cannot be used with ScoredQuery");
}
}
if (minimumShouldMatch != null) {
tQuery.setMinShouldMatch(minimumShouldMatch);
}
return tQuery;
}


public static void main(String[] args) {
String query = Values.any().of("a", "b", "c").withFields("title", "abstract").exclude().asString();

String query2 = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").exclude().asString();


FilterQuery filterQuery = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").exclude().asFilterQuery();

ScoredQuery scoredQuery = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").exclude().asScoredQuery();
}

}
81 changes: 81 additions & 0 deletions zulia-client/src/test/java/io/zulia/client/HelperTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.zulia.client;

import io.zulia.client.command.builder.FilterQuery;
import io.zulia.client.command.builder.ScoredQuery;
import io.zulia.client.command.factory.Values;
import io.zulia.message.ZuliaQuery;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class HelperTest {

@Test
public void valuesTest() {


{
String query = Values.any().of("a", "b").asString();
Assertions.assertEquals("(a OR b)", query);
}

{
String query = Values.any().of("slow cat", "Pink Shirt").asString();
Assertions.assertEquals("(\"slow cat\" OR \"Pink Shirt\")", query);
}

{
String query = Values.all().of("slow cat", "Pink Shirt").asString();
Assertions.assertEquals("(\"slow cat\" AND \"Pink Shirt\")", query);
}


{
String query = Values.any().of("a", "b").withFields("title", "abstract").asString();
Assertions.assertEquals("title,abstract:(a OR b)", query);
}


{
String query = Values.any().of("a", "b", "c").withFields("title", "abstract").exclude().asString();
Assertions.assertEquals("-title,abstract:(a OR b OR c)", query);
}


{
String query = Values.atLeast(2).of("fast dog", "b", "c").withFields("title", "abstract").asString();
Assertions.assertEquals("title,abstract:(\"fast dog\" OR b OR c)~2", query);
}

{
String query = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").exclude().asString();
Assertions.assertEquals("-title,abstract:(a OR b OR c)~2", query);
}

{
FilterQuery fq = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").exclude().asFilterQuery();
FilterQuery fq2 = new FilterQuery("a b c").setDefaultOperator(ZuliaQuery.Query.Operator.OR).exclude().addQueryFields("title", "abstract").setMinShouldMatch(2);
Assertions.assertEquals(fq.getQuery(), fq2.getQuery());
}

{
FilterQuery fq = Values.all().of("a", "b", "c").withFields("title", "abstract").exclude().asFilterQuery();
FilterQuery fq2 = new FilterQuery("a b c").setDefaultOperator(ZuliaQuery.Query.Operator.AND).exclude().addQueryFields("title", "abstract");
Assertions.assertEquals(fq.getQuery(), fq2.getQuery());
}

{
ScoredQuery sq = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").asScoredQuery();
ScoredQuery sq2 = new ScoredQuery("a b c").setDefaultOperator(ZuliaQuery.Query.Operator.OR).addQueryFields("title", "abstract").setMinShouldMatch(2);
Assertions.assertEquals(sq.getQuery(), sq2.getQuery());
}

{
Assertions.assertThrows(IllegalStateException.class, () -> {
// exclude not supported in scored queries
ScoredQuery sq = Values.atLeast(2).of("a", "b", "c").withFields("title", "abstract").exclude().asScoredQuery();
});
}

}

}
45 changes: 22 additions & 23 deletions zulia-client/src/test/java/io/zulia/client/MapperTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,34 @@

public class MapperTest {

@Test
public void testSimpleCase() throws Exception {
@Test
public void testSimpleCase() throws Exception {

Date d = new Date();
TestObj1 testObj1 = new TestObj1("test", 14, Arrays.asList("1", "2"), Set.of(4, 6, 7), d, Long.MIN_VALUE, true);
Date d = new Date();
TestObj1 testObj1 = new TestObj1("test", 14, Arrays.asList("1", "2"), Set.of(4, 6, 7), d, Long.MIN_VALUE, true);

Mapper<TestObj1> mapper = new Mapper<>(TestObj1.class);
Mapper<TestObj1> mapper = new Mapper<>(TestObj1.class);

Document doc = mapper.toDocument(testObj1);
Document doc = mapper.toDocument(testObj1);

Assertions.assertEquals("test", doc.getString("field1"));
Assertions.assertEquals(14, (int) doc.getInteger("field2"));
Assertions.assertEquals(2, doc.getList("field3", String.class).size());
Assertions.assertEquals(3, doc.getList("field4", Integer.class).size());
Assertions.assertEquals(doc.getDate("field5"), d);
Assertions.assertEquals(Long.MIN_VALUE, doc.getLong("field6"));
Assertions.assertEquals(true, doc.getBoolean("field7"));
Assertions.assertEquals("test", doc.getString("field1"));
Assertions.assertEquals(14, (int) doc.getInteger("field2"));
Assertions.assertEquals(2, doc.getList("field3", String.class).size());
Assertions.assertEquals(3, doc.getList("field4", Integer.class).size());
Assertions.assertEquals(doc.getDate("field5"), d);
Assertions.assertEquals(Long.MIN_VALUE, doc.getLong("field6"));
Assertions.assertEquals(true, doc.getBoolean("field7"));

TestObj1 testObj1a = mapper.fromDocument(doc);
Assertions.assertEquals(testObj1a.getField1(), testObj1.getField1());
Assertions.assertEquals(testObj1a.getField2(), testObj1.getField2());
Assertions.assertEquals(testObj1a.getField3(), testObj1.getField3());
Assertions.assertEquals(testObj1a.getField4(), testObj1.getField4());
Assertions.assertEquals(testObj1a.getField5(), testObj1.getField5());
Assertions.assertEquals(testObj1a.getField6(), testObj1.getField6());
Assertions.assertEquals(testObj1a.isField7(), testObj1.isField7());
TestObj1 testObj1a = mapper.fromDocument(doc);
Assertions.assertEquals(testObj1a.getField1(), testObj1.getField1());
Assertions.assertEquals(testObj1a.getField2(), testObj1.getField2());
Assertions.assertEquals(testObj1a.getField3(), testObj1.getField3());
Assertions.assertEquals(testObj1a.getField4(), testObj1.getField4());
Assertions.assertEquals(testObj1a.getField5(), testObj1.getField5());
Assertions.assertEquals(testObj1a.getField6(), testObj1.getField6());
Assertions.assertEquals(testObj1a.isField7(), testObj1.isField7());

System.out.println(doc);

}
}

}

0 comments on commit 1ed9254

Please sign in to comment.