Merge pull request #35 from baztian/bugfix/ancient-date
Don't fail on dates before 1900 on Python < 3master
commit
ef7ad4effa
|
|
@ -159,6 +159,9 @@ Changelog
|
||||||
=========
|
=========
|
||||||
|
|
||||||
- Next version - unreleased
|
- Next version - unreleased
|
||||||
|
|
||||||
|
- Don't fail on dates before 1900 on Python < 3.
|
||||||
|
|
||||||
- 1.1.0 - 2017-03-19
|
- 1.1.0 - 2017-03-19
|
||||||
|
|
||||||
- Support BIT and TINYINT type mappings (thanks @Mokubyow for
|
- Support BIT and TINYINT type mappings (thanks @Mokubyow for
|
||||||
|
|
|
||||||
|
|
@ -595,8 +595,12 @@ def _to_date(rs, col):
|
||||||
java_val = rs.getDate(col)
|
java_val = rs.getDate(col)
|
||||||
if not java_val:
|
if not java_val:
|
||||||
return
|
return
|
||||||
d = datetime.datetime.strptime(str(java_val)[:10], "%Y-%m-%d")
|
# The following code requires Python 3.3+ on dates before year 1900.
|
||||||
return d.strftime("%Y-%m-%d")
|
# d = datetime.datetime.strptime(str(java_val)[:10], "%Y-%m-%d")
|
||||||
|
# return d.strftime("%Y-%m-%d")
|
||||||
|
# Workaround / simpler soltution (see
|
||||||
|
# https://github.com/baztian/jaydebeapi/issues/18):
|
||||||
|
return str(java_val)[:10]
|
||||||
|
|
||||||
def _to_binary(rs, col):
|
def _to_binary(rs, col):
|
||||||
java_val = rs.getObject(col)
|
java_val = rs.getObject(col)
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,11 @@
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.7</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.7</maven.compiler.target>
|
||||||
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
|
|
|
||||||
|
|
@ -2,55 +2,23 @@ package org.jaydebeapi.mockdriver;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
|
import java.sql.Date;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.ResultSetMetaData;
|
import java.sql.ResultSetMetaData;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Types;
|
import java.sql.Types;
|
||||||
|
import java.util.Calendar;
|
||||||
import org.mockito.Mockito;
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
public abstract class MockConnection implements Connection {
|
public abstract class MockConnection implements Connection {
|
||||||
|
|
||||||
ResultSet mockResultSet;
|
ResultSet mockResultSet;
|
||||||
|
|
||||||
public final void mockExceptionOnCommit(String className, String exceptionMessage) throws SQLException {
|
|
||||||
Throwable exception = createException(className, exceptionMessage);
|
|
||||||
Mockito.doThrow(exception).when(this).commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void mockExceptionOnRollback(String className, String exceptionMessage) throws SQLException {
|
|
||||||
Throwable exception = createException(className, exceptionMessage);
|
|
||||||
Mockito.doThrow(exception).when(this).rollback();
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void mockExceptionOnExecute(String className, String exceptionMessage) throws SQLException {
|
|
||||||
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
|
|
||||||
Throwable exception = createException(className, exceptionMessage);
|
|
||||||
Mockito.when(mockPreparedStatement.execute()).thenThrow(exception);
|
|
||||||
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void mockType(String sqlTypesName) throws SQLException {
|
|
||||||
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
|
|
||||||
Mockito.when(mockPreparedStatement.execute()).thenReturn(true);
|
|
||||||
mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for type " + sqlTypesName + ")");
|
|
||||||
Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet);
|
|
||||||
Mockito.when(mockResultSet.next()).thenReturn(true);
|
|
||||||
ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class);
|
|
||||||
Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData);
|
|
||||||
Mockito.when(mockMetaData.getColumnCount()).thenReturn(1);
|
|
||||||
int sqlTypeCode = extractTypeCodeForName(sqlTypesName);
|
|
||||||
Mockito.when(mockMetaData.getColumnType(1)).thenReturn(sqlTypeCode);
|
|
||||||
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
|
|
||||||
}
|
|
||||||
|
|
||||||
public final ResultSet verifyResultSet() {
|
|
||||||
return Mockito.verify(mockResultSet);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Throwable createException(String className, String exceptionMessage) {
|
private static Throwable createException(String className, String exceptionMessage) {
|
||||||
try {
|
try {
|
||||||
return (Throwable) Class.forName(className).getConstructor(String.class).newInstance(exceptionMessage);
|
return (Throwable) Class.forName(className).getConstructor(String.class)
|
||||||
|
.newInstance(exceptionMessage);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("Couldn't initialize class " + className + ".", e);
|
throw new RuntimeException("Couldn't initialize class " + className + ".", e);
|
||||||
}
|
}
|
||||||
|
|
@ -71,4 +39,61 @@ public abstract class MockConnection implements Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void mockExceptionOnCommit(String className, String exceptionMessage)
|
||||||
|
throws SQLException {
|
||||||
|
Throwable exception = createException(className, exceptionMessage);
|
||||||
|
Mockito.doThrow(exception).when(this).commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void mockExceptionOnRollback(String className, String exceptionMessage)
|
||||||
|
throws SQLException {
|
||||||
|
Throwable exception = createException(className, exceptionMessage);
|
||||||
|
Mockito.doThrow(exception).when(this).rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void mockExceptionOnExecute(String className, String exceptionMessage)
|
||||||
|
throws SQLException {
|
||||||
|
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
|
||||||
|
Throwable exception = createException(className, exceptionMessage);
|
||||||
|
Mockito.when(mockPreparedStatement.execute()).thenThrow(exception);
|
||||||
|
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void mockDateResult(int year, int month, int day) throws SQLException {
|
||||||
|
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
|
||||||
|
Mockito.when(mockPreparedStatement.execute()).thenReturn(true);
|
||||||
|
mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for date)");
|
||||||
|
Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet);
|
||||||
|
Mockito.when(mockResultSet.next()).thenReturn(true);
|
||||||
|
ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class);
|
||||||
|
Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData);
|
||||||
|
Mockito.when(mockMetaData.getColumnCount()).thenReturn(1);
|
||||||
|
Calendar cal = Calendar.getInstance();
|
||||||
|
cal.clear();
|
||||||
|
cal.set(Calendar.YEAR, year);
|
||||||
|
cal.set(Calendar.MONTH, month - 1);
|
||||||
|
cal.set(Calendar.DAY_OF_MONTH, day);
|
||||||
|
Date ancientDate = new Date(cal.getTime().getTime());
|
||||||
|
Mockito.when(mockResultSet.getDate(1)).thenReturn(ancientDate);
|
||||||
|
Mockito.when(mockMetaData.getColumnType(1)).thenReturn(Types.DATE);
|
||||||
|
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void mockType(String sqlTypesName) throws SQLException {
|
||||||
|
PreparedStatement mockPreparedStatement = Mockito.mock(PreparedStatement.class);
|
||||||
|
Mockito.when(mockPreparedStatement.execute()).thenReturn(true);
|
||||||
|
mockResultSet = Mockito.mock(ResultSet.class, "ResultSet(for type " + sqlTypesName + ")");
|
||||||
|
Mockito.when(mockPreparedStatement.getResultSet()).thenReturn(mockResultSet);
|
||||||
|
Mockito.when(mockResultSet.next()).thenReturn(true);
|
||||||
|
ResultSetMetaData mockMetaData = Mockito.mock(ResultSetMetaData.class);
|
||||||
|
Mockito.when(mockResultSet.getMetaData()).thenReturn(mockMetaData);
|
||||||
|
Mockito.when(mockMetaData.getColumnCount()).thenReturn(1);
|
||||||
|
int sqlTypeCode = extractTypeCodeForName(sqlTypesName);
|
||||||
|
Mockito.when(mockMetaData.getColumnType(1)).thenReturn(sqlTypeCode);
|
||||||
|
Mockito.when(this.prepareStatement(Mockito.anyString())).thenReturn(mockPreparedStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
public final ResultSet verifyResultSet() {
|
||||||
|
return Mockito.verify(mockResultSet);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,13 @@ class MockTest(unittest.TestCase):
|
||||||
'getObject'))
|
'getObject'))
|
||||||
verify_get(1)
|
verify_get(1)
|
||||||
|
|
||||||
|
def test_ancient_date_mapped(self):
|
||||||
|
self.conn.jconn.mockDateResult(1899, 12, 31)
|
||||||
|
cursor = self.conn.cursor()
|
||||||
|
cursor.execute("dummy stmt")
|
||||||
|
result = cursor.fetchone()
|
||||||
|
self.assertEquals(result[0], "1899-12-31")
|
||||||
|
|
||||||
def test_sql_exception_on_execute(self):
|
def test_sql_exception_on_execute(self):
|
||||||
self.conn.jconn.mockExceptionOnExecute("java.sql.SQLException", "expected")
|
self.conn.jconn.mockExceptionOnExecute("java.sql.SQLException", "expected")
|
||||||
cursor = self.conn.cursor()
|
cursor = self.conn.cursor()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue