| 1 | from __future__ import annotations |
| 2 | |
| 3 | from typing import Generator |
| 4 | |
| 5 | import pytest |
| 6 | from mysql.connector import connect |
| 7 | |
| 8 | from harlequin_mysql.adapter import ( |
| 9 | HarlequinMySQLAdapter, |
| 10 | HarlequinMySQLConnection, |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | @pytest.fixture |
| 15 | def connection() -> Generator[HarlequinMySQLConnection, None, None]: |
| 16 | mysqlconn = connect( |
| 17 | host="localhost", |
| 18 | user="root", |
| 19 | password="example", |
| 20 | database="mysql", |
| 21 | autocommit=True, |
| 22 | ) |
| 23 | cur = mysqlconn.cursor() |
| 24 | cur.execute("drop database if exists test;") |
| 25 | cur.execute("drop database if exists one;") |
| 26 | cur.execute("drop database if exists two;") |
| 27 | cur.execute("drop database if exists three;") |
| 28 | cur.execute("create database test;") |
| 29 | cur.close() |
| 30 | conn = HarlequinMySQLAdapter( |
| 31 | conn_str=tuple(), |
| 32 | host="localhost", |
| 33 | user="root", |
| 34 | password="example", |
| 35 | database="test", |
| 36 | ).connect() |
| 37 | yield conn |
| 38 | cur = mysqlconn.cursor() |
| 39 | cur.execute("drop database if exists test;") |
| 40 | cur.execute("drop database if exists one;") |
| 41 | cur.execute("drop database if exists two;") |
| 42 | cur.execute("drop database if exists three;") |
| 43 | cur.close() |
| 44 | |
| 45 | |
| 46 | @pytest.fixture |
| 47 | def read_only_connection() -> Generator[HarlequinMySQLConnection, None, None]: |
| 48 | mysqlconn = connect( |
| 49 | host="localhost", |
| 50 | user="root", |
| 51 | password="example", |
| 52 | database="mysql", |
| 53 | autocommit=True, |
| 54 | ) |
| 55 | cur = mysqlconn.cursor() |
| 56 | cur.execute("drop database if exists test_read_only;") |
| 57 | cur.execute("drop database if exists test_read_only_new;") |
| 58 | cur.execute("create database test_read_only;") |
| 59 | cur.execute("create table test_read_only.foo (a int);") |
| 60 | cur.execute("insert into test_read_only.foo values (1);") |
| 61 | cur.close() |
| 62 | conn = HarlequinMySQLAdapter( |
| 63 | conn_str=tuple(), |
| 64 | read_only=True, |
| 65 | host="localhost", |
| 66 | user="root", |
| 67 | password="example", |
| 68 | database="test_read_only", |
| 69 | ).connect() |
| 70 | yield conn |
| 71 | cur = mysqlconn.cursor() |
| 72 | cur.execute("drop database if exists test_read_only;") |
| 73 | cur.execute("drop database if exists test_read_only_new;") |
| 74 | cur.close() |