Skip to content
This repository was archived by the owner on Sep 19, 2018. It is now read-only.

using get_or_create when testing for group membership #273

Merged
merged 4 commits into from
Aug 13, 2014

Conversation

ialja
Copy link
Contributor

@ialja ialja commented Aug 6, 2014

No description provided.

@ialja
Copy link
Contributor Author

ialja commented Aug 6, 2014

@gandalfar: Does this make sense?

group = Group.objects.get(name="ambassadors")
group, created = Group.objects.get_or_create(name="ambassadors")
if created:
group.save()
Copy link
Contributor

Choose a reason for hiding this comment

The 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...
if I correctly understand what this does, at least ;)

Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the group.save()...
you know, the line I commented on :)

Copy link
Contributor

Choose a reason for hiding this comment

The 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...

Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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 :)

Copy link
Contributor

Choose a reason for hiding this comment

The 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

@jurecuhalev
Copy link
Contributor

It's actually my bug. South migrations aren't being run. I'm checking what's going on now.

@jurecuhalev
Copy link
Contributor

Goran, would it make it too much of a problem if you unmered my pull request? Github allows you to revert it - https://github.com/blog/1857-introducing-the-revert-button

Sorry for this mess. I'll have a better pull request next week. Alternatively you can merge Alja's workaround and I'll assign myself a bug to recode it properly.

@goranche
Copy link
Contributor

goranche commented Aug 7, 2014

@gandalfar which one? you can revert the commits from the pull request yourself and create a new pull request, which "undoes" the first one ;)
might be easier and less error prone... I mean, it's me we're talking about :D

def test_get_ambassadors_for_country(self):
self.up1.country = "SI"
self.up1.save()

group = Group.objects.get(name="ambassadors")
group, created = Group.objects.get_or_create(name="ambassadors")
if created:
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Contributor

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.

Copy link
Contributor

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?

Copy link
Contributor

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 ;)

Copy link
Contributor

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.

Copy link
Contributor

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" 😬 )

ialja added 3 commits August 13, 2014 18:26
Also made sure the country updates when the marker is being manually marked and fixed a few other smaller issues related to that.
Getting read of some unused variables, making variable names more readable.
@ercchy
Copy link
Contributor

ercchy commented Aug 13, 2014

Good Job Cat!

ercchy added a commit that referenced this pull request Aug 13, 2014
using get_or_create when testing for group membership
@ercchy ercchy merged commit 22ef953 into codeeu:master Aug 13, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants