forked from ydb-platform/ydb-java-dialects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseTest.java
153 lines (129 loc) · 5.51 KB
/
BaseTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package tech.ydb.jooq;
import java.math.BigDecimal;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.List;
import jooq.generated.ydb.default_schema.tables.records.DateTableRecord;
import jooq.generated.ydb.default_schema.tables.records.SeriesRecord;
import org.jooq.CreateTableElementListStep;
import org.jooq.Table;
import org.jooq.types.ULong;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.RegisterExtension;
import tech.ydb.test.junit5.YdbHelperExtension;
public abstract class BaseTest {
protected static List<SeriesRecord> getExampleRecords() {
return List.of(
new SeriesRecord(ULong.valueOf(1), "Series One", "Info One", ULong.valueOf(20220101)),
new SeriesRecord(ULong.valueOf(2), "Series Two", "Info Two", ULong.valueOf(20220102)),
new SeriesRecord(ULong.valueOf(3), "Series Three", "Info Three", ULong.valueOf(20220103))
);
}
protected static List<DateTableRecord> getExampleDateRecords() {
LocalDate date = LocalDate.of(2024, 4, 1);
LocalDateTime datetime = LocalDateTime.of(date, LocalTime.of(13, 29, 30));
Instant instant = datetime.toInstant(ZoneOffset.UTC).plus(1, ChronoUnit.HOURS);
Duration duration = Duration.of(1, ChronoUnit.HOURS);
return List.of(
new DateTableRecord(ULong.valueOf(1), 2, 0.1, BigDecimal.valueOf(1), date, datetime, instant, duration),
new DateTableRecord(
ULong.valueOf(2),
3,
0.2,
BigDecimal.valueOf(2),
date.plusDays(1),
datetime.plusDays(1),
instant.plus(1, ChronoUnit.DAYS),
duration.plus(1, ChronoUnit.HOURS)
),
new DateTableRecord(
ULong.valueOf(3),
4,
0.3,
BigDecimal.valueOf(3),
date.plusDays(1),
datetime.plusDays(2),
instant.plus(2, ChronoUnit.DAYS),
duration.plus(2, ChronoUnit.HOURS)
)
);
}
@RegisterExtension
private static final YdbHelperExtension ydb = new YdbHelperExtension();
protected static CloseableYdbDSLContext dsl;
@BeforeAll
public static void beforeAll() {
dsl = YDB.using(jdbcUrl());
dsl.createTableIfNotExists("series")
.column("series_id", YdbTypes.UINT64)
.column("title", YdbTypes.UTF8)
.column("series_info", YdbTypes.UTF8)
.column("release_date", YdbTypes.UINT64)
.primaryKey("series_id")
.execute();
dsl.createTableIfNotExists("seasons")
.column("series_id", YdbTypes.UINT64)
.column("season_id", YdbTypes.UINT64)
.column("title", YdbTypes.UTF8)
.column("first_aired", YdbTypes.UINT64)
.column("last_aired", YdbTypes.UINT64)
.primaryKey("series_id", "season_id")
.execute();
dsl.createTableIfNotExists("episodes")
.column("series_id", YdbTypes.UINT64)
.column("season_id", YdbTypes.UINT64)
.column("episode_id", YdbTypes.UINT64)
.column("title", YdbTypes.UTF8)
.column("air_date", YdbTypes.UINT64)
.primaryKey("series_id", "season_id", "episode_id")
.execute();
dsl.createTableIfNotExists("hard_table")
.column("id", YdbTypes.STRING)
.column("first", YdbTypes.JSON)
.column("second", YdbTypes.JSONDOCUMENT)
.column("third", YdbTypes.YSON)
.primaryKey("id")
.execute();
dsl.createTableIfNotExists("date_table")
.column("id", YdbTypes.UINT64)
.column("int_col", YdbTypes.INT32)
.column("percent", YdbTypes.DOUBLE)
.column("big", YdbTypes.DECIMAL)
.column("date", YdbTypes.DATE)
.column("datetime", YdbTypes.DATETIME)
.column("timestamp", YdbTypes.TIMESTAMP)
.column("interval", YdbTypes.INTERVAL)
.primaryKey("id")
.execute();
CreateTableElementListStep createQuery = dsl.createTableIfNotExists("numeric").column("id", YdbTypes.INT32);
for (int i = 1; i <= 23; i++) {
createQuery.column(Integer.toString(i), YdbTypes.INT32);
}
createQuery.primaryKey("id").execute();
}
@AfterAll
public static void afterAll() {
dsl.close();
}
@AfterEach
public void afterEach() {
List<Table<?>> tables = dsl.meta().getTables();
for (Table<?> table : tables) {
if (!table.getName().startsWith(".sys")) {
dsl.deleteFrom(table).execute();
}
}
}
protected static String jdbcUrl() {
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
.append(ydb.useTls() ? "grpcs://" : "grpc://")
.append(ydb.endpoint())
.append(ydb.database());
if (ydb.authToken() != null) {
jdbc.append("?").append("token=").append(ydb.authToken());
}
return jdbc.toString();
}
}