annotate tests/test_db.py @ 49:fb6ae96756c1

Added unit tests. Refactored core APIs to make them more testable. Removed unused stuff like caching the configuration in the SQL database. Fixed the web bootstrap. Some cosmetic changes to be PEP8 compliant.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 28 Jan 2013 23:13:04 -0800
parents
children 2733871775cd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
49
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os.path
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 from tests import WikkedTest
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
3 from mock import MockFileSystem
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from wikked.fs import FileSystem
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5 from wikked.db import SQLiteDatabase
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8 class DatabaseTest(WikkedTest):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9 def tearDown(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 if hasattr(self, 'wiki') and self.wiki:
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11 self.wiki.db.close()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
12 WikkedTest.tearDown(self)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
13
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 def testEmpty(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 self.wiki = self._getWikiFromStructure({})
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 self.assertEqual([], list(self.wiki.getPageUrls()))
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
18 def testOnePage(self):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19 self.wiki = self._getWikiFromStructure({
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
20 'foo.txt': 'A test page.'
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
21 })
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
22 self.assertEqual(['foo'], list(self.wiki.getPageUrls()))
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
23 page = self.wiki.getPage('foo')
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
24 self.assertEqual('foo', page.url)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 self.assertEqual(os.path.join(self.root, 'foo.txt'), page.path)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 self.assertEqual('A test page.', page.raw_text)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28 def _getWikiFromStructure(self, structure):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
29 MockFileSystem.save_structure(self.root, structure)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 wiki = self.getWiki(
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 db_factory=self._dbFactory,
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
32 fs_factory=self._fsFactory
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33 )
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 wiki.db.open()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
35 wiki.start()
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
36 return wiki
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
37
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
38 def _fsFactory(self, config):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
39 return FileSystem(self.root)
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
40
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
41 def _dbFactory(self, config):
fb6ae96756c1 Added unit tests.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
42 return SQLiteDatabase(':memory:')