Skip to content

Commit 518594e

Browse files
committed
Improve the MySQL DB test storage feature.
1 parent 923abea commit 518594e

File tree

6 files changed

+98
-117
lines changed

6 files changed

+98
-117
lines changed

seleniumbase/core/mysql.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class DatabaseManager():
1010
"""
11-
This class wraps database functions for easy use.
11+
This class wraps MySQL database methods for easy use.
1212
It connects to the testcase database.
1313
"""
1414

@@ -40,7 +40,7 @@ def __init__(self, database_env='test', conf_creds=None):
4040

4141
def fetchall_query_and_close(self, query, values):
4242
"""
43-
Executes a query, gets all the values and then closes up the connection
43+
Executes a query, gets all the values, and closes the connection.
4444
"""
4545
self.cursor.execute(query, values)
4646
retval = self.cursor.fetchall()
@@ -49,7 +49,7 @@ def fetchall_query_and_close(self, query, values):
4949

5050
def fetchone_query_and_close(self, query, values):
5151
"""
52-
Executes a query, gets the first value, and closes up the connection
52+
Executes a query, gets the first value, and closes the connection.
5353
"""
5454
self.cursor.execute(query, values)
5555
retval = self.cursor.fetchone()
@@ -58,7 +58,7 @@ def fetchone_query_and_close(self, query, values):
5858

5959
def execute_query_and_close(self, query, values):
6060
"""
61-
Executes a query and closes the connection
61+
Executes a query and closes the connection.
6262
"""
6363
retval = self.cursor.execute(query, values)
6464
self.__close_db()

seleniumbase/core/mysql_conf.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""
2-
This file contains database credentials for the various databases
3-
that the tests need to access
2+
This file organizes connection details to the Testcase Database.
43
"""
54

65
from seleniumbase.config import settings

seleniumbase/core/testcase_manager.py

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
1-
"""
2-
Testcase database related methods
3-
"""
4-
51
from seleniumbase.core.mysql import DatabaseManager
62

73

84
class TestcaseManager:
9-
"""
10-
Helper for Testcase related DB stuff
11-
"""
125

136
def __init__(self, database_env):
147
self.database_env = database_env
158

169
def insert_execution_data(self, execution_query_payload):
17-
""" Inserts an execution into the database.
10+
""" Inserts a test execution row into the database.
1811
Returns the execution guid. """
1912

