1111import org .junit .BeforeClass ;
1212import org .junit .ClassRule ;
1313import org .junit .Test ;
14+ import org .junit .experimental .runners .Enclosed ;
15+ import org .junit .runner .RunWith ;
1416import org .neo4j .test .rule .DbmsRule ;
1517import org .neo4j .test .rule .ImpermanentDbmsRule ;
1618
17- public class MySQLJdbcTest extends AbstractJdbcTest {
19+ import java .time .LocalDate ;
20+ import java .time .LocalDateTime ;
21+ import java .time .LocalTime ;
22+ import java .time .ZonedDateTime ;
23+ import java .util .Map ;
1824
19- @ ClassRule
20- public static MySQLContainerExtension mysql = new MySQLContainerExtension ();
25+ import static apoc .util .TestUtil .testCall ;
26+ import static org .junit .Assert .assertEquals ;
27+ import static org .junit .Assert .assertTrue ;
2128
22- @ ClassRule
23- public static DbmsRule db = new ImpermanentDbmsRule ();
29+ @ RunWith (Enclosed .class )
30+ public class MySQLJdbcTest extends AbstractJdbcTest {
31+
32+ public static class MySQLJdbcLatestVersionTest {
33+
34+ @ ClassRule
35+ public static MySQLContainerExtension mysql = new MySQLContainerExtension ("mysql:8.0.31" );
36+
37+ @ ClassRule
38+ public static DbmsRule db = new ImpermanentDbmsRule ();
39+
40+ @ BeforeClass
41+ public static void setUpContainer () {
42+ mysql .start ();
43+ TestUtil .registerProcedure (db , Jdbc .class );
44+ }
45+ @ AfterClass
46+ public static void tearDown () {
47+ mysql .stop ();
48+ db .shutdown ();
49+ }
50+ @ Test
51+ public void testLoadJdbc () {
52+ MySQLJdbcTest .testLoadJdbc (db , mysql );
53+ }
2454
25- @ BeforeClass
26- public static void setUpContainer () {
27- mysql . start ( );
28- TestUtil . registerProcedure ( db , Jdbc . class );
55+ @ Test
56+ public void testIssue3496 () {
57+ MySQLJdbcTest . testIssue3496 ( db , mysql );
58+ }
2959 }
60+
61+ public static class MySQLJdbcFiveVersionTest {
62+
63+ @ ClassRule
64+ public static MySQLContainerExtension mysql = new MySQLContainerExtension ("mysql:5.7" );
65+
66+ @ ClassRule
67+ public static DbmsRule db = new ImpermanentDbmsRule ();
3068
31- @ AfterClass
32- public static void tearDown () {
33- mysql .stop ();
34- db .shutdown ();
69+ @ BeforeClass
70+ public static void setUpContainer () {
71+ mysql .start ();
72+ TestUtil .registerProcedure (db , Jdbc .class );
73+ }
74+
75+ @ AfterClass
76+ public static void tearDown () {
77+ mysql .stop ();
78+ db .shutdown ();
79+ }
80+
81+ @ Test
82+ public void testLoadJdbc () {
83+ MySQLJdbcTest .testLoadJdbc (db , mysql );
84+ }
85+
86+ @ Test
87+ public void testIssue3496 () {
88+ MySQLJdbcTest .testIssue3496 (db , mysql );
89+ }
3590 }
3691
37- @ Test
38- public void testLoadJdbc () {
39- testCall (
40- db ,
41- "CALL apoc.load.jdbc($url, $table, [])" ,
42- Util .map ("url" , mysql .getJdbcUrl (), "table" , "country" ),
92+ private static void testLoadJdbc (DbmsRule db , MySQLContainerExtension mysql ) {
93+ // with the config {timezone: 'UTC'} and `preserveInstants=true&connectionTimeZone=SERVER` to make the result deterministic,
94+ // since `TIMESTAMP` values are automatically converted from the session time zone to UTC for storage, and vice versa.
95+ testCall (db , "CALL apoc.load.jdbc($url, $table, [], {timezone: 'UTC'})" ,
96+ Util .map (
97+ "url" , mysql .getJdbcUrl () + "&preserveInstants=true&connectionTimeZone=SERVER" ,
98+ "table" , "country" ),
4399 row -> {
44100 Map <String , Object > expected = Util .map (
45101 "Code" , "NLD" ,
@@ -56,8 +112,36 @@ public void testLoadJdbc() {
56112 "GovernmentForm" , "Constitutional Monarchy" ,
57113 "HeadOfState" , "Beatrix" ,
58114 "Capital" , 5 ,
59- "Code2" , "NL" );
60- assertEquals (expected , row .get ("row" ));
115+ "Code2" , "NL" ,
116+ "myTime" , LocalTime .of (1 , 0 , 0 ),
117+ "myTimeStamp" , ZonedDateTime .parse ("2003-01-01T01:00Z" ),
118+ "myDate" , LocalDate .parse ("2003-01-01" ),
119+ "myYear" , LocalDate .parse ("2003-01-01" )
120+ );
121+ Map actual = (Map ) row .get ("row" );
122+ Object myDateTime = actual .remove ("myDateTime" );
123+ assertTrue (myDateTime instanceof LocalDateTime );
124+ assertEquals (expected , actual );
125+ });
126+ }
127+
128+ private static void testIssue3496 (DbmsRule db , MySQLContainerExtension mysql ) {
129+ testCall (db , "CALL apoc.load.jdbc($url,'SELECT DATE(NOW()), NOW(), CURDATE(), CURTIME(), UTC_DATE(), UTC_TIME(), UTC_TIMESTAMP(), DATE(UTC_TIMESTAMP());')" ,
130+ Util .map ("url" , mysql .getJdbcUrl ()),
131+ r -> {
132+ Map row = (Map ) r .get ("row" );
133+ assertEquals (8 , row .size ());
134+
135+ assertTrue (row .get ("UTC_DATE()" ) instanceof LocalDate );
136+ assertTrue (row .get ("CURDATE()" ) instanceof LocalDate );
137+
138+ assertTrue (row .get ("UTC_TIMESTAMP()" ) instanceof LocalDateTime );
139+ assertTrue (row .get ("NOW()" ) instanceof LocalDateTime );
140+ assertTrue (row .get ("DATE(UTC_TIMESTAMP())" ) instanceof LocalDate );
141+ assertTrue (row .get ("DATE(NOW())" ) instanceof LocalDate );
142+
143+ assertTrue (row .get ("CURTIME()" ) instanceof LocalTime );
144+ assertTrue (row .get ("UTC_TIME()" ) instanceof LocalTime );
61145 });
62146 }
63- }
147+ }
0 commit comments