|
| 1 | +import mysql.connector as mariadb |
| 2 | +from os import getenv |
| 3 | + |
| 4 | +TABLE = getenv('DB2I_TABLE') or 'test' |
| 5 | +DB = getenv('DB2I_DB') or 'db2itest' |
| 6 | +HOST = getenv('DB2I_HOST') or '127.0.0.1' |
| 7 | +USER = getenv('DB2I_USER') or 'root' |
| 8 | +PASS = getenv('DB2I_PASS') or '' |
| 9 | +EXPECTED = getenv('DB2I_EXPECTED') or 'Hello World' |
| 10 | + |
| 11 | +def create_insert_select(charset, collation="general_ci", expected=EXPECTED, encoding=None): |
| 12 | + collation = f'{charset}_{collation}' |
| 13 | + is_unicode = True if 'utf' in charset or 'ucs' in charset else False |
| 14 | + use_unicode = True if is_unicode else False |
| 15 | + conn_charset = 'utf8' if is_unicode else charset |
| 16 | + conn_collation = 'utf8_general_ci' if is_unicode else collation |
| 17 | + |
| 18 | + conn = mariadb.connect(user=USER, password=PASS, host=HOST, database=DB, |
| 19 | + charset=conn_charset, collation=conn_collation, use_unicode=use_unicode) |
| 20 | + |
| 21 | + cursor = conn.cursor() |
| 22 | + cursor.execute(f"DROP TABLE IF EXISTS {TABLE}") |
| 23 | + conn.commit() |
| 24 | + cursor.execute(f"CREATE TABLE {TABLE} (i char(255)) engine=ibmdb2i CHARACTER SET '{charset}' COLLATE '{collation}'") |
| 25 | + cursor.execute(f"INSERT INTO {TABLE} VALUES(%s)", (expected,)) |
| 26 | + conn.commit() |
| 27 | + cursor.execute(f"SELECT i FROM {TABLE}") |
| 28 | + data = cursor.fetchone() |
| 29 | + if encoding: |
| 30 | + data = data[0].decode(encoding) |
| 31 | + else: |
| 32 | + data = data[0] |
| 33 | + |
| 34 | + print(f'Data is: {data}') |
| 35 | + cursor.execute(f"DROP TABLE {TABLE}") |
| 36 | + conn.commit() |
| 37 | + cursor.close() |
| 38 | + conn.close() |
| 39 | + return data |
| 40 | + |
| 41 | +def test_utf8(): |
| 42 | + data = create_insert_select('utf8') |
| 43 | + assert data == EXPECTED |
| 44 | + |
| 45 | +def test_utf8mb4(): |
| 46 | + data = create_insert_select('utf8mb4') |
| 47 | + assert data == EXPECTED |
| 48 | + |
| 49 | +def test_utf8mb3(): |
| 50 | + data = create_insert_select('utf8mb3') |
| 51 | + assert data == EXPECTED |
| 52 | + |
| 53 | +def test_utf16(): |
| 54 | + data = create_insert_select('utf16') |
| 55 | + assert data == EXPECTED |
| 56 | + |
| 57 | +def test_ucs2(): |
| 58 | + data = create_insert_select('ucs2') |
| 59 | + assert data == EXPECTED |
| 60 | + |
| 61 | +def test_ascii(): |
| 62 | + # https://en.wikipedia.org/wiki/ASCII#Character_set |
| 63 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F))]).decode('ascii') |
| 64 | + data = create_insert_select('ascii', expected=EXPECTED, encoding='ascii') |
| 65 | + assert data == EXPECTED |
| 66 | + |
| 67 | +def test_latin1(): |
| 68 | + # https://en.wikipedia.org/wiki/ISO/IEC_8859-1#Code_page_layout |
| 69 | + # CCSID 1148 does not include ¤ (00A4) because its replaced by € |
| 70 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F)) + tuple(range(0XA0, 0XA4)) |
| 71 | + + tuple(range(0XA5, 0X100))]).decode('iso8859-1') |
| 72 | + data = create_insert_select('latin1', expected=EXPECTED, encoding='iso8859-1') |
| 73 | + assert data == EXPECTED |
| 74 | + |
| 75 | +def test_latin2(): |
| 76 | + # CCSID 1153 does not include ¤ (00A4) because its replaced by € |
| 77 | + # https://en.wikipedia.org/wiki/ISO/IEC_8859-2#Code_page_layout |
| 78 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F)) + tuple(range(0XA0, 0XA4)) |
| 79 | + + tuple(range(0XA5, 0X100))]).decode('iso8859-2') |
| 80 | + data = create_insert_select('latin2', expected=EXPECTED, encoding='iso8859-2') |
| 81 | + assert data == EXPECTED |
| 82 | + |
| 83 | +def test_greek(): |
| 84 | + # https://en.wikipedia.org/wiki/ISO/IEC_8859-7#Codepage_layout |
| 85 | + # CCSID 4971 does not support ₯ drachma sign (0XA5) and ͺ Greek ypogegrammeni (0XAA) |
| 86 | + # these are available in CCSID 9067 (March 2005) ? |
| 87 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F)) + tuple(range(0XA0, 0XA5)) |
| 88 | + + tuple(range(0XA6, 0XAA)) + tuple(range(0XAB, 0XAE)) + tuple(range(0XAF, 0XD2)) |
| 89 | + + tuple(range(0XD4, 0XFF))]).decode('iso8859-7') |
| 90 | + data = create_insert_select('greek', expected=EXPECTED, encoding='iso8859-7') |
| 91 | + assert data == EXPECTED |
| 92 | + |
| 93 | +def test_hebrew(): |
| 94 | + # https://en.wikipedia.org/wiki/ISO/IEC_8859-8#Codepage_layout |
| 95 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F)) + tuple(range(0XA0, 0XA1)) |
| 96 | + + tuple(range(0XA2, 0XBF)) + tuple(range(0XDF, 0XFB))]).decode('iso8859-8') |
| 97 | + data = create_insert_select('hebrew', expected=EXPECTED, encoding='iso8859-8') |
| 98 | + assert data == EXPECTED |
| 99 | + |
| 100 | +def test_latin5(): |
| 101 | + # https://en.wikipedia.org/wiki/ISO/IEC_8859-9#Codepage_layout |
| 102 | + # CCSID 1155 does not include ¤ (00A4) because its replaced by € |
| 103 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F)) + tuple(range(0XA0, 0XA4)) |
| 104 | + + tuple(range(0XA5, 0X100))]).decode('iso8859-9') |
| 105 | + data = create_insert_select('latin5', 'turkish_ci', expected=EXPECTED, encoding='iso8859-9') |
| 106 | + assert data == EXPECTED |
| 107 | + |
| 108 | +def test_tis620(): |
| 109 | + # https://en.wikipedia.org/wiki/Thai_Industrial_Standard_620-2533#Character_set |
| 110 | + # The sole difference is that ISO/IEC 8859-11 allocates non-breaking space to code 0xA0, |
| 111 | + # while TIS-620 leaves it undefined. (In practice, this small distinction is usually ignored.) |
| 112 | + # Here we skip over nbsp 0xA0 |
| 113 | + EXPECTED = b''.join([bytes([i]) for i in tuple(range(0X20, 0X7F)) + tuple(range(0XA1, 0XDB)) |
| 114 | + + tuple(range(0XDF, 0XFC))]).decode('iso8859-11') |
| 115 | + data = create_insert_select('tis620', 'thai_ci', expected=EXPECTED, encoding='iso8859-11') |
| 116 | + assert data == EXPECTED |
0 commit comments