From 03b990111fb9d83fe2ddde4350c6795181885db0 Mon Sep 17 00:00:00 2001
From: Tim Graham
Date: Wed, 25 Sep 2024 20:08:15 -0400
Subject: [PATCH] edits for many test apps
---
tests/admin_utils/test_logentry.py | 2 +-
tests/auth_tests/test_context_processors.py | 2 +-
tests/auth_tests/test_management.py | 8 ++--
tests/custom_columns/models.py | 6 ++-
tests/file_uploads/tests.py | 4 +-
tests/file_uploads/views.py | 2 +-
tests/forms_tests/models.py | 2 +-
tests/generic_relations_regress/tests.py | 11 ++---
tests/get_or_create/tests.py | 4 +-
tests/messages_tests/urls.py | 2 +-
tests/model_formsets/tests.py | 45 +++++++++++----------
tests/model_indexes/tests.py | 3 +-
tests/model_inheritance_regress/models.py | 4 +-
tests/multiple_database/tests.py | 18 ++++-----
tests/proxy_models/tests.py | 10 ++---
tests/test_utils/urls.py | 2 +-
16 files changed, 70 insertions(+), 55 deletions(-)
diff --git a/tests/admin_utils/test_logentry.py b/tests/admin_utils/test_logentry.py
index b700fe54b9..19fd4f95ca 100644
--- a/tests/admin_utils/test_logentry.py
+++ b/tests/admin_utils/test_logentry.py
@@ -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())
diff --git a/tests/auth_tests/test_context_processors.py b/tests/auth_tests/test_context_processors.py
index ab621313e8..defb9c0d96 100644
--- a/tests/auth_tests/test_context_processors.py
+++ b/tests/auth_tests/test_context_processors.py
@@ -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/")
diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py
index 872fe75e8a..0b5c777467 100644
--- a/tests/auth_tests/test_management.py
+++ b/tests/auth_tests/test_management.py
@@ -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="mymail@gmail.com")
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,
@@ -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)
diff --git a/tests/custom_columns/models.py b/tests/custom_columns/models.py
index 378a001820..9fe66e7088 100644
--- a/tests/custom_columns/models.py
+++ b/tests/custom_columns/models.py
@@ -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")
@@ -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(
diff --git a/tests/file_uploads/tests.py b/tests/file_uploads/tests.py
index 17e989040d..cc183a16af 100644
--- a/tests/file_uploads/tests.py
+++ b/tests/file_uploads/tests.py
@@ -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
@@ -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.
diff --git a/tests/file_uploads/views.py b/tests/file_uploads/views.py
index c1d4ca5358..d8186108f6 100644
--- a/tests/file_uploads/views.py
+++ b/tests/file_uploads/views.py
@@ -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):
diff --git a/tests/forms_tests/models.py b/tests/forms_tests/models.py
index d6d0725b32..b1319abe17 100644
--- a/tests/forms_tests/models.py
+++ b/tests/forms_tests/models.py
@@ -68,7 +68,7 @@ class Meta:
ordering = ("name",)
def __str__(self):
- return "ChoiceOption %d" % self.pk
+ return "ChoiceOption %s" % self.pk
def choice_default():
diff --git a/tests/generic_relations_regress/tests.py b/tests/generic_relations_regress/tests.py
index 20c59f839f..f95d8b39ab 100644
--- a/tests/generic_relations_regress/tests.py
+++ b/tests/generic_relations_regress/tests.py
@@ -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,
@@ -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"
diff --git a/tests/get_or_create/tests.py b/tests/get_or_create/tests.py
index 0b56d6b1a2..8e15ec536c 100644
--- a/tests/get_or_create/tests.py
+++ b/tests/get_or_create/tests.py
@@ -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]
diff --git a/tests/messages_tests/urls.py b/tests/messages_tests/urls.py
index 3f70911d4f..0cfbf2248f 100644
--- a/tests/messages_tests/urls.py
+++ b/tests/messages_tests/urls.py
@@ -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/",
+ "delete/msg/",
DeleteFormViewWithMsg.as_view(),
name="success_msg_on_delete",
),
diff --git a/tests/model_formsets/tests.py b/tests/model_formsets/tests.py
index f78772da56..11d1725427 100644
--- a/tests/model_formsets/tests.py
+++ b/tests/model_formsets/tests.py
@@ -130,6 +130,8 @@ 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)
@@ -137,13 +139,14 @@ def test_outdated_deletion(self):
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",
}
@@ -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):
@@ -234,7 +237,7 @@ def test_simple_save(self):
''
''
- '
'
+ '
'
% author2.id,
)
self.assertHTMLEqual(
@@ -242,7 +245,7 @@ def test_simple_save(self):
''
''
- '
'
+ ''
% author1.id,
)
self.assertHTMLEqual(
@@ -292,7 +295,7 @@ def test_simple_save(self):
'value="Arthur Rimbaud" maxlength="100">'
''
''
- '
'
+ ''
% author2.id,
)
self.assertHTMLEqual(
@@ -302,7 +305,7 @@ def test_simple_save(self):
'value="Charles Baudelaire" maxlength="100">'
''
''
- '
'
+ ''
% author1.id,
)
self.assertHTMLEqual(
@@ -312,7 +315,7 @@ def test_simple_save(self):
'value="Paul Verlaine" maxlength="100">'
''
''
- '
'
+ ''
% author3.id,
)
self.assertHTMLEqual(
@@ -604,7 +607,7 @@ def test_model_inheritance(self):
''
''
- '
' % hemingway_id,
)
self.assertHTMLEqual(
@@ -649,7 +652,7 @@ def test_inline_formsets(self):
''
''
- ''
''
"
" % author.id,
@@ -659,7 +662,7 @@ def test_inline_formsets(self):
''
''
- ''
'
'
% author.id,
@@ -669,7 +672,7 @@ def test_inline_formsets(self):
''
''
- ''
'
'
% author.id,
@@ -709,9 +712,9 @@ def test_inline_formsets(self):
''
''
- ''
- '
'
% (
author.id,
@@ -723,7 +726,7 @@ def test_inline_formsets(self):
''
''
- ''
'
'
% author.id,
@@ -733,7 +736,7 @@ def test_inline_formsets(self):
''
''
- ''
'
'
% author.id,
@@ -1216,7 +1219,7 @@ def test_custom_pk(self):
'value="Joe Perry" maxlength="100">'
''
- '' % owner1.auto_id,
)
self.assertHTMLEqual(
@@ -1268,8 +1271,8 @@ def test_custom_pk(self):
''
'
"
''
'
'
@@ -1289,7 +1292,7 @@ def test_custom_pk(self):
''
''
- '
' % owner1.auto_id,
)
@@ -1315,7 +1318,7 @@ def test_custom_pk(self):
''
''
- '
' % owner1.auto_id,
)
@@ -1588,7 +1591,7 @@ def test_callable_defaults(self):
''
''
- ''
'
' % person.id,
diff --git a/tests/model_indexes/tests.py b/tests/model_indexes/tests.py
index 0c8378f624..a30cb55223 100644
--- a/tests/model_indexes/tests.py
+++ b/tests/model_indexes/tests.py
@@ -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",
],
)
diff --git a/tests/model_inheritance_regress/models.py b/tests/model_inheritance_regress/models.py
index 11886bb48d..37b262f14e 100644
--- a/tests/model_inheritance_regress/models.py
+++ b/tests/model_inheritance_regress/models.py
@@ -1,5 +1,7 @@
import datetime
+from django_mongodb.fields import ObjectIdAutoField
+
from django.db import models
@@ -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)
diff --git a/tests/multiple_database/tests.py b/tests/multiple_database/tests.py
index 337ebae75e..383c8e80df 100644
--- a/tests/multiple_database/tests.py
+++ b/tests/multiple_database/tests.py
@@ -142,15 +142,15 @@ def test_basic_queries(self):
with self.assertRaises(Book.DoesNotExist):
Book.objects.using("default").get(published__year=2009)
- years = Book.objects.using("other").dates("published", "year")
- self.assertEqual([o.year for o in years], [2009])
- years = Book.objects.using("default").dates("published", "year")
- self.assertEqual([o.year for o in years], [])
-
- months = Book.objects.using("other").dates("published", "month")
- self.assertEqual([o.month for o in months], [5])
- months = Book.objects.using("default").dates("published", "month")
- self.assertEqual([o.month for o in months], [])
+ # years = Book.objects.using("other").dates("published", "year")
+ # self.assertEqual([o.year for o in years], [2009])
+ # years = Book.objects.using("default").dates("published", "year")
+ # self.assertEqual([o.year for o in years], [])
+
+ # months = Book.objects.using("other").dates("published", "month")
+ # self.assertEqual([o.month for o in months], [5])
+ # months = Book.objects.using("default").dates("published", "month")
+ # self.assertEqual([o.month for o in months], [])
def test_m2m_separation(self):
"M2M fields are constrained to a single database"
diff --git a/tests/proxy_models/tests.py b/tests/proxy_models/tests.py
index 7caa43d489..f1476fec3e 100644
--- a/tests/proxy_models/tests.py
+++ b/tests/proxy_models/tests.py
@@ -107,9 +107,9 @@ def test_proxy_included_in_ancestors(self):
Proxy models are included in the ancestors for a model's DoesNotExist
and MultipleObjectsReturned
"""
- Person.objects.create(name="Foo McBar")
- MyPerson.objects.create(name="Bazza del Frob")
- LowerStatusPerson.objects.create(status="low", name="homer")
+ Person.objects.create(name="Foo McBar", pk=1)
+ MyPerson.objects.create(name="Bazza del Frob", pk=2)
+ LowerStatusPerson.objects.create(status="low", name="homer", pk=3)
max_id = Person.objects.aggregate(max_id=models.Max("id"))["max_id"]
with self.assertRaises(Person.DoesNotExist):
@@ -119,8 +119,8 @@ def test_proxy_included_in_ancestors(self):
with self.assertRaises(Person.DoesNotExist):
StatusPerson.objects.get(name="Zathras")
- StatusPerson.objects.create(name="Bazza Jr.")
- StatusPerson.objects.create(name="Foo Jr.")
+ StatusPerson.objects.create(name="Bazza Jr.", pk=4)
+ StatusPerson.objects.create(name="Foo Jr.", pk=5)
max_id = Person.objects.aggregate(max_id=models.Max("id"))["max_id"]
with self.assertRaises(Person.MultipleObjectsReturned):
diff --git a/tests/test_utils/urls.py b/tests/test_utils/urls.py
index 37d0c76a11..f11066a5c8 100644
--- a/tests/test_utils/urls.py
+++ b/tests/test_utils/urls.py
@@ -3,7 +3,7 @@
from . import views
urlpatterns = [
- path("test_utils/get_person//", views.get_person),
+ path("test_utils/get_person//", views.get_person),
path(
"test_utils/no_template_used/", views.no_template_used, name="no_template_used"
),