def get_field_from_value(self, value: Any) -> Field: if type(value).__name__.endswith('UUID'): return Field(stringValue=str(value)) elif type(value).__name__.endswith('PGobject'): return Field(stringValue=str(value)) elif type(value).__name__.endswith('PgArray'): return Field(stringValue=str(value)) else: return super().get_field_from_value(value)
def get_filed_from_jdbc_type(self, value: Any, jdbc_type: Optional[int]) -> Field: type_ = type(value) if type_.__name__.endswith('UUID'): return Field(stringValue=str(value)) elif type_.__name__.endswith('PGobject'): return Field(stringValue=str(value)) elif type_.__name__.endswith('PgArray'): return Field(stringValue=str(value)) else: return super().get_filed_from_jdbc_type(value, jdbc_type)
def get_filed_from_jdbc_type(self, value: Any, jdbc_type: Optional[int]) -> Field: type_ = type(value) for pg_type in PG_TYPES: if type_.__name__.endswith(pg_type): return Field(stringValue=str(value)) return super().get_filed_from_jdbc_type(value, jdbc_type)
def test_from_value(mocker) -> None: connection_mock = mocker.Mock() dummy = PostgreSQLJDBC(connection_mock) class JavaUUID: def __init__(self, val: str): self._val: str = val def __str__(self) -> str: return self._val uuid = 'e9e1df6b-c6d3-4a34-9227-c27056d596c6' assert dummy.get_filed_from_jdbc_type(JavaUUID(uuid), None) == Field(stringValue=uuid) class PGobject: def __init__(self, val: str): self._val: str = val def __str__(self) -> str: return self._val assert dummy.get_filed_from_jdbc_type(PGobject("{}"), None) == Field(stringValue="{}") class PgArray: def __init__(self, val: str): self._val: str = val def __str__(self) -> str: return self._val assert dummy.get_filed_from_jdbc_type( PgArray("{ITEM1,ITEM2}"), None) == Field(stringValue="{ITEM1,ITEM2}") class PGpoint: def __init__(self, val: str): self._val: str = val def __str__(self) -> str: return self._val assert dummy.get_filed_from_jdbc_type( PGpoint("(50.074534,14.444137)"), None) == Field(stringValue="(50.074534,14.444137)") helper_default_test_field(dummy)
def get_filed_from_jdbc_type(self, value: Any, jdbc_type: Optional[int]) -> Field: type_: Optional[JDBCType] = None if jdbc_type: try: type_ = JDBCType(jdbc_type) except ValueError: pass if type_: if value is None: return Field(isNull=True) elif type_ in LONG: return Field(longValue=value) elif type_ in DOUBLE: return Field(doubleValue=value) elif type_ in STRING: return Field(stringValue=value) elif type_ in BOOLEAN: return Field(booleanValue=value) elif type_ in TIMESTAMP: return Field(stringValue=self._format_datetime(value)) elif type_ in BLOB: if isinstance(value, str): # pragma: no cover value = value.encode() return Field(blobValue=b64encode(value)) return self.get_field_from_value(value)
def test_from_value() -> None: assert Field.from_value('str') == Field(stringValue='str') assert Field.from_value(123) == Field(longValue=123) assert Field.from_value(1.23) == Field(doubleValue=1.23) assert Field.from_value(True) == Field(booleanValue=True) assert Field.from_value(False) == Field(booleanValue=False) assert Field.from_value(b'bytes') == Field(blobValue=b64encode(b'bytes')) assert Field.from_value(None) == Field(isNull=True) class JavaUUID: def __init__(self, val: str): self._val: str = val def __str__(self) -> str: return self._val uuid = 'e9e1df6b-c6d3-4a34-9227-c27056d596c6' assert Field.from_value(JavaUUID(uuid)) == Field(stringValue=uuid) class PGobject: def __init__(self, val: str): self._val: str = val def __str__(self) -> str: return self._val assert Field.from_value(PGobject("{}")) == Field(stringValue="{}") class BigInteger: def __init__(self, val: int): self._val: int = val def __str__(self) -> int: return self._val assert Field.from_value(BigInteger("55")) == Field(longValue=55) class Dummy: pass with pytest.raises(Exception): Field.from_value(Dummy())
def get_filed_from_jdbc_type(self, value: Any, jdbc_type: Optional[int]) -> Field: type_: Optional[JDBCType] = None if jdbc_type: try: type_ = JDBCType(jdbc_type) except ValueError: pass if type_: if type_ in LONG: return Field(longValue=value) elif type_ in DOUBLE: return Field(doubleValue=value) elif type_ in STRING: return Field(stringValue=value) elif type_ in BOOLEAN: return Field(booleanValue=value) elif type_ in BLOB: return Field(blobValue=b64encode(value)) return self.get_field_from_value(value)
def test_from_value(mocker) -> None: connection_mock = mocker.Mock() dummy = MySQLJDBC(connection_mock) class BigInteger: def __init__(self, val: int): self._val: int = val def __str__(self) -> int: return self._val assert dummy.get_filed_from_jdbc_type(BigInteger("55"), None) == Field(longValue=55) helper_default_test_field(dummy)
def test_execute_insert_with_generated(clear, mocker): connection_mock = mocker.Mock() cursor_mock = mocker.Mock() connection_mock.cursor.side_effect = [cursor_mock] cursor_mock.description = '' cursor_mock.rowcount = 1 cursor_mock.lastrowid = 1 dummy = DummyResource(connection_mock) assert dummy.execute("insert into users ('name') values ('abc')" ) == ExecuteStatementResponse( numberOfRecordsUpdated=1, generatedFields=[Field(longValue=1)]) cursor_mock.execute.assert_called_once_with( "insert into users ('name') values ('abc')") cursor_mock.close.assert_called_once_with()
def test_execute_insert_with_generated_field(mocked_connection, mocked_cursor, mocker): mocked_cursor.description = '' mocked_cursor.rowcount = 1 mocked_cursor.fetchone.side_effect = [[1]] dummy = MySQLJDBC(mocked_connection) assert dummy.execute( "insert into users (name) values ('abc')") == ExecuteStatementResponse( numberOfRecordsUpdated=1, generatedFields=[Field(longValue=1)]) mocked_cursor.execute.assert_has_calls([ mocker.call('SELECT LAST_INSERT_ID(NULL)'), mocker.call("insert into users (name) values ('abc')"), mocker.call('SELECT LAST_INSERT_ID()'), ]) mocked_cursor.close.assert_called_once_with()
def test_valid_field() -> None: assert SqlParameter(name='abc', value=Field(stringValue='abc')).valid_value == 'abc' assert SqlParameter(name='abc', value=Field(blobValue='abc')).valid_value == 'abc' assert SqlParameter(name='abc', value=Field(doubleValue=0.1)).valid_value == 0.1 assert SqlParameter(name='abc', value=Field(isNull=True)).valid_value is None assert SqlParameter(name='abc', value=Field(longValue=123)).valid_value == 123 assert SqlParameter(name='abc', value=Field(longValue=123)).valid_value == 123 assert SqlParameter(name='abc', value=Field()).valid_value is None
def test_from_value() -> None: assert Field.from_value('str') == Field(stringValue='str') assert Field.from_value(123) == Field(longValue=123) assert Field.from_value(1.23) == Field(doubleValue=1.23) assert Field.from_value(True) == Field(booleanValue=True) assert Field.from_value(False) == Field(booleanValue=False) assert Field.from_value(b'bytes') == Field(blobValue=b64encode(b'bytes')) assert Field.from_value(None) == Field(isNull=True) class Dummy: pass with pytest.raises(Exception): Field.from_value(Dummy())
def helper_default_test_field(dummyResource: Resource) -> None: assert dummyResource.get_field_from_value('str') == Field(stringValue='str') assert dummyResource.get_field_from_value(123) == Field(longValue=123) assert dummyResource.get_field_from_value(1.23) == Field(doubleValue=1.23) assert dummyResource.get_field_from_value(True) == Field(booleanValue=True) assert dummyResource.get_field_from_value(False) == Field(booleanValue=False) assert dummyResource.get_field_from_value(b'bytes') == Field( blobValue=b64encode(b'bytes') ) assert dummyResource.get_field_from_value(None) == Field(isNull=True) assert dummyResource.get_field_from_value( datetime(2019, 5, 18, 15, 17, 8) ) == Field(stringValue='2019-05-18 15:17:08') class Dummy: pass with pytest.raises(Exception): dummyResource.get_field_from_value(Dummy())
def get_field_from_value(self, value: Any) -> Field: if isinstance(value, bool): return Field(booleanValue=value) elif isinstance(value, str): return Field(stringValue=value) elif type(value).__name__ == 'datetime': return Field(stringValue=str(value)) elif isinstance(value, int): return Field(longValue=value) elif isinstance(value, float): return Field(doubleValue=value) elif isinstance(value, bytes): return Field(blobValue=b64encode(value)) elif value is None: return Field(isNull=True) else: raise Exception(f'unsupported type {type(value)}: {value} ')
def get_filed_from_jdbc_type(self, value: Any, jdbc_type: Optional[int]) -> Field: if type(value).__name__.endswith('BigInteger'): return Field(longValue=int(str(value))) else: return super().get_filed_from_jdbc_type(value, jdbc_type)
def test_valid_field() -> None: assert SqlParameter(name='abc', value=Field(stringValue='abc')).valid_value == 'abc' assert SqlParameter(name='abc', value=Field(blobValue='abc')).valid_value == 'abc' assert SqlParameter(name='abc', value=Field(doubleValue=0.1)).valid_value == 0.1 assert SqlParameter(name='abc', value=Field(isNull=True)).valid_value is None assert SqlParameter(name='abc', value=Field(longValue=123)).valid_value == 123 assert SqlParameter(name='abc', value=Field(longValue=123)).valid_value == 123 assert SqlParameter(name='abc', value=Field()).valid_value is None assert (SqlParameter(name='abc', value=Field(stringValue='123456789'), typeHint='DECIMAL').valid_value == '123456789') assert (SqlParameter( name='abc', value=Field(stringValue='2020-02-27 00:30:15.290'), typeHint='TIMESTAMP', ).valid_value == '2020-02-27 00:30:15.290') assert (SqlParameter(name='abc', value=Field(stringValue='00:30:15.290'), typeHint='TIME').valid_value == '00:30:15.290') assert (SqlParameter(name='abc', value=Field(stringValue='2020-02-27'), typeHint='DATE').valid_value == '2020-02-27')
def get_field_from_value(self, value: Any) -> Field: if type(value).__name__.endswith('BigInteger'): return Field(longValue=int(str(value))) else: return super().get_field_from_value(value)