4
4
import uuid
5
5
from seleniumbase .core .mysql import DatabaseManager
6
6
7
- DEFAULT_EXPIRATION = 1000 * 60 * 60 * 24 # A day later (in milliseconds)
7
+ DEFAULT_WAIT_TIME = 1000 * 60 * 60 * 24 # A day later (in milliseconds)
8
8
9
9
10
- class DelayedTestStorage :
10
+ class DividedTestStorage :
11
+ """
12
+ When you need a large time delay between two parts of a test, you can break
13
+ the test up into two tests, and use the divided_test_data table of the
14
+ MySQL DB to hold onto important information until the second part of the
15
+ divided test is ready to complete the remainder of the test.
16
+
17
+ *** Example ***:
18
+ You've created an email-marketing tool that sends out a follow-up email in
19
+ exactly 24 hours. The first part of the test can schedule the follow-up
20
+ email while the second part of the test verifies that the follow-up email
21
+ was sent and received successfully after enough time has passed.
22
+ """
11
23
12
24
@classmethod
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
25
+ def get_divided_test_data (self , test_address , is_done = 0 ):
26
+ """ This method queries the divided_test_data table in the test_db and
15
27
then returns a list of rows with the matching parameters.
16
28
:param test_address: The ID (address) of the test case.
17
29
:param is_done: (0 for test not done or 1 for test done)
18
30
:returns: A list of rows found with the matching test_address.
19
31
"""
20
32
db = DatabaseManager ()
21
- query = """SELECT guid,test_address,inserted_at,expected_result ,is_done
22
- FROM delayed_test_data
33
+ query = """SELECT guid,test_address,inserted_at,test_data ,is_done
34
+ FROM divided_test_data
23
35
WHERE test_address=%(test_address)s
24
36
AND is_done=%(is_done)s"""
25
37
data = db .query_fetch_all (
@@ -28,113 +40,112 @@ def get_delayed_test_data(self, test_address, is_done=0):
28
40
if data :
29
41
return data
30
42
else :
31
- logging .debug ("Could not find any rows in delayed_test_data ." )
43
+ logging .debug ("Could not find any rows in divided_test_data ." )
32
44
logging .debug ("DB Query = " + query %
33
45
{"test_address" : test_address , "is_done" : is_done })
34
46
return []
35
47
36
48
@classmethod
37
- def insert_delayed_test_data (self , guid_ , test_address ,
38
- expected_result , is_done = 0 ,
39
- expires_at = DEFAULT_EXPIRATION ):
40
- """ This method inserts rows into the delayed_test_data table
49
+ def insert_divided_test_data (self , guid_ , test_address ,
50
+ test_data , is_done = 0 ,
51
+ wait_time = DEFAULT_WAIT_TIME ):
52
+ """ This method inserts rows into the divided_test_data table
41
53
in the DB based on the given parameters where
42
54
inserted_at (Date format) is automatically set in this method.
43
55
:param guid_: The guid that is provided by the test case.
44
56
(Format: str(uuid.uuid4()))
45
57
:param test_address: The ID (address) of the test case.
46
- :param expected_result: The result string of persistent data
47
- that will be stored in the DB.
58
+ :param test_data: Additional data to store for the test.
48
59
:param is_done: (0 for test not done or 1 for test done)
49
60
:returns: True (when no exceptions or errors occur)
50
61
"""
51
62
inserted_at = int (time .time () * 1000 )
52
63
53
64
db = DatabaseManager ()
54
- query = """INSERT INTO delayed_test_data (
65
+ query = """INSERT INTO divided_test_data (
55
66
guid,test_address,inserted_at,
56
- expected_result ,is_done,expires_at )
67
+ test_data ,is_done,wait_time )
57
68
VALUES (%(guid)s,%(test_address)s,%(inserted_at)s,
58
- %(expected_result )s,%(is_done)s,%(expires_at )s)"""
69
+ %(test_data )s,%(is_done)s,%(wait_time )s)"""
59
70
60
71
db .execute_query (
61
72
query , {"guid" : guid_ ,
62
73
"test_address" : test_address ,
63
74
"inserted_at" : inserted_at ,
64
- "expected_result " : expected_result ,
75
+ "test_data " : test_data ,
65
76
"is_done" : is_done ,
66
- "expires_at " : inserted_at + expires_at })
77
+ "wait_time " : inserted_at + wait_time })
67
78
return True
68
79
69
80
@classmethod
70
- def set_delayed_test_to_done (self , guid_ ):
71
- """ This method updates the delayed_test_data table in the DB
81
+ def set_divided_test_to_done (self , guid_ ):
82
+ """ This method updates the divided_test_data table in the DB
72
83
to set the test with the selected guid to done.
73
84
:param guid_: The guid that is provided by the test case.
74
85
(Format: str(uuid.uuid4()))
75
86
:returns: True (when no exceptions or errors occur)
76
87
"""
77
88
db = DatabaseManager ()
78
- query = """UPDATE delayed_test_data
89
+ query = """UPDATE divided_test_data
79
90
SET is_done=TRUE
80
91
WHERE guid=%(guid)s
81
92
AND is_done=FALSE"""
82
93
db .execute_query (query , {"guid" : guid_ })
83
94
return True
84
95
85
96
86
- class DelayedTestAssistant :
97
+ class DividedTestAssistant :
87
98
""" Some methods for assisting tests (that don't call the DB directly) """
88
99
89
100
@classmethod
90
- def get_delayed_results (self , test_id , seconds , set_done = True ):
101
+ def get_divided_test_results (self , test_id , seconds , set_done = True ):
91
102
"""
92
- This method gets the delayed_test_data and sets the applicable rows
103
+ This method gets the divided_test data and sets the applicable rows
93
104
in the DB to done.
94
105
The results is a list of dicts where each list item contains
95
106
item[0] = guid
96
107
item[1] = test_address
97
- item[2] = seconds from epoch
98
- item[3] = expected results dict encoded in json
108
+ item[2] = time (in seconds from Epoch)
109
+ item[3] = test data dict encoded in json
99
110
:param test_id: the self.id() of the test
100
111
:param seconds: the wait period until the data can be checked
101
112
:returns: the results for a specific test where enough time has passed
102
113
"""
103
- delayed_test_data = DelayedTestStorage . get_delayed_test_data (
114
+ divided_test_data = DividedTestStorage . get_divided_test_data (
104
115
test_address = test_id )
105
116
now = int (time .time () * 1000 )
106
117
results_to_check = []
107
- if delayed_test_data is None :
118
+ if divided_test_data is None :
108
119
return results_to_check
109
- for item in delayed_test_data :
120
+ for item in divided_test_data :
110
121
if item [2 ] < now - (seconds * 1000 ):
111
122
results_to_check .append (item )
112
123
if set_done :
113
- DelayedTestStorage . set_delayed_test_to_done (item [0 ])
124
+ DividedTestStorage . set_divided_test_to_done (item [0 ])
114
125
return results_to_check
115
126
116
127
@classmethod
117
- def store_delayed_data (self , test_id , expected_result_dict ,
118
- expires_at = DEFAULT_EXPIRATION ):
128
+ def store_divided_test_data (self , test_id , test_data_dict ,
129
+ wait_time = DEFAULT_WAIT_TIME ):
119
130
"""
120
- Loads the dictionary of information into the delayed test database
131
+ Loads the dictionary of information into the divided_test_data table
121
132
:param test_id: the self.id() of the test
122
- :param expected_result_dict : a dictionary of what's to be checked later
133
+ :param test_data_dict : a dictionary of data to store for later
123
134
"""
124
- expected_result_json = json .JSONEncoder ().encode (expected_result_dict )
125
- DelayedTestStorage . insert_delayed_test_data (str (uuid .uuid4 ()),
135
+ test_data_json = json .JSONEncoder ().encode (test_data_dict )
136
+ DividedTestStorage . insert_divided_test_data (str (uuid .uuid4 ()),
126
137
test_id ,
127
- expected_result_json ,
138
+ test_data_json ,
128
139
0 ,
129
- expires_at )
140
+ wait_time )
130
141
131
142
@classmethod
132
143
def set_test_done (self , test_guid ):
133
- """ This method calls set_delayed_test_to_done to set a
144
+ """ This method calls set_divided_test_to_done to set a
134
145
row in the db to done.
135
146
:param test_guid: The guid that is provided by the test.
136
147
(Format: str(uuid.uuid4()))
137
148
:returns: True (when no exceptions or errors occur)
138
149
"""
139
- DelayedTestStorage . set_delayed_test_to_done (test_guid )
150
+ DividedTestStorage . set_divided_test_to_done (test_guid )
140
151
return True
0 commit comments