Skip to content

Add Reverse ordering support for Task API #816

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

Merged
merged 10 commits into from
Mar 19, 2025
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
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();
}
}
36 changes: 34 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 @@ -203,6 +206,35 @@ public void testClientGetTasksAllQueryParameters() throws Exception {
assertThat(result.getResults().length, is(notNullValue()));
}

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

client.index(indexUid).addDocuments(testData.getRaw());
client.waitForTask(client.index(indexUid).addDocuments(testData.getRaw()).getTaskUid());
List<Integer> tasks =
Arrays.stream(client.getTasks(query).getResults())
.map(Task::getUid)
.collect(Collectors.toList());
List<Integer> reversedTasks =
Arrays.stream(client.getTasks(queryWithReverseFlag).getResults())
.map(Task::getUid)
.collect(Collectors.toList());

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

/** Test Cancel Task */
@Test
public void testClientCancelTask() throws Exception {
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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is not necessary entirely, as it works find even without it. but when running the test cases locally test cases related to dates, they are bound to fail.
this will allow devs to be sure that no False negative happens when testing locally.

if my above reasoning is fine, will apply to this to entire test suite in a new commit

Copy link
Member

Choose a reason for hiding this comment

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

Yes please add in a new PR :)

@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