Skip to content

Commit b77b951

Browse files
committed
Updated tests
1 parent 2767a8d commit b77b951

File tree

6 files changed

+380
-117
lines changed

6 files changed

+380
-117
lines changed

spring-data-jdbc-ydb/src/test/java/tech/ydb/data/all_types_table/AllTypesTableTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
import java.time.temporal.ChronoUnit;
88
import java.util.List;
99
import java.util.Optional;
10+
1011
import org.junit.jupiter.api.Assertions;
1112
import org.junit.jupiter.api.Test;
1213
import org.springframework.beans.factory.annotation.Autowired;
1314
import org.springframework.data.jdbc.core.JdbcAggregateOperations;
1415
import org.springframework.data.relational.core.conversion.DbActionExecutionException;
16+
1517
import tech.ydb.data.YdbBaseTest;
1618
import tech.ydb.data.all_types_table.entity.AllTypesEntity;
1719
import tech.ydb.data.all_types_table.repository.AllTypesEntityRepository;
@@ -41,6 +43,7 @@ public void allTypesTableCrudTest() {
4143
);
4244
repository.save(expected);
4345

46+
Assertions.assertEquals(expected.getDecimalColumn(), entity1.get().getDecimalColumn());
4447
Assertions.assertEquals(expected, entity1.get());
4548
Assertions.assertEquals(expected.getTextColumn(),
4649
repository.findAllByTextColumn("Madiyar Nurgazin").get(0).getTextColumn());
@@ -69,10 +72,11 @@ public void allTypesTableCrudTest() {
6972

7073
entities = repository.findAllByDateColumnAfterNow();
7174
Assertions.assertEquals(1, entities.size());
72-
Assertions.assertEquals(4, entities.get(0).getId());
75+
Assertions.assertEquals(Integer.valueOf(4), entities.get(0).getId());
7376

7477
entity3.setJsonColumn("Not json");
75-
Assertions.assertThrows(DbActionExecutionException.class, () -> repository.save(entity3));
78+
var ex = Assertions.assertThrows(DbActionExecutionException.class, () -> repository.save(entity3));
79+
Assertions.assertTrue(ex.getMessage().startsWith("Failed to execute DbAction.UpdateRoot"));
7680

7781
entity3.setJsonColumn("{\"values\": [1, 2, 3]}");
7882
AllTypesEntity updated = repository.save(entity3);

spring-data-jdbc-ydb/src/test/java/tech/ydb/data/all_types_table/entity/AllTypesEntity.java

Lines changed: 117 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
import java.time.Instant;
55
import java.time.LocalDate;
66
import java.time.LocalDateTime;
7-
import lombok.AllArgsConstructor;
8-
import lombok.Data;
7+
import java.util.Arrays;
8+
import java.util.Objects;
9+
910
import org.springframework.data.annotation.Id;
10-
import org.springframework.data.domain.Persistable;
1111
import org.springframework.data.relational.core.mapping.Table;
12-
import tech.ydb.data.core.convert.YdbType;
12+
13+
import tech.ydb.data.core.convert.YQLType;
14+
import tech.ydb.data.core.convert.annotation.YdbType;
1315

1416
/**
1517
* @author Madiyar Nurgazin
1618
*/
17-
@AllArgsConstructor
18-
@Data
1919
@Table("all_types_table")
20-
public class AllTypesEntity implements Persistable<Integer> {
20+
public class AllTypesEntity {
2121
@Id
2222
private Integer id;
2323
private String textColumn;
@@ -29,28 +29,128 @@ public class AllTypesEntity implements Persistable<Integer> {
2929
private double doubleColumn;
3030
private BigDecimal decimalColumn;
3131
private byte[] binaryColumn;
32-
@YdbType("Date")
32+
@YdbType(YQLType.Date)
3333
private LocalDate dateColumn;
34-
@YdbType("Datetime")
34+
@YdbType(YQLType.Datetime)
3535
private LocalDateTime datetimeColumn;
3636
private Instant timestampColumn;
37-
@YdbType("Json")
37+
@YdbType(YQLType.Json)
3838
private String jsonColumn;
39-
@YdbType("JsonDocument")
39+
@YdbType(YQLType.JsonDocument)
4040
private String jsonDocumentColumn;
41-
@YdbType("Uint8")
41+
@YdbType(YQLType.Uint8)
4242
private byte uint8Column;
43-
@YdbType("Uint16")
43+
@YdbType(YQLType.Uint16)
4444
private short uint16Column;
45-
@YdbType("Uint32")
45+
@YdbType(YQLType.Uint32)
4646
private int uint32Column;
47-
@YdbType("Uint64")
47+
@YdbType(YQLType.Uint64)
4848
private long uint64Column;
4949

5050
public AllTypesEntity() {
5151
}
52+
53+
public AllTypesEntity(Integer id, String textColumn, boolean boolColumn, byte tinyintColumn, short smallintColumn,
54+
long bigintColumn, float floatColumn, double doubleColumn, BigDecimal decimalColumn, byte[] binaryColumn,
55+
LocalDate dateColumn, LocalDateTime datetimeColumn, Instant timestampColumn, String jsonColumn,
56+
String jsonDocumentColumn, byte uint8Column, short uint16Column, int uint32Column, long uint64Column) {
57+
this.id = id;
58+
this.textColumn = textColumn;
59+
this.boolColumn = boolColumn;
60+
this.tinyintColumn = tinyintColumn;
61+
this.smallintColumn = smallintColumn;
62+
this.bigintColumn = bigintColumn;
63+
this.floatColumn = floatColumn;
64+
this.doubleColumn = doubleColumn;
65+
this.decimalColumn = decimalColumn;
66+
this.binaryColumn = binaryColumn;
67+
this.dateColumn = dateColumn;
68+
this.datetimeColumn = datetimeColumn;
69+
this.timestampColumn = timestampColumn;
70+
this.jsonColumn = jsonColumn;
71+
this.jsonDocumentColumn = jsonDocumentColumn;
72+
this.uint8Column = uint8Column;
73+
this.uint16Column = uint16Column;
74+
this.uint32Column = uint32Column;
75+
this.uint64Column = uint64Column;
76+
}
77+
78+
public Integer getId() {
79+
return id;
80+
}
81+
82+
public String getTextColumn() {
83+
return textColumn;
84+
}
85+
86+
public BigDecimal getDecimalColumn() {
87+
return decimalColumn;
88+
}
89+
90+
public void setJsonColumn(String jsonColumn) {
91+
this.jsonColumn = jsonColumn;
92+
}
93+
94+
public void setJsonDocumentColumn(String jsonDocumentColumn) {
95+
this.jsonDocumentColumn = jsonDocumentColumn;
96+
}
97+
5298
@Override
53-
public boolean isNew() {
54-
return false;
99+
public int hashCode() {
100+
return Objects.hash(
101+
id,
102+
textColumn,
103+
boolColumn,
104+
tinyintColumn,
105+
smallintColumn,
106+
bigintColumn,
107+
floatColumn,
108+
doubleColumn,
109+
decimalColumn,
110+
binaryColumn,
111+
dateColumn,
112+
datetimeColumn,
113+
timestampColumn,
114+
jsonColumn,
115+
jsonDocumentColumn,
116+
uint8Column,
117+
uint16Column,
118+
uint32Column,
119+
uint64Column
120+
);
55121
}
122+
123+
@Override
124+
public boolean equals(Object obj) {
125+
if (this == obj) {
126+
return true;
127+
}
128+
129+
if (obj == null || obj.getClass() != getClass()) {
130+
return false;
131+
}
132+
133+
AllTypesEntity other = (AllTypesEntity) obj;
134+
return Objects.equals(id, other.id)
135+
&& Objects.equals(textColumn, other.textColumn)
136+
&& boolColumn == other.boolColumn
137+
&& tinyintColumn == other.tinyintColumn
138+
&& smallintColumn == other.smallintColumn
139+
&& bigintColumn == other.bigintColumn
140+
&& floatColumn == other.floatColumn
141+
&& doubleColumn == other.doubleColumn
142+
&& Objects.equals(decimalColumn, other.decimalColumn)
143+
&& Arrays.equals(binaryColumn, other.binaryColumn)
144+
&& Objects.equals(dateColumn, other.dateColumn)
145+
&& Objects.equals(datetimeColumn, other.datetimeColumn)
146+
&& Objects.equals(timestampColumn, other.timestampColumn)
147+
&& Objects.equals(jsonColumn, other.jsonColumn)
148+
&& Objects.equals(jsonDocumentColumn, other.jsonDocumentColumn)
149+
&& uint8Column == other.uint8Column
150+
&& uint16Column == other.uint16Column
151+
&& uint32Column == other.uint32Column
152+
&& uint64Column == other.uint64Column;
153+
}
154+
155+
56156
}
Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package tech.ydb.data.books;
22

33
import java.time.Instant;
4+
import java.util.Iterator;
45
import java.util.List;
56
import java.util.Optional;
67
import java.util.Set;
8+
79
import org.junit.jupiter.api.Assertions;
810
import org.junit.jupiter.api.Test;
911
import org.springframework.beans.factory.annotation.Autowired;
1012
import org.springframework.data.domain.PageRequest;
1113
import org.springframework.data.domain.Sort;
14+
1215
import tech.ydb.data.YdbBaseTest;
1316
import tech.ydb.data.books.entity.Author;
1417
import tech.ydb.data.books.entity.Book;
@@ -32,29 +35,27 @@ public class RepositoriesIntegrationTest extends YdbBaseTest {
3235

3336
@Test
3437
public void crudTest() {
35-
Assertions.assertEquals(1, authorRepository.findAuthorByName("Leo Tolstoy").get(0).getId());
38+
Assertions.assertEquals(Long.valueOf(1), authorRepository.findAuthorByName("Leo Tolstoy").get(0).getId());
3639

37-
Review review1 = createReview(
38-
1, 1, "Ivan Ivanov", "Masterpiece!", 10, Instant.parse("2024-03-19T15:52:26Z")
39-
);
40+
Review review1 = reviewRepository.findById(1L).orElseThrow();
41+
Review review2 = reviewRepository.findById(2L).orElseThrow();
4042

41-
Review review2 = createReview(
42-
2, 1, "Sergey Petrov", "Complex work, but I liked it", 9, Instant.parse("2024-03-19T16:14:05Z")
43-
);
43+
Book book1 = new Book(1, "War and Peace", "1234", 1869);
44+
book1.getReviews().add(review1);
45+
book1.getReviews().add(review2);
46+
book1.getAuthors().add(new BookAuthor(1, 1));
4447

45-
List<Book> expected = List.of(
46-
createBook(1, "War and Peace", "1234", 1869, Set.of(review1, review2), Set.of(new BookAuthor(1, 1))),
47-
createBook(2, "Anna Karenina", "5678", 1878, Set.of(), Set.of(new BookAuthor(1, 2)))
48-
);
48+
Book book2 = new Book(2, "Anna Karenina", "5678", 1878);
49+
book1.getAuthors().add(new BookAuthor(1, 2));
50+
51+
List<Book> expected = List.of(book1, book2);
4952
Iterable<Book> books = bookRepository.findAll();
5053
Assertions.assertEquals(expected, books);
5154

5255
Optional<Book> bookO = bookRepository.findBookByTitle("War and Peace");
5356
Assertions.assertTrue(bookO.isPresent());
5457

55-
Review review3 = createReview(
56-
3, 1, "Madiyar Nurgazin", "Great", 8, Instant.parse("2024-03-19T20:00:00Z")
57-
);
58+
Review review3 = new Review(3, 1, "Madiyar Nurgazin", "Great", 8, Instant.parse("2024-03-19T20:00:00Z"));
5859

5960
Book book = bookO.get();
6061
book.getReviews().add(review3);
@@ -63,11 +64,11 @@ public void crudTest() {
6364
Assertions.assertEquals(3, reviewRepository.count());
6465

6566
review1.setRating(100);
66-
review1.setNew(false);
67+
review1.setIsNew(false);
6768
review2.setRating(90);
68-
review2.setNew(false);
69+
review2.setIsNew(false);
6970
review3.setRating(80);
70-
review3.setNew(false);
71+
review3.setIsNew(false);
7172

7273
Set<Review> reviews = Set.of(review1, review2, review3);
7374
reviewRepository.saveAll(reviews);
@@ -78,13 +79,15 @@ public void crudTest() {
7879
book = bookO.get();
7980
Assertions.assertEquals(reviews, book.getReviews());
8081

81-
Author author1 = createAuthor(2, "Author 1");
82-
Author author2 = createAuthor(3, "Author 2");
82+
Author author1 = new Author(2, "Author 1");
83+
Author author2 = new Author(3, "Author 2");
8384

8485
authorRepository.saveAll(List.of(author1, author2));
8586
Assertions.assertEquals(3, authorRepository.count());
8687

87-
book = createBook(3, "Title", "Isbn", 2024, Set.of(), Set.of(new BookAuthor(2, 3), new BookAuthor(3, 3)));
88+
book = new Book(3, "Title", "Isbn", 2024);
89+
book.getAuthors().add(new BookAuthor(2, 3));
90+
book.getAuthors().add(new BookAuthor(3, 3));
8891
bookRepository.save(book);
8992

9093
expected.get(0).setReviews(Set.of(review1, review2, review3));
@@ -97,7 +100,7 @@ public void crudTest() {
97100
List<Author> authors = authorRepository.findAuthorsByBookId(3);
98101
Assertions.assertEquals(Set.of(author1, author2), Set.copyOf(authors));
99102

100-
Review review = createReview(4, 3, "Reader", "Text", 5, Instant.now());
103+
Review review = new Review(4, 3, "Reader", "Text", 5, Instant.now());
101104
reviewRepository.save(review);
102105

103106
bookRepository.deleteById(3L);
@@ -114,16 +117,12 @@ public void crudTest() {
114117

115118
@Test
116119
public void pagingAndSortingTest() {
117-
Review review1 = createReview(
118-
1, 1, "Ivan Ivanov", "Masterpiece!", 10, Instant.parse("2024-03-19T15:52:26Z")
119-
);
120-
Review review2 = createReview(
121-
2, 1, "Sergey Petrov", "Complex work, but I liked it", 9, Instant.parse("2024-03-19T16:14:05Z")
122-
);
123-
Review review3 = createReview(3, 1, "Reader", "Text", 100, Instant.parse("2024-03-19T21:00:00Z"));
124-
Review review4 = createReview(4, 1, "Reader", "Text2", 80, Instant.parse("2024-03-19T22:00:00Z"));
125-
Review review5 = createReview(5, 1, "Reader", "Text3", 75, Instant.parse("2024-03-19T23:00:00Z"));
126-
Review review6 = createReview(6, 1, "Reader", "Text4", 50, Instant.parse("2024-03-20T00:00:00Z"));
120+
Review review1 = reviewRepository.findById(1L).orElseThrow();
121+
Review review2 = reviewRepository.findById(2L).orElseThrow();
122+
Review review3 = new Review(3, 1, "Reader", "Text", 100, Instant.parse("2024-03-19T21:00:00Z"));
123+
Review review4 = new Review(4, 1, "Reader", "Text2", 80, Instant.parse("2024-03-19T22:00:00Z"));
124+
Review review5 = new Review(5, 1, "Reader", "Text3", 75, Instant.parse("2024-03-19T23:00:00Z"));
125+
Review review6 = new Review(6, 1, "Reader", "Text4", 50, Instant.parse("2024-03-20T00:00:00Z"));
127126
reviewRepository.saveAll(List.of(review3, review4, review5, review6));
128127

129128
Iterable<Review> reviews = reviewRepository.findByReader(
@@ -137,6 +136,21 @@ public void pagingAndSortingTest() {
137136
Assertions.assertEquals(List.of(review5, review6), reviews);
138137

139138
reviews = reviewRepository.findAll(Sort.by("created").descending());
139+
140+
Iterator<Review> it = reviews.iterator();
141+
Assertions.assertTrue(it.hasNext());
142+
Assertions.assertEquals(review6, it.next());
143+
Assertions.assertTrue(it.hasNext());
144+
Assertions.assertEquals(review5, it.next());
145+
Assertions.assertTrue(it.hasNext());
146+
Assertions.assertEquals(review4, it.next());
147+
Assertions.assertTrue(it.hasNext());
148+
Assertions.assertEquals(review3, it.next());
149+
Assertions.assertTrue(it.hasNext());
150+
Assertions.assertEquals(review2, it.next());
151+
Assertions.assertTrue(it.hasNext());
152+
Assertions.assertEquals(review1, it.next());
153+
140154
Assertions.assertEquals(List.of(review6, review5, review4, review3, review2, review1), reviews);
141155

142156
reviews = reviewRepository.findAll(PageRequest.of(0, 3, Sort.by("id"))).getContent();
@@ -148,38 +162,4 @@ public void pagingAndSortingTest() {
148162
reviews = reviewRepository.findAll(PageRequest.of(2, 2)).getContent();
149163
Assertions.assertEquals(List.of(review5, review6), reviews);
150164
}
151-
152-
private Review createReview(long id, long bookId, String reader, String text, long rating, Instant created) {
153-
Review review = new Review();
154-
review.setId(id);
155-
review.setBookId(bookId);
156-
review.setReader(reader);
157-
review.setText(text);
158-
review.setRating(rating);
159-
review.setCreated(created);
160-
review.setNew(true);
161-
return review;
162-
}
163-
164-
private Book createBook(
165-
long id, String title, String isbn, long year, Set<Review> reviews, Set<BookAuthor> authors
166-
) {
167-
Book book = new Book();
168-
book.setId(id);
169-
book.setTitle(title);
170-
book.setIsbn(isbn);
171-
book.setYear(year);
172-
book.setReviews(reviews);
173-
book.setAuthors(authors);
174-
book.setNew(true);
175-
return book;
176-
}
177-
178-
private Author createAuthor(long id, String name) {
179-
Author author = new Author();
180-
author.setId(id);
181-
author.setName(name);
182-
author.setNew(true);
183-
return author;
184-
}
185165
}

0 commit comments

Comments
 (0)