Skip to content

Commit

Permalink
add tests for DBConnection.parmlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Dustin J. Mitchell committed Feb 21, 2010
1 parent 5d41eef commit 33e52d4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
28 changes: 18 additions & 10 deletions buildbot/db.py
Expand Up @@ -496,7 +496,8 @@ def __init__(self, spec):

self._started = False

def getCurrentTime(self):
def _getCurrentTime(self):
# this is a seam for use in testing
return time.time()

def start(self):
Expand All @@ -514,7 +515,8 @@ def stop(self):
del self._pool

def quoteq(self, query):
"""Given a query that contains qmark-style placeholders, like::
"""
Given a query that contains qmark-style placeholders, like::
INSERT INTO foo (col1, col2) VALUES (?,?)
replace the '?' with '%s' if the backend uses format-style
placeholders, like::
Expand All @@ -526,6 +528,12 @@ def quoteq(self, query):
return query

def parmlist(self, count):
"""
When passing long lists of values to e.g., an INSERT query, it is
tedious to pass long strings of ? placeholders. This function will
create a parenthesis-enclosed list of COUNT placeholders. Note that
the placeholders have already had quoteq() applied.
"""
p = self.quoteq("?")
return "(" + ",".join([p]*count) + ")"

Expand Down Expand Up @@ -570,7 +578,7 @@ def _end_operation(self, t):
def runInteractionNow(self, interaction, *args, **kwargs):
# synchronous+blocking version of runInteraction()
assert self._started
start = self.getCurrentTime()
start = self._getCurrentTime()
t = self._start_operation()
try:
return self._runInteractionNow(interaction, *args, **kwargs)
Expand Down Expand Up @@ -621,7 +629,7 @@ def subscribe_to(self, category, observer):
def runQuery(self, *args, **kwargs):
assert self._started
self._pending_operation_count += 1
start = self.getCurrentTime()
start = self._getCurrentTime()
#t = self._start_operation()
d = self._pool.runQuery(*args, **kwargs)
#d.addBoth(self._runQuery_done, start, t)
Expand All @@ -633,15 +641,15 @@ def _runQuery_done(self, res, start, t):
return res

def _add_query_time(self, start):
elapsed = self.getCurrentTime() - start
elapsed = self._getCurrentTime() - start
self._query_times.append(elapsed)
if len(self._query_times) > self.MAX_QUERY_TIMES:
self._query_times.popleft()

def runInteraction(self, *args, **kwargs):
assert self._started
self._pending_operation_count += 1
start = self.getCurrentTime()
start = self._getCurrentTime()
t = self._start_operation()
d = self._pool.runInteraction(*args, **kwargs)
d.addBoth(self._runInteraction_done, start, t)
Expand Down Expand Up @@ -1005,7 +1013,7 @@ def get_sourcestampid(self, ss, t):
def create_buildset(self, ssid, reason, properties, builderNames, t,
external_idstring=None):
# this creates both the BuildSet and the associated BuildRequests
now = self.getCurrentTime()
now = self._getCurrentTime()
t.execute("SELECT id FROM buildsets ORDER BY id DESC LIMIT 1")
bsid = _one_or_else(t.fetchall(), 0) + 1
t.execute(self.quoteq("INSERT INTO buildsets"
Expand Down Expand Up @@ -1154,7 +1162,7 @@ def _txn_claim_buildrequests(self, t, now, master_name, master_incarnation,
def build_started(self, brid, buildnumber):
return self.runInteractionNow(self._txn_build_started, brid, buildnumber)
def _txn_build_started(self, t, brid, buildnumber):
now = self.getCurrentTime()
now = self._getCurrentTime()
t.execute("SELECT id FROM builds ORDER BY id DESC LIMIT 1")
bid = _one_or_else(t.fetchall(), 0) + 1
t.execute(self.quoteq("INSERT INTO builds (id, number, brid, start_time)"
Expand All @@ -1166,7 +1174,7 @@ def _txn_build_started(self, t, brid, buildnumber):
def builds_finished(self, bids):
return self.runInteractionNow(self._txn_build_finished, bids)
def _txn_build_finished(self, t, bids):
now = self.getCurrentTime()
now = self._getCurrentTime()
q = self.quoteq("UPDATE builds SET finish_time = ?"
" WHERE id IN " + self.parmlist(len(bids)))
qargs = [now] + list(bids)
Expand Down Expand Up @@ -1207,7 +1215,7 @@ def _txn_resubmit_buildreqs(self, t, brids):
def retire_buildrequests(self, brids, results):
return self.runInteractionNow(self._txn_retire_buildreqs, brids,results)
def _txn_retire_buildreqs(self, t, brids, results):
now = self.getCurrentTime()
now = self._getCurrentTime()
#q = self.db.quoteq("DELETE FROM buildrequests WHERE id IN "
# + self.db.parmlist(len(brids)))
q = self.quoteq("UPDATE buildrequests"
Expand Down
21 changes: 15 additions & 6 deletions buildbot/test/unit/test_db.py
Expand Up @@ -139,17 +139,26 @@ def setUp(self):
def tearDown(self):
self.dbc.stop()

# TODO: why is this method here??
def test_getCurrentTime(self):
self.assertTrue(self.dbc.getCurrentTime() > 0)
def test_quoteq_format(self):
self.dbc.paramstyle = "format" # override default
self.assertEqual(
self.dbc.quoteq("SELECT * from developers where name='?'"),
"SELECT * from developers where name='%s'")

def test_quoteq_identity(self):
# FYI, sqlite uses qmark, so this test is somewhat tautalogical
assert self.dbc.paramstyle == "qmark"
def test_quoteq_qmark(self):
assert self.dbc.paramstyle == "qmark" # default for sqlite
self.assertEqual(
self.dbc.quoteq("SELECT * from developers where name='?'"),
"SELECT * from developers where name='?'")

def test_paramlist_single(self):
self.dbc.paramstyle = "format" # override default
self.assertEqual(self.dbc.parmlist(1), "(%s)")

def test_paramlist_multiple(self):
self.dbc.paramstyle = "format" # override default
self.assertEqual(self.dbc.parmlist(3), "(%s,%s,%s)")

def test_runQueryNow_simple(self):
self.assertEqual(self.dbc.runQueryNow("SELECT 1"),
[(1,)])
Expand Down

0 comments on commit 33e52d4

Please sign in to comment.