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

feature request, modifying sequences #84

Closed
julienaubert opened this issue Apr 20, 2014 · 2 comments
Closed

feature request, modifying sequences #84

julienaubert opened this issue Apr 20, 2014 · 2 comments

Comments

@julienaubert
Copy link

Typos such as mistakenly writing user.pk instead of e.g. user.profile.pk are sometimes not caught in tests (e.g. assert may pass as both have pk==1). I wrote a small snippet to make such mistakes more easily blow up in tests. Perhaps a modification of it could be considered for an optional feature?

def setup_modified_seq(connection):
    # detect cases where we are using the wrong pk (either in test or in code, e.g. user.pk vs user.profile.pk)
    if connection.vendor == 'postgresql':
        print('is postgres vnedor:', connection.vendor)
        def get_seqs(cursor):
            cursor.execute("SELECT c.relname FROM pg_class c WHERE c.relkind = 'S'")
            for seq_row in cursor.fetchall():
                seq = seq_row[0]
                yield seq
        cursor = connection.cursor()
        for count, seq in enumerate(get_seqs(cursor)):
            cursor.execute("ALTER SEQUENCE {} RESTART WITH %s;".format(seq), [(count+1)*1000])

and in hook def pytest_configure(config): adding:

    from django.db import connections
    for connection in connections.all():
        from pytest_django.db_reuse import _monkeypatch
        create_test_db = connection.creation.create_test_db
        def create_test_db_with_modified_sequences(self, *args, **kwargs):
            create_test_db(*args, **kwargs)
            setup_modified_seq(connection)
        _monkeypatch(connection.creation, 'create_test_db', create_test_db_with_modified_sequences)
@pelme
Copy link
Member

pelme commented Apr 21, 2014

Thanks for the suggestion. I am working on a refactor of how databases are setup, and this kind of extra configuration will be easy to achieve. I will keep this in mind and make an example out of this use case.

@blueyed
Copy link
Contributor

blueyed commented Aug 23, 2018

Support for sequences has been added in #619.
Closing this therefore, also since db setup is more granular by now anyway.

@blueyed blueyed closed this as completed Aug 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants