Пример #1
0
def test_create_column_metadata_set(mocker):
    mock_meta = mocker.Mock()
    mock_meta.isAutoIncrement.side_effect = [True, False]
    mock_meta.isCaseSensitive.side_effect = [False, True]
    mock_meta.isCurrency.side_effect = [True, False]
    mock_meta.isSigned.side_effect = [True, False]
    mock_meta.getColumnLabel.side_effect = ['a', 'b']
    mock_meta.getColumnName.side_effect = ['c', 'd']
    mock_meta.isNullable.side_effect = [0, 1]
    mock_meta.getPrecision.side_effect = [1, 2]
    mock_meta.getScale.side_effect = [3, 4]
    mock_meta.getSchemaName.side_effect = ['e', 'f']
    mock_meta.getTableName.side_effect = ['g', 'h']
    mock_meta.getColumnType.side_effect = [5, 6]
    mock_meta.getColumnTypeName.side_effect = ['i', 'j']
    mock_meta.getColumnCount.return_value = 2
    mock_cursor = mocker.Mock()
    mock_cursor._meta = mock_meta
    assert DummyJDBC(
        mocker.Mock()).create_column_metadata_set(mock_cursor) == [
            ColumnMetadata(
                arrayBaseColumnType=0,
                isAutoIncrement=True,
                isCaseSensitive=False,
                isCurrency=True,
                isSigned=True,
                label='a',
                name='c',
                nullable=0,
                precision=1,
                scale=3,
                schema='e',
                tableName='g',
                type=5,
                typeName='i',
            ),
            ColumnMetadata(
                arrayBaseColumnType=0,
                isAutoIncrement=False,
                isCaseSensitive=True,
                isCurrency=False,
                isSigned=False,
                label='b',
                name='d',
                nullable=1,
                precision=2,
                scale=4,
                schema='f',
                tableName='h',
                type=6,
                typeName='j',
            ),
        ]
Пример #2
0
def test_execute_select_with_include_metadata(clear, mocker):
    connection_mock = mocker.Mock()
    cursor_mock = mocker.Mock()
    connection_mock.cursor.side_effect = [cursor_mock]
    cursor_mock.description = (1, 2, 3, 4, 5, 6, 7), (8, 9, 10, 11, 12, 13, 14)
    cursor_mock.fetchall.side_effect = [((1, 'abc'), )]
    dummy = DummyResource(connection_mock, transaction_id='123')
    dummy.use_database = mocker.Mock()
    assert dummy.execute(
        "select * from users",
        database_name='test',
        include_result_metadata=True).dict() == ExecuteStatementResponse(
            numberOfRecordsUpdated=0,
            records=[[Field.from_value(1),
                      Field.from_value('abc')]],
            columnMetadata=[
                ColumnMetadata(
                    arrayBaseColumnType=0,
                    isAutoIncrement=False,
                    isCaseSensitive=False,
                    isCurrency=False,
                    isSigned=False,
                    label='1',
                    name='1',
                    nullable=1,
                    precision=5,
                    scale=6,
                    tableName=None,
                    type=None,
                    typeName=None,
                ),
                ColumnMetadata(
                    arrayBaseColumnType=0,
                    isAutoIncrement=False,
                    isCaseSensitive=False,
                    isCurrency=False,
                    isSigned=False,
                    label='8',
                    name='8',
                    nullable=1,
                    precision=12,
                    scale=13,
                    tableName=None,
                    type=None,
                    typeName=None,
                ),
            ],
        )

    cursor_mock.execute.assert_called_once_with('select * from users')
    cursor_mock.close.assert_called_once_with()
Пример #3
0
def create_column_metadata(
    name: str,
    type_code: int,
    display_size: Optional[int],
    internal_size: int,
    precision: int,
    scale: int,
    null_ok: bool,
) -> ColumnMetadata:
    return ColumnMetadata(
        arrayBaseColumnType=0,
        isAutoIncrement=False,
        isCaseSensitive=False,
        isCurrency=False,
        isSigned=False,
        label=name,
        name=name,
        nullable=1 if null_ok else 0,
        precision=precision,
        scale=scale,
        schema=None,
        tableName=None,
        type=None,
        typeName=None,
    )
