~launchpad-pqm/launchpad/devel

8687.15.18 by Karl Fogel
Add the copyright header block to files under lib/canonical/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
3
4
import unittest
5
6620.9.2 by Francis J. Lacoste
Reset the store in the librarian after each transactions.
6
import transaction
7
from zope.component import getUtility
8
14606.2.5 by William Grant
Move the rest of canonical.librarian to lp.services.librarianserver.
9
from lp.services.librarian.model import LibraryFileContent
10
from lp.services.librarianserver import db
14600.2.2 by Curtis Hovey
Moved webapp to lp.services.
11
from lp.services.webapp.interfaces import (
14578.2.1 by William Grant
Move librarian stuff from canonical.launchpad to lp.services.librarian. canonical.librarian remains untouched.
12
    DEFAULT_FLAVOR,
13
    IStoreSelector,
14
    MAIN_STORE,
15
    )
14604.1.1 by Curtis Hovey
Separate test-authoring classes from test-running classes.
16
from lp.testing.layers import LaunchpadZopelessLayer
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
17
18
4450.7.1 by James Henstridge
move librarian over to use the webapp database adapter
19
class DBTestCase(unittest.TestCase):
5821.2.97 by James Henstridge
Fix remaining librarian tests.
20
    layer = LaunchpadZopelessLayer
4450.7.1 by James Henstridge
move librarian over to use the webapp database adapter
21
22
    def setUp(self):
5821.2.97 by James Henstridge
Fix remaining librarian tests.
23
        self.layer.switchDbUser('librarian')
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
24
25
    def test_lookupByDigest(self):
26
        # Create library
27
        library = db.Library()
28
29
        # Initially it should be empty
30
        self.assertEqual([], library.lookupBySHA1('deadbeef'))
31
32
        # Add a file, check it is found by lookupBySHA1
3258.8.5 by Andrew Bennetts
Trivially update librarian tests for md5.
33
        fileID = library.add('deadbeef', 1234, 'abababab')
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
34
        self.assertEqual([fileID], library.lookupBySHA1('deadbeef'))
35
36
        # Add a new file with the same digest
3258.8.5 by Andrew Bennetts
Trivially update librarian tests for md5.
37
        newFileID = library.add('deadbeef', 1234, 'abababab')
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
38
        # Check it gets a new ID anyway
39
        self.assertNotEqual(fileID, newFileID)
40
        # Check it is found by lookupBySHA1
41
        self.assertEqual(sorted([fileID, newFileID]),
1336 by Canonical.com Patch Queue Manager
Fix transaction handling bugs in the librarian, and more tests
42
                         sorted(library.lookupBySHA1('deadbeef')))
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
43
1336 by Canonical.com Patch Queue Manager
Fix transaction handling bugs in the librarian, and more tests
44
        aliasID = library.addAlias(fileID, 'file1', 'text/unknown')
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
45
        alias = library.getAlias(aliasID, None, '/')
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
46
        self.assertEqual('file1', alias.filename)
47
        self.assertEqual('text/unknown', alias.mimetype)
4450.7.1 by James Henstridge
move librarian over to use the webapp database adapter
48
1102 by Canonical.com Patch Queue Manager
Lucille had some XXXs which should have been NOTEs
49
11627.1.2 by Julian Edwards
move the tests from the old librarian module to the services module
50
class TestLibrarianStuff(unittest.TestCase):
51
    """Tests for the librarian."""
6620.9.2 by Francis J. Lacoste
Reset the store in the librarian after each transactions.
52
53
    layer = LaunchpadZopelessLayer
54
55
    def setUp(self):
56
        self.layer.switchDbUser('librarian')
6555.7.9 by Stuart Bishop
Use new store selection api
57
        self.store = getUtility(IStoreSelector).get(
58
                MAIN_STORE, DEFAULT_FLAVOR)
6620.9.2 by Francis J. Lacoste
Reset the store in the librarian after each transactions.
59
        self.content_id = db.Library().add('deadbeef', 1234, 'abababab')
60
        self.file_content = self._getTestFileContent()
61
        transaction.commit()
62
63
    def _getTestFileContent(self):
64
        """Return the file content object that created."""
65
        return self.store.find(LibraryFileContent, id=self.content_id).one()
66
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
67
    def test_getAlias(self):
68
        # Library.getAlias() returns the LibrarayFileAlias for a given
69
        # LibrarayFileAlias ID.
