forked from fangci221/library
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fangci221#2 from Hank8933/origin
test: Add test for MySQL server
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
pytest | ||
pytest-playwright | ||
ymysql | ||
cryptography | ||
python-dotenv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
import pymysql | ||
from dotenv import load_dotenv | ||
|
||
|
||
def connect_mysql(): | ||
|
||
# Load environment variables from .env | ||
load_dotenv() | ||
|
||
# Set connection parameters | ||
config = { | ||
'user': os.getenv('DB_USER'), | ||
'passwd': os.getenv('DB_PASSWD'), | ||
'host': os.getenv('DB_HOST'), | ||
'port': int(os.getenv('DB_PORT')), | ||
'db': os.getenv('DB_NAME') | ||
} | ||
|
||
# Connect to the database | ||
try: | ||
cnx = pymysql.connect(**config) | ||
except pymysql.err.OperationalError as e: | ||
print(f"Can't connect to MySQL database: {e}") | ||
return None | ||
else: | ||
return cnx | ||
|
||
|
||
if __name__ == "__main__": | ||
cnx = connect_mysql() | ||
print("Connection is OK") | ||
cnx.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import pytest | ||
import database | ||
|
||
|
||
class TestMySQL: | ||
|
||
# Define a fixture to import the .sql file | ||
@pytest.fixture(scope="class") | ||
def setup(self): | ||
|
||
# Connect to the database | ||
cnx = database.connect_mysql() | ||
assert cnx is not None, "Connection is OK" | ||
|
||
# Create a cursor object, which is used to execute SQL statements | ||
cursor = cnx.cursor() | ||
|
||
# Open the .sql file and read the content | ||
with open("./Dump20231016.sql", "r") as f: | ||
sql_content = f.read() | ||
sql_list = sql_content.split(";") | ||
|
||
# Execute the SQL statements | ||
for sql in sql_list: | ||
# If the SQL statement is not empty, execute it | ||
if sql.strip(): | ||
cursor.execute(sql) | ||
# If the SQL statement is a CREATE statement, commit the changes | ||
if sql.startswith("CREATE"): | ||
cnx.commit() | ||
|
||
yield cnx, cursor | ||
|
||
cursor.close() | ||
cnx.close() | ||
|
||
|
||
# Test if the database is created | ||
def test_import_sql(self, setup): | ||
|
||
# Get the cursor object | ||
cnx, cursor = setup | ||
|
||
# Check if the database is created | ||
cursor.execute("SHOW TABLES") | ||
tables = cursor.fetchall() | ||
assert len(tables) == 5, "The number of tables is correct" | ||
|
||
# Check if the table "account" is created | ||
cursor.execute("SELECT * FROM account") | ||
accounts = cursor.fetchall() | ||
assert len(accounts) == 2, "The number of accounts is correct" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import database | ||
|
||
|
||
def test_mysql_login(): | ||
|
||
# Connect to the database | ||
cnx = database.connect_mysql() | ||
|
||
# Check if the connection is successful | ||
assert cnx is not None, "Connection is OK" |