diff --git a/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java b/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java index 66eca9a..994a46a 100644 --- a/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java +++ b/src/test/java/org/springframework/data/reindexer/repository/ReindexerRepositoryTests.java @@ -21,8 +21,10 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; @@ -189,6 +191,25 @@ public void getByName() { assertEquals(testItem.getValue(), item.getValue()); } + @Test + public void getOneSqlByName() { + TestItem testItem = this.repository.save(new TestItem(1L, "TestName", null)); + TestItem item = this.repository.getOneSqlByName("TestName"); + assertEquals(testItem.getId(), item.getId()); + assertEquals(testItem.getName(), item.getName()); + assertEquals(testItem.getValue(), item.getValue()); + } + + @Test + public void findOneSqlByName() { + TestItem testItem = this.repository.save(new TestItem(1L, "TestName", null)); + TestItem item = this.repository.findOneSqlByName("TestName").orElse(null); + assertNotNull(item); + assertEquals(testItem.getId(), item.getId()); + assertEquals(testItem.getName(), item.getName()); + assertEquals(testItem.getValue(), item.getValue()); + } + @Test public void getByNameWhenNotExistsThenException() { assertThrows(IllegalStateException.class, () -> this.repository.getByName("notExists"), @@ -287,6 +308,56 @@ public void findAll() { assertEquals(0, expectedItems.size()); } + @Test + public void findAllListSql() { + Map expectedItems = new HashMap<>(); + for (long i = 0; i < 100; i++) { + expectedItems.put(i, this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + for (TestItem actual : this.repository.findAllListSql()) { + TestItem expected = expectedItems.remove(actual.getId()); + assertNotNull(expected); + assertEquals(expected.getId(), actual.getId()); + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getValue(), actual.getValue()); + } + assertEquals(0, expectedItems.size()); + } + + @Test + public void findAllSetSql() { + Map expectedItems = new HashMap<>(); + for (long i = 0; i < 100; i++) { + expectedItems.put(i, this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + for (TestItem actual : this.repository.findAllSetSql()) { + TestItem expected = expectedItems.remove(actual.getId()); + assertNotNull(expected); + assertEquals(expected.getId(), actual.getId()); + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getValue(), actual.getValue()); + } + assertEquals(0, expectedItems.size()); + } + + @Test + public void findAllStreamSql() { + Map expectedItems = new HashMap<>(); + for (long i = 0; i < 100; i++) { + expectedItems.put(i, this.repository.save(new TestItem(i, "TestName" + i, "TestValue" + i))); + } + try (Stream itemStream = this.repository.findAllStreamSql()) { + itemStream.forEach(actual -> { + TestItem expected = expectedItems.remove(actual.getId()); + assertNotNull(expected); + assertEquals(expected.getId(), actual.getId()); + assertEquals(expected.getName(), actual.getName()); + assertEquals(expected.getValue(), actual.getValue()); + }); + } + assertEquals(0, expectedItems.size()); + } + @Test public void findAllById() { Map expectedItems = new HashMap<>(); @@ -432,6 +503,21 @@ interface TestItemReindexerRepository extends ReindexerRepository findOneSqlByName(String name); + + @Query("SELECT * FROM items WHERE name = '%s'") + TestItem getOneSqlByName(String name); + + @Query("SELECT * FROM items") + List findAllListSql(); + + @Query("SELECT * FROM items") + Set findAllSetSql(); + + @Query("SELECT * FROM items") + Stream findAllStreamSql(); + } @Namespace(name = NAMESPACE_NAME)