Skip to content

Commit ee7306a

Browse files
committed
test_db method naming
1 parent 8798dba commit ee7306a

File tree

3 files changed

+60
-49
lines changed

3 files changed

+60
-49
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -643,25 +643,25 @@ The following example below (taken from the Delayed Test Data Manager) shows how
643643
import logging
644644
from seleniumbase.core.mysql import DatabaseManager
645645

646-
def get_delayed_test_data(self, test_address, is_done=0):
646+
def get_divided_test_data(self, test_address, is_done=0):
647647
""" Returns a list of rows """
648648
db = DatabaseManager()
649-
query = """SELECT guid,test_address,inserted_at,expected_result,is_done
650-
FROM delayed_test_data
649+
query = """SELECT guid,test_address,inserted_at,test_data,is_done
650+
FROM divided_test_data
651651
WHERE test_address=%(test_address)s
652652
AND is_done=%(is_done)s"""
653653
data = db.fetchall_query(query, {"test_address":test_address, "is_done":is_done})
654654
if data:
655655
return data
656656
else:
657-
logging.debug("Could not find any rows in delayed_test_data.")
657+
logging.debug("Could not find any rows in divided_test_data.")
658658
logging.debug("DB Query = " + query % {"test_address":test_address, "is_done":is_done})
659659
return []
660660
```
661661

662662
Now you know how to pull data from your MySQL DB.
663663

664-
Delayed Data usage example: If you scheduled an email to go out 3 hours from now and you wanted to check that the email gets received (but you don't want your test sitting idle for 3 hours) you can store the email credentials as a unique time-stamp for the email subject in the DB (along with a time for when it's safe for the email to be searched for) and then a later-running test can do the checking after the right amount of time has passed.
664+
Divided Test usage example: If you scheduled an email to go out 3 hours from now and you wanted to check that the email gets received (but you don't want your test sitting idle for 3 hours) you can store the email credentials as a unique time-stamp for the email subject in the DB (along with a time for when it's safe for the email to be searched for) and then a later-running test can do the checking after the right amount of time has passed.
665665

666666

667667
### ![http://seleniumbase.com](https://cdn2.hubspot.net/hubfs/100006/images/super_logo_tiny.png "SeleniumBase") Wrap-Up

seleniumbase/core/create_db_tables.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ CREATE TABLE `execution` (
2929
PRIMARY KEY (`guid`)
3030
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3131

32-
# delayed_test_data table
32+
# divided_test_data table
3333
# -----------------------------------
34-
CREATE TABLE `delayed_test_data` (
34+
CREATE TABLE `divided_test_data` (
3535
`guid` varchar(64) NOT NULL DEFAULT '',
3636
`test_address` varchar(255) NOT NULL DEFAULT '',
3737
`inserted_at` bigint(20) NOT NULL,
38-
`expected_result` text,
38+
`test_data` text,
3939
`is_done` tinyint(1) DEFAULT '0',
40-
`expires_at` bigint(20) DEFAULT NULL,
40+
`wait_time` bigint(20) DEFAULT NULL,
4141
PRIMARY KEY (`guid`),
4242
UNIQUE KEY `uuid` (`guid`)
4343
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

seleniumbase/fixtures/delayed_data_manager.py

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,34 @@
44
import uuid
55
from seleniumbase.core.mysql import DatabaseManager
66

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)
88

99

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+
"""
1123

1224
@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
1527
then returns a list of rows with the matching parameters.
1628
:param test_address: The ID (address) of the test case.
1729
:param is_done: (0 for test not done or 1 for test done)
1830
:returns: A list of rows found with the matching test_address.
1931
"""
2032
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
2335
WHERE test_address=%(test_address)s
2436
AND is_done=%(is_done)s"""
2537
data = db.query_fetch_all(
@@ -28,113 +40,112 @@ def get_delayed_test_data(self, test_address, is_done=0):
2840
if data:
2941
return data
3042
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.")
3244
logging.debug("DB Query = " + query %
3345
{"test_address": test_address, "is_done": is_done})
3446
return []
3547

3648
@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
4153
in the DB based on the given parameters where
4254
inserted_at (Date format) is automatically set in this method.
4355
:param guid_: The guid that is provided by the test case.
4456
(Format: str(uuid.uuid4()))
4557
: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.
4859
:param is_done: (0 for test not done or 1 for test done)
4960
:returns: True (when no exceptions or errors occur)
5061
"""
5162
inserted_at = int(time.time() * 1000)
5263

5364
db = DatabaseManager()
54-
query = """INSERT INTO delayed_test_data(
65+
query = """INSERT INTO divided_test_data(
5566
guid,test_address,inserted_at,
56-
expected_result,is_done,expires_at)
67+
test_data,is_done,wait_time)
5768
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)"""
5970

6071
db.execute_query(
6172
query, {"guid": guid_,
6273
"test_address": test_address,
6374
"inserted_at": inserted_at,
64-
"expected_result": expected_result,
75+
"test_data": test_data,
6576
"is_done": is_done,
66-
"expires_at": inserted_at + expires_at})
77+
"wait_time": inserted_at + wait_time})
6778
return True
6879

6980
@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
7283
to set the test with the selected guid to done.
7384
:param guid_: The guid that is provided by the test case.
7485
(Format: str(uuid.uuid4()))
7586
:returns: True (when no exceptions or errors occur)
7687
"""
7788
db = DatabaseManager()
78-
query = """UPDATE delayed_test_data
89+
query = """UPDATE divided_test_data
7990
SET is_done=TRUE
8091
WHERE guid=%(guid)s
8192
AND is_done=FALSE"""
8293
db.execute_query(query, {"guid": guid_})
8394
return True
8495

8596

86-
class DelayedTestAssistant:
97+
class DividedTestAssistant:
8798
""" Some methods for assisting tests (that don't call the DB directly) """
8899

89100
@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):
91102
"""
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
93104
in the DB to done.
94105
The results is a list of dicts where each list item contains
95106
item[0] = guid
96107
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
99110
:param test_id: the self.id() of the test
100111
:param seconds: the wait period until the data can be checked
101112
:returns: the results for a specific test where enough time has passed
102113
"""
103-
delayed_test_data = DelayedTestStorage.get_delayed_test_data(
114+
divided_test_data = DividedTestStorage.get_divided_test_data(
104115
test_address=test_id)
105116
now = int(time.time() * 1000)
106117
results_to_check = []
107-
if delayed_test_data is None:
118+
if divided_test_data is None:
108119
return results_to_check
109-
for item in delayed_test_data:
120+
for item in divided_test_data:
110121
if item[2] < now - (seconds * 1000):
111122
results_to_check.append(item)
112123
if set_done:
113-
DelayedTestStorage.set_delayed_test_to_done(item[0])
124+
DividedTestStorage.set_divided_test_to_done(item[0])
114125
return results_to_check
115126

116127
@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):
119130
"""
120-
Loads the dictionary of information into the delayed test database
131+
Loads the dictionary of information into the divided_test_data table
121132
: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
123134
"""
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()),
126137
test_id,
127-
expected_result_json,
138+
test_data_json,
128139
0,
129-
expires_at)
140+
wait_time)
130141

131142
@classmethod
132143
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
134145
row in the db to done.
135146
:param test_guid: The guid that is provided by the test.
136147
(Format: str(uuid.uuid4()))
137148
:returns: True (when no exceptions or errors occur)
138149
"""
139-
DelayedTestStorage.set_delayed_test_to_done(test_guid)
150+
DividedTestStorage.set_divided_test_to_done(test_guid)
140151
return True

0 commit comments

Comments
 (0)