Skip to content

Commit 4f45df3

Browse files
author
Philipp Wagner
committed
Add Test for Increasing PK Values
1 parent 623eadd commit 4f45df3

5 files changed

Lines changed: 95 additions & 9 deletions

File tree

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/SqlServerBulkInsert.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
import java.math.BigDecimal;
1717
import java.math.BigInteger;
18-
import java.math.RoundingMode;
1918
import java.sql.Connection;
2019
import java.sql.SQLException;
2120
import java.sql.Types;

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/records/SqlServerRecord.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package de.bytefish.jsqlserverbulkinsert.records;
55

66
import com.microsoft.sqlserver.jdbc.ISQLServerBulkRecord;
7+
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
78
import com.microsoft.sqlserver.jdbc.SQLServerException;
89
import de.bytefish.jsqlserverbulkinsert.model.ColumnDefinition;
910
import de.bytefish.jsqlserverbulkinsert.model.ColumnMetaData;

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/util/BinaryUtils.java

Lines changed: 0 additions & 8 deletions
This file was deleted.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright (c) Philipp Wagner. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
package de.bytefish.jsqlserverbulkinsert.test.mapping;
5+
6+
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopyOptions;
7+
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
8+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
9+
import org.junit.Assert;
10+
import org.junit.Test;
11+
12+
import java.sql.ResultSet;
13+
import java.sql.SQLException;
14+
import java.sql.Statement;
15+
import java.util.Arrays;
16+
import java.util.List;
17+
18+
public class AutoIncrementIntMappingTest extends TransactionalTestBase {
19+
20+
private class IntegerEntity extends SampleEntity<Integer> {
21+
22+
public IntegerEntity(Integer value) {
23+
super(value);
24+
}
25+
}
26+
27+
private class IntegerInsert extends SqlServerBulkInsert<IntegerEntity> {
28+
29+
public IntegerInsert() {
30+
super("dbo", "UnitTest");
31+
32+
mapInt("PK_ID", x -> null);
33+
mapInt("IntegerValue", IntegerEntity::getValue);
34+
}
35+
36+
}
37+
38+
@Override
39+
protected void onSetUpInTransaction() throws Exception {
40+
createTestTable();
41+
}
42+
43+
@Test
44+
public void bulkInsertPersonDataTest() throws SQLException {
45+
Integer IntegerValue = 12;
46+
// Create the Value:
47+
List<IntegerEntity> entities = Arrays.asList(new IntegerEntity(IntegerValue), new IntegerEntity(IntegerValue));
48+
// Create the BulkInserter:
49+
IntegerInsert localDateInsert = new IntegerInsert();
50+
// Now save all entities of a given stream:
51+
localDateInsert.saveAll(connection, entities.stream());
52+
// And assert all have been written to the database:
53+
ResultSet rs = getAll();
54+
// We have a Value:
55+
Assert.assertEquals(true, rs.next());
56+
// Get the Date we have written:
57+
Integer resultIntegerValue = rs.getInt("PK_ID");
58+
// Assert both are equal:
59+
Assert.assertEquals(new Integer(1), resultIntegerValue);
60+
61+
// We have a second Value:
62+
Assert.assertEquals(true, rs.next());
63+
// Get the Date we have written:
64+
Integer resultSecondIntegerValue = rs.getInt("PK_ID");
65+
// Assert both are equal:
66+
Assert.assertEquals(new Integer(2), resultSecondIntegerValue);
67+
68+
// Assert only one record was read:
69+
Assert.assertEquals(false, rs.next());
70+
}
71+
72+
private ResultSet getAll() throws SQLException {
73+
74+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
75+
76+
Statement statement = connection.createStatement();
77+
78+
return statement.executeQuery(sqlStatement);
79+
}
80+
81+
private void createTestTable() throws SQLException {
82+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
83+
" (\n" +
84+
" PK_ID INT IDENTITY(1,1) PRIMARY KEY,\n" +
85+
" IntegerValue INT" +
86+
" );";
87+
88+
Statement statement = connection.createStatement();
89+
90+
statement.execute(sqlStatement);
91+
}
92+
93+
}

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/mapping/BigIntegerMappingTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
package de.bytefish.jsqlserverbulkinsert.test.mapping;
55

6+
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
67
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
78
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
89
import org.junit.Assert;

0 commit comments

Comments
 (0)