Skip to content

Commit

Permalink
Tests fixed to be robust to accidentally aligned primary keys
Browse files Browse the repository at this point in the history
  • Loading branch information
boxed committed Oct 23, 2023
1 parent ed0e570 commit 5e3728a
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 113 deletions.
16 changes: 16 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pathlib import Path

import pytest
from django.db import connection

from docs.models import (
Album,
Expand Down Expand Up @@ -37,6 +38,21 @@ def pytest_sessionstart(session):
generate_api_docs_tests((Path(__file__).parent / 'docs').absolute())


@pytest.fixture(autouse=True)
def reset_sequences(request, django_db_blocker):
if request.node.get_closest_marker('django_db'):
with django_db_blocker.unblock():
cursor = connection.cursor()

# noinspection SqlResolve
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
for i, (table,) in enumerate(cursor.fetchall()):
cursor.execute(f"""
INSERT INTO SQLITE_SEQUENCE (name,seq) SELECT '{table}', {(i + 1) * 1000} WHERE NOT EXISTS
(SELECT changes() AS change FROM sqlite_sequence WHERE change <> 0);
""")


@pytest.fixture
def artist(transactional_db):
return Artist.objects.create(name='Black Sabbath')
Expand Down
12 changes: 7 additions & 5 deletions iommi/edit_table__tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def test_edit_table_related_objects():
request=req(
'POST',
**{
'columns/foo/1': str(baz.pk),
f'columns/foo/{baz.pk}': str(foo.pk),
'-actions/submit': '',
},
)
Expand Down Expand Up @@ -360,15 +360,17 @@ def test_edit_table_post_row_group(small_discography):
request=req(
'POST',
**{
'columns/year/1': '1980',
'columns/year/2': '1979',
f'columns/year/{small_discography[0].pk}': '5',
f'columns/year/{small_discography[1].pk}': '7',
'-actions/submit': '',
},
)
)
assert not edit_table.get_errors()
response = bound.render_to_response()
assert response.status_code == 302
assert not edit_table.get_errors()
assert response.status_code == 302, response.content.decode()
assert Album.objects.get(pk=small_discography[0].pk).year == 5
assert Album.objects.get(pk=small_discography[1].pk).year == 7


# TODO: attr=None on a column crashes
4 changes: 2 additions & 2 deletions iommi/form__tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3239,10 +3239,10 @@ def test_delete_form_default_text():
verify_part_html(
part=Form.delete(instance=lambda **_: foo),
# language=HTML
expected_html="""
expected_html=f"""
<form action="" enctype="multipart/form-data" method="post">
<h1> Delete foo </h1>
<p> Are you sure you want to delete the foo "Foo object (1)"?</p>
<p> Are you sure you want to delete the foo "Foo object ({foo.pk})"?</p>
<div class="links">
<button accesskey="s" name="-submit"> Delete </button>
</div>
Expand Down
Loading

0 comments on commit 5e3728a

Please sign in to comment.