-
Notifications
You must be signed in to change notification settings - Fork 132
/
RiderDSLIt.java
402 lines (351 loc) · 18.4 KB
/
RiderDSLIt.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package com.github.database.rider.core.dsl;
import com.github.database.rider.core.DataSetProviderIt;
import com.github.database.rider.core.api.dataset.CompareOperation;
import com.github.database.rider.core.configuration.DBUnitConfig;
import com.github.database.rider.core.configuration.DataSetConfig;
import com.github.database.rider.core.configuration.ExpectedDataSetConfig;
import com.github.database.rider.core.model.Follower;
import com.github.database.rider.core.model.User;
import com.github.database.rider.core.util.EntityManagerProvider;
import junit.framework.ComparisonFailure;
import org.dbunit.DatabaseUnitException;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import java.util.List;
import static com.github.database.rider.core.api.dataset.SeedStrategy.INSERT;
import static com.github.database.rider.core.api.dataset.SeedStrategy.TRUNCATE_INSERT;
import static com.github.database.rider.core.dsl.RiderDSL.DataSetConfigDSL.withDataSetConfig;
import static com.github.database.rider.core.dsl.RiderDSL.withConnection;
import static com.github.database.rider.core.util.EntityManagerProvider.em;
import static com.github.database.rider.core.util.EntityManagerProvider.tx;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(JUnit4.class)
public class RiderDSLIt {
@Rule
public EntityManagerProvider emProvider = EntityManagerProvider.instance("rules-it");
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void shouldCreateDataSetUsingDSL() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yml"))
.createDataSet();
List<User> users = em().createQuery("select u from User u").getResultList();
assertThat(users).hasSize(2)
.extracting("name")
.contains("@realpestano", "@dbunit");
}
@Test
public void shouldCreateMultipleDataSetsReusingDSLInstance() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yml"))
.createDataSet();
List<User> users = em().createQuery("select u from User u").getResultList();
assertThat(users).hasSize(2)
.extracting("name")
.contains("@realpestano", "@dbunit");
withConnection()
.withDataSetConfig(new DataSetConfig("datasets/yml/empty.yml"))
.createDataSet();
users = em().createQuery("select u from User u").getResultList();
assertThat(users).isEmpty();
//you also don't need to use connection dsl if it is already initialized
RiderDSL.DataSetConfigDSL
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yml"))
.createDataSet();
users = em().createQuery("select u from User u").getResultList();
assertThat(users).hasSize(2)
.extracting("name")
.contains("@realpestano", "@dbunit");
//you can also use static import if you're sure the connection is initialized
withDataSetConfig(new DataSetConfig("datasets/yml/empty.yml"))
.createDataSet();
users = em().createQuery("select u from User u").getResultList();
assertThat(users).isEmpty();
}
@Test
/* @DataSet(provider = UserDataSetProvider.class,
cleanBefore = true)*/
public void shouldSeedDatabaseProgrammatically() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig().datasetProvider(DataSetProviderIt.UserDataSetProvider.class)
.cleanBefore(true))
.createDataSet();
List<User> users = EntityManagerProvider.em().createQuery("select u from User u ").getResultList();
assertThat(users).
isNotNull().
isNotEmpty().hasSize(2).
extracting("name").
contains("@dbunit", "@dbrider");
}
@Test
//@DataSet(provider = UserDataSetProviderWithColumnsSyntax.class)
public void shouldSeedDatabaseUsingDataSetProviderWithColumnsSyntax() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig().datasetProvider(DataSetProviderIt.UserDataSetProviderWithColumnsSyntax.class)
.cleanBefore(true))
.createDataSet();
List<User> users = EntityManagerProvider.em().createQuery("select u from User u ").getResultList();
assertThat(users).
isNotNull().
isNotEmpty().hasSize(2).
extracting("name").
contains("@dbunit", "@dbrider");
}
@Test
/*same as: @DataSet(value = "yml/lowercaseUsers.yml") // note that hsqldb tables are in uppercase
@DBUnit(caseSensitiveTableNames = false) */
public void shouldListUsersWithCaseInSensitiveTableNames() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/lowercaseUsers.yml"))
.withDBUnitConfig(new DBUnitConfig().addDBUnitProperty("caseSensitiveTableNames", false))
.createDataSet();
List<com.github.database.rider.core.model.lowercase.User> users = EntityManagerProvider.em().createQuery("select u from User u").getResultList();
assertThat(users).isNotNull().isNotEmpty().hasSize(2);
}
@Test
//@DataSet(value = "datasets/yml/user.yml", cleanBefore = true)
public void shouldSeedDatabase() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/user.yml")
.cleanBefore(true))
.createDataSet();
List<User> users = EntityManagerProvider.em().createQuery("select u from User u ").getResultList();
assertThat(users).
isNotNull().
isNotEmpty().
hasSize(2);
}
@Test
//@DataSet(value = "datasets/yml/users.yml", executeStatementsBefore = "SET DATABASE REFERENTIAL INTEGRITY FALSE;", executeStatementsAfter = "SET DATABASE REFERENTIAL INTEGRITY TRUE;")
public void shouldSeedDataSetDisablingContraintsViaStatement() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yml")
.executeStatementsBefore("SET DATABASE REFERENTIAL INTEGRITY FALSE;")
.executeStatementsAfter("SET DATABASE REFERENTIAL INTEGRITY TRUE;"))
.createDataSet();
User user = (User) EntityManagerProvider.em().createQuery("select u from User u join fetch u.tweets join fetch u.followers join fetch u.tweets join fetch u.followers where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
assertThat(user.getTweets()).hasSize(1);
}
@Test
/*@DataSet(value = "datasets/yml/users.yml",
useSequenceFiltering = false,
tableOrdering = {"USER", "TWEET", "FOLLOWER"},
executeStatementsBefore = {"DELETE FROM FOLLOWER", "DELETE FROM TWEET", "DELETE FROM USER"})*/
public void shouldSeedDataSetUsingTableCreationOrder() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yml")
.useSequenceFiltering(true)
.tableOrdering("USER", "TWEET", "FOLLOWER")
.executeStatementsBefore("DELETE FROM FOLLOWER", "DELETE FROM TWEET", "DELETE FROM USER"))
.createDataSet();
List<User> users = EntityManagerProvider.em().createQuery("select u from User u left join fetch u.tweets left join fetch u.followers").getResultList();
assertThat(users).hasSize(2);
}
@Test
//@DataSet(value = "datasets/yml/users.yml", strategy = SeedStrategy.TRUNCATE_INSERT, useSequenceFiltering = true)
public void shouldSeedUserDataSetUsingTruncateInsert() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yml")
.useSequenceFiltering(true)
.strategy(TRUNCATE_INSERT))
.createDataSet();
List<User> users = EntityManagerProvider.em("rules-it").createQuery("select u from User u", User.class).getResultList();
assertThat(users).isNotNull();
assertThat(users.size()).isEqualTo(2);
assertThat(users.get(0).getId()).isEqualTo(1);
assertThat(users.get(0).getName()).isEqualTo("@realpestano");
assertThat(users.get(1).getId()).isEqualTo(2);
assertThat(users.get(1).getName()).isEqualTo("@dbunit");
}
@Test
//@DataSet(value = "datasets/json/users.json")
public void shouldLoadUsersFromJsonDataset() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/json/users.json"))
.createDataSet();
User user = (User) EntityManagerProvider.em().createQuery("select u from User u join fetch u.tweets join fetch u.followers left join fetch u.followers where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
assertThat(user.getTweets()).hasSize(1);
Assert.assertEquals("dbunit rules json example", user.getTweets().get(0).getContent());
assertThat(user.getFollowers()).isNotNull().hasSize(1);
Follower expectedFollower = new Follower(2, 1);
assertThat(user.getFollowers()).contains(expectedFollower);
}
@Test
//@DataSet(value = "datasets/xml/users.xml")
public void shouldLoadUsersFromXmlDataset() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/xml/users.xml"))
.createDataSet();
User user = (User) EntityManagerProvider.em().createQuery("select u from User u join fetch u.tweets join fetch u.followers left join fetch u.followers where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
assertThat(user.getTweets()).hasSize(1);
Assert.assertEquals("dbunit rules flat xml example", user.getTweets().get(0).getContent());
assertThat(user.getFollowers()).isNotNull().hasSize(1);
Follower expectedFollower = new Follower(2, 1);
assertThat(user.getFollowers()).contains(expectedFollower);
}
@Test
//@DataSet(strategy = SeedStrategy.INSERT, value = {"yml/user.yml", "yml/tweet.yml", "yml/follower.yml"}, executeStatementsBefore = {"DELETE FROM FOLLOWER", "DELETE FROM TWEET", "DELETE FROM USER"})
public void shouldLoadDataFromMultipleDataSets() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/user.yml", "yml/tweet.yml", "yml/follower.yml")
.strategy(INSERT)
.executeStatementsBefore("DELETE FROM FOLLOWER", "DELETE FROM TWEET", "DELETE FROM USER"))
.createDataSet();
User user = (User) EntityManagerProvider.em("rules-it").createQuery("select u from User u join fetch u.tweets join fetch u.followers left join fetch u.followers where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
assertThat(user.getTweets()).hasSize(1);
Assert.assertEquals("dbunit rules again!", user.getTweets().get(0).getContent());
assertThat(user.getFollowers()).isNotNull().hasSize(1);
Follower expectedFollower = new Follower(2, 1);
assertThat(user.getFollowers()).contains(expectedFollower);
}
@Test
//@DataSet(strategy = SeedStrategy.INSERT, value = "yml/user.yml, yml/tweet.yml, yml/follower.yml",
// executeStatementsBefore = {"DELETE FROM FOLLOWER", "DELETE FROM TWEET", "DELETE FROM USER"})
public void shouldLoadDataFromMultipleDataSetsUsingCommaToSeparateNames() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/user.yml, yml/tweet.yml, yml/follower.yml")
.strategy(INSERT)
.executeStatementsBefore("DELETE FROM FOLLOWER", "DELETE FROM TWEET", "DELETE FROM USER"))
.createDataSet();
User user = (User) EntityManagerProvider.em("rules-it").createQuery("select u from User u join fetch u.tweets join fetch u.followers left join fetch u.followers where u.id = 1").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getId()).isEqualTo(1);
assertThat(user.getTweets()).hasSize(1);
Assert.assertEquals("dbunit rules again!", user.getTweets().get(0).getContent());
assertThat(user.getFollowers()).isNotNull().hasSize(1);
Follower expectedFollower = new Follower(2, 1);
assertThat(user.getFollowers()).contains(expectedFollower);
}
@Test
//@DataSet(value = "datasets/csv/USER.csv", cleanBefore = true)
public void shouldSeedDatabaseWithCSVDataSet() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/csv/USER.csv")
.cleanBefore(true))
.createDataSet();
User user = (User) EntityManagerProvider.em().createQuery("select u from User u join u.tweets t where t.content = 'dbunit rules!'").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getName()).isEqualTo("@realpestano");
}
@Test
//@DataSet("xls/users.xls")
public void shouldSeedDatabaseWithXLSDataSet() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("xls/users.xls")
.cleanBefore(true))
.createDataSet();
User user = (User) EntityManagerProvider.em().createQuery("select u from User u join u.tweets t where t.content = 'dbunit rules!'").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getName()).isEqualTo("@realpestano");
}
@Test
//@DataSet("xlsx/users.xlsx")
public void shouldSeedDatabaseWithXLSXDataSet() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("xlsx/users.xlsx")
.cleanBefore(true))
.createDataSet();
User user = (User) EntityManagerProvider.em().createQuery("select u from User u join u.tweets t where t.content = 'dbunit rules!'").getSingleResult();
assertThat(user).isNotNull();
assertThat(user.getName()).isEqualTo("@realpestano");
}
//expected datasets
/**
* same as:
* @DataSet(cleanBefore = true)
* @ExpectedDataSet(value = "yml/expectedUsers.yml", ignoreCols = "id")
*/
@Test
public void shouldMatchExpectedDataSet() throws DatabaseUnitException {
withConnection(emProvider.connection())
.cleanDB();
User u = new User();
u.setName("expected user1");
User u2 = new User();
u2.setName("expected user2");
tx().begin();
em().persist(u);
em().persist(u2);
tx().commit();
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/expectedUsers.yml"))
.expectDataSet(new ExpectedDataSetConfig().ignoreCols("id"));
}
@Test
//@ExpectedDataSet(value = "yml/expectedUsers.yml", ignoreCols = "id")
public void shouldNotMatchExpectedDataSet() throws DatabaseUnitException {
withConnection(emProvider.connection())
.cleanDB();
User u = new User();
u.setName("non expected user1");
User u2 = new User();
u2.setName("non expected user2");
EntityManagerProvider.tx().begin();
EntityManagerProvider.em().persist(u);
EntityManagerProvider.em().persist(u2);
EntityManagerProvider.tx().commit();
exceptionRule.expect(ComparisonFailure.class);
exceptionRule.expectMessage("value (table=USER, row=0, col=NAME) expected:<[]expected user1> but was:<[non ]expected user1>");
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/expectedUsers.yml"))
.expectDataSet(new ExpectedDataSetConfig().ignoreCols("id"));
}
@Test
//@DataSet(cleanBefore = true)
//@ExpectedDataSet(value = "yml/expectedUsersRegex.yml")
public void shouldMatchExpectedDataSetUsingRegex() throws DatabaseUnitException {
withConnection(emProvider.connection())
.cleanDB();
User u = new User();
u.setName("expected user1");
User u2 = new User();
u2.setName("expected user2");
EntityManagerProvider.tx().begin();
EntityManagerProvider.em().persist(u);
EntityManagerProvider.em().persist(u2);
EntityManagerProvider.tx().commit();
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/expectedUsersRegex.yml"))
.expectDataSet();
}
@Test
//@DataSet(value = "yml/user.yml", transactional = true)
//@ExpectedDataSet(value = "yml/expectedUsersContains.yml", compareOperation = CompareOperation.CONTAINS)
public void shouldMatchExpectedDataSetContains() throws DatabaseUnitException {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/user.yml").cleanBefore(true))
.createDataSet();
tx().begin();
User u = new User();
u.setId(3);
u.setName("@dbrider");
em().persist(u);
tx().commit();
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("yml/expectedUsersContains.yml"))
.expectDataSet(new ExpectedDataSetConfig().compareOperation(CompareOperation.CONTAINS));
}
@Test
public void shouldCreateDataSetUsingDSLAndYamlDataSet() {
withConnection(emProvider.connection())
.withDataSetConfig(new DataSetConfig("datasets/yml/users.yaml"))
.createDataSet();
List<User> users = em().createQuery("select u from User u").getResultList();
assertThat(users).hasSize(2)
.extracting("name")
.contains("@realpestano", "@dbunit");
}
}