From cad7be531e560a7ee3cddc5429467810905cccfc Mon Sep 17 00:00:00 2001 From: Timothy Sehn Date: Mon, 24 Aug 2020 14:45:29 -0700 Subject: [PATCH] First pass at MySQL client tests. This one adds a test for the native python MySQL client. The native python MySQL client does not use the C connection library instead connecting to MySQL using the DB API v2.0 specification (PEP-249) --- mysql-client-tests/mysql-client-tests.bats | 30 +++++++++++++ mysql-client-tests/python/mysql-client.py | 51 ++++++++++++++++++++++ mysql-client-tests/python/python.bats | 3 ++ 3 files changed, 84 insertions(+) create mode 100644 mysql-client-tests/mysql-client-tests.bats create mode 100644 mysql-client-tests/python/mysql-client.py create mode 100644 mysql-client-tests/python/python.bats diff --git a/mysql-client-tests/mysql-client-tests.bats b/mysql-client-tests/mysql-client-tests.bats new file mode 100644 index 0000000000..0a8428bbee --- /dev/null +++ b/mysql-client-tests/mysql-client-tests.bats @@ -0,0 +1,30 @@ +# MySQL client tests are set up to test Dolt as a MySQL server and +# standard MySQL Clients in a wide array of languages. I used BATS because +# it was easy to set up the Dolt piece using the command line. +# +# We create a Dolt database and start a server in the setup(). Then, each test +# will attempt to access the server through a client. We'll do some basic +# reads and writes to make sure the client works. As we discover any +# gotchas, we can add tests for that specific language. + +setup() { + REPO_NAME="dolt_repo_$$" + mkdir $REPO_NAME + cd $REPO_NAME + + dolt init + let PORT="$$ % (65536-1024) + 1024" + USER="dolt" + dolt sql-server --host 0.0.0.0 --port=$PORT --user $USER & + SERVER_PID=$! +} + +teardown() { + cd .. + rm -rf $REPO_NAME + kill -HUP $SERVER_PID +} + +@test "python mysql client" { + python3 $BATS_TEST_DIRNAME/python/mysql-client.py $USER $PORT $REPO_NAME +} diff --git a/mysql-client-tests/python/mysql-client.py b/mysql-client-tests/python/mysql-client.py new file mode 100644 index 0000000000..01f8af7f4c --- /dev/null +++ b/mysql-client-tests/python/mysql-client.py @@ -0,0 +1,51 @@ +import mysql.connector +import sys + +QUERY_RESPONSE = [ + { "create table test (pk int, value int, primary key(pk))": '' }, + { "describe test": [ + ('pk', 'int', 'NO', 'PRI', '', ''), + ('value', 'int', 'YES', '', '', '') + ] }, + { "insert into test (pk, value) values (0,0)": '' }, + { "select * from test": [(0,0)] } +] + +def main(): + print(sys.argv) + user = sys.argv[1] + port = sys.argv[2] + db = sys.argv[3] + + connection = mysql.connector.connect(user=user, + host="127.0.0.1", + port=port, + database=db) + + + for query_response in QUERY_RESPONSE: + query = list(query_response.keys())[0] + exp_results = query_response[query] + cursor = connection.cursor() + cursor.execute(query) + try: + results = cursor.fetchall() + print(exp_results) + print(results) + if ( results != exp_results ): + print("Expected:") + print(exp_results) + print("Received:") + print(results) + exit(1) + except mysql.connector.errors.InterfaceError: + # This is a write query + pass + + cursor.close() + + connection.close() + + exit(0) + +main() diff --git a/mysql-client-tests/python/python.bats b/mysql-client-tests/python/python.bats new file mode 100644 index 0000000000..de68b21128 --- /dev/null +++ b/mysql-client-tests/python/python.bats @@ -0,0 +1,3 @@ +@test "make sure this works" { + echo "poop" +} \ No newline at end of file