/usr/lib/python2.7/dist-packages/txdav/common/datastore/test/test_sql.py is in calendarserver 5.2+dfsg-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | ##
# Copyright (c) 2012-2014 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##
"""
Tests for L{txdav.common.datastore.sql}.
"""
from twext.enterprise.dal.syntax import Select
from txdav.xml import element as davxml
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.internet.task import Clock
from twisted.trial.unittest import TestCase
from twisted.internet.defer import Deferred
from txdav.common.datastore.sql import log, CommonStoreTransactionMonitor, \
CommonHome, CommonHomeChild, ECALENDARTYPE
from txdav.common.datastore.sql_tables import schema
from txdav.common.datastore.test.util import CommonCommonTests, buildStore
from txdav.common.icommondatastore import AllRetriesFailed
from twext.enterprise.dal.syntax import Insert
from txdav.common.datastore.sql import fixUUIDNormalization
class CommonSQLStoreTests(CommonCommonTests, TestCase):
"""
Tests for shared functionality in L{txdav.common.datastore.sql}.
"""
@inlineCallbacks
def setUp(self):
"""
Set up two stores to migrate between.
"""
yield super(CommonSQLStoreTests, self).setUp()
self._sqlStore = yield buildStore(self, self.notifierFactory)
def storeUnderTest(self):
"""
Return a store for testing.
"""
return self._sqlStore
@inlineCallbacks
def test_logging(self):
"""
txn.execSQL works with all logging options on.
"""
# Patch config to turn on logging then rebuild the store
self.patch(self._sqlStore, "logLabels", True)
self.patch(self._sqlStore, "logStats", True)
self.patch(self._sqlStore, "logSQL", True)
txn = self.transactionUnderTest()
cs = schema.CALENDARSERVER
version = (yield Select(
[cs.VALUE, ],
From=cs,
Where=cs.NAME == 'VERSION',
).on(txn))
self.assertNotEqual(version, None)
self.assertEqual(len(version), 1)
self.assertEqual(len(version[0]), 1)
def test_logWaits(self):
"""
CommonStoreTransactionMonitor logs waiting transactions.
"""
c = Clock()
self.patch(CommonStoreTransactionMonitor, "callLater", c.callLater)
# Patch config to turn on log waits then rebuild the store
self.patch(self._sqlStore, "logTransactionWaits", 1)
ctr = [0]
def counter(_ignore):
ctr[0] += 1
self.patch(log, "error", counter)
txn = self.transactionUnderTest()
c.advance(2)
self.assertNotEqual(ctr[0], 0)
txn.abort()
def test_txnTimeout(self):
"""
CommonStoreTransactionMonitor terminates long transactions.
"""
c = Clock()
self.patch(CommonStoreTransactionMonitor, "callLater", c.callLater)
# Patch config to turn on transaction timeouts then rebuild the store
self.patch(self._sqlStore, "timeoutTransactions", 1)
ctr = [0]
def counter(_ignore):
ctr[0] += 1
self.patch(log, "error", counter)
txn = self.transactionUnderTest()
self.assertFalse(txn.timedout)
c.advance(2)
self.assertNotEqual(ctr[0], 0)
self.assertTrue(txn._sqlTxn._completed)
self.assertTrue(txn.timedout)
def test_logWaitsAndTxnTimeout(self):
"""
CommonStoreTransactionMonitor logs waiting transactions and terminates long transactions.
"""
c = Clock()
self.patch(CommonStoreTransactionMonitor, "callLater", c.callLater)
# Patch config to turn on log waits then rebuild the store
self.patch(self._sqlStore, "logTransactionWaits", 1)
self.patch(self._sqlStore, "timeoutTransactions", 2)
ctr = [0, 0]
def counter(logStr):
if "wait" in logStr:
ctr[0] += 1
elif "abort" in logStr:
ctr[1] += 1
self.patch(log, "error", counter)
txn = self.transactionUnderTest()
c.advance(2)
self.assertNotEqual(ctr[0], 0)
self.assertNotEqual(ctr[1], 0)
self.assertTrue(txn._sqlTxn._completed)
@inlineCallbacks
def test_subtransactionOK(self):
"""
txn.subtransaction runs loop once.
"""
txn = self.transactionUnderTest()
ctr = [0]
def _test(subtxn):
ctr[0] += 1
cs = schema.CALENDARSERVER
return Select(
[cs.VALUE, ],
From=cs,
Where=cs.NAME == 'VERSION',
).on(subtxn)
(yield txn.subtransaction(_test, retries=0))[0][0]
self.assertEqual(ctr[0], 1)
@inlineCallbacks
def test_subtransactionOKAfterRetry(self):
"""
txn.subtransaction runs loop twice when one failure.
"""
txn = self.transactionUnderTest()
ctr = [0]
def _test(subtxn):
ctr[0] += 1
if ctr[0] == 1:
raise ValueError
cs = schema.CALENDARSERVER
return Select(
[cs.VALUE, ],
From=cs,
Where=cs.NAME == 'VERSION',
).on(subtxn)
(yield txn.subtransaction(_test, retries=1))[0][0]
self.assertEqual(ctr[0], 2)
@inlineCallbacks
def test_subtransactionFailNoRetry(self):
"""
txn.subtransaction runs loop once when one failure and no retries.
"""
txn = self.transactionUnderTest()
ctr = [0]
def _test(subtxn):
ctr[0] += 1
raise ValueError
cs = schema.CALENDARSERVER
return Select(
[cs.VALUE, ],
From=cs,
Where=cs.NAME == 'VERSION',
).on(subtxn)
try:
(yield txn.subtransaction(_test, retries=0))[0][0]
except AllRetriesFailed:
pass
else:
self.fail("AllRetriesFailed not raised")
self.assertEqual(ctr[0], 1)
@inlineCallbacks
def test_subtransactionFailSomeRetries(self):
"""
txn.subtransaction runs loop three times when all fail and two retries
requested.
"""
txn = self.transactionUnderTest()
ctr = [0]
def _test(subtxn):
ctr[0] += 1
raise ValueError
cs = schema.CALENDARSERVER
return Select(
[cs.VALUE, ],
From=cs,
Where=cs.NAME == 'VERSION',
).on(subtxn)
try:
(yield txn.subtransaction(_test, retries=2))[0][0]
except AllRetriesFailed:
pass
else:
self.fail("AllRetriesFailed not raised")
self.assertEqual(ctr[0], 3)
@inlineCallbacks
def test_subtransactionAbortOuterTransaction(self):
"""
If an outer transaction that is holding a subtransaction open is
aborted, then the L{Deferred} returned by L{subtransaction} raises
L{AllRetriesFailed}.
"""
txn = self.transactionUnderTest()
cs = schema.CALENDARSERVER
waitAMoment = Deferred()
@inlineCallbacks
def later(subtxn):
yield waitAMoment
value = yield Select([cs.VALUE], From=cs).on(subtxn)
returnValue(value)
started = txn.subtransaction(later)
txn.abort()
waitAMoment.callback(True)
try:
result = yield started
except AllRetriesFailed:
pass
else:
self.fail("AllRetriesFailed not raised, %r returned instead" %
(result,))
@inlineCallbacks
def test_changeRevision(self):
"""
CommonHomeChild._changeRevision actions.
"""
class TestCommonHome(CommonHome):
pass
class TestCommonHomeChild(CommonHomeChild):
_homeChildSchema = schema.CALENDAR
_homeChildMetaDataSchema = schema.CALENDAR_METADATA
_bindSchema = schema.CALENDAR_BIND
_revisionsSchema = schema.CALENDAR_OBJECT_REVISIONS
def resourceType(self):
return davxml.ResourceType.calendar
txn = self.transactionUnderTest()
home = yield txn.homeWithUID(ECALENDARTYPE, "uid", create=True)
homeChild = yield TestCommonHomeChild.create(home, "B")
# insert test
token = yield homeChild.syncToken()
yield homeChild._changeRevision("insert", "C")
changed = yield homeChild.resourceNamesSinceToken(token)
self.assertEqual(changed, (["C"], [],))
# update test
token = yield homeChild.syncToken()
yield homeChild._changeRevision("update", "C")
changed = yield homeChild.resourceNamesSinceToken(token)
self.assertEqual(changed, (["C"], [],))
# delete test
token = yield homeChild.syncToken()
yield homeChild._changeRevision("delete", "C")
changed = yield homeChild.resourceNamesSinceToken(token)
self.assertEqual(changed, ([], ["C"],))
# missing update test
token = yield homeChild.syncToken()
yield homeChild._changeRevision("update", "D")
changed = yield homeChild.resourceNamesSinceToken(token)
self.assertEqual(changed, (["D"], [],))
# missing delete test
token = yield homeChild.syncToken()
yield homeChild._changeRevision("delete", "E")
changed = yield homeChild.resourceNamesSinceToken(token)
self.assertEqual(changed, ([], [],))
yield txn.abort()
@inlineCallbacks
def test_normalizeColumnUUIDs(self):
"""
L{_normalizeColumnUUIDs} upper-cases only UUIDs in a given column.
"""
rp = schema.RESOURCE_PROPERTY
txn = self.transactionUnderTest()
# setup
yield Insert({rp.RESOURCE_ID: 1,
rp.NAME: "asdf",
rp.VALUE: "property-value",
rp.VIEWER_UID: "not-a-uuid"}).on(txn)
yield Insert({rp.RESOURCE_ID: 2,
rp.NAME: "fdsa",
rp.VALUE: "another-value",
rp.VIEWER_UID: "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"}
).on(txn)
# test
from txdav.common.datastore.sql import _normalizeColumnUUIDs
yield _normalizeColumnUUIDs(txn, rp.VIEWER_UID)
self.assertEqual(
(yield Select([rp.RESOURCE_ID, rp.NAME,
rp.VALUE, rp.VIEWER_UID], From=rp,
OrderBy=rp.RESOURCE_ID, Ascending=True,
).on(txn)),
[[1, "asdf", "property-value", "not-a-uuid"],
[2, "fdsa", "another-value",
"AAAAAAAA-AAAA-AAAA-AAAA-AAAAAAAAAAAA"]]
)
@inlineCallbacks
def allHomeUIDs(self, table=schema.CALENDAR_HOME):
"""
Get a listing of all UIDs in the current store.
"""
results = yield (Select([table.OWNER_UID], From=table)
.on(self.transactionUnderTest()))
yield self.commit()
returnValue(results)
@inlineCallbacks
def test_fixUUIDNormalization_lowerToUpper(self):
"""
L{fixUUIDNormalization} will fix the normalization of UUIDs. If a home
is found with the wrong case but no duplicate, it will simply be
upper-cased.
"""
t1 = self.transactionUnderTest()
yield t1.calendarHomeWithUID(denormalizedUID, create=True)
yield self.commit()
yield fixUUIDNormalization(self.storeUnderTest())
self.assertEqual((yield self.allHomeUIDs()), [[normalizedUID]])
@inlineCallbacks
def test_fixUUIDNormalization_lowerToUpper_notification(self):
"""
L{fixUUIDNormalization} will fix the normalization of UUIDs. If a home
is found with the wrong case but no duplicate, it will simply be
upper-cased.
"""
t1 = self.transactionUnderTest()
yield t1.notificationsWithUID(denormalizedUID, create=True)
yield self.commit()
yield fixUUIDNormalization(self.storeUnderTest())
self.assertEqual((yield self.allHomeUIDs(schema.NOTIFICATION_HOME)),
[[normalizedUID]])
@inlineCallbacks
def test_fixUUIDNormalization_lowerToUpper_addressbook(self):
"""
L{fixUUIDNormalization} will fix the normalization of UUIDs. If a home
is found with the wrong case but no duplicate, it will simply be
upper-cased.
"""
t1 = self.transactionUnderTest()
yield t1.addressbookHomeWithUID(denormalizedUID, create=True)
yield self.commit()
yield fixUUIDNormalization(self.storeUnderTest())
self.assertEqual((yield self.allHomeUIDs(schema.ADDRESSBOOK_HOME)),
[[normalizedUID]])
from uuid import UUID
exampleUID = UUID("a" * 32)
denormalizedUID = str(exampleUID)
normalizedUID = denormalizedUID.upper()
|