~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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright 2010-2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Model tests for the DistroSeriesDifferenceComment class."""

__metaclass__ = type

from datetime import timedelta
from random import randint

from storm.store import Store
from zope.component import getUtility

from canonical.launchpad.webapp.testing import verifyObject
from canonical.testing.layers import (
    DatabaseFunctionalLayer,
    ZopelessDatabaseLayer,
    )
from lp.registry.interfaces.distroseriesdifferencecomment import (
    IDistroSeriesDifferenceComment,
    IDistroSeriesDifferenceCommentSource,
    )
from lp.testing import TestCaseWithFactory
from lp.testing.matchers import Provides


def get_comment_source():
    """Get `IDistroSeriesDifferemtCommentSource` utility."""
    return getUtility(IDistroSeriesDifferenceCommentSource)


def flip_coin(*args):
    """Random comparison function.  Returns -1 or 1 randomly."""
    return 1 - 2 * randint(0, 1)


def randomize_list(original_list):
    """Sort a list (or other iterable) in random order.  Return list."""
    return sorted(original_list, cmp=flip_coin)


class DistroSeriesDifferenceCommentTestCase(TestCaseWithFactory):

    layer = DatabaseFunctionalLayer

    def test_implements_interface(self):
        # The implementation implements the interface correctly.
        dsd_comment = self.factory.makeDistroSeriesDifferenceComment()
        # Flush the store to ensure db constraints are triggered.
        Store.of(dsd_comment).flush()

        verifyObject(IDistroSeriesDifferenceComment, dsd_comment)

    def test_body_text(self):
        # The comment attribute returns the text of the comment.
        dsd_comment = self.factory.makeDistroSeriesDifferenceComment(
            comment="Wait until version 2.3")

        self.assertEqual("Wait until version 2.3", dsd_comment.body_text)

    def test_subject(self):
        # The subject of the message is set from the distro series diff
        # title.
        dsd_comment = self.factory.makeDistroSeriesDifferenceComment()

        self.assertEqual(
            dsd_comment.distro_series_difference.title,
            dsd_comment.message.subject)

    def test_comment_author(self):
        # The comment author just proxies the author from the message.
        dsd_comment = self.factory.makeDistroSeriesDifferenceComment()

        self.assertEqual(
            dsd_comment.message.owner, dsd_comment.comment_author)

    def test_comment_date(self):
        # The comment date attribute just proxies from the message.
        dsd_comment = self.factory.makeDistroSeriesDifferenceComment()

        self.assertEqual(
            dsd_comment.message.datecreated, dsd_comment.comment_date)

    def test_getForDifference(self):
        # The utility can get comments by id.
        dsd_comment = self.factory.makeDistroSeriesDifferenceComment()
        Store.of(dsd_comment).flush()

        self.assertEqual(
            dsd_comment, get_comment_source().getForDifference(
                dsd_comment.distro_series_difference, dsd_comment.id))

    def test_source_package_name_returns_package_name(self):
        # The comment "knows" the name of the source package it's for.
        package_name = self.factory.getUniqueUnicode()
        dsd = self.factory.makeDistroSeriesDifference(
            source_package_name_str=package_name)
        comment = self.factory.makeDistroSeriesDifferenceComment(dsd)
        self.assertEqual(package_name, comment.source_package_name)


class TestDistroSeriesDifferenceCommentSource(TestCaseWithFactory):

    layer = ZopelessDatabaseLayer

    def test_implements_interface(self):
        self.assertThat(
            get_comment_source(),
            Provides(IDistroSeriesDifferenceCommentSource))

    def test_getForDistroSeries_returns_result_set(self):
        series = self.factory.makeDistroSeries()
        source = get_comment_source()
        self.assertTrue(source.getForDistroSeries(series).is_empty())

    def test_getForDistroSeries_matches_on_distroseries(self):
        dsd = self.factory.makeDistroSeriesDifference()
        series = dsd.derived_series
        comment = self.factory.makeDistroSeriesDifferenceComment(dsd)
        source = get_comment_source()
        self.assertContentEqual([comment], source.getForDistroSeries(series))

    def test_getForDistroSeries_filters_by_distroseries(self):
        dsd = self.factory.makeDistroSeriesDifference()
        other_series = self.factory.makeDistroSeries()
        self.factory.makeDistroSeriesDifferenceComment(dsd)
        source = get_comment_source()
        self.assertContentEqual([], source.getForDistroSeries(other_series))

    def test_getForDistroSeries_matches_on_since(self):
        dsd = self.factory.makeDistroSeriesDifference()
        series = dsd.derived_series
        comment = self.factory.makeDistroSeriesDifferenceComment(dsd)
        source = get_comment_source()
        yesterday = comment.comment_date - timedelta(1)
        self.assertContentEqual(
            [comment], source.getForDistroSeries(series, since=yesterday))

    def test_getForDistroSeries_filters_by_since(self):
        dsd = self.factory.makeDistroSeriesDifference()
        series = dsd.derived_series
        comment = self.factory.makeDistroSeriesDifferenceComment(dsd)
        source = get_comment_source()
        tomorrow = comment.comment_date + timedelta(1)
        self.assertContentEqual(
            [], source.getForDistroSeries(series, since=tomorrow))

    def test_getForDistroSeries_orders_by_age(self):
        series = self.factory.makeDistroSeries()
        dsds = randomize_list([
            self.factory.makeDistroSeriesDifference(derived_series=series)
            for counter in xrange(5)])
        comments = [
            self.factory.makeDistroSeriesDifferenceComment(dsd)
            for dsd in dsds]
        source = get_comment_source()
        self.assertEqual(comments, list(source.getForDistroSeries(series)))

    def test_getForDistroSeries_matches_on_package_name(self):
        dsd = self.factory.makeDistroSeriesDifference()
        series = dsd.derived_series
        package_name = dsd.source_package_name.name
        comment = self.factory.makeDistroSeriesDifferenceComment(dsd)
        source = get_comment_source()
        self.assertContentEqual([comment], source.getForDistroSeries(
            series, source_package_name=package_name))

    def test_getForDistroSeries_filters_by_package_name(self):
        dsd = self.factory.makeDistroSeriesDifference()
        series = dsd.derived_series
        other_package = self.factory.getUniqueUnicode()
        self.factory.makeDistroSeriesDifferenceComment(dsd)
        source = get_comment_source()
        self.assertContentEqual([], source.getForDistroSeries(
            series, source_package_name=other_package))