Merge lp:~jtv/launchpad/fakelibrarian-commit into lp:launchpad

Proposed by Jeroen T. Vermeulen
Status: Merged
Approved by: Jeroen T. Vermeulen
Approved revision: no longer in the source branch.
Merged at revision: 11420
Proposed branch: lp:~jtv/launchpad/fakelibrarian-commit
Merge into: lp:launchpad
Diff against target: 54 lines (+23/-3)
2 files modified
lib/lp/testing/fakelibrarian.py (+14/-3)
lib/lp/testing/tests/test_fakelibrarian.py (+9/-0)
To merge this branch: bzr merge lp:~jtv/launchpad/fakelibrarian-commit
Reviewer Review Type Date Requested Status
Abel Deuring (community) code Approve
Robert Collins code Pending
Review via email: mp+33322@code.launchpad.net

Commit message

FakeLibrarian.pretendCommit

Description of the change

= FakeLibrarian commit =

The FakeLibrarian is a usable substitute for the Librarian in tests that only need the librarian inside their own process. Like the real Librarian, the FakeLibrarian makes a file available only after you commit the transaction that added it.

This branch adds a "fake commit" method to the fake librarian. The fake commit allows tests to add files to the fake librarian and continue using them as if there had been a commit—but without incurring a database commit. Commits in tests can be quite costly because they make it necessary to restore a pristine test database for the next test.

To post a comment you must log in.
Revision history for this message
Abel Deuring (adeuring) :
review: Approve (code)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'lib/lp/testing/fakelibrarian.py'
2--- lib/lp/testing/fakelibrarian.py 2010-08-20 20:31:18 +0000
3+++ lib/lp/testing/fakelibrarian.py 2010-08-22 09:27:48 +0000
4@@ -151,6 +151,19 @@
5 alias.checkCommitted()
6 return StringIO(alias.content_string)
7
8+ def pretendCommit(self):
9+ """Pretend that there's been a commit.
10+
11+ When you add a file to the librarian (real or fake), it is not
12+ fully available until the transaction that added the file has
13+ been committed. Call this method to make the FakeLibrarian act
14+ as if there's been a commit, without actually committing a
15+ database transaction.
16+ """
17+ # Note that all files have been committed to storage.
18+ for alias in self.aliases.itervalues():
19+ alias.file_committed = True
20+
21 def _makeAlias(self, file_id, name, content, content_type):
22 """Create a `LibraryFileAlias`."""
23 alias = InstrumentedLibraryFileAlias(
24@@ -195,9 +208,7 @@
25
26 def afterCompletion(self, txn):
27 """See `ISynchronizer`."""
28- # Note that all files have been committed to storage.
29- for alias in self.aliases.itervalues():
30- alias.file_committed = True
31+ self.pretendCommit()
32
33 def newTransaction(self, txn):
34 """See `ISynchronizer`."""
35
36=== modified file 'lib/lp/testing/tests/test_fakelibrarian.py'
37--- lib/lp/testing/tests/test_fakelibrarian.py 2010-08-20 20:31:18 +0000
38+++ lib/lp/testing/tests/test_fakelibrarian.py 2010-08-22 09:27:48 +0000
39@@ -109,6 +109,15 @@
40 self.assertTrue(verifyObject(ISynchronizer, self.fake_librarian))
41 self.assertIsInstance(self.fake_librarian, FakeLibrarian)
42
43+ def test_pretend_commit(self):
44+ name, text, alias_id = self._storeFile()
45+
46+ self.fake_librarian.pretendCommit()
47+
48+ retrieved_alias = getUtility(ILibraryFileAliasSet)[alias_id]
49+ retrieved_alias.open()
50+ self.assertEqual(text, retrieved_alias.read())
51+
52
53 class TestRealLibrarian(LibraryAccessScenarioMixin, TestCaseWithFactory):
54 """Test the supported interface subset on the real librarian."""