Пример #4
0
def test_execute_select(mocked_connection, mocked_cursor, mocker):
    mocked_cursor.description = 1, 1, 1, 1, 1, 1, 1
    mocked_cursor.fetchall.side_effect = [((1, 'abc'), )]
    dummy = PostgreSQLJDBC(mocked_connection, transaction_id='123')
    dummy.create_column_metadata_set = create_column_metadata_set_mock = mocker.Mock(
    )
    create_column_metadata_set_mock.side_effect = [[
        ColumnMetadata(
            arrayBaseColumnType=0,
            isAutoIncrement=False,
            isCaseSensitive=False,
            isCurrency=False,
            isSigned=False,
            label=1,
            name=1,
            precision=5,
            scale=6,
            tableName=None,
            type=None,
            typeName=None,
        ),
        ColumnMetadata(
            arrayBaseColumnType=0,
            isAutoIncrement=False,
            isCaseSensitive=False,
            isCurrency=False,
            isSigned=False,
            label=8,
            name=8,
            precision=12,
            scale=13,
            tableName=None,
            type=None,
            typeName=None,
        ),
    ]]
    assert dummy.execute("select * from users", ) == ExecuteStatementResponse(
        numberOfRecordsUpdated=0,
        records=[[
            dummy.get_field_from_value(1),
            dummy.get_field_from_value('abc')
        ]],
    )

    mocked_cursor.execute.assert_has_calls(
        [mocker.call('select * from users')])
    mocked_cursor.close.assert_called_once_with()
Пример #5
0
def create_column_metadata(field_descriptor_packet: Column) -> ColumnMetadata:
    return ColumnMetadata(
        arrayBaseColumnType=0,
        isAutoIncrement=False,
        isCaseSensitive=False,
        isCurrency=False,
        isSigned=False,
        label=field_descriptor_packet.name,
        name=field_descriptor_packet.name,
        nullable=None,
        precision=None,  # TODO: Implement
        scale=field_descriptor_packet.scale,
        schema=None,
        tableName=field_descriptor_packet.name,
        type=None,  # JDBC Type unsupported
        typeName=None,  # JDBC TypeName unsupported
    )
Пример #6
0
def create_column_metadata(
    field_descriptor_packet: FieldDescriptorPacket, ) -> ColumnMetadata:
    return ColumnMetadata(
        arrayBaseColumnType=0,
        isAutoIncrement=False,
        isCaseSensitive=False,
        isCurrency=False,
        isSigned=False,
        label=field_descriptor_packet.name,
        name=field_descriptor_packet.org_name,
        nullable=1 if field_descriptor_packet.flags % 2 == 0 else 0,
        precision=field_descriptor_packet.get_column_length(),
        scale=field_descriptor_packet.scale,
        schema=None,
        tableName=field_descriptor_packet.table_name,
        type=None,  # JDBC Type unsupported
        typeName=None,  # JDBC TypeName unsupported
    )
Пример #7
0
def test_create_column_metadata(clear):
    assert create_column_metadata('name', 1, 2, 3, 4, 5,
                                  True) == ColumnMetadata(
                                      arrayBaseColumnType=0,
                                      isAutoIncrement=False,
                                      isCaseSensitive=False,
                                      isCurrency=False,
                                      isSigned=False,
                                      label='name',
                                      name='name',
                                      nullable=1,
                                      precision=4,
                                      scale=5,
                                      tableName=None,
                                      type=None,
                                      typeName=None,
                                      schema_=None,
                                  )
Пример #8
0
def create_column_metadata_set(meta: Any) -> List[ColumnMetadata]:
    return [
        ColumnMetadata(
            arrayBaseColumnType=0,
            isAutoIncrement=meta.isAutoIncrement(i),
            isCaseSensitive=meta.isCaseSensitive(i),
            isCurrency=meta.isCurrency(i),
            isSigned=meta.isSigned(i),
            label=meta.getColumnLabel(i),
            name=meta.getColumnName(i),
            nullable=meta.isNullable(i),
            precision=meta.getPrecision(i),
            scale=meta.getScale(i),
            schema=meta.getSchemaName(i),
            tableName=meta.getTableName(i),
            type=meta.getColumnType(i),
            typeName=meta.getColumnTypeName(i),
        ) for i in range(1,
                         meta.getColumnCount() + 1)
    ]
