Exemple #1
0
    def runTest(self):
        folder = self.setUpFolder()
        layout = FilesLayout(folder)
        db = sqlite3.connect(':memory:')
        db.row_factory = sqlite3.Row

        file_indexer = tests.MockObject()

        indexer = PagesIndexer(db, layout, file_indexer)

        id1 = indexer.insert_page(Path('Test'), None)
        with tests.LoggingFilter('zim.notebook.index',
                                 'Error while inserting page'):
            id2 = indexer.insert_page(Path('Test'), None)

        self.assertEqual(id1, id2)
Exemple #2
0
    def runTest(self):
        def basename(name):
            if ":" in name:
                return name.split(":")[-1]
            else:
                return name

        db = sqlite3.connect(':memory:')
        db.row_factory = sqlite3.Row
        pi = PagesIndexer(db, None, tests.MockObject())
        for i, name, cont in self.PAGES:
            db.execute(
                'INSERT INTO pages(id, name, lowerbasename, sortkey, parent, source_file) VALUES (?, ?, ?, ?, 1, 1)',
                (i, name, basename(name).lower(), natural_sort_key(name)))

        ## Test PagesViewInternal methods
        iview = PagesViewInternal(db)
        i, pn = iview.resolve_pagename(Path(''), ['foo'])
        self.assertEqual((i, pn), (3, Path('Foo')))

        i, pn = iview.resolve_link(Path('Foo'), HRef.new_from_wiki_link('Bar'))
        self.assertEqual((i, pn), (2, Path('Bar')))

        ## Test the actual indexer
        pageindexer = tests.MaskedObject(pi, 'connect')
        indexer = LinksIndexer(db, pageindexer)

        for i, name, cont in self.PAGES:
            row = {
                'id': i,
                'name': name,
                'sortkey': natural_sort_key(name),
                'is_link_placeholder': False
            }
            indexer.on_page_row_inserted(pageindexer, row)

        ###
        pageindexer.setObjectAccess('insert_link_placeholder')
        for i, name, text in self.PAGES:
            tree = WikiParser().parse(text)
            row = {'id': i, 'name': name}
            indexer.on_page_changed(pageindexer, row, tree)

        indexer.update()

        links = sorted((r['source'], r['target'])
                       for r in db.execute('SELECT * FROM links'))
        self.assertEqual(links, [(3, 2), (3, 4)])

        ###
        pageindexer.setObjectAccess('remove_page')
        for i, name, cont in self.PAGES:
            row = {'id': i, 'name': name, 'is_link_placeholder': False}
            indexer.on_page_row_deleted(pageindexer, row)

        indexer.update()

        rows = db.execute('SELECT * FROM links').fetchall()
        self.assertEqual(rows, [])
Exemple #3
0
    def runTest(self):
        # Test in 4 parts:
        #   1. insert files
        #   2. update files
        #   3. add some placeholders
        #   4. delete files

        self.root = self.setUpFolder()
        layout = FilesLayout(self.root)
        db = sqlite3.connect(':memory:')
        db.row_factory = sqlite3.Row

        file_indexer = tests.MockObject()

        indexer = PagesIndexer(db, layout, file_indexer)

        def cb_filter_func(name, o, a):
            if name == 'page-changed':
                row, content = a
            elif name == 'page-row-changed':
                row, oldrow = a
            else:
                row, = a

            self.assertIsInstance(row, sqlite3.Row)
            return row['name']

        signals = tests.SignalLogger(indexer, cb_filter_func)

        # 1. insert files
        for i, path in enumerate(self.FILES):
            file = self.root.file(path)
            file.write('test 123')
            row = {'id': i, 'path': path}
            indexer.on_file_row_inserted(file_indexer, row)
            self.assertPagesDBConsistent(db)

        self.assertPagesDBEquals(db, self.PAGES)
        self.assertEqual(set(signals['page-row-inserted']), set(self.PAGES))
        self.assertEqual(set(signals['page-row-changed']),
                         set(self.NAMESPACES))
        self.assertEqual(signals['page-row-deleted'], [])
        self.assertEqual(signals['page-changed'], [])

        # 2. update files
        signals.clear()
        for i, path in enumerate(self.FILES):
            row = {'id': i, 'path': path}
            indexer.on_file_row_changed(file_indexer, row)
            self.assertPagesDBConsistent(db)

        self.assertPagesDBEquals(db, self.PAGES)
        self.assertEqual(signals['page-row-inserted'], [])
        self.assertEqual(set(signals['page-row-changed']), set(self.CONTENT))
        self.assertEqual(signals['page-row-deleted'], [])
        self.assertEqual(set(signals['page-changed']), set(self.CONTENT))

        # 3. add some placeholders
        for pagename in self.PLACEHOLDERS:
            indexer.insert_link_placeholder(Path(pagename))
            self.assertPagesDBConsistent(db)

        self.assertPagesDBEquals(db, self.PAGES + self.PLACEHOLDERS_ALL)

        for pagename in self.PLACEHOLDERS:
            indexer.delete_link_placeholder(Path(pagename))
            self.assertPagesDBConsistent(db)

        self.assertPagesDBEquals(db, self.PAGES)

        # 4. delete files
        signals.clear()
        for i, path in enumerate(self.FILES):
            file = self.root.file(path)
            file.remove()
            row = {'id': i, 'path': path}
            indexer.on_file_row_deleted(file_indexer, row)
            self.assertPagesDBConsistent(db)

        self.assertPagesDBEquals(db, [])
        self.assertEqual(signals['page-row-inserted'], [])
        self.assertEqual(set(signals['page-row-changed']), set(['foo']))
        # "foo" has source that is deleted before children
        self.assertEqual(set(signals['page-row-deleted']), set(self.PAGES))
        self.assertEqual(signals['page-changed'], ['foo'])