2013
query = """INSERT INTO execution
21-
(guid, executionStart, totalExecutionTime, username)
14+
(guid, execution_start, total_execution_time, username)
2215
VALUES (%(guid)s,%(execution_start_time)s,
2316
%(total_execution_time)s,%(username)s)"""
2417
DatabaseManager(self.database_env).execute_query_and_close(
@@ -27,70 +20,63 @@ def insert_execution_data(self, execution_query_payload):
2720
return execution_query_payload.guid
2821

2922
def update_execution_data(self, execution_guid, execution_time):
30-
"""updates an existing execution in the database"""
31-
23+
""" Updates an existing test execution row in the database. """
3224
query = """UPDATE execution
33-
SET totalExecutionTime=%(execution_time)s
25+
SET total_execution_time=%(execution_time)s
3426
WHERE guid=%(execution_guid)s """
3527
DatabaseManager(self.database_env).execute_query_and_close(
3628
query,
3729
{"execution_guid": execution_guid,
3830
"execution_time": execution_time})
3931

4032
def insert_testcase_data(self, testcase_run_payload):
41-
"""inserts all data for the test case, returns the new row guid"""
42-
43-
query = """INSERT INTO testcaseRunData
44-
(guid, browser, state, execution_guid, env, start_time,
45-
testcaseAddress, runtime, retryCount, message, stackTrace)
33+
""" Inserts all data for the test in the DB. Returns new row guid. """
34+
query = """INSERT INTO test_run_data(
35+
guid, browser, state, execution_guid, env, start_time,
36+
test_address, runtime, retry_count, message, stack_trace)
4637
VALUES (
4738
%(guid)s,
4839
%(browser)s,
4940
%(state)s,
5041
%(execution_guid)s,
5142
%(env)s,
5243
%(start_time)s,
53-
%(testcaseAddress)s,
44+
%(test_address)s,
5445
%(runtime)s,
55-
%(retryCount)s,
46+
%(retry_count)s,
5647
%(message)s,
57-
%(stackTrace)s) """
48+
%(stack_trace)s) """
5849
DatabaseManager(self.database_env).execute_query_and_close(
5950
query, testcase_run_payload.get_params())
6051

6152
def update_testcase_data(self, testcase_payload):
62-
"""updates an existing testcase run in the database"""
63-
64-
query = """UPDATE testcaseRunData SET
65-
runtime=%(runtime)s,
66-
state=%(state)s,
67-
retryCount=%(retryCount)s,
68-
stackTrace=%(stackTrace)s,
69-
message=%(message)s
70-
WHERE guid=%(guid)s """
53+
""" Updates an existing test run in the database. """
54+
query = """UPDATE test_run_data SET
55+
runtime=%(runtime)s,
56+
state=%(state)s,
57+
retry_count=%(retry_count)s,
58+
stack_trace=%(stack_trace)s,
59+
message=%(message)s
60+
WHERE guid=%(guid)s """
7161
DatabaseManager(self.database_env).execute_query_and_close(
7262
query, testcase_payload.get_params())
7363

7464
def update_testcase_log_url(self, testcase_payload):
75-
"""updates an existing testcase run's logging URL in the database"""
76-
77-
query = """UPDATE testcaseRunData
78-
SET logURL=%(logURL)s
65+
query = """UPDATE test_run_data
66+
SET log_url=%(log_url)s
7967
WHERE guid=%(guid)s """
8068
DatabaseManager(self.database_env).execute_query_and_close(
8169
query, testcase_payload.get_params())
8270

8371

8472
class ExecutionQueryPayload:
85-
""" Helper class for containing the execution query data """
8673
def __init__(self):
8774
self.execution_start_time = None
8875
self.total_execution_time = -1
8976
self.username = "Default"
9077
self.guid = None
9178

9279
def get_params(self):
93-
""" Returns a params object for use with the pool """
9480
return {
9581
"execution_start_time": self.execution_start_time,
9682
"total_execution_time": self.total_execution_time,
@@ -100,10 +86,9 @@ def get_params(self):
10086

10187

10288
class TestcaseDataPayload:
103-
""" Helper class for containing all the testcase query data """
10489
def __init__(self):
10590
self.guid = None
106-
self.testcaseAddress = None
91+
self.test_address = None
10792
self.browser = None
10893
self.state = None
10994
self.execution_guid = None
@@ -113,21 +98,20 @@ def __init__(self):
11398
self.retry_count = 0
11499
self.stack_trace = None
115100
self.message = None
116-
self.logURL = None
101+
self.log_url = None
117102

118103
def get_params(self):
119-
""" Returns a params object for use with the pool """
120104
return {
121105
"guid": self.guid,
122-
"testcaseAddress": self.testcaseAddress,
106+
"test_address": self.test_address,
123107
"browser": self.browser,
124108
"state": self.state,
125109
"execution_guid": self.execution_guid,
126110
"env": self.env,
127111
"start_time": self.start_time,
128112
"runtime": self.runtime,
129-
"retryCount": self.retry_count,
130-
"stackTrace": self.stack_trace,
113+
"retry_count": self.retry_count,
114+
"stack_trace": self.stack_trace,
131115
"message": self.message,
132-
"logURL": self.logURL
116+
"log_url": self.log_url
133117
}
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
# table delayedTestData
1+
# test_run_data table
22
# -----------------------------------
3-
CREATE TABLE `delayedTestData` (
3+
CREATE TABLE `test_run_data` (
44
`guid` varchar(64) NOT NULL DEFAULT '',
5-
`testcaseAddress` varchar(255) NOT NULL DEFAULT '',
6-
`insertedAt` bigint(20) NOT NULL,
7-
`expectedResult` text,
8-
`done` tinyint(1) DEFAULT '0',
9-
`expiresAt` bigint(20) DEFAULT NULL,
10-
PRIMARY KEY (`guid`),
11-
UNIQUE KEY `uuid` (`guid`)
5+
`test_address` varchar(255) DEFAULT NULL,
6+
`env` varchar(64) DEFAULT NULL,
7+
`start_time` varchar(64) DEFAULT NULL,
8+
`execution_guid` varchar(64) DEFAULT NULL,
9+
`runtime` int(11),
10+
`state` varchar(64) DEFAULT NULL,
11+
`browser` varchar(64) DEFAULT NULL,
12+
`message` text,
13+
`stack_trace` text,
14+
`retry_count` int(11) DEFAULT '0',
15+
`exception_map_guid` varchar(64) DEFAULT NULL,
16+
`log_url` text,
17+
PRIMARY KEY (`guid`)
1218
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
1319

14-
# table execution
20+
# execution table
1521
# -----------------------------------
1622
CREATE TABLE `execution` (
1723
`guid` varchar(64) NOT NULL DEFAULT '',
18-
`totalExecutionTime` int(11),
24+
`total_execution_time` int(11),
1925
`username` varchar(255) DEFAULT NULL,
20-
`executionStart` bigint(20) DEFAULT '0',
26+
`execution_start` bigint(20) DEFAULT '0',
2127
PRIMARY KEY (`guid`)
2228
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2329

24-
# table testcaseRunData
30+
# delayed_test_data table
2531
# -----------------------------------
26-
CREATE TABLE `testcaseRunData` (
32+
CREATE TABLE `delayed_test_data` (
2733
`guid` varchar(64) NOT NULL DEFAULT '',
28-
`testcaseAddress` varchar(255) DEFAULT NULL,
29-
`env` varchar(64) DEFAULT NULL,
30-
`start_time` varchar(64) DEFAULT NULL,
31-
`execution_guid` varchar(64) DEFAULT NULL,
32-
`runtime` int(11),
33-
`state` varchar(64) DEFAULT NULL,
34-
`browser` varchar(64) DEFAULT NULL,
35-
`message` text,
36-
`stackTrace` text,
37-
`retryCount` int(11) DEFAULT '0',
38-
`exceptionMap_guid` varchar(64) DEFAULT NULL,
39-
`logURL` text,
40-
PRIMARY KEY (`guid`)
34+
`test_address` varchar(255) NOT NULL DEFAULT '',
35+
`inserted_at` bigint(20) NOT NULL,
36+
`expected_result` text,
37+
`is_done` tinyint(1) DEFAULT '0',
38+
`expires_at` bigint(20) DEFAULT NULL,
39+
PRIMARY KEY (`guid`),
40+
UNIQUE KEY `uuid` (`guid`)
4141
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

seleniumbase/fixtures/delayed_data_manager.py

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,81 @@
44
import uuid
55
from seleniumbase.core.mysql import DatabaseManager
66

7-
DEFAULT_EXPIRATION = 1000 * 60 * 60 * 48
7+
DEFAULT_EXPIRATION = 1000 * 60 * 60 * 24 # A day later (in milliseconds)
88

99

1010
class DelayedTestStorage:
11-
""" The database-calling methods of the Delayed Test Framework """
1211

1312
@classmethod
14-
def get_delayed_test_data(self, testcase_address, done=0):
15-
""" This method queries the delayedTestData table in the DB and
13+
def get_delayed_test_data(self, test_address, is_done=0):
14+
""" This method queries the delayed_test_data table in the DB and
1615
then returns a list of rows with the matching parameters.
17-
:param testcase_address: The ID (address) of the test case.
18-
:param done: (0 for test not done or 1 for test done)
19-
:returns: A list of rows found with the matching testcase_address.
16+
:param test_address: The ID (address) of the test case.
17+
:param is_done: (0 for test not done or 1 for test done)
18+
:returns: A list of rows found with the matching test_address.
2019
"""
2120
db = DatabaseManager()
22-
query = """SELECT guid,testcaseAddress,insertedAt,expectedResult,done
23-
FROM delayedTestData
24-
WHERE testcaseAddress=%(testcase_address)s
25-
AND done=%(done)s"""
21+
query = """SELECT guid,test_address,inserted_at,expected_result,is_done
22+
FROM delayed_test_data
23+
WHERE test_address=%(test_address)s
24+
AND is_done=%(is_done)s"""
2625
data = db.fetchall_query_and_close(
27-
query, {"testcase_address": testcase_address,
28-
"done": done})
26+
query, {"test_address": test_address,
27+
"is_done": is_done})
2928
if data:
3029
return data
3130
else:
32-
logging.debug("Could not find any rows in delayedTestData.")
31+
logging.debug("Could not find any rows in delayed_test_data.")
3332
logging.debug("DB Query = " + query %
34-
{"testcase_address": testcase_address, "done": done})
33+
{"test_address": test_address, "is_done": is_done})
3534
return []
3635

