Merge lp:~jtv/launchpad/bug-629442 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: 11499
Proposed branch: lp:~jtv/launchpad/bug-629442
Merge into: lp:launchpad
Diff against target: 70 lines (+18/-6)
2 files modified
lib/lp/testing/fakelibrarian.py (+7/-3)
lib/lp/testing/tests/test_fakelibrarian.py (+11/-3)
To merge this branch: bzr merge lp:~jtv/launchpad/bug-629442
Reviewer Review Type Date Requested Status
Brad Crittenden (community) code Approve
Review via email: mp+34539@code.launchpad.net

Commit message

FakeLibrarian.create returns LibraryFileAlias.

Description of the change

= Bug 629442 =

Fixes the bug where FakeLibrarian.create returns a LibraryFileAlias id instead of a LibraryFileAlias.

Unlike the real librarian (and I mean, why!?) this does not involve creating a LibraryFileAlias, returning an id, then querying the database for the LibraryFileAlias with that id. I just made addFile and create both use the same private method that returns the LibraryFileAlias. (This seemed a bit cleaner than having the call relationship inverted compared to the real librarian and risking problems with future changes).

To test,
{{{
./bin/test -vvc lp.testing.tests.test_fakelibrarian
}}}

No lint.

Jeroen

To post a comment you must log in.
Revision history for this message
Brad Crittenden (bac) wrote :

Looks quite nice Jeroen.

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-27 11:08:27 +0000
3+++ lib/lp/testing/fakelibrarian.py 2010-09-03 13:19:43 +0000
4@@ -129,6 +129,11 @@
5
6 def addFile(self, name, size, file, contentType, expires=None):
7 """See `IFileUploadClient`."""
8+ return self._storeFile(
9+ name, size, file, contentType, expires=expires).id
10+
11+ def _storeFile(self, name, size, file, contentType, expires=None):
12+ """Like `addFile`, but returns the `LibraryFileAlias`."""
13 content = file.read()
14 real_size = len(content)
15 if real_size != size:
16@@ -139,8 +144,7 @@
17 file_ref = self._makeLibraryFileContent(content)
18 alias = self._makeAlias(file_ref.id, name, content, contentType)
19 self.aliases[alias.id] = alias
20-
21- return alias.id
22+ return alias
23
24 def remoteAddFile(self, name, size, file, contentType, expires=None):
25 """See `IFileUploadClient`."""
26@@ -191,7 +195,7 @@
27 def create(self, name, size, file, contentType, expires=None,
28 debugID=None, restricted=False):
29 "See `ILibraryFileAliasSet`."""
30- return self.addFile(name, size, file, contentType, expires=expires)
31+ return self._storeFile(name, size, file, contentType, expires=expires)
32
33 def __getitem__(self, key):
34 "See `ILibraryFileAliasSet`."""
35
36=== modified file 'lib/lp/testing/tests/test_fakelibrarian.py'
37--- lib/lp/testing/tests/test_fakelibrarian.py 2010-08-27 11:16:39 +0000
38+++ lib/lp/testing/tests/test_fakelibrarian.py 2010-09-03 13:19:43 +0000
39@@ -11,7 +11,10 @@
40 from transaction.interfaces import ISynchronizer
41 from zope.component import getUtility
42
43-from canonical.launchpad.database.librarian import LibraryFileAliasSet
44+from canonical.launchpad.database.librarian import (
45+ LibraryFileAlias,
46+ LibraryFileAliasSet,
47+ )
48 from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
49 from canonical.launchpad.webapp.testing import verifyObject
50 from canonical.librarian.client import LibrarianClient
51@@ -90,12 +93,17 @@
52 getUtility(ILibrarianClient).addFile,
53 name, wrong_length, StringIO(text), 'text/plain')
54
55+ def test_create_returns_alias(self):
56+ alias = getUtility(ILibraryFileAliasSet).create(
57+ 'foo.txt', 3, StringIO('foo'), 'text/plain', debugID='txt')
58+ self.assertIsInstance(alias, LibraryFileAlias)
59+
60 def test_debugID_is_harmless(self):
61 # addFile takes an argument debugID that doesn't do anything
62 # observable. We get a LibraryFileAlias regardless.
63- alias_id = getUtility(ILibraryFileAliasSet).create(
64+ alias = getUtility(ILibraryFileAliasSet).create(
65 'txt.txt', 3, StringIO('txt'), 'text/plain', debugID='txt')
66- self.assertNotEqual(None, alias_id)
67+ self.assertNotEqual(None, alias)
68
69
70 class TestFakeLibrarian(LibraryAccessScenarioMixin, TestCaseWithFactory):