def setUp(self): super(SQLIndexTests, self).setUp() self.site.resource.isAddressBookCollection = lambda: True self.indexDirPath = self.site.resource.fp # FIXME: since this resource lies about isCalendarCollection, it doesn't # have all the associated backend machinery to actually get children. self.db = AddressBookIndex(MinimalResourceReplacement(self.indexDirPath))
class Index(object): # # OK, here's where we get ugly. # The index code needs to be rewritten also, but in the meantime... # def __init__(self, addressbook): self.addressbook = addressbook stubResource = AddressBookStubResource(addressbook) self._oldIndex = OldIndex(stubResource) def addressbookObjects(self): addressbook = self.addressbook for name, uid, componentType in self._oldIndex.bruteForceSearch(): addressbookObject = addressbook.addressbookObjectWithName(name) # Precache what we found in the index addressbookObject._uid = uid addressbookObject._componentType = componentType yield addressbookObject
def __init__(self, addressbook): self.addressbook = addressbook stubResource = AddressBookStubResource(addressbook) self._oldIndex = OldIndex(stubResource)
class SQLIndexTests (twistedcaldav.test.util.TestCase): """ Test abstract SQL DB class """ def setUp(self): super(SQLIndexTests, self).setUp() self.site.resource.isAddressBookCollection = lambda: True self.indexDirPath = self.site.resource.fp # FIXME: since this resource lies about isCalendarCollection, it doesn't # have all the associated backend machinery to actually get children. self.db = AddressBookIndex(MinimalResourceReplacement(self.indexDirPath)) def test_reserve_uid_ok(self): uid = "test-test-test" d = self.db.isReservedUID(uid) d.addCallback(self.assertFalse) d.addCallback(lambda _: self.db.reserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: self.db.unreserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertFalse) return d def test_reserve_uid_twice(self): uid = "test-test-test" d = self.db.reserveUID(uid) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: self.assertFailure(self.db.reserveUID(uid), ReservationError)) return d def test_unreserve_unreserved(self): uid = "test-test-test" return self.assertFailure(self.db.unreserveUID(uid), ReservationError) def test_reserve_uid_timeout(self): # WARNING: This test is fundamentally flawed and will fail # intermittently because it uses the real clock. uid = "test-test-test" from twistedcaldav.config import config old_timeout = config.UIDReservationTimeOut config.UIDReservationTimeOut = 1 def _finally(): config.UIDReservationTimeOut = old_timeout d = self.db.isReservedUID(uid) d.addCallback(self.assertFalse) d.addCallback(lambda _: self.db.reserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: deferLater(reactor, 2, lambda: None)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertFalse) self.addCleanup(_finally) return d def test_index(self): data = ( ( "#1.1 Simple component", "1.1", """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-1.1 FN:Cyrus Daboo N:Daboo;Cyrus EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """, ), ) for description, name, vcard_txt in data: calendar = Component.fromString(vcard_txt) f = open(os.path.join(self.site.resource.fp.path, name), "w") f.write(vcard_txt) del f self.db.addResource(name, calendar) self.assertTrue(self.db.resourceExists(name), msg=description) self.db._db_recreate() for description, name, vcard_txt in data: self.assertTrue(self.db.resourceExists(name), msg=description) def test_index_revisions(self): data1 = """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-1-1.1 FN:Cyrus Daboo N:Daboo;Cyrus EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """ data2 = """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-2-1.1 FN:Wilfredo Sanchez N:Sanchez;Wilfredo EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """ data3 = """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-3-1.1 FN:Bruce Gaya N:Gaya;Bruce EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """ vcard = Component.fromString(data1) self.db.addResource("data1.vcf", vcard) vcard = Component.fromString(data2) self.db.addResource("data2.vcf", vcard) vcard = Component.fromString(data3) self.db.addResource("data3.vcf", vcard) self.db.deleteResource("data3.vcf") tests = ( (0, (["data1.vcf", "data2.vcf", ], [],)), (1, (["data2.vcf", ], ["data3.vcf", ],)), (2, ([], ["data3.vcf", ],)), (3, ([], ["data3.vcf", ],)), (4, ([], [],)), (5, ([], [],)), ) for revision, results in tests: self.assertEquals(self.db.whatchanged(revision), results, "Mismatched results for whatchanged with revision %d" % (revision,))
class SQLIndexTests (twistedcaldav.test.util.TestCase): """ Test abstract SQL DB class """ def setUp(self): super(SQLIndexTests, self).setUp() self.site.resource.isAddressBookCollection = lambda: True self.indexDirPath = self.site.resource.fp # FIXME: since this resource lies about isCalendarCollection, it doesn't # have all the associated backend machinery to actually get children. self.db = AddressBookIndex(MinimalResourceReplacement(self.indexDirPath)) def tearDown(self): self.db._db_close() def test_reserve_uid_ok(self): uid = "test-test-test" d = self.db.isReservedUID(uid) d.addCallback(self.assertFalse) d.addCallback(lambda _: self.db.reserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: self.db.unreserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertFalse) return d def test_reserve_uid_twice(self): uid = "test-test-test" d = self.db.reserveUID(uid) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: self.assertFailure(self.db.reserveUID(uid), ReservationError)) return d def test_unreserve_unreserved(self): uid = "test-test-test" return self.assertFailure(self.db.unreserveUID(uid), ReservationError) def test_reserve_uid_timeout(self): # WARNING: This test is fundamentally flawed and will fail # intermittently because it uses the real clock. uid = "test-test-test" from twistedcaldav.config import config old_timeout = config.UIDReservationTimeOut config.UIDReservationTimeOut = 1 def _finally(): config.UIDReservationTimeOut = old_timeout d = self.db.isReservedUID(uid) d.addCallback(self.assertFalse) d.addCallback(lambda _: self.db.reserveUID(uid)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertTrue) d.addCallback(lambda _: deferLater(reactor, 2, lambda: None)) d.addCallback(lambda _: self.db.isReservedUID(uid)) d.addCallback(self.assertFalse) self.addCleanup(_finally) return d def test_index(self): data = ( ( "#1.1 Simple component", "1.1", """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-1.1 FN:Cyrus Daboo N:Daboo;Cyrus EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """, ), ) for description, name, vcard_txt in data: calendar = Component.fromString(vcard_txt) with open(os.path.join(self.site.resource.fp.path, name), "w") as f: f.write(vcard_txt) self.db.addResource(name, calendar) self.assertTrue(self.db.resourceExists(name), msg=description) self.db._db_recreate() for description, name, vcard_txt in data: self.assertTrue(self.db.resourceExists(name), msg=description) def test_index_revisions(self): data1 = """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-1-1.1 FN:Cyrus Daboo N:Daboo;Cyrus EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """ data2 = """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-2-1.1 FN:Wilfredo Sanchez N:Sanchez;Wilfredo EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """ data3 = """BEGIN:VCARD VERSION:3.0 PRODID:-//CALENDARSERVER.ORG//NONSGML Version 1//EN UID:12345-67890-3-1.1 FN:Bruce Gaya N:Gaya;Bruce EMAIL;TYPE=INTERNET,PREF:[email protected] END:VCARD """ vcard = Component.fromString(data1) self.db.addResource("data1.vcf", vcard) vcard = Component.fromString(data2) self.db.addResource("data2.vcf", vcard) vcard = Component.fromString(data3) self.db.addResource("data3.vcf", vcard) self.db.deleteResource("data3.vcf") tests = ( (0, (["data1.vcf", "data2.vcf", ], [], [],)), (1, (["data2.vcf", ], ["data3.vcf", ], [],)), (2, ([], ["data3.vcf", ], [],)), (3, ([], ["data3.vcf", ], [],)), (4, ([], [], [],)), (5, ([], [], [],)), ) for revision, results in tests: self.assertEquals(self.db.whatchanged(revision), results, "Mismatched results for whatchanged with revision %d" % (revision,))