~launchpad-pqm/launchpad/devel

10637.3.1 by Guilherme Salgado
Use the default python version instead of a hard-coded version
1
#!/usr/bin/python -S
8687.15.22 by Karl Fogel
Add the copyright header block to the remaining .py files.
2
#
3
# Copyright 2009 Canonical Ltd.  This software is licensed under the
4
# GNU Affero General Public License version 3 (see the file LICENSE).
5
7094.1.12 by Jeroen Vermeulen
Suppressed warning.
6
# pylint: disable-msg=W0403
7
7094.1.5 by Jeroen Vermeulen
Python-wrapped version of original SQL test script.
8
"""Test Translation Message Sharing schema initializations.
9
10
This script verifies the results of the message-sharing-populate.py
11
one-off script.  It checks that all schema elements that were added for
12
translation message sharing were initialized correctly.
13
14
Run this after message-sharing-populate.py, but also occasionally
15
afterwards to ensure that the code that initializes the added schema
16
elements on new data is working correctly.
17
18
The new schema elements are not used yet.  Once they are, a next step is
19
to start unifying TranslationMessages that are common between
20
POTemplates.  When that happens, this script will start reporting errors
21
at points marked in the source below.  Change those points & run again!
22
"""
7094.1.6 by Jeroen Vermeulen
Cosmetic.
23
7094.1.5 by Jeroen Vermeulen
Python-wrapped version of original SQL test script.
24
import _pythonpath
25
26
from canonical.database.sqlbase import cursor
7094.1.11 by Jeroen Vermeulen
SQL fix in test script.
27
from canonical.launchpad.scripts import execute_zcml_for_scripts
7094.1.5 by Jeroen Vermeulen
Python-wrapped version of original SQL test script.
28
7094.1.6 by Jeroen Vermeulen
Cosmetic.
29
7094.1.5 by Jeroen Vermeulen
Python-wrapped version of original SQL test script.
30
class SchemaElementsInitializationFailure(Exception):
31
    """Convenience exception class for errors found in our data."""
32
    pass
33
34
35
def check(query, error_message, expected_result=0):
36
    """Report error if `query` result does not match `expected_result`."""
37
    cur = cursor()
38
    cur.execute(query)
39
    result, = cur.fetchone()
40
41
    if result != expected_result:
42
        counts = "expected %s, found %s" % (expected_result, result)
43
        raise SchemaElementsInitializationFailure(
44
            "%s (%s)" % (error_message, counts))
45
46
47
def test_schema():
48
    """Test schema initializations."""
49
    cur = cursor()
50
51
    # Are all TranslationMessage.language fields initialized?
52
    query = """
53
        SELECT count(*)
54
        FROM TranslationMessage
55
        WHERE language IS NULL
56
       """
57
    check(query, "Found uninitialized TranslationMessage.language")
58
59
    # Are all TranslationMessages.potemplate fields initialized?
60
    # XXX JeroenVermeulen 2008-10-06 spec=message-sharing-migration:
61
    # potemplate will be allowed to be null later on, when we really
62
    # start sharing messages.  The field means "this message is specific
63
    # to this potemplate, rather than shared").  Remove this check then.
64
    query = """
65
        SELECT count(*)
66
        FROM TranslationMessage
67
        WHERE potemplate IS NULL OR language IS NULL
68
       """
69
    check(query, "Found uninitialized TranslationMessages.potemplate.")
70
71
    # Are all TranslationMessages linked to the right languages?
72
    query = """
73
        SELECT count(*)
74
        FROM TranslationMessage
75
        LEFT JOIN POFile ON POFile.id = TranslationMessage.pofile
76
        WHERE
77
            POFile.id IS NULL OR
78
            POFile.language <> TranslationMessage.language
79
        """
80
    check(query, "Found TranslationMessages with incorrect language.")
81
82
    # Are all language variants set up?
83
    query = """
84
        SELECT count(*)
85
        FROM TranslationMessage
86
        JOIN POFile ON POFile.id = TranslationMessage.pofile
87
        WHERE (POFile.variant IS NULL) <> (TranslationMessage.variant IS NULL)
88
        """
89
    check(query, "Found TranslationMessages with wrong variant nullness.")
90
91
    # Are all variants correct?  (Easier to compare now that we know
92
    # that all the nulls are in the right places).
93
    query = """
94
        SELECT count(*)
95
        FROM TranslationMessage
96
        JOIN POFile ON POFile.id = TranslationMessage.pofile
97
        WHERE POFile.variant <> TranslationMessage.variant
98
        """
99
    check(query, "Found TranslationMessages with incorrect variants.")
100
101
    # Do all POTMsgSets with nonzero sequence numbers have linking-table
102
    # entries linking them to their POTemplates?  (Zero sequence number
103
    # means "does not participate in this POTemplate," a wart that will
104
    # go away with this schema change)
105
    query = """
106
        SELECT count(*)
107
        FROM POTMsgSet
108
        LEFT JOIN TranslationTemplateItem AS i ON i.potmsgset = POTMsgSet.id
109
        WHERE POTMsgSet.sequence <> 0 AND i.id IS NULL
110
        """
111
    check(query, "Found unlinked POTMsgSets.")
112
113
    # Conversely, is the linking table free of unexpected rows?
114
    query = """
115
        SELECT count(*)
116
        FROM TranslationTemplateItem i
117
        LEFT JOIN POTMsgSet ON i.potmsgset = POTMsgSet.id
118
        WHERE POTMsgSet.id IS NULL or POTMsgSet.sequence = 0
119
        """
120
    check(query, "Found unexpected TranslationTemplateItem rows.")
121
122
    # Are all TranslationTemplateItems correct?
123
    query = """
124
        SELECT count(*)
125
        FROM POTMsgSet
126
        JOIN TranslationTemplateItem AS i ON i.potmsgset = POTMsgSet.id
127
        WHERE
128
            i.potemplate <> POTMsgSet.potemplate OR
129
            i.sequence <> POTMsgSet.sequence
130
        """
131
    check(query, "Found incorrect TranslationTemplateItem contents.")
132
133
    # Is each POTMsgSet linked to no more than one POTemplate?
134
    # XXX JeroenVermeulen 2008-10-06 spec=message-sharing-migration:
135
    # Once we start sharing messages across templates, this will become
136
    # a real m:n relationship between POTMsgSet and POTemplate.  At that
137
    # point, remove his check.
138
    query = """
139
        SELECT count(*)
140
        FROM (
141
            SELECT count(*)
142
            FROM TranslationTemplateItem
143
            GROUP BY potmsgset
144
            HAVING count(*) > 1
7094.1.11 by Jeroen Vermeulen
SQL fix in test script.
145
            ) AS shared_potmsgset
7094.1.5 by Jeroen Vermeulen
Python-wrapped version of original SQL test script.
146
        """
147
    check(query, "Found shared POTMsgSets.")
148
149
150
if __name__ == '__main__':
7094.1.11 by Jeroen Vermeulen
SQL fix in test script.
151
    execute_zcml_for_scripts()
7094.1.5 by Jeroen Vermeulen
Python-wrapped version of original SQL test script.
152
    test_schema()
153
    print "Done."