-
Notifications
You must be signed in to change notification settings - Fork 36
using get_or_create when testing for group membership #273
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,12 +30,15 @@ def setUp(self): | |
pub_date=datetime.datetime.now(), | ||
tags=["tag1", "tag2"]) | ||
|
||
@pytest.mark.xfail | ||
def test_get_ambassadors_for_country(self): | ||
self.up1.country = "SI" | ||
self.up1.save() | ||
|
||
group = Group.objects.get(name="ambassadors") | ||
# temporary workaround because South migrations don't work yet | ||
# fix me in the future | ||
group, created = Group.objects.get_or_create(name="ambassadors") | ||
if created: | ||
group.save() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if this should ever be done in a test... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you feel should not be done, the get_or_create invocation, or something else? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the group.save()... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I have to ask then, why do you think object should not be saved in a test? I am asking, so you can share your knowledge about this stuff... You know us being a learning group and all... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because in this case the ambassadors group should be created during migrate (0002_adding_groups_and_setting_permissions.py), but that doesn't happen for tests at the moment (as far as I understand, this is the issue @gandalfar is having) a workaround in tests that "fix" stuff that should have happened elsewhere is bad practice There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well if this is neessasy for now until we figure out the fix for the migration than let's leave it for the refactathlon. You said it so perfectly :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in that case I recommend a note (in a comment) stating it should be removed later on |
||
|
||
group.user_set.add(self.u1) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not necessary, since get_or_create saves the created object already.
https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create
Not sure what you felt that this has to be here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just an additional test to ensure that the underlying layer actually saves the object
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Second south migration adds 'ambassadors' group permission. There is a bug in py.test and django_pytest that doesn't correctly run south migrations. I'm still looking up for a fix but it's getting a bit tricky.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@goranche I don't know. We could then (re)write the tests for all of the Django, if we adopt this mindset. Not to mention, this is a "bolognese" at it's best :).
I am not exactly sure what is the problem with South? Is this needed because the migrations arent runinig? I am intrigued about pytest having this bug, what exactly is it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ercchy everything is bolognese to you ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only the latest version actually runs South migration - pytest-dev/pytest-django#129 . Last week this patch blew up django-taggit and I haven't got time yet to make a reproducible test case for them. I plan to do it on Wednesday.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ugh... @ercchy somehow I thought your comment was about another test...
never mind me 👼
(except for the "everything is bolognese to you" 😬 )