mirror of
https://github.com/imgfloat/server.git
synced 2026-02-05 11:49:25 +00:00
Optimize search
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.imgfloat.app;
|
||||
|
||||
import com.imgfloat.app.model.Channel;
|
||||
import com.imgfloat.app.repository.ChannelRepository;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
|
||||
import static org.hamcrest.Matchers.hasSize;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringBootTest(properties = {
|
||||
"spring.security.oauth2.client.registration.twitch.client-id=test-client-id",
|
||||
"spring.security.oauth2.client.registration.twitch.client-secret=test-client-secret"
|
||||
})
|
||||
@AutoConfigureMockMvc
|
||||
class ChannelDirectoryApiIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Autowired
|
||||
private ChannelRepository channelRepository;
|
||||
|
||||
@BeforeEach
|
||||
void cleanChannels() {
|
||||
channelRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void searchesBroadcastersCaseInsensitiveAndSorted() throws Exception {
|
||||
channelRepository.save(new Channel("Beta"));
|
||||
channelRepository.save(new Channel("alpha"));
|
||||
channelRepository.save(new Channel("ALPINE"));
|
||||
|
||||
mockMvc.perform(get("/api/channels").param("q", "Al"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(jsonPath("$", hasSize(2)))
|
||||
.andExpect(jsonPath("$[0]").value("alpha"))
|
||||
.andExpect(jsonPath("$[1]").value("alpine"));
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
@@ -172,6 +173,13 @@ class ChannelDirectoryServiceTest {
|
||||
});
|
||||
when(channelRepository.findAll())
|
||||
.thenAnswer(invocation -> List.copyOf(channels.values()));
|
||||
when(channelRepository.findTop50ByBroadcasterContainingIgnoreCaseOrderByBroadcasterAsc(anyString()))
|
||||
.thenAnswer(invocation -> channels.values().stream()
|
||||
.filter(channel -> Optional.ofNullable(channel.getBroadcaster()).orElse("")
|
||||
.contains(Optional.ofNullable(invocation.getArgument(0, String.class)).orElse("").toLowerCase()))
|
||||
.sorted(Comparator.comparing(Channel::getBroadcaster))
|
||||
.limit(50)
|
||||
.toList());
|
||||
|
||||
when(assetRepository.save(any(Asset.class)))
|
||||
.thenAnswer(invocation -> {
|
||||
|
||||
Reference in New Issue
Block a user