Skip to content

Commit

Permalink
edits for many test apps
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Oct 4, 2024
1 parent 9b8ae4b commit 5a247e8
Show file tree
Hide file tree
Showing 20 changed files with 74 additions and 59 deletions.
2 changes: 1 addition & 1 deletion tests/admin_utils/test_logentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def test_logentry_get_admin_url(self):
"admin:admin_utils_article_change", args=(quote(self.a1.pk),)
)
self.assertEqual(logentry.get_admin_url(), expected_url)
self.assertIn("article/%d/change/" % self.a1.pk, logentry.get_admin_url())
self.assertIn("article/%s/change/" % self.a1.pk, logentry.get_admin_url())

logentry.content_type.model = "nonexistent"
self.assertIsNone(logentry.get_admin_url())
Expand Down
2 changes: 1 addition & 1 deletion tests/auth_tests/test_context_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_user_attrs(self):
user = authenticate(username="super", password="secret")
response = self.client.get("/auth_processor_user/")
self.assertContains(response, "unicode: super")
self.assertContains(response, "id: %d" % self.superuser.pk)
self.assertContains(response, "id: %s" % self.superuser.pk)
self.assertContains(response, "username: super")
# bug #12037 is tested by the {% url %} in the template:
self.assertContains(response, "url: /userpage/super/")
Expand Down
8 changes: 5 additions & 3 deletions tests/auth_tests/test_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,12 @@ def test_validate_fk(self):

@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserWithFK")
def test_validate_fk_environment_variable(self):
from bson import ObjectId

email = Email.objects.create(email="[email protected]")
Group.objects.all().delete()
nonexistent_group_id = 1
msg = f"group instance with id {nonexistent_group_id} does not exist."
nonexistent_group_id = ObjectId()
msg = f"group instance with id {nonexistent_group_id!r} does not exist."

