~launchpad-pqm/launchpad/devel

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
The POFileTranslator table is an eager materialized view, containing
references to the most recent posubmission made by a particular person
to a particular pofile.

This table is read only to most users, but for this test we connect
as a superuser so we can poke at it directly.

    >>> from lp.services.database.sqlbase import connect
    >>> connection = connect(user='testadmin')
    >>> cur = connection.cursor()
    >>> def pofiletranslator(person_id, pofile_id):
    ...     cur.execute("""
    ...         SELECT latest_message, date_last_touched
    ...         FROM POFileTranslator
    ...         WHERE person = %(person_id)s AND pofile = %(pofile_id)s
    ...         """, vars())
    ...     result = cur.fetchone()
    ...     if result is None:
    ...         return None
    ...     return list(result)

    >>> stub_id = 22
    >>> pofile_id = 1
    >>> language_id = 387
    >>> potmsgset_id = 1

Note that our oldest TranslationMessage belongs to pofile #1.

Stub has so far not translated anything in this pofile

    >>> pofiletranslator(stub_id, pofile_id) is None
    True

If we add a message, the cache is updated

    >>> cur.execute("""
    ...     INSERT INTO TranslationMessage(
    ...         potmsgset, msgstr1, origin, submitter, language
    ...       ) VALUES (
    ...         %(potmsgset_id)s, 1, 1, %(stub_id)s, %(language_id)s)
    ...     """, vars())
    >>> posubmission_id, date_touched = pofiletranslator(stub_id, pofile_id)


We now set the last touched timestamp into the past so we can detect that
it is correctly updated. This is only possible because we are connected to
the database as the testadmin user.

    >>> cur.execute("""
    ...     UPDATE POFileTranslator
    ...     SET date_last_touched = CURRENT_TIMESTAMP - '1 day'::interval
    ...     WHERE person=%(stub_id)s AND pofile=%(pofile_id)s
    ...     """, vars())


If we add a new submission to that pofile, the cache is updated including
the last touched timestamp.

    >>> cur.execute("""
    ...     INSERT INTO TranslationMessage(
    ...         potmsgset, msgstr1, origin, submitter, language
    ...       ) VALUES (
    ...         %(potmsgset_id)s, 2, 1, %(stub_id)s, %(language_id)s)
    ...     """, vars())
    >>> new_posubmission_id, new_date_touched = pofiletranslator(
    ...     stub_id, pofile_id)
    >>> posubmission_id == new_posubmission_id
    False


We should get the same timestamp as before, despite having updated the
cache manually, as we are in the same database transaction and
CURRENT_TIMESTAMP will return a constant value throughout the transaction
and the triggers will have reset the value in the cache.

    >>> date_touched == new_date_touched
    True


If we update the submissin, the cache is updated

    >>> mark_id = 1
    >>> cur.execute("""
    ...     UPDATE TranslationMessage SET submitter=%(mark_id)s
    ...     WHERE id = %(new_posubmission_id)s
    ...     """, vars())
    >>> mark_posubmission_id, date_touched = pofiletranslator(
    ...     mark_id, pofile_id)
    >>> stub_posubmission_id, date_touched = pofiletranslator(
    ...     stub_id, pofile_id)
    >>> mark_posubmission_id == stub_posubmission_id
    False


The trigger was smart enough to locate the previous submission from stub

    >>> stub_posubmission_id == posubmission_id
    True