Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Reverse ordering support for Task API #816

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 19 additions & 1 deletion src/main/java/com/meilisearch/sdk/http/URLBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public URLBuilder addParameter(String parameter, int[] value) {

public URLBuilder addParameter(String parameter, Date value) {
if (value != null) {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// Changed to utilise RFC 3339 format
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
addSeparator();
params.append(parameter);
params.append("=");
Expand All @@ -76,6 +77,23 @@ public URLBuilder addParameter(String parameter, Date value) {
return this;
}

/**
* To add a boolean parameter to url
*
* @param parameter - parameter name
* @param value - value of the parameter
* @return - updated url object
*/
public URLBuilder addParameter(String parameter, Boolean value) {
if (value != null) {
addSeparator();
params.append(parameter);
params.append("=");
params.append(value);
}
return this;
}

public URLBuilder addQuery(String query) {
this.params.append(query);
return this;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/meilisearch/sdk/model/TasksQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class TasksQuery {
private String[] types;
private String[] indexUids;
private int[] canceledBy;
private Boolean reverse;
private Date beforeEnqueuedAt;
private Date afterEnqueuedAt;
private Date beforeStartedAt;
Expand All @@ -47,7 +48,8 @@ public String toQuery() {
.addParameter("beforeStartedAt", this.getBeforeStartedAt())
.addParameter("afterStartedAt", this.getAfterStartedAt())
.addParameter("beforeFinishedAt", this.getBeforeFinishedAt())
.addParameter("afterFinishedAt", this.getAfterFinishedAt());
.addParameter("afterFinishedAt", this.getAfterFinishedAt())
.addParameter("reverse", this.getReverse());
return urlb.getURL();
}
}
39 changes: 37 additions & 2 deletions src/test/java/com/meilisearch/integration/TasksTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;

import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.model.*;
import com.meilisearch.sdk.utils.Movie;
import java.util.Date;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
Expand All @@ -32,6 +34,7 @@ public void initialize() {
this.setUp();
this.setUpJacksonClient();
if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class);
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

@AfterAll
Expand Down Expand Up @@ -202,6 +205,38 @@ public void testClientGetTasksAllQueryParameters() throws Exception {
assertThat(result.getNext(), is(notNullValue()));
assertThat(result.getResults().length, is(notNullValue()));
}
/** Test to check if task list is reversed when enabled */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to explain what you're testing, this is usually a smell that your test is too complex. Which is not the case :)

@Test
public void testGetTasksInReverse() {
String indexUid = "tasksOnReverseOrder";
Date currentTime = Date.from(Instant.now());
TestData<Movie> testData = this.getTestData(MOVIES_INDEX, Movie.class);

TaskInfo taskA = client.index(indexUid).addDocuments(testData.getRaw());
TaskInfo taskB = client.index(indexUid).addDocuments(testData.getRaw());

client.waitForTask(taskA.getTaskUid());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wait for the last task the first one will be finished in any case so you can remove this line.

client.waitForTask(taskB.getTaskUid());

Task[] defaultTaskList =
client.getTasks(new TasksQuery().setAfterEnqueuedAt(currentTime)).getResults();
Task[] reversedTaskList =
client.getTasks(new TasksQuery().setAfterEnqueuedAt(currentTime).setReverse(true))
.getResults();
Comment on lines +221 to +225
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you map only the task ids here, and then you assert them later, so the test will be less complex to read.


List<Integer> originalTaskOrder =
Arrays.stream(defaultTaskList).map(Task::getUid).collect(Collectors.toList());
List<Integer> reversedTaskOrder =
Arrays.stream(reversedTaskList).map(Task::getUid).collect(Collectors.toList());
assertFalse(originalTaskOrder.isEmpty());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also remove one of these assertions.

assertFalse(reversedTaskOrder.isEmpty());
assertIterableEquals(
originalTaskOrder,
reversedTaskOrder.stream()
.sorted(Collections.reverseOrder())
.collect(Collectors.toList()),
"The lists are not reversed versions of each other");
}

/** Test Cancel Task */
@Test
Expand Down
34 changes: 16 additions & 18 deletions src/test/java/com/meilisearch/sdk/http/URLBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
import static org.junit.jupiter.api.Assertions.*;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.TimeZone;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class URLBuilderTest {

private final URLBuilder classToTest = new URLBuilder();

@BeforeEach
void beforeEach() {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}

Comment on lines +18 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you need to set this? And if "yes" why not to set this for the whole test suite?

@Test
void addSubroute() {
classToTest.addSubroute("route");
Expand Down Expand Up @@ -92,28 +98,20 @@ void addParameterStringIntArray() {

@Test
void addParameterStringDate() throws Exception {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Date date = format.parse("2042-01-30");
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssXXX");
Date date = format.parse("2042-01-30T10:30:00-05:00");

classToTest.addParameter("parameter1", date);
String parameterDate1 =
classToTest
.getParams()
.toString()
.substring(12, classToTest.getParams().toString().length());
assertDoesNotThrow(() -> DateTimeFormatter.ISO_DATE.parse(parameterDate1));
assertThat(classToTest.getParams().toString(), is(equalTo("?parameter1=2042-01-30")));
String parameterDate1 = classToTest.getParams().substring(12);
assertDoesNotThrow(() -> format.parse(parameterDate1));
assertEquals(classToTest.getParams().toString(), "?parameter1=2042-01-30T15:30:00Z");

classToTest.addParameter("parameter2", date);
String parameterDate2 =
classToTest
.getParams()
.toString()
.substring(34, classToTest.getParams().toString().length());
assertDoesNotThrow(() -> DateTimeFormatter.ISO_DATE.parse(parameterDate2));
assertThat(
String parameterDate2 = classToTest.getParams().substring(44);
assertDoesNotThrow(() -> format.parse(parameterDate2));
assertEquals(
classToTest.getParams().toString(),
is(equalTo("?parameter1=2042-01-30&parameter2=2042-01-30")));
"?parameter1=2042-01-30T15:30:00Z&parameter2=2042-01-30T15:30:00Z");
}

@Test
Expand Down