70
        library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
71
        alias = library.getAlias(1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
72
        self.assertEqual(1, alias.id)
73
74
    def test_getAlias_no_such_record(self):
75
        # Library.getAlias() raises a LookupError, if no record with
76
        # the given ID exists.
77
        library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
78
        self.assertRaises(LookupError, library.getAlias, -1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
79
80
    def test_getAlias_content_is_null(self):
81
        # Library.getAlias() raises a LookupError, if no content
82
        # record for the given alias exists.
83
        library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
84
        alias = library.getAlias(1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
85
        alias.content = None
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
86
        self.assertRaises(LookupError, library.getAlias, 1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
87
88
    def test_getAlias_content_is_none(self):
89
        # Library.getAlias() raises a LookupError, if the matching
90
        # record does not reference any LibraryFileContent record.
91
        library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
92
        alias = library.getAlias(1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
93
        alias.content = None
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
94
        self.assertRaises(LookupError, library.getAlias, 1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
95
96
    def test_getAlias_content_wrong_library(self):
97
        # Library.getAlias() raises a LookupError, if a restricted
98
        # library looks up a unrestricted LibraryFileAlias and
99
        # vice versa.
100
        restricted_library = db.Library(restricted=True)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
101
        self.assertRaises(
102
            LookupError, restricted_library.getAlias, 1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
103
104
        unrestricted_library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
105
        alias = unrestricted_library.getAlias(1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
106
        alias.restricted = True
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
107
        self.assertRaises(
108
            LookupError, unrestricted_library.getAlias, 1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
109
110
    def test_getAliases(self):
111
        # Library.getAliases() returns a sequence
112
        # [(LFA.id, LFA.filename, LFA.mimetype), ...] where LFA are
113
        # LibrarayFileAlias records having the given LibraryFileContent
114
        # ID.
115
        library = db.Library(restricted=False)
116
        aliases = library.getAliases(1)
7675.415.5 by Abel Deuring
implemented reviewer's comments
117
        expected_aliases = [
118
            (1, u'netapplet-1.0.0.tar.gz', u'application/x-gtar'),
119
            (2, u'netapplet_1.0.0.orig.tar.gz', u'application/x-gtar'),
120
            ]
121
        self.assertEqual(expected_aliases, aliases)
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
122
123
    def test_getAliases_content_is_none(self):
124
        # Library.getAliases() does not return records which do not
125
        # reference any LibraryFileContent record.
126
        library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
127
        alias = library.getAlias(1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
128
        alias.content = None
129
        aliases = library.getAliases(1)
7675.415.5 by Abel Deuring
implemented reviewer's comments
130
        expected_aliases = [
131
            (2, u'netapplet_1.0.0.orig.tar.gz', u'application/x-gtar'),
132
            ]
133
        self.assertEqual(expected_aliases, aliases)
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
134
135
    def test_getAliases_content_wrong_library(self):
136
        # Library.getAliases() does not return data from restriceded
137
        # LibrarayFileAlias records when called from a unrestricted
138
        # library and vice versa.
139
        unrestricted_library = db.Library(restricted=False)
7675.810.1 by Abel Deuring
fixed test failures in canonical.librarian.ftests.test_db
140
        alias = unrestricted_library.getAlias(1, None, '/')
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
141
        alias.restricted = True
142
143
        aliases = unrestricted_library.getAliases(1)
7675.415.5 by Abel Deuring
implemented reviewer's comments
144
        expected_aliases = [
145
            (2, u'netapplet_1.0.0.orig.tar.gz', u'application/x-gtar'),
146
            ]
147
        self.assertEqual(expected_aliases, aliases)
7675.415.4 by Abel Deuring
adjust storm classes to no longer define and use the dropped columns LibraryFileContent.deleted, LibraryFileContent.datemirrored (exception: librarian_gc.py is not yet fixed); add a property 'deleted' to LibraryFileAlias; replace usage of LibraryFileAlias.content.deleted by LibraryFileAlias.deleted; fix failing tests, except test_gc.py
148
149
        restricted_library = db.Library(restricted=True)
150
        aliases = restricted_library.getAliases(1)
7675.415.5 by Abel Deuring
implemented reviewer's comments
151
        expected_aliases = [
152
            (1, u'netapplet-1.0.0.tar.gz', u'application/x-gtar'),
153
            ]
154
        self.assertEqual(expected_aliases, aliases)