Merge lp:~jamalta/launchpad/bug-127489 into lp:launchpad
- bug-127489
- Merge into devel
Status: | Merged |
---|---|
Approved by: | Guilherme Salgado |
Approved revision: | 9026 |
Merged at revision: | not available |
Proposed branch: | lp:~jamalta/launchpad/bug-127489 |
Merge into: | lp:launchpad |
Diff against target: | None lines |
To merge this branch: | bzr merge lp:~jamalta/launchpad/bug-127489 |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Guilherme Salgado (community) | Approve | ||
Review via email: mp+12018@code.launchpad.net |
This proposal supersedes a proposal from 2009-09-13.
Commit message
Description of the change
Stuart Bishop (stub) wrote : Posted in a previous version of this proposal | # |
Gavin Panella (allenap) wrote : Posted in a previous version of this proposal | # |
Hi Jamal,
Thank you for working on this bug!
As Stuart commented, this will need a pagetest. In fact, there is
already one that is probably broken by this change. Have a look at the
end of lib/lp/
Did you have a mentor for this branch, or a pre-implementation
discussion? Perhaps that might have spotted the need to update the
pagetest. Have a look at https:/
you haven't already. It's the same process as all the Launchpad devs
from Canonical use.
I have a comment about the code, because the cmp argument to sorted()
is deprecated (and I think it's gone entirely in Python 3).
Thanks again!
Gavin.
> === modified file 'lib/lp/
> --- lib/lp/
> +++ lib/lp/
> @@ -2517,7 +2517,10 @@
> categories = set()
> for contrib in self.contributions:
> categories.
> - return sorted(categories, key=attrgetter(
> + sort = {'code': 0, 'bugs': 1, 'blueprints': 2, 'translations': 3,
> + 'answers': 4, 'specs': 5, 'soyuz': 6}
> + return sorted(categories, key=attrgetter(
> + cmp=lambda x,y: cmp(sort[x], sort[y]))
Because cmp is deprecated, I think this would be better as:
return sorted(categories, key=lambda category: sort[category.
>
> @cachedproperty
> def context_
Jamal Fanaian (jamalta) wrote : Posted in a previous version of this proposal | # |
Hi Gavin,
Thanks for your input regarding this bug. I have actually been working on a test for this. I did not have a mentor when I started working on this issue, but went on #launchpad-dev to get some help. Salgado had recommended that I write a unittest to call the view, and make sure the data was returned in the right order, using a LaunchpadObject
> Hi Jamal,
>
> Thank you for working on this bug!
>
> As Stuart commented, this will need a pagetest. In fact, there is
> already one that is probably broken by this change. Have a look at the
> end of lib/lp/
>
> Did you have a mentor for this branch, or a pre-implementation
> discussion? Perhaps that might have spotted the need to update the
> pagetest. Have a look at https:/
> you haven't already. It's the same process as all the Launchpad devs
> from Canonical use.
>
> I have a comment about the code, because the cmp argument to sorted()
> is deprecated (and I think it's gone entirely in Python 3).
>
> Thanks again!
>
> Gavin.
>
>
> > === modified file 'lib/lp/
> > --- lib/lp/
> > +++ lib/lp/
> > @@ -2517,7 +2517,10 @@
> > categories = set()
> > for contrib in self.contributions:
> > categories.
> contrib[
> > - return sorted(categories, key=attrgetter(
> > + sort = {'code': 0, 'bugs': 1, 'blueprints': 2, 'translations': 3,
> > + 'answers': 4, 'specs': 5, 'soyuz': 6}
> > + return sorted(categories, key=attrgetter(
> > + cmp=lambda x,y: cmp(sort[x], sort[y]))
>
> Because cmp is deprecated, I think this would be better as:
>
> return sorted(categories, key=lambda category: sort[category.
>
> >
> > @cachedproperty
> > def context_
Jamal Fanaian (jamalta) wrote : Posted in a previous version of this proposal | # |
I went through the pagetest you referenced, and it will actually not fail. It is also not possible to test my changes with this because there is not enough data. The problem is that the only two icons displayed are bugs and translations. Sorted alphabetically, bugs will obviously always be first, which is the case with the replacement sort.
To test this I need to be able to add a third entry, say, Answers for example. I have this in my local environment (which I should really revert since it's going to break tests in the future), but I don't know how to properly write a pagetest for it because of this limitation. I think this is the reason salgado recommended writing a unittest.
Let me give the unittests another try to see if I can get them done. Thanks for your help so far!
> Hi Gavin,
>
> Thanks for your input regarding this bug. I have actually been working on a
> test for this. I did not have a mentor when I started working on this issue,
> but went on #launchpad-dev to get some help. Salgado had recommended that I
> write a unittest to call the view, and make sure the data was returned in the
> right order, using a LaunchpadObject
> figure out how to do that exactly, and haven't gotten back to it in a few days
> now. I will take a look at the test you mentioned and make sure I fix it.
> Also, I appreciate your comment regarding the depracated cmp() function. I am
> fairly new to Python and was not aware of this. I will fix that shortly as
> well, using your recommendation.
>
> > Hi Jamal,
> >
> > Thank you for working on this bug!
> >
> > As Stuart commented, this will need a pagetest. In fact, there is
> > already one that is probably broken by this change. Have a look at the
> > end of lib/lp/
> >
> > Did you have a mentor for this branch, or a pre-implementation
> > discussion? Perhaps that might have spotted the need to update the
> > pagetest. Have a look at https:/
> > you haven't already. It's the same process as all the Launchpad devs
> > from Canonical use.
> >
> > I have a comment about the code, because the cmp argument to sorted()
> > is deprecated (and I think it's gone entirely in Python 3).
> >
> > Thanks again!
> >
> > Gavin.
> >
> >
> > > === modified file 'lib/lp/
> > > --- lib/lp/
> > > +++ lib/lp/
> > > @@ -2517,7 +2517,10 @@
> > > categories = set()
> > > for contrib in self.contributions:
> > > categories.
> > contrib[
> > > - return sorted(categories, key=attrgetter(
> > > + sort = {'code': 0, 'bugs': 1, 'blueprints': 2, 'translations': 3,
> > > + 'answers': 4, 'specs': 5, 'soyuz': 6}
> > > + return sorted(categories, key=attrgetter(
> > > + cmp=lambda x,y: cmp(sort[x], sort[y]))
> >
> > Because cmp is deprecated, I think this would be better as:
> >
> > return sorted(categories, key=lambda category: sort[categ...
Gavin Panella (allenap) wrote : Posted in a previous version of this proposal | # |
Have a look at using lp.testing.
Guilherme Salgado (salgado) wrote : Posted in a previous version of this proposal | # |
On Fri, 2009-08-07 at 09:19 +0000, Gavin Panella wrote:
> Have a look at using lp.testing.
I don't think so. That list of categories is generated based on the kind
of karma the person has on the KarmaCache table, and that table is
populated by a script. Even though the person would get karma when
factory.
table until we ran the script. We could run the script in the test, but
that'd make the test *a lot* slower.
That's why I suggested a new method on LPObjectFactory to create the
KarmaCache entries directly.
Jamal, I remember you were able to write the new factory method, so you
should be quite close to getting a test for your fix. If you need any
help, just drop by #launchpad-
Cheers,
--
Guilherme Salgado <email address hidden>
Jamal Fanaian (jamalta) wrote : Posted in a previous version of this proposal | # |
I have added a test that makes sure the categories returned by PersonView.
I may not be doing this the best way, so I am requesting a review and hopefully I can chat with someone about it come Monday to figure out what needs to be improved or changed.
Thanks again for all your help and sorry this took so long!
> On Fri, 2009-08-07 at 09:19 +0000, Gavin Panella wrote:
> > Have a look at using lp.testing.
> this to quickly create, say, branches, so that there are code artifacts for
> your user. That will probably be enough to test this code.
>
> I don't think so. That list of categories is generated based on the kind
> of karma the person has on the KarmaCache table, and that table is
> populated by a script. Even though the person would get karma when
> factory.
> table until we ran the script. We could run the script in the test, but
> that'd make the test *a lot* slower.
>
> That's why I suggested a new method on LPObjectFactory to create the
> KarmaCache entries directly.
>
> Jamal, I remember you were able to write the new factory method, so you
> should be quite close to getting a test for your fix. If you need any
> help, just drop by #launchpad-
>
> Cheers,
>
> --
> Guilherme Salgado <email address hidden>
Jamal Fanaian (jamalta) wrote : Posted in a previous version of this proposal | # |
Sorry forgot to mention this on my last comment. I will try to ping someone in #launchpad-reviews come Monday to go over my branch, that way we don't have to go back and forth over email.
Thanks!
> I have added a test that makes sure the categories returned by
> PersonView.
> script to update Karma I am using the test factories and creating the
> KarmaCache records based on how the Karma update script does it.
>
> I may not be doing this the best way, so I am requesting a review and
> hopefully I can chat with someone about it come Monday to figure out what
> needs to be improved or changed.
>
> Thanks again for all your help and sorry this took so long!
>
> > On Fri, 2009-08-07 at 09:19 +0000, Gavin Panella wrote:
> > > Have a look at using lp.testing.
> use
> > this to quickly create, say, branches, so that there are code artifacts for
> > your user. That will probably be enough to test this code.
> >
> > I don't think so. That list of categories is generated based on the kind
> > of karma the person has on the KarmaCache table, and that table is
> > populated by a script. Even though the person would get karma when
> > factory.
> > table until we ran the script. We could run the script in the test, but
> > that'd make the test *a lot* slower.
> >
> > That's why I suggested a new method on LPObjectFactory to create the
> > KarmaCache entries directly.
> >
> > Jamal, I remember you were able to write the new factory method, so you
> > should be quite close to getting a test for your fix. If you need any
> > help, just drop by #launchpad-
> >
> > Cheers,
> >
> > --
> > Guilherme Salgado <email address hidden>
Jamal Fanaian (jamalta) wrote : Posted in a previous version of this proposal | # |
After taking to salgado in #launchpad-reviews, he recommended that the makeKarmaCache() be modified so that it creates the total records (with category=NULL), instead of processing all entries in KarmaCache to create the sums.
Guilherme Salgado (salgado) wrote : Posted in a previous version of this proposal | # |
Hi Jamal,
Thanks a lot for fixing this bug, and going through the effort for
writing a test for the fix. The bug itself was indeed easy to fix, but
now it's quite clear the test for it was anything but easy.
I have some suggestions for improvement below, most of them are stylish
though and they should all be easy to deal with. If you have any
comments/questions, just ping me on IRC or leave a comment here.
Cheers,
review needs-fixing
On Thu, 2009-08-27 at 03:20 +0000, Jamal Fanaian wrote:
> === modified file 'lib/lp/
> --- lib/lp/
> +++ lib/lp/
> @@ -2517,7 +2517,9 @@
> categories = set()
> for contrib in self.contributions:
> categories.
> - return sorted(categories, key=attrgetter(
> + sort = {'code': 0, 'bugs': 1, 'blueprints': 2, 'translations': 3,
> + 'answers': 4, 'specs': 5, 'soyuz': 6}
> + return sorted(categories, key=lambda category: sort[category.
>
> @cachedproperty
> def context_
>
> === added file 'lib/lp/
> --- lib/lp/
> +++ lib/lp/
> @@ -0,0 +1,71 @@
> +# Copyright 2009 Canonical Ltd. This software is licensed under the
> +# GNU Affero General Public License version 3 (see the file LICENSE).
> +
> +__metaclass__ = type
> +
> +import unittest
> +
> +from canonical.config import config
> +from canonical.
> +from canonical.testing import LaunchpadZopele
> +from canonical.
> +from lp.registry.
> +from lp.testing import TestCaseWithFactory
> +from zope.component import getUtility
> +from canonical.
> +
There should be one extra blank line here. Our style guide[1] has this
and other conventions we use.
[1] https:/
> +class TestPerson(
TestPersonView would be a better name for this.
> + """Test Person view."""
Since the docstring is nearly identical to the class' name, we can get
rid of it.
> +
> + layer = LaunchpadZopele
> +
> + def setUp(self):
> + # Create a person
This comment doesn't add much either, so it can be removed.
> + TestCaseWithFac
> + self.person = self.factory.
> + self.view = PersonView(
> + LaunchpadTestRe
> +
> + def test_karma_
> + # Add karma to some categories for the user
Same here.
> + self.makeKarmaC
> + self.makeKarmaC
> + self.makeKarmaC
I'd move these lines to the setUp() method above.
>...
Guilherme Salgado (salgado) wrote : Posted in a previous version of this proposal | # |
Hi Jamal,
This looks very good. I have only some comments/
On Sun, 2009-08-30 at 04:30 +0000, Jamal Fanaian wrote:
> === modified file 'lib/lp/
> --- lib/lp/
> +++ lib/lp/
> @@ -2517,7 +2517,9 @@
> categories = set()
> for contrib in self.contributions:
> categories.
> - return sorted(categories, key=attrgetter(
> + sort = {'code': 0, 'bugs': 1, 'blueprints': 2, 'translations': 3,
> + 'answers': 4, 'specs': 5, 'soyuz': 6}
> + return sorted(categories, key=lambda category: sort[category.
>
> @cachedproperty
> def context_
>
> === added file 'lib/lp/
> --- lib/lp/
> +++ lib/lp/
> @@ -0,0 +1,68 @@
> +# Copyright 2009 Canonical Ltd. This software is licensed under the
> +# GNU Affero General Public License version 3 (see the file LICENSE).
> +
> +__metaclass__ = type
> +
> +import unittest
> +
Some of the imports below are not needed anymore, so they should be
removed. If you run 'make lint', it will tell you which ones are not
used.
> +from canonical.config import config
> +from canonical.
> +from canonical.testing import LaunchpadZopele
> +from canonical.
> +from lp.registry.
> +from lp.testing import TestCaseWithFactory
> +from zope.component import getUtility
> +from canonical.
> +from lp.registry.
These imports should be grouped like this:
1. standard library imports
2. related third party imports
3. local application/library specific imports
(For more conventions about imports and other things, check out
http://
So, this is what it'd look like
import unittest
import zope...
import canonical...
import lp...
> +
You're also missing one blank line here.
> +class TestPersonView(
> +
> + layer = LaunchpadZopele
> +
> + def setUp(self):
> + TestCaseWithFac
> + self.person = self.factory.
I don't think you need to store the newly created person in an instance
variable, as it is only needed here in this method.
> + self.view = PersonView(
> + LaunchpadTestRe
> + self.makeKarmaC
> + category=
You don't need to pass person/category as a keyword argument here, and
the indentation is not correct as per our style guide. This is what it
should look like.
self.
person, KarmaCategory.
Guilherme Salgado (salgado) : Posted in a previous version of this proposal | # |
Jamal Fanaian (jamalta) wrote : Posted in a previous version of this proposal | # |
The commit had to stay, if it is removed the data is not seen when PersonView.
Thanks again, and let me know if there are any outstanding issues. Hopefully this will be the last round, hehe.
Guilherme Salgado (salgado) wrote : Posted in a previous version of this proposal | # |
Hi Jamal,
This branch looks really good, but I'm not quite comfortable having the makeKarmaCache() method do a switchDBUser(
def _makeKarmaCache
"""Create and return a KarmaCache entry with the given arguments.
In order to create a KarmaCache entry we'll switch the DB user 'karma',
so tests that need a different user after calling this method should do
a switchDBUser() themselves.
"""
...
# Must commit here so that the change is seen in other transactions
# (e.g. when the callsite issues a switchDBUser() after we return).
return karmacache
Also, please run the 'make lint' command as I see some unused imports in test_person_
Cheers,
Guilherme
Jamal Fanaian (jamalta) wrote : | # |
Hi Guilherme,
Thanks for your comments. I have made the modifications as you requested. I ran `make lint` but it didn't tell me I had any unused libraries. I proceeded to double-check by hand and I am using all the included libraries. Please let me know if this is incorrect.
Thanks again!
Jamal
Guilherme Salgado (salgado) wrote : | # |
On Fri, 2009-09-18 at 00:45 +0000, Jamal Fanaian wrote:
> Thanks for your comments. I have made the modifications as you
> requested. I ran `make lint` but it didn't tell me I had any unused
> libraries. I proceeded to double-check by hand and I am using all the
> included libraries. Please let me know if this is incorrect.
Indeed; I thought your test was not using LaunchpadTestRe
is. I'll submit your changes on your behalf.
status approved
--
Guilherme Salgado <email address hidden>
Jamal Fanaian (jamalta) wrote : | # |
> On Fri, 2009-09-18 at 00:45 +0000, Jamal Fanaian wrote:
> > Thanks for your comments. I have made the modifications as you
> > requested. I ran `make lint` but it didn't tell me I had any unused
> > libraries. I proceeded to double-check by hand and I am using all the
> > included libraries. Please let me know if this is incorrect.
>
> Indeed; I thought your test was not using LaunchpadTestRe
> is. I'll submit your changes on your behalf.
>
> status approved
>
> --
> Guilherme Salgado <email address hidden>
Ah, thanks! I'm glad to hear that. I really appreciate all the help and guidance you provided through the completion of this test. It took a long time, but I definitely learned a lot from it.
Preview Diff
1 | === modified file 'lib/lp/registry/browser/person.py' | |||
2 | --- lib/lp/registry/browser/person.py 2009-07-30 22:35:16 +0000 | |||
3 | +++ lib/lp/registry/browser/person.py 2009-08-05 19:55:13 +0000 | |||
4 | @@ -2517,7 +2517,9 @@ | |||
5 | 2517 | categories = set() | 2517 | categories = set() |
6 | 2518 | for contrib in self.contributions: | 2518 | for contrib in self.contributions: |
7 | 2519 | categories.update(category for category in contrib['categories']) | 2519 | categories.update(category for category in contrib['categories']) |
9 | 2520 | return sorted(categories, key=attrgetter('title')) | 2520 | sort = {'code': 0, 'bugs': 1, 'blueprints': 2, 'translations': 3, |
10 | 2521 | 'answers': 4, 'specs': 5, 'soyuz': 6} | ||
11 | 2522 | return sorted(categories, key=lambda category: sort[category.name]) | ||
12 | 2521 | 2523 | ||
13 | 2522 | @cachedproperty | 2524 | @cachedproperty |
14 | 2523 | def context_is_probably_a_team(self): | 2525 | def context_is_probably_a_team(self): |
15 | 2524 | 2526 | ||
16 | === added file 'lib/lp/registry/browser/tests/test_person_view.py' | |||
17 | --- lib/lp/registry/browser/tests/test_person_view.py 1970-01-01 00:00:00 +0000 | |||
18 | +++ lib/lp/registry/browser/tests/test_person_view.py 2009-09-18 00:27:26 +0000 | |||
19 | @@ -0,0 +1,72 @@ | |||
20 | 1 | # Copyright 2009 Canonical Ltd. This software is licensed under the | ||
21 | 2 | # GNU Affero General Public License version 3 (see the file LICENSE). | ||
22 | 3 | |||
23 | 4 | __metaclass__ = type | ||
24 | 5 | |||
25 | 6 | import unittest | ||
26 | 7 | |||
27 | 8 | from zope.component import getUtility | ||
28 | 9 | |||
29 | 10 | from canonical.launchpad.interfaces import IKarmaCacheManager, NotFoundError | ||
30 | 11 | from canonical.launchpad.webapp.servers import LaunchpadTestRequest | ||
31 | 12 | from canonical.testing import LaunchpadZopelessLayer | ||
32 | 13 | from lp.registry.browser.person import PersonView | ||
33 | 14 | from lp.registry.model.karma import KarmaCategory | ||
34 | 15 | from lp.testing import TestCaseWithFactory | ||
35 | 16 | |||
36 | 17 | |||
37 | 18 | class TestPersonView(TestCaseWithFactory): | ||
38 | 19 | |||
39 | 20 | layer = LaunchpadZopelessLayer | ||
40 | 21 | |||
41 | 22 | def setUp(self): | ||
42 | 23 | TestCaseWithFactory.setUp(self) | ||
43 | 24 | person = self.factory.makePerson() | ||
44 | 25 | product = self.factory.makeProduct() | ||
45 | 26 | self.view = PersonView( | ||
46 | 27 | person, LaunchpadTestRequest()) | ||
47 | 28 | self._makeKarmaCache( | ||
48 | 29 | person, product, KarmaCategory.byName('bugs')) | ||
49 | 30 | self._makeKarmaCache( | ||
50 | 31 | person, product, KarmaCategory.byName('answers')) | ||
51 | 32 | self._makeKarmaCache( | ||
52 | 33 | person, product, KarmaCategory.byName('code')) | ||
53 | 34 | |||
54 | 35 | def test_karma_category_sort(self): | ||
55 | 36 | categories = self.view.contributed_categories | ||
56 | 37 | category_names = [] | ||
57 | 38 | for category in categories: | ||
58 | 39 | category_names.append(category.name) | ||
59 | 40 | |||
60 | 41 | self.assertEqual(category_names, [u'code', u'bugs', u'answers'], | ||
61 | 42 | 'Categories are not sorted correctly') | ||
62 | 43 | |||
63 | 44 | def _makeKarmaCache(self, person, product, category, value=10): | ||
64 | 45 | """ Create and return a KarmaCache entry with the given arguments. | ||
65 | 46 | |||
66 | 47 | In order to create the KarmaCache record we must switch to the DB | ||
67 | 48 | user 'karma', so tests that need a different user after calling | ||
68 | 49 | this method should do run switchDbUser() themselves. | ||
69 | 50 | """ | ||
70 | 51 | |||
71 | 52 | LaunchpadZopelessLayer.switchDbUser('karma') | ||
72 | 53 | |||
73 | 54 | cache_manager = getUtility(IKarmaCacheManager) | ||
74 | 55 | karmacache = cache_manager.new( | ||
75 | 56 | value, person.id, category.id, product_id=product.id) | ||
76 | 57 | |||
77 | 58 | try: | ||
78 | 59 | cache_manager.updateKarmaValue( | ||
79 | 60 | value, person.id, category_id=None, product_id=product.id) | ||
80 | 61 | except NotFoundError: | ||
81 | 62 | cache_manager.new( | ||
82 | 63 | value, person.id, category_id=None, product_id=product.id) | ||
83 | 64 | |||
84 | 65 | # We must commit here so that the change is seen in other transactions | ||
85 | 66 | # (e.g. when the callsite issues a switchDbUser() after we return). | ||
86 | 67 | LaunchpadZopelessLayer.commit() | ||
87 | 68 | return karmacache | ||
88 | 69 | |||
89 | 70 | |||
90 | 71 | def test_suite(): | ||
91 | 72 | return unittest.TestLoader().loadTestsFromName(__name__) |
This is going to need a page test to catch future regressions.