Skip to content

Commit fef6a6d

Browse files
author
Philipp Wagner
committed
Add a String and Double Mapping Test
1 parent 6bdd4b2 commit fef6a6d

3 files changed

Lines changed: 171 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected void mapBoolean(String columnName, Func2<TEntity, Boolean> propertyGet
7373
addColumn(columnName, Types.BIT, propertyGetter);
7474
}
7575

76-
protected void mapNumeric(String columnName, int scale, int precision, Func2<TEntity, BigDecimal> propertyGetter) {
76+
protected void mapNumeric(String columnName, int precision, int scale, Func2<TEntity, BigDecimal> propertyGetter) {
7777
// We need to scale the incoming decimal, before writing it to SQL Server:
7878
final Func2<TEntity, BigDecimal> wrapper = entity -> {
7979
BigDecimal result = propertyGetter
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 DoubleMappingTest extends TransactionalTestBase {
18+
19+
private class DoubleEntity extends SampleEntity<Double> {
20+
21+
public DoubleEntity(Double value) {
22+
super(value);
23+
}
24+
}
25+
26+
private class BigDecimalInsert extends SqlServerBulkInsert<DoubleEntity> {
27+
28+
public BigDecimalInsert() {
29+
super("dbo", "UnitTest");
30+
31+
mapDouble("DoubleValue", DoubleEntity::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+
Double doubleValue = 123.123d;
44+
// Create the Value:
45+
List<DoubleEntity> entities = Arrays.asList(new DoubleEntity(doubleValue));
46+
// Create the BulkInserter:
47+
BigDecimalInsert localDateInsert = new BigDecimalInsert();
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+
Double resultDoubleValue = rs.getDouble("DoubleValue");
56+
// Assert both are equal:
57+
Assert.assertEquals(doubleValue, resultDoubleValue, 1e-10);
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+
" DoubleValue FLOAT\n" +
75+
" );";
76+
77+
Statement statement = connection.createStatement();
78+
79+
statement.execute(sqlStatement);
80+
}
81+
82+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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 StringMappingTest extends TransactionalTestBase {
18+
19+
private class StringEntity {
20+
21+
private final String value;
22+
23+
public StringEntity(String value) {
24+
this.value = value;
25+
}
26+
27+
public String getValue() {
28+
return value;
29+
}
30+
}
31+
32+
private class StringInsert extends SqlServerBulkInsert<StringEntity> {
33+
34+
public StringInsert() {
35+
super("dbo", "UnitTest");
36+
37+
mapString("StringValue", StringEntity::getValue);
38+
}
39+
40+
}
41+
42+
@Override
43+
protected void onSetUpInTransaction() throws Exception {
44+
createTestTable();
45+
}
46+
47+
@Test
48+
public void bulkInsertPersonDataTest() throws SQLException {
49+
String stringData = "Halli Hallo Hallöchen";
50+
// Create te
51+
List<StringEntity> entities = Arrays.asList(new StringEntity(stringData));
52+
// Create the BulkInserter:
53+
StringInsert localDateInsert = new StringInsert();
54+
// Now save all entities of a given stream:
55+
localDateInsert.saveAll(connection, entities.stream());
56+
// And assert all have been written to the database:
57+
ResultSet rs = getAll();
58+
// We have a Value:
59+
Assert.assertEquals(true, rs.next());
60+
// Get the Date we have written:
61+
String resultString = rs.getString("StringValue");
62+
// Assert both are equal:
63+
Assert.assertEquals(stringData, resultString);
64+
// Assert only one record was read:
65+
Assert.assertEquals(false, rs.next());
66+
}
67+
68+
private ResultSet getAll() throws SQLException {
69+
70+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
71+
72+
Statement statement = connection.createStatement();
73+
74+
return statement.executeQuery(sqlStatement);
75+
}
76+
77+
private void createTestTable() throws SQLException {
78+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
79+
" (\n" +
80+
" StringValue NVARCHAR(255)\n" +
81+
" );";
82+
83+
Statement statement = connection.createStatement();
84+
85+
statement.execute(sqlStatement);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)