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 10, 2024
1 parent b283c45 commit 75652ae
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 71 deletions.
12 changes: 6 additions & 6 deletions tests/admin_inlines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ def test_inline_change_m2m_change_perm(self):
)
self.assertContains(
response,
'<input type="hidden" id="id_Author_books-0-id" value="%i" '
'<input type="hidden" id="id_Author_books-0-id" value="%s" '
'name="Author_books-0-id">' % self.author_book_auto_m2m_intermediate_id,
html=True,
)
Expand All @@ -1093,7 +1093,7 @@ def test_inline_change_fk_add_perm(self):
)
self.assertNotContains(
response,
'<input type="hidden" id="id_inner2_set-0-id" value="%i" '
'<input type="hidden" id="id_inner2_set-0-id" value="%s" '
'name="inner2_set-0-id">' % self.inner2.id,
html=True,
)
Expand All @@ -1115,7 +1115,7 @@ def test_inline_change_fk_change_perm(self):
)
self.assertContains(
response,
'<input type="hidden" id="id_inner2_set-0-id" value="%i" '
'<input type="hidden" id="id_inner2_set-0-id" value="%s" '
'name="inner2_set-0-id">' % self.inner2.id,
html=True,
)
Expand Down Expand Up @@ -1158,7 +1158,7 @@ def test_inline_change_fk_add_change_perm(self):
)
self.assertContains(
response,
'<input type="hidden" id="id_inner2_set-0-id" value="%i" '
'<input type="hidden" id="id_inner2_set-0-id" value="%s" '
'name="inner2_set-0-id">' % self.inner2.id,
html=True,
)
Expand All @@ -1184,7 +1184,7 @@ def test_inline_change_fk_change_del_perm(self):
)
self.assertContains(
response,
'<input type="hidden" id="id_inner2_set-0-id" value="%i" '
'<input type="hidden" id="id_inner2_set-0-id" value="%s" '
'name="inner2_set-0-id">' % self.inner2.id,
html=True,
)
Expand Down Expand Up @@ -1215,7 +1215,7 @@ def test_inline_change_fk_all_perms(self):
)
self.assertContains(
response,
'<input type="hidden" id="id_inner2_set-0-id" value="%i" '
'<input type="hidden" id="id_inner2_set-0-id" value="%s" '
'name="inner2_set-0-id">' % self.inner2.id,
html=True,
)
Expand Down
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
5 changes: 2 additions & 3 deletions tests/async/test_async_queryset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime

from asgiref.sync import async_to_sync, sync_to_async
from bson import ObjectId

from django.db import NotSupportedError, connection
from django.db.models import Prefetch, Sum
Expand Down Expand Up @@ -207,9 +208,7 @@ async def test_acontains(self):
check = await SimpleModel.objects.acontains(self.s1)
self.assertIs(check, True)
# Unsaved instances are not allowed, so use an ID known not to exist.
check = await SimpleModel.objects.acontains(
SimpleModel(id=self.s3.id + 1, field=4)
)
check = await SimpleModel.objects.acontains(SimpleModel(id=ObjectId(), field=4))
self.assertIs(check, False)

async def test_aupdate(self):
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("db.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
Loading

0 comments on commit 75652ae

Please sign in to comment.