Skip to content

Commit caca6d7

Browse files
committed
Add UpsertAssemblerContextTest and Improving UpsertAssemblerContext validation
1 parent 0fb92ef commit caca6d7

File tree

2 files changed

+299
-0
lines changed

2 files changed

+299
-0
lines changed

doma-core/src/main/java/org/seasar/doma/jdbc/query/UpsertAssemblerContext.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ public UpsertAssemblerContext(
7777
"keys",
7878
"The keys must not be empty when performing an upsert. At least one key must be specified.");
7979
}
80+
if (insertValues.isEmpty()) {
81+
throw new DomaIllegalArgumentException(
82+
"insertValues",
83+
"The insertValues must not be empty when performing an upsert. At least one insert value must be specified.");
84+
}
8085
if (duplicateKeyType == DuplicateKeyType.UPDATE && setValues.isEmpty()) {
8186
throw new DomaIllegalArgumentException(
8287
"setValues",
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
package org.seasar.doma.jdbc.query;
2+
3+
import static java.util.stream.Collectors.toList;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.Optional;
10+
import org.junit.jupiter.api.Test;
11+
import org.seasar.doma.DomaIllegalArgumentException;
12+
import org.seasar.doma.internal.jdbc.mock.MockConfig;
13+
import org.seasar.doma.internal.jdbc.sql.PreparedSqlBuilder;
14+
import org.seasar.doma.jdbc.InParameter;
15+
import org.seasar.doma.jdbc.Naming;
16+
import org.seasar.doma.jdbc.SqlKind;
17+
import org.seasar.doma.jdbc.SqlLogType;
18+
import org.seasar.doma.jdbc.SqlParameterVisitor;
19+
import org.seasar.doma.jdbc.criteria.entity.Dept_;
20+
import org.seasar.doma.jdbc.criteria.metamodel.PropertyMetamodel;
21+
import org.seasar.doma.jdbc.criteria.tuple.Tuple2;
22+
import org.seasar.doma.jdbc.entity.EntityPropertyType;
23+
import org.seasar.doma.wrapper.Wrapper;
24+
25+
class UpsertAssemblerContextTest {
26+
private final MockConfig config = new MockConfig();
27+
28+
private final PreparedSqlBuilder buf =
29+
new PreparedSqlBuilder(config, SqlKind.BATCH_INSERT, SqlLogType.RAW);
30+
private final Dept_ metaDept = new Dept_();
31+
private final List<EntityPropertyType<?, ?>> propertyTypes =
32+
metaDept.allPropertyMetamodels().stream().map(PropertyMetamodel::asType).collect(toList());
33+
34+
@Test
35+
void onDuplicateKeyUpdate() {
36+
List<EntityPropertyType<?, ?>> keys =
37+
propertyTypes.stream().filter(EntityPropertyType::isId).collect(toList());
38+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues =
39+
propertyTypes.stream()
40+
.filter(c -> !c.isId())
41+
.map(c -> new Tuple2<EntityPropertyType<?, ?>, InParameter<?>>(c, mockInParameter()))
42+
.collect(toList());
43+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues =
44+
propertyTypes.stream()
45+
.filter(c -> !c.isId())
46+
.map(
47+
c ->
48+
new Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>(
49+
c, new UpsertSetValue.Prop(c)))
50+
.collect(toList());
51+
UpsertAssemblerContext context =
52+
new UpsertAssemblerContext(
53+
buf,
54+
metaDept.asType(),
55+
DuplicateKeyType.UPDATE,
56+
Naming.NONE,
57+
config.getDialect(),
58+
keys,
59+
insertValues,
60+
setValues);
61+
assertNotNull(context);
62+
}
63+
64+
@Test
65+
void onDuplicateKeyUpdate_emptyKey() {
66+
List<EntityPropertyType<?, ?>> keys = Collections.emptyList();
67+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues =
68+
propertyTypes.stream()
69+
.filter(c -> !c.isId())
70+
.map(c -> new Tuple2<EntityPropertyType<?, ?>, InParameter<?>>(c, mockInParameter()))
71+
.collect(toList());
72+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues =
73+
propertyTypes.stream()
74+
.filter(c -> !c.isId())
75+
.map(
76+
c ->
77+
new Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>(
78+
c, new UpsertSetValue.Prop(c)))
79+
.collect(toList());
80+
DomaIllegalArgumentException ex =
81+
assertThrows(
82+
DomaIllegalArgumentException.class,
83+
() -> {
84+
new UpsertAssemblerContext(
85+
buf,
86+
metaDept.asType(),
87+
DuplicateKeyType.UPDATE,
88+
Naming.NONE,
89+
config.getDialect(),
90+
keys,
91+
insertValues,
92+
setValues);
93+
});
94+
System.out.println(ex.getMessage());
95+
}
96+
97+
@Test
98+
void onDuplicateKeyUpdate_emptyInsertValues() {
99+
List<EntityPropertyType<?, ?>> keys =
100+
propertyTypes.stream().filter(EntityPropertyType::isId).collect(toList());
101+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues = Collections.emptyList();
102+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues =
103+
propertyTypes.stream()
104+
.filter(c -> !c.isId())
105+
.map(
106+
c ->
107+
new Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>(
108+
c, new UpsertSetValue.Prop(c)))
109+
.collect(toList());
110+
DomaIllegalArgumentException ex =
111+
assertThrows(
112+
DomaIllegalArgumentException.class,
113+
() -> {
114+
new UpsertAssemblerContext(
115+
buf,
116+
metaDept.asType(),
117+
DuplicateKeyType.UPDATE,
118+
Naming.NONE,
119+
config.getDialect(),
120+
keys,
121+
insertValues,
122+
setValues);
123+
});
124+
System.out.println(ex.getMessage());
125+
}
126+
127+
@Test
128+
void onDuplicateKeyUpdate_emptySetValues() {
129+
List<EntityPropertyType<?, ?>> keys =
130+
propertyTypes.stream().filter(EntityPropertyType::isId).collect(toList());
131+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues =
132+
propertyTypes.stream()
133+
.filter(c -> !c.isId())
134+
.map(c -> new Tuple2<EntityPropertyType<?, ?>, InParameter<?>>(c, mockInParameter()))
135+
.collect(toList());
136+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues = Collections.emptyList();
137+
DomaIllegalArgumentException ex =
138+
assertThrows(
139+
DomaIllegalArgumentException.class,
140+
() -> {
141+
new UpsertAssemblerContext(
142+
buf,
143+
metaDept.asType(),
144+
DuplicateKeyType.UPDATE,
145+
Naming.NONE,
146+
config.getDialect(),
147+
keys,
148+
insertValues,
149+
setValues);
150+
});
151+
System.out.println(ex.getMessage());
152+
}
153+
154+
@Test
155+
void onDuplicateKeyIgnore() {
156+
List<EntityPropertyType<?, ?>> keys =
157+
propertyTypes.stream().filter(EntityPropertyType::isId).collect(toList());
158+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues =
159+
propertyTypes.stream()
160+
.filter(c -> !c.isId())
161+
.map(c -> new Tuple2<EntityPropertyType<?, ?>, InParameter<?>>(c, mockInParameter()))
162+
.collect(toList());
163+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues =
164+
propertyTypes.stream()
165+
.filter(c -> !c.isId())
166+
.map(
167+
c ->
168+
new Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>(
169+
c, new UpsertSetValue.Prop(c)))
170+
.collect(toList());
171+
UpsertAssemblerContext context =
172+
new UpsertAssemblerContext(
173+
buf,
174+
metaDept.asType(),
175+
DuplicateKeyType.IGNORE,
176+
Naming.NONE,
177+
config.getDialect(),
178+
keys,
179+
insertValues,
180+
setValues);
181+
assertNotNull(context);
182+
}
183+
184+
@Test
185+
void onDuplicateKeyIgnore_emptyKey() {
186+
List<EntityPropertyType<?, ?>> keys = Collections.emptyList();
187+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues =
188+
propertyTypes.stream()
189+
.filter(c -> !c.isId())
190+
.map(c -> new Tuple2<EntityPropertyType<?, ?>, InParameter<?>>(c, mockInParameter()))
191+
.collect(toList());
192+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues =
193+
propertyTypes.stream()
194+
.filter(c -> !c.isId())
195+
.map(
196+
c ->
197+
new Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>(
198+
c, new UpsertSetValue.Prop(c)))
199+
.collect(toList());
200+
DomaIllegalArgumentException ex =
201+
assertThrows(
202+
DomaIllegalArgumentException.class,
203+
() -> {
204+
new UpsertAssemblerContext(
205+
buf,
206+
metaDept.asType(),
207+
DuplicateKeyType.IGNORE,
208+
Naming.NONE,
209+
config.getDialect(),
210+
keys,
211+
insertValues,
212+
setValues);
213+
});
214+
System.out.println(ex.getMessage());
215+
}
216+
217+
@Test
218+
void onDuplicateKeyIgnore_emptyInsertValues() {
219+
List<EntityPropertyType<?, ?>> keys =
220+
propertyTypes.stream().filter(EntityPropertyType::isId).collect(toList());
221+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues = Collections.emptyList();
222+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues =
223+
propertyTypes.stream()
224+
.filter(c -> !c.isId())
225+
.map(
226+
c ->
227+
new Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>(
228+
c, new UpsertSetValue.Prop(c)))
229+
.collect(toList());
230+
DomaIllegalArgumentException ex =
231+
assertThrows(
232+
DomaIllegalArgumentException.class,
233+
() -> {
234+
new UpsertAssemblerContext(
235+
buf,
236+
metaDept.asType(),
237+
DuplicateKeyType.IGNORE,
238+
Naming.NONE,
239+
config.getDialect(),
240+
keys,
241+
insertValues,
242+
setValues);
243+
});
244+
System.out.println(ex.getMessage());
245+
}
246+
247+
@Test
248+
void onDuplicateKeyIgnore_emptySetValues() {
249+
List<EntityPropertyType<?, ?>> keys =
250+
propertyTypes.stream().filter(EntityPropertyType::isId).collect(toList());
251+
List<Tuple2<EntityPropertyType<?, ?>, InParameter<?>>> insertValues =
252+
propertyTypes.stream()
253+
.filter(c -> !c.isId())
254+
.map(c -> new Tuple2<EntityPropertyType<?, ?>, InParameter<?>>(c, mockInParameter()))
255+
.collect(toList());
256+
List<Tuple2<EntityPropertyType<?, ?>, UpsertSetValue>> setValues = Collections.emptyList();
257+
UpsertAssemblerContext context =
258+
new UpsertAssemblerContext(
259+
buf,
260+
metaDept.asType(),
261+
DuplicateKeyType.IGNORE,
262+
Naming.NONE,
263+
config.getDialect(),
264+
keys,
265+
insertValues,
266+
setValues);
267+
assertNotNull(context);
268+
}
269+
270+
private InParameter<Object> mockInParameter() {
271+
return new InParameter<Object>() {
272+
@Override
273+
public Object getValue() {
274+
return null;
275+
}
276+
277+
@Override
278+
public <R, P, TH extends Throwable> R accept(SqlParameterVisitor<R, P, TH> visitor, P p)
279+
throws TH {
280+
return null;
281+
}
282+
283+
@Override
284+
public Optional<Class<?>> getDomainClass() {
285+
return Optional.empty();
286+
}
287+
288+
@Override
289+
public Wrapper getWrapper() {
290+
return null;
291+
}
292+
};
293+
}
294+
}

0 commit comments

Comments
 (0)