3736
@classmethod
38-
def insert_delayed_test_data(self, guid_, testcase_address,
39-
expected_result, done=0,
37+
def insert_delayed_test_data(self, guid_, test_address,
38+
expected_result, is_done=0,
4039
expires_at=DEFAULT_EXPIRATION):
41-
""" This method inserts rows into the delayedTestData table
40+
""" This method inserts rows into the delayed_test_data table
4241
in the DB based on the given parameters where
4342
inserted_at (Date format) is automatically set in this method.
4443
:param guid_: The guid that is provided by the test case.
4544
(Format: str(uuid.uuid4()))
46-
:param testcase_address: The ID (address) of the test case.
45+
:param test_address: The ID (address) of the test case.
4746
:param expected_result: The result string of persistent data
4847
that will be stored in the DB.
49-
:param done: (0 for test not done or 1 for test done)
48+
:param is_done: (0 for test not done or 1 for test done)
5049
:returns: True (when no exceptions or errors occur)
5150
"""
5251
inserted_at = int(time.time() * 1000)
5352

5453
db = DatabaseManager()
55-
query = """INSERT INTO delayedTestData(
56-
guid,testcaseAddress,insertedAt,
57-
expectedResult,done,expiresAt)
58-
VALUES (%(guid)s,%(testcaseAddress)s,%(inserted_at)s,
59-
%(expected_result)s,%(done)s,%(expires_at)s)"""
54+
query = """INSERT INTO delayed_test_data(
55+
guid,test_address,inserted_at,
56+
expected_result,is_done,expires_at)
57+
VALUES (%(guid)s,%(test_address)s,%(inserted_at)s,
58+
%(expected_result)s,%(is_done)s,%(expires_at)s)"""
6059

6160
db.execute_query_and_close(
6261
query, {"guid": guid_,
63-
"testcaseAddress": testcase_address,
62+
"test_address": test_address,
6463
"inserted_at": inserted_at,
6564
"expected_result": expected_result,
66-
"done": done,
65+
"is_done": is_done,
6766
"expires_at": inserted_at + expires_at})
6867
return True
6968

7069
@classmethod
7170
def set_delayed_test_to_done(self, guid_):
72-
""" This method updates the delayedTestData table in the DB
71+
""" This method updates the delayed_test_data table in the DB
7372
to set the test with the selected guid to done.
7473
:param guid_: The guid that is provided by the test case.
7574
(Format: str(uuid.uuid4()))
7675
:returns: True (when no exceptions or errors occur)
7776
"""
7877
db = DatabaseManager()
79-
query = """UPDATE delayedTestData
80-
SET done=TRUE
78+
query = """UPDATE delayed_test_data
79+
SET is_done=TRUE
8180
WHERE guid=%(guid)s
82-
AND done=FALSE"""
81+
AND is_done=FALSE"""
8382
db.execute_query_and_close(query, {"guid": guid_})
8483
return True
8584

@@ -94,15 +93,15 @@ def get_delayed_results(self, test_id, seconds, set_done=True):
9493
in the DB to done.
9594
The results is a list of dicts where each list item contains
9695
item[0] = guid
97-
item[1] = testcaseAddress
96+
item[1] = test_address
9897
item[2] = seconds from epoch
9998
item[3] = expected results dict encoded in json
10099
:param test_id: the self.id() of the test
101100
:param seconds: the wait period until the data can be checked
102101
:returns: the results for a specific test where enough time has passed
103102
"""
104103
delayed_test_data = DelayedTestStorage.get_delayed_test_data(
105-
testcase_address=test_id)
104+
test_address=test_id)
106105
now = int(time.time() * 1000)
107106
results_to_check = []
108107
if delayed_test_data is None:

0 commit comments

Comments
 (0)