with mock.patch.dict(
os.environ,
Expand Down Expand Up @@ -1532,5 +1534,5 @@ def test_set_permissions_fk_to_using_parameter(self):
Permission.objects.using("other").delete()
with self.assertNumQueries(6, using="other") as captured_queries:
create_permissions(apps.get_app_config("auth"), verbosity=0, using="other")
self.assertIn("INSERT INTO", captured_queries[-1]["sql"].upper())
self.assertIn("INSERT_MANY", captured_queries[-1]["sql"].upper())
self.assertGreater(Permission.objects.using("other").count(), 0)
2 changes: 1 addition & 1 deletion tests/contenttypes_tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_get_object_cache_respects_deleted_objects(self):

post = Post.objects.get(pk=post.pk)
with self.assertNumQueries(1):
self.assertEqual(post.object_id, question_pk)
self.assertEqual(post.object_id, str(question_pk))
self.assertIsNone(post.parent)
self.assertIsNone(post.parent)

Expand Down
2 changes: 1 addition & 1 deletion tests/contenttypes_tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
from django.urls import re_path

urlpatterns = [
re_path(r"^shortcut/([0-9]+)/(.*)/$", views.shortcut),
re_path(r"^shortcut/([\w]+)/(.*)/$", views.shortcut),
]
6 changes: 4 additions & 2 deletions tests/custom_columns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
"""

from django_mongodb.fields import ObjectIdAutoField

from django.db import models


class Author(models.Model):
Author_ID = models.AutoField(primary_key=True, db_column="Author ID")
Author_ID = ObjectIdAutoField(primary_key=True, db_column="Author ID")
first_name = models.CharField(max_length=30, db_column="firstname")
last_name = models.CharField(max_length=30, db_column="last")

Expand All @@ -32,7 +34,7 @@ def __str__(self):


class Article(models.Model):
Article_ID = models.AutoField(primary_key=True, db_column="Article ID")
Article_ID = ObjectIdAutoField(primary_key=True, db_column="Article ID")
headline = models.CharField(max_length=100)
authors = models.ManyToManyField(Author, db_table="my_m2m_table")
primary_author = models.ForeignKey(
Expand Down
4 changes: 3 additions & 1 deletion tests/file_uploads/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from unittest import mock
from urllib.parse import quote

from bson import ObjectId

from django.conf import DEFAULT_STORAGE_ALIAS
from django.core.exceptions import SuspiciousFileOperation
from django.core.files import temp as tempfile
Expand Down Expand Up @@ -740,7 +742,7 @@ def test_filename_case_preservation(self):
"multipart/form-data; boundary=%(boundary)s" % vars,
)
self.assertEqual(response.status_code, 200)
id = int(response.content)
id = ObjectId(response.content.decode())
obj = FileModel.objects.get(pk=id)
# The name of the file uploaded and the file stored in the server-side
# shouldn't differ.
Expand Down
2 changes: 1 addition & 1 deletion tests/file_uploads/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def file_upload_filename_case_view(request):
file = request.FILES["file_field"]
obj = FileModel()
obj.testfile.save(file.name, file)
return HttpResponse("%d" % obj.pk)
return HttpResponse("%s" % obj.pk)


def file_upload_content_type_extra(request):
Expand Down
2 changes: 1 addition & 1 deletion tests/forms_tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Meta:
ordering = ("name",)

def __str__(self):
return "ChoiceOption %d" % self.pk
return "ChoiceOption %s" % self.pk


def choice_default():
Expand Down
11 changes: 6 additions & 5 deletions tests/generic_relations_regress/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ def test_annotate(self):
HasLinkThing.objects.create()
b = Board.objects.create(name=str(hs1.pk))
Link.objects.create(content_object=hs2)
link = Link.objects.create(content_object=hs1)
# An integer PK is required for the Sum() queryset that follows.
link = Link.objects.create(content_object=hs1, pk=10)
Link.objects.create(content_object=b)
qs = HasLinkThing.objects.annotate(Sum("links")).filter(pk=hs1.pk)
# If content_type restriction isn't in the query's join condition,
Expand All @@ -265,11 +266,11 @@ def test_annotate(self):
# clear cached results
qs = qs.all()
self.assertEqual(qs.count(), 1)
# Note - 0 here would be a nicer result...
self.assertIs(qs[0].links__sum, None)
# Unlike other databases, MongoDB returns 0 instead of null (None).
self.assertIs(qs[0].links__sum, 0)
# Finally test that filtering works.
self.assertEqual(qs.filter(links__sum__isnull=True).count(), 1)
self.assertEqual(qs.filter(links__sum__isnull=False).count(), 0)
self.assertEqual(qs.filter(links__sum__isnull=True).count(), 0)
self.assertEqual(qs.filter(links__sum__isnull=False).count(), 1)

def test_filter_targets_related_pk(self):
# Use hardcoded PKs to ensure different PKs for "link" and "hs2"
Expand Down
4 changes: 3 additions & 1 deletion tests/get_or_create/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,9 @@ def test_update_only_defaults_and_pre_save_fields_when_local_fields(self):
)
self.assertIs(created, False)
update_sqls = [
q["sql"] for q in captured_queries if q["sql"].startswith("UPDATE")
q["sql"]
for q in captured_queries
if q["sql"].startswith("get_or_create_book.update_many")
]
self.assertEqual(len(update_sqls), 1)
update_sql = update_sqls[0]
Expand Down
2 changes: 1 addition & 1 deletion tests/messages_tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class DeleteFormViewWithMsg(SuccessMessageMixin, DeleteView):
re_path("^add/(debug|info|success|warning|error)/$", add, name="add_message"),
path("add/msg/", ContactFormViewWithMsg.as_view(), name="add_success_msg"),
path(
"delete/msg/<int:pk>",
"delete/msg/<str:pk>",
DeleteFormViewWithMsg.as_view(),
name="success_msg_on_delete",
),
Expand Down
45 changes: 24 additions & 21 deletions tests/model_formsets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,23 @@ def test_change_form_deletion_when_invalid(self):
self.assertEqual(Poet.objects.count(), 0)

def test_outdated_deletion(self):
from bson import ObjectId

poet = Poet.objects.create(name="test")
poem = Poem.objects.create(name="Brevity is the soul of wit", poet=poet)

PoemFormSet = inlineformset_factory(
Poet, Poem, fields="__all__", can_delete=True
)

new_id = ObjectId()
# Simulate deletion of an object that doesn't exist in the database
data = {
"form-TOTAL_FORMS": "2",
"form-INITIAL_FORMS": "2",
"form-0-id": str(poem.pk),
"form-0-name": "foo",
"form-1-id": str(poem.pk + 1), # doesn't exist
"form-1-id": new_id, # doesn't exist
"form-1-name": "bar",
"form-1-DELETE": "on",
}
Expand All @@ -158,7 +161,7 @@ def test_outdated_deletion(self):
# Make sure the save went through correctly
self.assertEqual(Poem.objects.get(pk=poem.pk).name, "foo")
self.assertEqual(poet.poem_set.count(), 1)
self.assertFalse(Poem.objects.filter(pk=poem.pk + 1).exists())
self.assertFalse(Poem.objects.filter(pk=new_id).exists())


class ModelFormsetTest(TestCase):
Expand Down Expand Up @@ -234,15 +237,15 @@ def test_simple_save(self):
'<p><label for="id_form-0-name">Name:</label>'
'<input id="id_form-0-name" type="text" name="form-0-name" '
'value="Arthur Rimbaud" maxlength="100">'
'<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id"></p>'
'<input type="hidden" name="form-0-id" value="%s" id="id_form-0-id"></p>'
% author2.id,
)
self.assertHTMLEqual(
formset.forms[1].as_p(),
'<p><label for="id_form-1-name">Name:</label>'
'<input id="id_form-1-name" type="text" name="form-1-name" '
'value="Charles Baudelaire" maxlength="100">'
'<input type="hidden" name="form-1-id" value="%d" id="id_form-1-id"></p>'
'<input type="hidden" name="form-1-id" value="%s" id="id_form-1-id"></p>'
% author1.id,
)
self.assertHTMLEqual(
Expand Down Expand Up @@ -292,7 +295,7 @@ def test_simple_save(self):
'value="Arthur Rimbaud" maxlength="100"></p>'
'<p><label for="id_form-0-DELETE">Delete:</label>'
'<input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE">'
'<input type="hidden" name="form-0-id" value="%d" id="id_form-0-id"></p>'
'<input type="hidden" name="form-0-id" value="%s" id="id_form-0-id"></p>'
% author2.id,
)
self.assertHTMLEqual(
Expand All @@ -302,7 +305,7 @@ def test_simple_save(self):
'value="Charles Baudelaire" maxlength="100"></p>'
'<p><label for="id_form-1-DELETE">Delete:</label>'
'<input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE">'
'<input type="hidden" name="form-1-id" value="%d" id="id_form-1-id"></p>'
'<input type="hidden" name="form-1-id" value="%s" id="id_form-1-id"></p>'
% author1.id,
)
self.assertHTMLEqual(
Expand All @@ -312,7 +315,7 @@ def test_simple_save(self):
'value="Paul Verlaine" maxlength="100"></p>'
'<p><label for="id_form-2-DELETE">Delete:</label>'
'<input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE">'
'<input type="hidden" name="form-2-id" value="%d" id="id_form-2-id"></p>'
'<input type="hidden" name="form-2-id" value="%s" id="id_form-2-id"></p>'
% author3.id,
)
self.assertHTMLEqual(
Expand Down Expand Up @@ -604,7 +607,7 @@ def test_model_inheritance(self):
'<p><label for="id_form-0-write_speed">Write speed:</label>'
'<input type="number" name="form-0-write_speed" value="10" '
'id="id_form-0-write_speed">'
'<input type="hidden" name="form-0-author_ptr" value="%d" '
'<input type="hidden" name="form-0-author_ptr" value="%s" '
'id="id_form-0-author_ptr"></p>' % hemingway_id,
)
self.assertHTMLEqual(
Expand Down Expand Up @@ -649,7 +652,7 @@ def test_inline_formsets(self):
'<p><label for="id_book_set-0-title">Title:</label>'
'<input id="id_book_set-0-title" type="text" name="book_set-0-title" '
'maxlength="100">'
'<input type="hidden" name="book_set-0-author" value="%d" '
'<input type="hidden" name="book_set-0-author" value="%s" '
'id="id_book_set-0-author">'
'<input type="hidden" name="book_set-0-id" id="id_book_set-0-id">'
"</p>" % author.id,
Expand All @@ -659,7 +662,7 @@ def test_inline_formsets(self):
'<p><label for="id_book_set-1-title">Title:</label>'
'<input id="id_book_set-1-title" type="text" name="book_set-1-title" '
'maxlength="100">'
'<input type="hidden" name="book_set-1-author" value="%d" '
'<input type="hidden" name="book_set-1-author" value="%s" '
'id="id_book_set-1-author">'
'<input type="hidden" name="book_set-1-id" id="id_book_set-1-id"></p>'
% author.id,
Expand All @@ -669,7 +672,7 @@ def test_inline_formsets(self):
'<p><label for="id_book_set-2-title">Title:</label>'
'<input id="id_book_set-2-title" type="text" name="book_set-2-title" '
'maxlength="100">'
'<input type="hidden" name="book_set-2-author" value="%d" '
'<input type="hidden" name="book_set-2-author" value="%s" '
'id="id_book_set-2-author">'
'<input type="hidden" name="book_set-2-id" id="id_book_set-2-id"></p>'
% author.id,
Expand Down Expand Up @@ -709,9 +712,9 @@ def test_inline_formsets(self):
'<p><label for="id_book_set-0-title">Title:</label>'
'<input id="id_book_set-0-title" type="text" name="book_set-0-title" '
'value="Les Fleurs du Mal" maxlength="100">'
'<input type="hidden" name="book_set-0-author" value="%d" '
'<input type="hidden" name="book_set-0-author" value="%s" '
'id="id_book_set-0-author">'
'<input type="hidden" name="book_set-0-id" value="%d" '
'<input type="hidden" name="book_set-0-id" value="%s" '
'id="id_book_set-0-id"></p>'
% (
author.id,
Expand All @@ -723,7 +726,7 @@ def test_inline_formsets(self):
'<p><label for="id_book_set-1-title">Title:</label>'
'<input id="id_book_set-1-title" type="text" name="book_set-1-title" '
'maxlength="100">'
'<input type="hidden" name="book_set-1-author" value="%d" '
'<input type="hidden" name="book_set-1-author" value="%s" '
'id="id_book_set-1-author">'
'<input type="hidden" name="book_set-1-id" id="id_book_set-1-id"></p>'
% author.id,
Expand All @@ -733,7 +736,7 @@ def test_inline_formsets(self):
'<p><label for="id_book_set-2-title">Title:</label>'
'<input id="id_book_set-2-title" type="text" name="book_set-2-title" '
'maxlength="100">'
'<input type="hidden" name="book_set-2-author" value="%d" '
'<input type="hidden" name="book_set-2-author" value="%s" '
'id="id_book_set-2-author">'
'<input type="hidden" name="book_set-2-id" id="id_book_set-2-id"></p>'
% author.id,
Expand Down Expand Up @@ -1216,7 +1219,7 @@ def test_custom_pk(self):
'value="Joe Perry" maxlength="100">'
'<input type="hidden" name="owner_set-0-place" value="1" '
'id="id_owner_set-0-place">'
'<input type="hidden" name="owner_set-0-auto_id" value="%d" '
'<input type="hidden" name="owner_set-0-auto_id" value="%s" '
'id="id_owner_set-0-auto_id"></p>' % owner1.auto_id,
)
self.assertHTMLEqual(
Expand Down Expand Up @@ -1268,8 +1271,8 @@ def test_custom_pk(self):
'<p><label for="id_form-0-owner">Owner:</label>'
'<select name="form-0-owner" id="id_form-0-owner">'
'<option value="" selected>---------</option>'
'<option value="%d">Joe Perry at Giordanos</option>'
'<option value="%d">Jack Berry at Giordanos</option>'
'<option value="%s">Joe Perry at Giordanos</option>'
'<option value="%s">Jack Berry at Giordanos</option>'
"</select></p>"
'<p><label for="id_form-0-age">Age:</label>'
'<input type="number" name="form-0-age" id="id_form-0-age" min="0"></p>'
Expand All @@ -1289,7 +1292,7 @@ def test_custom_pk(self):
'<p><label for="id_ownerprofile-0-age">Age:</label>'
'<input type="number" name="ownerprofile-0-age" '
'id="id_ownerprofile-0-age" min="0">'
'<input type="hidden" name="ownerprofile-0-owner" value="%d" '
'<input type="hidden" name="ownerprofile-0-owner" value="%s" '
'id="id_ownerprofile-0-owner"></p>' % owner1.auto_id,
)

Expand All @@ -1315,7 +1318,7 @@ def test_custom_pk(self):
'<p><label for="id_ownerprofile-0-age">Age:</label>'
'<input type="number" name="ownerprofile-0-age" value="54" '
'id="id_ownerprofile-0-age" min="0">'
'<input type="hidden" name="ownerprofile-0-owner" value="%d" '
'<input type="hidden" name="ownerprofile-0-owner" value="%s" '
'id="id_ownerprofile-0-owner"></p>' % owner1.auto_id,
)

Expand Down Expand Up @@ -1588,7 +1591,7 @@ def test_callable_defaults(self):
'<p><label for="id_membership_set-0-karma">Karma:</label>'
'<input type="number" name="membership_set-0-karma" '
'id="id_membership_set-0-karma">'
'<input type="hidden" name="membership_set-0-person" value="%d" '
'<input type="hidden" name="membership_set-0-person" value="%s" '
'id="id_membership_set-0-person">'
'<input type="hidden" name="membership_set-0-id" '
'id="id_membership_set-0-id"></p>' % person.id,
Expand Down
3 changes: 2 additions & 1 deletion tests/model_indexes/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,8 @@ def test_name_set(self):
index_names,
[
"model_index_title_196f42_idx",
"model_index_isbn_34f975_idx",
# Edited since MongoDB's id column is _id.
"model_index_isbn_8cecda_idx",
"model_indexes_book_barcode_idx",
],
)
Expand Down
4 changes: 3 additions & 1 deletion tests/model_inheritance_regress/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import datetime

from django_mongodb.fields import ObjectIdAutoField

from django.db import models


Expand Down Expand Up @@ -195,7 +197,7 @@ class Profile(User):

# Check concrete + concrete -> concrete -> concrete
class Politician(models.Model):
politician_id = models.AutoField(primary_key=True)
politician_id = ObjectIdAutoField(primary_key=True)
title = models.CharField(max_length=50)


Expand Down
Loading

0 comments on commit 5a247e8

Please sign in to comment.