Skip to content

Commit 4d940b4

Browse files
author
Philipp Wagner
committed
Integer Arithmetics Tests
1 parent fef6a6d commit 4d940b4

5 files changed

Lines changed: 336 additions & 2 deletions

File tree

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,17 @@ protected void mapDouble(String columnName, Func2<TEntity, Double> propertyGette
126126
addColumn(columnName, Types.DOUBLE, propertyGetter);
127127
}
128128

129-
protected void mapSmallInt(String columnName, Func2<TEntity, String> propertyGetter)
129+
protected void mapInt(String columnName, Func2<TEntity, Integer> propertyGetter)
130+
{
131+
addColumn(columnName, Types.INTEGER, propertyGetter);
132+
}
133+
134+
protected void mapSmallInt(String columnName, Func2<TEntity, Short> propertyGetter)
130135
{
131136
addColumn(columnName, Types.SMALLINT, propertyGetter);
132137
}
133138

134-
protected void mapTinyInt(String columnName, Func2<TEntity, String> propertyGetter)
139+
protected void mapTinyInt(String columnName, Func2<TEntity, Byte> propertyGetter)
135140
{
136141
addColumn(columnName, Types.TINYINT, propertyGetter);
137142
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.math.BigInteger;
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 BigIntegerMappingTest extends TransactionalTestBase {
19+
20+
private class BigIntegerEntity extends SampleEntity<BigInteger> {
21+
22+
public BigIntegerEntity(BigInteger value) {
23+
super(value);
24+
}
25+
}
26+
27+
private class BigIntegerInsert extends SqlServerBulkInsert<BigIntegerEntity> {
28+
29+
public BigIntegerInsert() {
30+
super("dbo", "UnitTest");
31+
32+
mapBigInt("BigIntegerValue", BigIntegerEntity::getValue);
33+
}
34+
35+
}
36+
37+
@Override
38+
protected void onSetUpInTransaction() throws Exception {
39+
createTestTable();
40+
}
41+
42+
@Test
43+
public void bulkInsertPersonDataTest() throws SQLException {
44+
BigInteger BigIntegerValue = new BigInteger("47878778228484");
45+
// Create the Value:
46+
List<BigIntegerEntity> entities = Arrays.asList(new BigIntegerEntity(BigIntegerValue));
47+
// Create the BulkInserter:
48+
BigIntegerInsert localDateInsert = new BigIntegerInsert();
49+
// Now save all entities of a given stream:
50+
localDateInsert.saveAll(connection, entities.stream());
51+
// And assert all have been written to the database:
52+
ResultSet rs = getAll();
53+
// We have a Value:
54+
Assert.assertEquals(true, rs.next());
55+
// Get the Date we have written:
56+
long resultBigIntegerValue = rs.getLong("BigIntegerValue");
57+
// Assert both are equal:
58+
Assert.assertEquals(BigIntegerValue.longValueExact(), resultBigIntegerValue);
59+
// Assert only one record was read:
60+
Assert.assertEquals(false, rs.next());
61+
}
62+
63+
private ResultSet getAll() throws SQLException {
64+
65+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
66+
67+
Statement statement = connection.createStatement();
68+
69+
return statement.executeQuery(sqlStatement);
70+
}
71+
72+
private void createTestTable() throws SQLException {
73+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
74+
" (\n" +
75+
" BigIntegerValue bigint\n" +
76+
" );";
77+
78+
Statement statement = connection.createStatement();
79+
80+
statement.execute(sqlStatement);
81+
}
82+
83+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
public class IntMappingTest extends TransactionalTestBase {
18+
19+
private class IntegerEntity extends SampleEntity<Integer> {
20+
21+
public IntegerEntity(Integer value) {
22+
super(value);
23+
}
24+
}
25+
26+
private class IntegerInsert extends SqlServerBulkInsert<IntegerEntity> {
27+
28+
public IntegerInsert() {
29+
super("dbo", "UnitTest");
30+
31+
mapInt("IntegerValue", IntegerEntity::getValue);
32+
}
33+
34+
}
35+
36+
@Override
37+
protected void onSetUpInTransaction() throws Exception {
38+
createTestTable();
39+
}
40+
41+
@Test
42+
public void bulkInsertPersonDataTest() throws SQLException {
43+
Integer IntegerValue = 3412358;
44+
// Create the Value:
45+
List<IntegerEntity> entities = Arrays.asList(new IntegerEntity(IntegerValue));
46+
// Create the BulkInserter:
47+
IntegerInsert localDateInsert = new IntegerInsert();
48+
// Now save all entities of a given stream:
49+
localDateInsert.saveAll(connection, entities.stream());
50+
// And assert all have been written to the database:
51+
ResultSet rs = getAll();
52+
// We have a Value:
53+
Assert.assertEquals(true, rs.next());
54+
// Get the Date we have written:
55+
Integer resultIntegerValue = rs.getInt("IntegerValue");
56+
// Assert both are equal:
57+
Assert.assertEquals(IntegerValue, resultIntegerValue);
58+
// Assert only one record was read:
59+
Assert.assertEquals(false, rs.next());
60+
}
61+
62+
private ResultSet getAll() throws SQLException {
63+
64+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
65+
66+
Statement statement = connection.createStatement();
67+
68+
return statement.executeQuery(sqlStatement);
69+
}
70+
71+
private void createTestTable() throws SQLException {
72+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
73+
" (\n" +
74+
" IntegerValue INT\n" +
75+
" );";
76+
77+
Statement statement = connection.createStatement();
78+
79+
statement.execute(sqlStatement);
80+
}
81+
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
public class SmallIntMappingTest extends TransactionalTestBase {
18+
19+
private class ShortEntity extends SampleEntity<Short> {
20+
21+
public ShortEntity(Short value) {
22+
super(value);
23+
}
24+
}
25+
26+
private class ShortInsert extends SqlServerBulkInsert<ShortEntity> {
27+
28+
public ShortInsert() {
29+
super("dbo", "UnitTest");
30+
31+
mapSmallInt("ShortValue", ShortEntity::getValue);
32+
}
33+
34+
}
35+
36+
@Override
37+
protected void onSetUpInTransaction() throws Exception {
38+
createTestTable();
39+
}
40+
41+
@Test
42+
public void bulkInsertPersonDataTest() throws SQLException {
43+
Short ShortValue = 3458;
44+
// Create the Value:
45+
List<ShortEntity> entities = Arrays.asList(new ShortEntity(ShortValue));
46+
// Create the BulkInserter:
47+
ShortInsert localDateInsert = new ShortInsert();
48+
// Now save all entities of a given stream:
49+
localDateInsert.saveAll(connection, entities.stream());
50+
// And assert all have been written to the database:
51+
ResultSet rs = getAll();
52+
// We have a Value:
53+
Assert.assertEquals(true, rs.next());
54+
// Get the Date we have written:
55+
Short resultShortValue = rs.getShort("ShortValue");
56+
// Assert both are equal:
57+
Assert.assertEquals(ShortValue, resultShortValue);
58+
// Assert only one record was read:
59+
Assert.assertEquals(false, rs.next());
60+
}
61+
62+
private ResultSet getAll() throws SQLException {
63+
64+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
65+
66+
Statement statement = connection.createStatement();
67+
68+
return statement.executeQuery(sqlStatement);
69+
}
70+
71+
private void createTestTable() throws SQLException {
72+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
73+
" (\n" +
74+
" ShortValue SMALLINT\n" +
75+
" );";
76+
77+
Statement statement = connection.createStatement();
78+
79+
statement.execute(sqlStatement);
80+
}
81+
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
7+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
8+
import org.junit.Assert;
9+
import org.junit.Test;
10+
11+
import java.sql.ResultSet;
12+
import java.sql.SQLException;
13+
import java.sql.Statement;
14+
import java.util.Arrays;
15+
import java.util.List;
16+
17+
public class TinyIntMappingTest extends TransactionalTestBase {
18+
19+
private class ByteEntity extends SampleEntity<Byte> {
20+
21+
public ByteEntity(Byte value) {
22+
super(value);
23+
}
24+
}
25+
26+
private class ByteInsert extends SqlServerBulkInsert<ByteEntity> {
27+
28+
public ByteInsert() {
29+
super("dbo", "UnitTest");
30+
31+
mapTinyInt("ByteValue", ByteEntity::getValue);
32+
}
33+
34+
}
35+
36+
@Override
37+
protected void onSetUpInTransaction() throws Exception {
38+
createTestTable();
39+
}
40+
41+
@Test
42+
public void bulkInsertPersonDataTest() throws SQLException {
43+
Byte ByteValue = 15;
44+
// Create the Value:
45+
List<ByteEntity> entities = Arrays.asList(new ByteEntity(ByteValue));
46+
// Create the BulkInserter:
47+
ByteInsert localDateInsert = new ByteInsert();
48+
// Now save all entities of a given stream:
49+
localDateInsert.saveAll(connection, entities.stream());
50+
// And assert all have been written to the database:
51+
ResultSet rs = getAll();
52+
// We have a Value:
53+
Assert.assertEquals(true, rs.next());
54+
// Get the Date we have written:
55+
Byte resultByteValue = rs.getByte("ByteValue");
56+
// Assert both are equal:
57+
Assert.assertEquals(ByteValue, resultByteValue);
58+
// Assert only one record was read:
59+
Assert.assertEquals(false, rs.next());
60+
}
61+
62+
private ResultSet getAll() throws SQLException {
63+
64+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
65+
66+
Statement statement = connection.createStatement();
67+
68+
return statement.executeQuery(sqlStatement);
69+
}
70+
71+
private void createTestTable() throws SQLException {
72+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
73+
" (\n" +
74+
" ByteValue TINYINT\n" +
75+
" );";
76+
77+
Statement statement = connection.createStatement();
78+
79+
statement.execute(sqlStatement);
80+
}
81+
82+
}

0 commit comments

Comments
 (0)