def test_purgeAttachmentsWithCutoffOld(self):
        """
        L{PurgeAttachmentsService.purgeAttachments} purges only orphaned attachments, not current ones.
        """

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota1 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertEqual(quota1, 0)

        (yield self._addAttachment("home1", "calendar1", "oldattachment1.ics", "att1"))
        (yield self._addAttachment("home1", "calendar1", "oldattachment2.ics", "att2.1"))
        (yield self._addAttachment("home1", "calendar1", "oldattachment2.ics", "att2.2"))
        (yield self._addAttachment("home1", "calendar1", "currentattachment3.ics", "att3"))
        (yield self._addAttachment("home2", "calendar2", "oldattachment1.ics", "att4"))
        (yield self._addAttachment("home2", "calendar2", "currentattachment2.ics", "att5"))
        (yield self._addAttachment("home2", "calendar2", "oldattachment3.ics", "att6"))
        (yield self._addAttachment("home2", "calendar2", "oldattachment4.ics", "att7"))
        (yield self._orphanAttachment("home1", "calendar1", "oldattachment1.ics"))
        (yield self._orphanAttachment("home2", "calendar2", "oldattachment1.ics"))
        (yield self._orphanAttachment("home2", "calendar2", "currentattachment2.ics"))

        (yield self._addManagedAttachment("home1", "calendar1", "oldmattachment1.ics", "matt1"))
        (yield self._addManagedAttachment("home1", "calendar1", "oldmattachment2.ics", "matt2.1"))
        (yield self._addManagedAttachment("home1", "calendar1", "oldmattachment2.ics", "matt2.2"))
        (yield self._addManagedAttachment("home1", "calendar1", "currentmattachment3.ics", "matt3"))
        (yield self._addManagedAttachment("home2", "calendar2", "oldmattachment1.ics", "matt4"))
        (yield self._addManagedAttachment("home2", "calendar2", "currentmattachment2.ics", "matt5"))
        (yield self._addManagedAttachment("home2", "calendar2", "oldmattachment3.ics", "matt6"))
        (yield self._addManagedAttachment("home2", "calendar2", "oldmattachment4.ics", "matt7"))

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota2 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertTrue(quota2 > quota1)

        # Dry run
        total = (yield PurgeAttachmentsService.purgeAttachments(self._sqlCalendarStore, None, 14, 2, dryrun=True, verbose=False))
        self.assertEquals(total, 13)

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota3 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertTrue(quota3 == quota2)

        # Actually remove
        total = (yield PurgeAttachmentsService.purgeAttachments(self._sqlCalendarStore, None, 14, 2, dryrun=False, verbose=False))
        self.assertEquals(total, 13)

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota4 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertTrue(quota4 < quota3)

        # There should be no more left
        total = (yield PurgeAttachmentsService.purgeAttachments(self._sqlCalendarStore, None, 14, 2, dryrun=False, verbose=False))
        self.assertEquals(total, 0)
    def test_purgeAttachmentsWithoutCutoffWithMatchingUUID(self):
        """
        L{PurgeAttachmentsService.purgeAttachments} purges only orphaned attachments, not current ones.
        """

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota1 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertEqual(quota1, 0)

        (yield self._addAttachment("home1", "calendar1", "oldattachment1.ics", "att1"))
        (yield self._addAttachment("home1", "calendar1", "oldattachment2.ics", "att2.1"))
        (yield self._addAttachment("home1", "calendar1", "oldattachment2.ics", "att2.2"))
        (yield self._addAttachment("home1", "calendar1", "currentattachment3.ics", "att3"))
        (yield self._addAttachment("home2", "calendar2", "oldattachment1.ics", "att4"))
        (yield self._addAttachment("home2", "calendar2", "currentattachment2.ics", "att5"))
        (yield self._addAttachment("home2", "calendar2", "oldattachment3.ics", "att6"))
        (yield self._addAttachment("home2", "calendar2", "oldattachment4.ics", "att7"))
        (yield self._orphanAttachment("home1", "calendar1", "oldattachment1.ics"))
        (yield self._orphanAttachment("home2", "calendar2", "oldattachment1.ics"))
        (yield self._orphanAttachment("home2", "calendar2", "currentattachment2.ics"))

        (yield self._addManagedAttachment("home1", "calendar1", "oldmattachment1.ics", "matt1"))
        (yield self._addManagedAttachment("home1", "calendar1", "oldmattachment2.ics", "matt2.1"))
        (yield self._addManagedAttachment("home1", "calendar1", "oldmattachment2.ics", "matt2.2"))
        (yield self._addManagedAttachment("home1", "calendar1", "currentmattachment3.ics", "matt3"))
        (yield self._addManagedAttachment("home2", "calendar2", "oldmattachment1.ics", "matt4"))
        (yield self._addManagedAttachment("home2", "calendar2", "currentmattachment2.ics", "matt5"))
        (yield self._addManagedAttachment("home2", "calendar2", "oldmattachment3.ics", "matt6"))
        (yield self._addManagedAttachment("home2", "calendar2", "oldmattachment4.ics", "matt7"))

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota2 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertTrue(quota2 > quota1)

        # Dry run
        total = (yield PurgeAttachmentsService.purgeAttachments(self._sqlCalendarStore, "home1", 0, 2, dryrun=True, verbose=False))
        self.assertEquals(total, 1)

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota3 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertTrue(quota3 == quota2)

        # Actually remove
        total = (yield PurgeAttachmentsService.purgeAttachments(self._sqlCalendarStore, "home1", 0, 2, dryrun=False, verbose=False))
        self.assertEquals(total, 1)

        home = (yield self.transactionUnderTest().calendarHomeWithUID("home1"))
        quota4 = (yield home.quotaUsedBytes())
        (yield self.commit())
        self.assertTrue(quota4 < quota3)

        # There should be no more left
        total = (yield PurgeAttachmentsService.purgeAttachments(self._sqlCalendarStore, "home1", 0, 2, dryrun=False, verbose=False))
        self.assertEquals(total, 0)