1- //// Copyright (c) Philipp Wagner and Victor Lee. 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.mapping.AbstractMapping;
8- //import de.bytefish.jsqlserverbulkinsert.test.base.TransactionalTestBase;
9- //import org.junit.Assert;
10- //import org.junit.Test;
11- //
12- //import java.sql.*;
13- //import java.time.LocalDateTime;
14- //import java.util.Arrays;
15- //import java.util.List;
16- //
17- //public class UTCNanoTest extends TransactionalTestBase {
18- //
19- // private class TimestampEntity {
20- //
21- // private Long utcNano;
22- //
23- // private TimestampEntity(Long localDate) {
24- // this.utcNano = localDate;
25- // }
26- //
27- // public Long getUtcNano() {
28- // return utcNano;
29- // }
30- // }
31- //
32- // private class LocalDateEntityMapping extends AbstractMapping<TimestampEntity> {
33- //
34- // public LocalDateEntityMapping() {
35- // super("dbo", "UnitTest");
36- //
37- // mapUTCNano("utcnanocolumn", TimestampEntity::getUtcNano);
38- // }
39- // }
40- //
41- // @Override
42- // protected void onSetUpInTransaction() throws Exception {
43- // createTestTable();
44- // }
45- //
46- // @Test
47- // public void bulkInsertUTCTest() throws SQLException {
48- // // Expected LocalDate 2017-05-15T12:09:07.161013600
49- // long utcNanos = 1494850147161013648L;
50- // // Create entities
51- // List<TimestampEntity> entities = Arrays.asList(new TimestampEntity(utcNanos));
52- // // Create the BulkInserter:
53- // LocalDateEntityMapping mapping = new LocalDateEntityMapping();
54- // // Now save all entities of a given stream:
55- // new SqlServerBulkInsert<>(mapping).saveAll(connection, entities.stream());
56- //
57- // // And assert all have been written to the database:
58- // ResultSet rs = getAll();
59- // while (rs.next()) {
60- // // for debugging purposes, can look at how the dates are stored in the DB
61- // String dbDate = rs.getString("utcnanocolumn");
62- // // Get the Date we have written:
63- // Timestamp timeStampResult = rs.getTimestamp("utcnanocolumn");
64- // LocalDateTime date = timeStampResult.toLocalDateTime();
65- //
66- // // We should have a date:
67- // Assert.assertNotNull(date);
68- //
69- // Assert.assertEquals("2017-05-15 12:09:07.1610136", dbDate);
70- // Assert.assertEquals("2017-05-15 12:09:07.1610136", timeStampResult.toString());
71- // Assert.assertEquals("2017-05-15T12:09:07.161013600", date.toString());
72- // }
73- // }
74- //
75- // @Test
76- // public void bulkInsertNullTest() throws SQLException {
77- // // Expected: null
78- // Long utcNanos = null;
79- // // Create entities
80- // List<TimestampEntity> entities = Arrays.asList(new TimestampEntity(utcNanos));
81- // // Create the BulkInserter:
82- // LocalDateEntityMapping mapping = new LocalDateEntityMapping();
83- // // Now save all entities of a given stream:
84- // new SqlServerBulkInsert<>(mapping).saveAll(connection, entities.stream());
85- //
86- // // And assert all have been written to the database:
87- // ResultSet rs = getAll();
88- // while (rs.next()) {
89- // // for debugging purposes, can look at how the dates are stored in the DB
90- // String dbDate = rs.getString("utcnanocolumn");
91- // // Get the Date we have written:
92- // Timestamp timeStampResult = rs.getTimestamp("utcnanocolumn");
93- // Timestamp date = timeStampResult;
94- //
95- // Assert.assertEquals(null, dbDate);
96- // Assert.assertEquals(null, timeStampResult);
97- // Assert.assertEquals(null, date);
98- // }
99- // }
100- //
101- // private ResultSet getAll() throws SQLException {
102- //
103- // String sqlStatement = "SELECT * FROM dbo.UnitTest";
104- //
105- // Statement statement = connection.createStatement();
106- //
107- // return statement.executeQuery(sqlStatement);
108- // }
109- //
110- // private void createTestTable() throws SQLException {
111- // String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n" +
112- // " (\n" +
113- // " utcnanocolumn datetime2\n" +
114- // " );";
115- //
116- // Statement statement = connection.createStatement();
117- //
118- // statement.execute(sqlStatement);
119- // }
120- //}
1+ // Copyright (c) Philipp Wagner and Victor Lee. 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 .extensions .DateTimeExtensions ;
8+ import de .bytefish .jsqlserverbulkinsert .mapping .AbstractMapping ;
9+ import de .bytefish .jsqlserverbulkinsert .test .base .TransactionalTestBase ;
10+ import org .junit .Assert ;
11+ import org .junit .Test ;
12+
13+ import java .sql .*;
14+ import java .time .LocalDateTime ;
15+ import java .util .Arrays ;
16+ import java .util .List ;
17+
18+ public class UTCNanoTest extends TransactionalTestBase {
19+
20+ private class TimestampEntity {
21+
22+ private Long utcNano ;
23+
24+ private TimestampEntity (Long localDate ) {
25+ this .utcNano = localDate ;
26+ }
27+
28+ public Long getUtcNano () {
29+ return utcNano ;
30+ }
31+ }
32+
33+ private class LocalDateEntityMapping extends AbstractMapping <TimestampEntity > {
34+
35+ public LocalDateEntityMapping () {
36+ super ("dbo" , "UnitTest" );
37+
38+ DateTimeExtensions .mapUTCNano (this , "utcnanocolumn" , TimestampEntity ::getUtcNano );
39+ }
40+ }
41+
42+ @ Override
43+ protected void onSetUpInTransaction () throws Exception {
44+ createTestTable ();
45+ }
46+
47+ @ Test
48+ public void bulkInsertUTCTest () throws SQLException {
49+ // Expected LocalDate 2017-05-15T12:09:07.161013600
50+ long utcNanos = 1494850147161013648L ;
51+ // Create entities
52+ List <TimestampEntity > entities = Arrays .asList (new TimestampEntity (utcNanos ));
53+ // Create the BulkInserter:
54+ LocalDateEntityMapping mapping = new LocalDateEntityMapping ();
55+ // Now save all entities of a given stream:
56+ new SqlServerBulkInsert <>(mapping ).saveAll (connection , entities .stream ());
57+
58+ // And assert all have been written to the database:
59+ ResultSet rs = getAll ();
60+ while (rs .next ()) {
61+ // for debugging purposes, can look at how the dates are stored in the DB
62+ String dbDate = rs .getString ("utcnanocolumn" );
63+ // Get the Date we have written:
64+ Timestamp timeStampResult = rs .getTimestamp ("utcnanocolumn" );
65+ LocalDateTime date = timeStampResult .toLocalDateTime ();
66+
67+ // We should have a date:
68+ Assert .assertNotNull (date );
69+
70+ Assert .assertEquals ("2017-05-15 12:09:07.1610136" , dbDate );
71+ Assert .assertEquals ("2017-05-15 12:09:07.1610136" , timeStampResult .toString ());
72+ Assert .assertEquals ("2017-05-15T12:09:07.161013600" , date .toString ());
73+ }
74+ }
75+
76+ @ Test
77+ public void bulkInsertNullTest () throws SQLException {
78+ // Expected: null
79+ Long utcNanos = null ;
80+ // Create entities
81+ List <TimestampEntity > entities = Arrays .asList (new TimestampEntity (utcNanos ));
82+ // Create the BulkInserter:
83+ LocalDateEntityMapping mapping = new LocalDateEntityMapping ();
84+ // Now save all entities of a given stream:
85+ new SqlServerBulkInsert <>(mapping ).saveAll (connection , entities .stream ());
86+
87+ // And assert all have been written to the database:
88+ ResultSet rs = getAll ();
89+ while (rs .next ()) {
90+ // for debugging purposes, can look at how the dates are stored in the DB
91+ String dbDate = rs .getString ("utcnanocolumn" );
92+ // Get the Date we have written:
93+ Timestamp timeStampResult = rs .getTimestamp ("utcnanocolumn" );
94+ Timestamp date = timeStampResult ;
95+
96+ Assert .assertEquals (null , dbDate );
97+ Assert .assertEquals (null , timeStampResult );
98+ Assert .assertEquals (null , date );
99+ }
100+ }
101+
102+ private ResultSet getAll () throws SQLException {
103+
104+ String sqlStatement = "SELECT * FROM dbo.UnitTest" ;
105+
106+ Statement statement = connection .createStatement ();
107+
108+ return statement .executeQuery (sqlStatement );
109+ }
110+
111+ private void createTestTable () throws SQLException {
112+ String sqlStatement = "CREATE TABLE [dbo].[UnitTest]\n " +
113+ " (\n " +
114+ " utcnanocolumn datetime2\n " +
115+ " );" ;
116+
117+ Statement statement = connection .createStatement ();
118+
119+ statement .execute (sqlStatement );
120+ }
121+ }
0 commit comments