Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badge creation metaclasses don't play nicely with tests #6

Open
alastair opened this issue Feb 4, 2015 · 2 comments
Open

Badge creation metaclasses don't play nicely with tests #6

alastair opened this issue Feb 4, 2015 · 2 comments

Comments

@alastair
Copy link

alastair commented Feb 4, 2015

I've been chasing down a problem with badges not being in my database when I run unit tests. It's related to the registered_badges dictionary.

The post_syncdb hook in listeners.py creates database objects based on MetaBadges and populates Badge._badge with a reference to the model. However, my transaction-wrapped unit tests truncate the database at the beginning and end of each test. Now the database is empty but the Badge believes it still has an object.

In the end, I've solved it by running this in my setUp method:

    from badges import listeners
    from badges.utils import registered_badges
    for badge in registered_badges.values():
        del badge._badge
    listeners.sync_badges_to_db()

My view is that a method like sync_badges_to_db should always create badges in the database, or maybe there should be a method to force this.

More generally the post_syncdb hook in listeners has been causing problems too - in fact, the django documentation for this hook explicitly says that it's not recommended to change the database in this hook: https://docs.djangoproject.com/en/1.7/ref/signals/#post-syncdb
I understand that it's a useful way of making sure the database reflects the MetaBadges, but it feels like magic that is a little bit unexplained. I prefer the current system to when badges were created in __new__, but I would like it more if badge creation was explicit. Maybe now django 1.7's AppConfig.ready could be something to look at.

@jiaaro
Copy link
Owner

jiaaro commented May 14, 2015

yeah...

I have this embarrassing helper function in the tests.py for some of my projects that use django-badges:

def generateBadges():
    from badges.utils import registered_badges
    for badge in registered_badges.values():
        delattr(badge, "_badge")
        badge.badge

I'll take a look at AppConfig.ready, thanks for the pointer

@jiaaro
Copy link
Owner

jiaaro commented May 14, 2015

Actually, looking over it now, it has a scary disclaimer about doing queries in AppConfig.ready:

Warning

Although you can access model classes as described above, avoid interacting with the database in your ready() implementation.

For example, even though the test database configuration is separate from the production settings, manage.py test would still execute some queries against your production database!

igorkramaric pushed a commit to igorkramaric/django-badges that referenced this issue Nov 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants