1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
# Copyright 2010 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Test the fake librarian."""
__metaclass__ = type
from StringIO import StringIO
import transaction
from transaction.interfaces import ISynchronizer
from zope.component import getUtility
from canonical.launchpad.database.librarian import LibraryFileAliasSet
from canonical.launchpad.interfaces.librarian import ILibraryFileAliasSet
from canonical.launchpad.webapp.testing import verifyObject
from canonical.librarian.client import LibrarianClient
from canonical.librarian.interfaces import ILibrarianClient
from canonical.testing import (
DatabaseFunctionalLayer,
LaunchpadFunctionalLayer,
)
from lp.testing import TestCaseWithFactory
from lp.testing.fakelibrarian import FakeLibrarian
class LibraryAccessScenarioMixin:
"""Simple Librarian uses that can be serviced by the FakeLibrarian.
This tests the subset of the Librarian interface that is also
implemented by the FakeLibrarian. If your test needs anything more
than this, then you want the real Librarian.
"""
def _storeFile(self):
"""Store a file in the `FakeLibrarian`.
:return: Tuple of filename, file contents, alias id.
"""
name = self.factory.getUniqueString() + '.txt'
text = self.factory.getUniqueString()
alias_id = getUtility(ILibrarianClient).addFile(
name, len(text), StringIO(text), 'text/plain')
return name, text, alias_id
def test_baseline(self):
self.assertTrue(
verifyObject(
ILibrarianClient, getUtility(ILibrarianClient)))
self.assertTrue(
verifyObject(
ILibraryFileAliasSet, getUtility(ILibraryFileAliasSet)))
def test_insert_retrieve(self):
name, text, alias_id = self._storeFile()
self.assertIsInstance(alias_id, (int, long))
transaction.commit()
library_file = getUtility(ILibrarianClient).getFileByAlias(alias_id)
self.assertEqual(text, library_file.read())
def test_alias_set(self):
name, text, alias_id = self._storeFile()
retrieved_alias = getUtility(ILibraryFileAliasSet)[alias_id]
self.assertEqual(alias_id, retrieved_alias.id)
self.assertEqual(name, retrieved_alias.filename)
def test_read(self):
name, text, alias_id = self._storeFile()
transaction.commit()
retrieved_alias = getUtility(ILibraryFileAliasSet)[alias_id]
retrieved_alias.open()
self.assertEqual(text, retrieved_alias.read())
def test_uncommitted_file(self):
name, text, alias_id = self._storeFile()
retrieved_alias = getUtility(ILibraryFileAliasSet)[alias_id]
self.assertRaises(LookupError, retrieved_alias.open)
def test_incorrect_upload_size(self):
name = self.factory.getUniqueString()
text = self.factory.getUniqueString()
wrong_length = len(text) + 1
self.assertRaises(
AssertionError,
getUtility(ILibrarianClient).addFile,
name, wrong_length, StringIO(text), 'text/plain')
def test_debugID_is_harmless(self):
# addFile takes an argument debugID that doesn't do anything
# observable. We get a LibraryFileAlias regardless.
alias_id = getUtility(ILibraryFileAliasSet).create(
'txt.txt', 3, StringIO('txt'), 'text/plain', debugID='txt')
self.assertNotEqual(None, alias_id)
class TestFakeLibrarian(LibraryAccessScenarioMixin, TestCaseWithFactory):
"""Test the supported interface subset on the fake librarian."""
layer = DatabaseFunctionalLayer
def setUp(self):
super(TestFakeLibrarian, self).setUp()
self.fake_librarian = self.installFixture(FakeLibrarian())
def test_fake(self):
self.assertTrue(verifyObject(ISynchronizer, self.fake_librarian))
self.assertIsInstance(self.fake_librarian, FakeLibrarian)
def test_pretend_commit(self):
name, text, alias_id = self._storeFile()
self.fake_librarian.pretendCommit()
retrieved_alias = getUtility(ILibraryFileAliasSet)[alias_id]
retrieved_alias.open()
self.assertEqual(text, retrieved_alias.read())
class TestRealLibrarian(LibraryAccessScenarioMixin, TestCaseWithFactory):
"""Test the supported interface subset on the real librarian."""
layer = LaunchpadFunctionalLayer
def test_real(self):
self.assertIsInstance(getUtility(ILibrarianClient), LibrarianClient)
self.assertIsInstance(
getUtility(ILibraryFileAliasSet), LibraryFileAliasSet)
|