Skip to content

Commit

Permalink
Implemented tests for a native query
Browse files Browse the repository at this point in the history
  • Loading branch information
evgeniycheban committed Aug 4, 2022
1 parent f82aa1d commit fcc6569
Showing 1 changed file with 86 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -287,6 +308,56 @@ public void findAll() {
assertEquals(0, expectedItems.size());
}

@Test
public void findAllListSql() {
Map<Long, TestItem> 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<Long, TestItem> 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<Long, TestItem> 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<TestItem> 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<Long, TestItem> expectedItems = new HashMap<>();
Expand Down Expand Up @@ -432,6 +503,21 @@ interface TestItemReindexerRepository extends ReindexerRepository<TestItem, Long

TestItem getByName(String name);

@Query("SELECT * FROM items WHERE name = '%s'")
Optional<TestItem> findOneSqlByName(String name);

@Query("SELECT * FROM items WHERE name = '%s'")
TestItem getOneSqlByName(String name);

@Query("SELECT * FROM items")
List<TestItem> findAllListSql();

@Query("SELECT * FROM items")
Set<TestItem> findAllSetSql();

@Query("SELECT * FROM items")
Stream<TestItem> findAllStreamSql();

}

@Namespace(name = NAMESPACE_NAME)
Expand Down

0 comments on commit fcc6569

Please sign in to comment.