Exemplo n.º 1
0
    def testDeleteWithError(self):
        ids = Ids(self.tmp_path / "test.ids")
        try:
            for i in range(10):
                ids.add('id:{}'.format(i))

            deleteIds = DeleteIds(Bucket(id='test'))

            def delete(anUpload):
                if anUpload.id == 'id:7':
                    raise ValueError('fout')

            observer = CallTrace(
                methods=dict(delete=delete,
                             deleteIdentifier=ids.remove,
                             flushIds=lambda **kwargs: ids.reopen()))
            deleteIds.addObserver(observer)
            self.assertRaises(ValueError, lambda: deleteIds.delete(ids))
            self.assertEqual(['id:7', 'id:8', 'id:9'],
                             readIds(join(self.tempdir, 'test.ids')))
            self.assertEqual(
                8,
                len([m for m in observer.calledMethodNames()
                     if m == 'delete']))
        finally:
            ids.close()
Exemplo n.º 2
0
 def testNoIdsNoFile(self):
     myidsPath = self.tmp_path / 'my.ids'
     ids = Ids(myidsPath)
     ids.add('some:id')
     ids.close()
     self.assertTrue(myidsPath.is_file())
     ids.remove('some:id')
     ids.close()
     self.assertFalse(myidsPath.is_file())
Exemplo n.º 3
0
 def testEscaping(self):
     idsPath = self.tmp_path / 'pruebo.ids'
     ids = Ids(idsPath)
     try:
         ids.add("needs_\n_escape")
         self.assertEqual('needs_%0A_escape\n', idsPath.read_text())
         self.assertEqual(['needs_\n_escape'], ids.getIds())
         ids.reopen()
         self.assertEqual('needs_%0A_escape\n', idsPath.read_text())
         self.assertEqual(['needs_\n_escape'], ids.getIds())
         ids.reopen()
         self.assertEqual(['needs_\n_escape'], ids.getIds())
     finally:
         ids.close()
Exemplo n.º 4
0
class IdsTest(SeecrTestCase):
    def tearDown(self):
        ids = getattr(self, 'ids', None)
        if ids: ids.close()
        SeecrTestCase.tearDown(self)
        
    def testAddOne(self):
        self.ids = Ids(self.tempdir + '/doesnotexistyet/', 'idstest')
        self.ids.add('id:1')
        self.assertEquals(1, len(self.ids))
        
    def testAddTwice(self):
        self.ids = Ids(self.tempdir, 'idstest')
        self.ids.add('id:1')
        self.ids.add('id:1')
        self.assertEquals(1, len(self.ids))
        self.assertEquals(1, len(open(self.tempdir + '/idstest.ids').readlines()))
        
    def testInit(self):
        self.writeTestIds('one',['id:1'])
        self.ids = Ids(self.tempdir, 'one')
        self.assertEquals(1, len(self.ids))
        self.writeTestIds('three',['id:1', 'id:2', 'id:3'])
        self.ids = Ids(self.tempdir, 'three')
        self.assertEquals(3, len(self.ids))
        
    def testRemoveExistingId(self):
        self.writeTestIds('three',['id:1', 'id:2', 'id:3'])
        self.ids = Ids(self.tempdir, 'three')
        self.ids.remove('id:1')
        self.assertEquals(2, len(self.ids))
        self.assertEquals(2, len(open(self.tempdir + '/three.ids').readlines()))
        self.ids.close()
        
    def testRemoveNonExistingId(self):
        self.writeTestIds('three',['id:1', 'id:2', 'id:3'])
        self.ids = Ids(self.tempdir, 'three')
        self.ids.remove('id:4')
        self.assertEquals(3, len(self.ids))
        self.ids.close()
        self.assertEquals(3, len(open(self.tempdir + '/three.ids').readlines()))

    def testAddStrangeIds(self):
        self.ids = Ids(self.tempdir, 'idstest')
        self.ids.add('id:1')
        self.ids.add('\n   id:1')
        self.ids.add('   id:2')
        self.assertEquals(3, len(open(self.tempdir + '/idstest.ids').readlines()))
        self.assertEquals(['id:1', '\n   id:1', '   id:2'], list(self.ids))

        self.ids = Ids(self.tempdir, 'idstest')
        self.assertEquals(['id:1', '\n   id:1', '   id:2'], list(self.ids))

    def testRemoveStrangeId(self):
        self.ids = Ids(self.tempdir, 'idstest')
        self.ids.add('id:1')
        self.ids.add('\n   id:1')
        self.ids.add('   id:2')
        self.assertEquals(['id:1', '\n   id:1', '   id:2'], list(self.ids))
        self.ids.remove('id:1')
        self.ids.remove('\n   id:1')
        self.ids.remove('   id:2')
        self.assertEquals([], list(self.ids))
        
        
    def writeTestIds(self, name, ids):
        w = open(self.tempdir+ '/' + name + '.ids', 'w')
        try:
            for anId in ids:
                w.write(anId + '\n')
        finally:
            w.close()
Exemplo n.º 5
0
def _Ids(path):
    _ids = Ids(path)
    try:
        yield _ids
    finally:
        _ids.close()