Example #1
0
def test_decode_df():
    """It should decode the bytes columns"""
    df = pd.DataFrame({'date': [b'2013-08-01', b'2013-08-02'],
                       'country': ['France', 'Germany'],
                       'number': [1, 2],
                       'other': [b'pikka', b'chuuu'],
                       'random': [3, 4]})
    res = MySQLConnector.decode_df(df)
    assert res['date'].tolist() == ['2013-08-01', '2013-08-02']
    assert res['other'].tolist() == ['pikka', 'chuuu']
    assert res[['country', 'number', 'random']].equals(df[['country', 'number', 'random']])

    df2 = df[['number', 'random']]
    res = MySQLConnector.decode_df(df2)
    assert res.equals(df2)
Example #2
0
def test_clean_response():
    """ It should replace None by np.nan and decode bytes data """
    response = [{'name': 'fway', 'age': 13}, {'name': b'zbruh', 'age': None}]
    res = MySQLConnector.clean_response(response)

    assert len(res) == 2
    assert res[1]['name'] == 'zbruh'
    assert np.isnan(res[1]['age'])
Example #3
0
def mysql_connector(mysql_server):
    return MySQLConnector(
        name='mycon',
        host='localhost',
        port=mysql_server['port'],
        user='******',
        password='******',
    )
Example #4
0
def test_get_df(mocker):
    """ It should call the sql extractor """
    snock = mocker.patch('pymysql.connect')
    reasq = mocker.patch('pandas.read_sql')
    mocker.patch(
        'toucan_connectors.mysql.mysql_connector.MySQLConnector.get_foreign_key_info'
    ).return_value = []

    data_sources_spec = [
        {
            'domain': 'MySQL test',
            'type': 'external_database',
            'name': 'Some MySQL provider',
            'database': 'mysql_db',
            'table': 'City'
        }
    ]
    mysql_connector = MySQLConnector(name='mycon', host='localhost', port=22,
                                     user='******', password='******')

    data_source = MySQLDataSource(**data_sources_spec[0])
    mysql_connector.get_df(data_source)

    conv = pymysql.converters.conversions.copy()
    conv[246] = float
    snock.assert_called_once_with(
        host='localhost',
        user='******',
        database='mysql_db',
        password='******',
        port=22,
        charset='utf8mb4',
        conv=conv,
        cursorclass=pymysql.cursors.DictCursor
    )

    reasq.assert_called_once_with(
        'select * from City',
        con=snock(),
        params={}
    )
Example #5
0
def test_get_connection_params():
    connector = MySQLConnector(name='my_mysql_con',
                               host='myhost',
                               user='******')
    params = connector.get_connection_params()
    params.pop('conv')
    assert params == {
        'host': 'myhost',
        'user': '******',
        'charset': 'utf8mb4',
        'cursorclass': pymysql.cursors.DictCursor,
    }

    connector = MySQLConnector(
        name='my_mssql_con',
        host='myhost',
        user='******',
        password='******',
        port=123,
        charset='utf8',
        connect_timeout=50,
    )
    params = connector.get_connection_params()
    params.pop('conv')
    assert params == {
        'host': 'myhost',
        'user': '******',
        'charset': 'utf8',
        'cursorclass': pymysql.cursors.DictCursor,
        'password': '******',
        'port': 123,
        'connect_timeout': 50,
    }