Пример #9
0
def test_execute_select_with_include_metadata(mocked_connection, mocked_cursor,
                                              mocker):
    meta_mock = mocker.Mock()
    mocked_cursor._meta = meta_mock
    mocked_cursor.description = (1, 2, 3, 4, 5, 6, 7), (8, 9, 10, 11, 12, 13,
                                                        14)
    mocked_cursor.fetchall.side_effect = [((1, 'abc'), )]
    dummy = MySQLJDBC(mocked_connection, transaction_id='123')
    dummy.use_database = mocker.Mock()
    create_column_metadata_set_mock = mocker.patch(
        'local_data_api.resources.jdbc.create_column_metadata_set')
    create_column_metadata_set_mock.side_effect = [[
        ColumnMetadata(
            arrayBaseColumnType=0,
            isAutoIncrement=False,
            isCaseSensitive=False,
            isCurrency=False,
            isSigned=False,
            label=1,
            name=1,
            precision=5,
            scale=6,
            tableName=None,
            type=None,
            typeName=None,
        ),
        ColumnMetadata(
            arrayBaseColumnType=0,
            isAutoIncrement=False,
            isCaseSensitive=False,
            isCurrency=False,
            isSigned=False,
            label=8,
            name=8,
            precision=12,
            scale=13,
            tableName=None,
            type=None,
            typeName=None,
        ),
    ]]

    assert dummy.execute(
        "select * from users",
        database_name='test',
        include_result_metadata=True) == ExecuteStatementResponse(
            numberOfRecordsUpdated=0,
            records=[[Field.from_value(1),
                      Field.from_value('abc')]],
            columnMetadata=[
                ColumnMetadata(
                    arrayBaseColumnType=0,
                    isAutoIncrement=False,
                    isCaseSensitive=False,
                    isCurrency=False,
                    isSigned=False,
                    label=1,
                    name=1,
                    precision=5,
                    scale=6,
                    tableName=None,
                    type=None,
                    typeName=None,
                ),
                ColumnMetadata(
                    arrayBaseColumnType=0,
                    isAutoIncrement=False,
                    isCaseSensitive=False,
                    isCurrency=False,
                    isSigned=False,
                    label=8,
                    name=8,
                    precision=12,
                    scale=13,
                    tableName=None,
                    type=None,
                    typeName=None,
                ),
            ],
        )

    create_column_metadata_set_mock.assert_called_once_with(meta_mock)
    mocked_cursor.execute.assert_has_calls([
        mocker.call('SELECT LAST_INSERT_ID(NULL)'),
        mocker.call('select * from users')
    ])
    mocked_cursor.close.assert_called_once_with()
Пример #10
0
def test_execute_select_with_include_metadata(clear, mocker):

    connection_mock = mocker.Mock()
    cursor_mock = mocker.Mock()
    connection_mock.cursor.side_effect = [cursor_mock]
    cursor_mock.description = (1, 2, 3, 4, 5, 6, 7), (8, 9, 10, 11, 12, 13, 14)
    cursor_mock.fetchall.side_effect = [((1, 'abc'),)]
    field_1 = mocker.Mock()
    field_1.name = '1'
    field_1.org_name = '1'
    field_1.flags = 2
    field_1.get_column_length.return_value = 5
    field_1.scale = 6
    field_1.table_name = None
    field_2 = mocker.Mock()
    field_2.name = '8'
    field_2.org_name = '8'
    field_2.flags = 2
    field_2.get_column_length.return_value = 12
    field_2.scale = 13
    field_2.table_name = None
    cursor_mock._result.fields = [field_1, field_2]
    dummy = MySQL(connection_mock, transaction_id='123')
    assert (
        dummy.execute("select * from users", include_result_metadata=True).dict()
        == ExecuteStatementResponse(
            numberOfRecordsUpdated=0,
            records=[[Field.from_value(1), Field.from_value('abc')]],
            columnMetadata=[
                ColumnMetadata(
                    arrayBaseColumnType=0,
                    isAutoIncrement=False,
                    isCaseSensitive=False,
                    isCurrency=False,
                    isSigned=False,
                    label='1',
                    name='1',
                    nullable=1,
                    precision=5,
                    scale=6,
                    tableName=None,
                    type=None,
                    typeName=None,
                ),
                ColumnMetadata(
                    arrayBaseColumnType=0,
                    isAutoIncrement=False,
                    isCaseSensitive=False,
                    isCurrency=False,
                    isSigned=False,
                    label='8',
                    name='8',
                    nullable=1,
                    precision=12,
                    scale=13,
                    tableName=None,
                    type=None,
                    typeName=None,
                ),
            ],
        ).dict()
    )

    cursor_mock.execute.assert_called_once_with('select * from users')
    cursor_mock.close.assert_called_once_with()