Skip to content

Commit 95d0e47

Browse files
author
Philipp Wagner
committed
Add Unit Tests
1 parent c246bee commit 95d0e47

12 files changed

Lines changed: 221 additions & 136 deletions

File tree

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ protected void mapNumeric(String columnName, int scale, int precision, Func2<TEn
7878
addColumn(columnName, Types.NUMERIC, precision, scale, false, propertyGetter);
7979
}
8080

81-
protected void mapDecimal(String columnName, int scale, int precision, Func2<TEntity, BigDecimal> propertyGetter)
81+
protected void mapDecimal(String columnName, int precision, int scale, Func2<TEntity, BigDecimal> propertyGetter)
8282
{
8383
// We need to scale the incoming decimal, before writing it to SQL Server:
8484
final Func2<TEntity, BigDecimal> wrapper = entity -> {
85-
BigDecimal result = propertyGetter.invoke(entity);
85+
BigDecimal result = propertyGetter
86+
.invoke(entity)
87+
.setScale(10, BigDecimal.ROUND_HALF_UP);
8688

87-
return result.setScale(scale, RoundingMode.HALF_UP);
89+
return result;
8890
};
8991

9092
addColumn(columnName, Types.DECIMAL, precision, scale, false, wrapper);

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/exceptions/BinaryWriteFailedException.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/exceptions/PgConnectionException.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/exceptions/SaveEntityFailedException.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/exceptions/ValueHandlerAlreadyRegisteredException.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

JSqlServerBulkInsert/src/main/java/de/bytefish/jsqlserverbulkinsert/exceptions/ValueHandlerNotRegisteredException.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/TransactionalTestBase.java renamed to JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/base/TransactionalTestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Philipp Wagner. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
package de.bytefish.jsqlserverbulkinsert;
4+
package de.bytefish.jsqlserverbulkinsert.test.base;
55

66

77
import org.junit.After;

JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/IntegrationTest.java renamed to JSqlServerBulkInsert/src/test/java/de/bytefish/jsqlserverbulkinsert/test/integration/IntegrationTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
// Copyright (c) Philipp Wagner. All rights reserved.
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

4-
package de.bytefish.jsqlserverbulkinsert;
4+
package de.bytefish.jsqlserverbulkinsert.test.integration;
55

6+
import de.bytefish.jsqlserverbulkinsert.test.model.Person;
7+
import de.bytefish.jsqlserverbulkinsert.SqlServerBulkInsert;
8+
import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
69
import org.junit.Assert;
710
import org.junit.Test;
811

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.BigDecimal;
12+
import java.sql.Date;
13+
import java.sql.ResultSet;
14+
import java.sql.SQLException;
15+
import java.sql.Statement;
16+
import java.time.LocalDate;
17+
import java.util.Arrays;
18+
import java.util.List;
19+
20+
public class DecimalMappingTest extends TransactionalTestBase {
21+
22+
private class BigDecimalEntity {
23+
24+
private final BigDecimal value;
25+
26+
public BigDecimalEntity(BigDecimal value) {
27+
this.value = value;
28+
}
29+
30+
public BigDecimal getValue() {
31+
return value;
32+
}
33+
}
34+
35+
private class BigDecimalInsert extends SqlServerBulkInsert<BigDecimalEntity> {
36+
37+
public BigDecimalInsert() {
38+
super("dbo", "UnitTest");
39+
40+
mapDecimal("DecimalValue", 20, 10, BigDecimalEntity::getValue);
41+
}
42+
43+
}
44+
45+
@Override
46+
protected void onSetUpInTransaction() throws Exception {
47+
createTestTable();
48+
}
49+
50+
@Test
51+
public void bulkInsertPersonDataTest() throws SQLException {
52+
// Expected LocalDate 2013/01/01:
53+
BigDecimal bigDecimal = new BigDecimal("100.123");
54+
// Create te
55+
List<BigDecimalEntity> entities = Arrays.asList(new BigDecimalEntity(bigDecimal));
56+
// Create the BulkInserter:
57+
BigDecimalInsert localDateInsert = new BigDecimalInsert();
58+
// Now save all entities of a given stream:
59+
localDateInsert.saveAll(connection, entities.stream());
60+
// And assert all have been written to the database:
61+
ResultSet rs = getAll();
62+
// We have a Value:
63+
Assert.assertEquals(true, rs.next());
64+
// Get the Date we have written:
65+
BigDecimal resultBigDecimal = rs.getBigDecimal("DecimalValue");
66+
// Assert both are equal:
67+
Assert.assertEquals(bigDecimal.stripTrailingZeros(), resultBigDecimal.stripTrailingZeros());
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+
" DecimalValue NUMERIC(20, 10)\n" +
85+
" );";
86+
87+
Statement statement = connection.createStatement();
88+
89+
statement.execute(sqlStatement);
90+
}
91+
92+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 de.bytefish.jsqlserverbulkinsert.test.integration.IntegrationTest;
9+
import de.bytefish.jsqlserverbulkinsert.test.model.Person;
10+
import org.junit.Assert;
11+
import org.junit.Test;
12+
13+
import java.sql.Date;
14+
import java.sql.ResultSet;
15+
import java.sql.SQLException;
16+
import java.sql.Statement;
17+
import java.time.LocalDate;
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
public class LocalDateMappingTest extends TransactionalTestBase {
22+
23+
private class LocalDateEntity {
24+
25+
private LocalDate localDate;
26+
27+
private LocalDateEntity(LocalDate localDate) {
28+
this.localDate = localDate;
29+
}
30+
31+
public LocalDate getLocalDate() {
32+
return localDate;
33+
}
34+
}
35+
36+
private class LocalDateInsert extends SqlServerBulkInsert<LocalDateEntity> {
37+
38+
public LocalDateInsert() {
39+
super("dbo", "UnitTest");
40+
41+
mapDate("LocalDate", LocalDateEntity::getLocalDate);
42+
}
43+
44+
}
45+
46+
@Override
47+
protected void onSetUpInTransaction() throws Exception {
48+
createTestTable();
49+
}
50+
51+
@Test
52+
public void bulkInsertPersonDataTest() throws SQLException {
53+
// Expected LocalDate 2013/01/01:
54+
LocalDate localDate = LocalDate.of(2013, 1, 1);
55+
// Create te
56+
List<LocalDateEntity> persons = Arrays.asList(new LocalDateEntity(localDate));
57+
// Create the BulkInserter:
58+
LocalDateInsert localDateInsert = new LocalDateInsert();
59+
// Now save all entities of a given stream:
60+
localDateInsert.saveAll(connection, persons.stream());
61+
// And assert all have been written to the database:
62+
ResultSet rs = getAll();
63+
64+
while (rs.next()) {
65+
66+
// Get the Date we have written:
67+
Date date = rs.getDate("LocalDate");
68+
69+
// We should have a date:
70+
Assert.assertNotNull(date);
71+
72+
// Get the LocalDate:
73+
LocalDate resultDate = date.toLocalDate();
74+
75+
Assert.assertEquals(localDate.getYear(), resultDate.getYear());
76+
Assert.assertEquals(localDate.getMonthValue(), resultDate.getMonthValue());
77+
Assert.assertEquals(localDate.getDayOfMonth(), resultDate.getDayOfMonth());
78+
}
79+
}
80+
81+
private ResultSet getAll() throws SQLException {
82+
83+
String sqlStatement = "SELECT * FROM dbo.UnitTest";
84+
85+
Statement statement = connection.createStatement();
86+
87+
return statement.executeQuery(sqlStatement);
88+
}
89+
90+
private void createTestTable() throws SQLException {
91+
String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
92+
" (\n" +
93+
" LocalDate DATE\n" +
94+
" );";
95+
96+
Statement statement = connection.createStatement();
97+
98+
statement.execute(sqlStatement);
99+
}
100+
101+
}

0 commit comments

Comments
 (0)