diff options
| author | Tim Graham <timograham@gmail.com> | 2026-02-06 11:06:17 -0500 |
|---|---|---|
| committer | Jacob Walls <jacobtylerwalls@gmail.com> | 2026-02-10 16:04:24 -0500 |
| commit | d007fcf7291cc3c24d4545e23c759bde22b6a8a6 (patch) | |
| tree | 0ed3491b9e2e0d61694eff2535c99e1eded34b27 /tests/generic_views | |
| parent | 64a4d8ad005721b17fafd3399bdf49d7d0f94455 (diff) | |
Modified tests to format PKs with %s rather than %d.
It's how Django formats values internally and makes tests compatible
with databases that use non-integer primary keys.
Diffstat (limited to 'tests/generic_views')
| -rw-r--r-- | tests/generic_views/test_dates.py | 8 | ||||
| -rw-r--r-- | tests/generic_views/test_edit.py | 56 |
2 files changed, 32 insertions, 32 deletions
diff --git a/tests/generic_views/test_dates.py b/tests/generic_views/test_dates.py index 140083d315..d12b1f6baa 100644 --- a/tests/generic_views/test_dates.py +++ b/tests/generic_views/test_dates.py @@ -895,7 +895,7 @@ class DateDetailViewTests(TestDataMixin, TestCase): def test_datetime_date_detail(self): bs = BookSigning.objects.create(event_date=datetime.datetime(2008, 4, 2, 12, 0)) - res = self.client.get("/dates/booksignings/2008/apr/2/%d/" % bs.pk) + res = self.client.get("/dates/booksignings/2008/apr/2/%s/" % bs.pk) self.assertEqual(res.status_code, 200) @requires_tz_support @@ -904,17 +904,17 @@ class DateDetailViewTests(TestDataMixin, TestCase): bs = BookSigning.objects.create( event_date=datetime.datetime(2008, 4, 2, 12, 0, tzinfo=datetime.UTC) ) - res = self.client.get("/dates/booksignings/2008/apr/2/%d/" % bs.pk) + res = self.client.get("/dates/booksignings/2008/apr/2/%s/" % bs.pk) self.assertEqual(res.status_code, 200) # 2008-04-02T00:00:00+03:00 (beginning of day) > # 2008-04-01T22:00:00+00:00 (book signing event date). bs.event_date = datetime.datetime(2008, 4, 1, 22, 0, tzinfo=datetime.UTC) bs.save() - res = self.client.get("/dates/booksignings/2008/apr/2/%d/" % bs.pk) + res = self.client.get("/dates/booksignings/2008/apr/2/%s/" % bs.pk) self.assertEqual(res.status_code, 200) # 2008-04-03T00:00:00+03:00 (end of day) > 2008-04-02T22:00:00+00:00 # (book signing event date). bs.event_date = datetime.datetime(2008, 4, 2, 22, 0, tzinfo=datetime.UTC) bs.save() - res = self.client.get("/dates/booksignings/2008/apr/2/%d/" % bs.pk) + res = self.client.get("/dates/booksignings/2008/apr/2/%s/" % bs.pk) self.assertEqual(res.status_code, 404) diff --git a/tests/generic_views/test_edit.py b/tests/generic_views/test_edit.py index d66b316090..b483ea1028 100644 --- a/tests/generic_views/test_edit.py +++ b/tests/generic_views/test_edit.py @@ -124,7 +124,7 @@ class CreateViewTests(TestCase): res = self.client.post("/edit/artists/create/", {"name": "Rene Magritte"}) self.assertEqual(res.status_code, 302) artist = Artist.objects.get(name="Rene Magritte") - self.assertRedirects(res, "/detail/artist/%d/" % artist.pk) + self.assertRedirects(res, "/detail/artist/%s/" % artist.pk) self.assertQuerySetEqual(Artist.objects.all(), [artist]) def test_create_with_redirect(self): @@ -148,7 +148,7 @@ class CreateViewTests(TestCase): ) self.assertEqual(res.status_code, 302) pk = Author.objects.first().pk - self.assertRedirects(res, "/edit/author/%d/update/" % pk) + self.assertRedirects(res, "/edit/author/%s/update/" % pk) # Also test with escaped chars in URL res = self.client.post( "/edit/authors/create/interpolate_redirect_nonascii/", @@ -245,7 +245,7 @@ class UpdateViewTests(TestCase): ) def test_update_post(self): - res = self.client.get("/edit/author/%d/update/" % self.author.pk) + res = self.client.get("/edit/author/%s/update/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.context["form"], forms.ModelForm) self.assertEqual(res.context["object"], self.author) @@ -255,7 +255,7 @@ class UpdateViewTests(TestCase): # Modification with both POST and PUT (browser compatible) res = self.client.post( - "/edit/author/%d/update/" % self.author.pk, + "/edit/author/%s/update/" % self.author.pk, {"name": "Randall Munroe (xkcd)", "slug": "randall-munroe"}, ) self.assertEqual(res.status_code, 302) @@ -266,7 +266,7 @@ class UpdateViewTests(TestCase): def test_update_invalid(self): res = self.client.post( - "/edit/author/%d/update/" % self.author.pk, + "/edit/author/%s/update/" % self.author.pk, {"name": "A" * 101, "slug": "randall-munroe"}, ) self.assertEqual(res.status_code, 200) @@ -278,15 +278,15 @@ class UpdateViewTests(TestCase): def test_update_with_object_url(self): a = Artist.objects.create(name="Rene Magritte") res = self.client.post( - "/edit/artists/%d/update/" % a.pk, {"name": "Rene Magritte"} + "/edit/artists/%s/update/" % a.pk, {"name": "Rene Magritte"} ) self.assertEqual(res.status_code, 302) - self.assertRedirects(res, "/detail/artist/%d/" % a.pk) + self.assertRedirects(res, "/detail/artist/%s/" % a.pk) self.assertQuerySetEqual(Artist.objects.all(), [a]) def test_update_with_redirect(self): res = self.client.post( - "/edit/author/%d/update/redirect/" % self.author.pk, + "/edit/author/%s/update/redirect/" % self.author.pk, {"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"}, ) self.assertEqual(res.status_code, 302) @@ -298,7 +298,7 @@ class UpdateViewTests(TestCase): def test_update_with_interpolated_redirect(self): res = self.client.post( - "/edit/author/%d/update/interpolate_redirect/" % self.author.pk, + "/edit/author/%s/update/interpolate_redirect/" % self.author.pk, {"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"}, ) self.assertQuerySetEqual( @@ -307,10 +307,10 @@ class UpdateViewTests(TestCase): ) self.assertEqual(res.status_code, 302) pk = Author.objects.first().pk - self.assertRedirects(res, "/edit/author/%d/update/" % pk) + self.assertRedirects(res, "/edit/author/%s/update/" % pk) # Also test with escaped chars in URL res = self.client.post( - "/edit/author/%d/update/interpolate_redirect_nonascii/" % self.author.pk, + "/edit/author/%s/update/interpolate_redirect_nonascii/" % self.author.pk, {"name": "John Doe", "slug": "john-doe"}, ) self.assertEqual(res.status_code, 302) @@ -318,7 +318,7 @@ class UpdateViewTests(TestCase): self.assertRedirects(res, "/%C3%A9dit/author/{}/update/".format(pk)) def test_update_with_special_properties(self): - res = self.client.get("/edit/author/%d/update/special/" % self.author.pk) + res = self.client.get("/edit/author/%s/update/special/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.context["form"], views.AuthorForm) self.assertEqual(res.context["object"], self.author) @@ -327,11 +327,11 @@ class UpdateViewTests(TestCase): self.assertTemplateUsed(res, "generic_views/form.html") res = self.client.post( - "/edit/author/%d/update/special/" % self.author.pk, + "/edit/author/%s/update/special/" % self.author.pk, {"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"}, ) self.assertEqual(res.status_code, 302) - self.assertRedirects(res, "/detail/author/%d/" % self.author.pk) + self.assertRedirects(res, "/detail/author/%s/" % self.author.pk) self.assertQuerySetEqual( Author.objects.values_list("name", flat=True), ["Randall Munroe (author of xkcd)"], @@ -344,7 +344,7 @@ class UpdateViewTests(TestCase): ) with self.assertRaisesMessage(ImproperlyConfigured, msg): self.client.post( - "/edit/author/%d/update/naive/" % self.author.pk, + "/edit/author/%s/update/naive/" % self.author.pk, {"name": "Randall Munroe (author of xkcd)", "slug": "randall-munroe"}, ) @@ -379,37 +379,37 @@ class DeleteViewTests(TestCase): ) def test_delete_by_post(self): - res = self.client.get("/edit/author/%d/delete/" % self.author.pk) + res = self.client.get("/edit/author/%s/delete/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertEqual(res.context["object"], self.author) self.assertEqual(res.context["author"], self.author) self.assertTemplateUsed(res, "generic_views/author_confirm_delete.html") # Deletion with POST - res = self.client.post("/edit/author/%d/delete/" % self.author.pk) + res = self.client.post("/edit/author/%s/delete/" % self.author.pk) self.assertEqual(res.status_code, 302) self.assertRedirects(res, "/list/authors/") self.assertQuerySetEqual(Author.objects.all(), []) def test_delete_by_delete(self): # Deletion with browser compatible DELETE method - res = self.client.delete("/edit/author/%d/delete/" % self.author.pk) + res = self.client.delete("/edit/author/%s/delete/" % self.author.pk) self.assertEqual(res.status_code, 302) self.assertRedirects(res, "/list/authors/") self.assertQuerySetEqual(Author.objects.all(), []) def test_delete_with_redirect(self): - res = self.client.post("/edit/author/%d/delete/redirect/" % self.author.pk) + res = self.client.post("/edit/author/%s/delete/redirect/" % self.author.pk) self.assertEqual(res.status_code, 302) self.assertRedirects(res, "/edit/authors/create/") self.assertQuerySetEqual(Author.objects.all(), []) def test_delete_with_interpolated_redirect(self): res = self.client.post( - "/edit/author/%d/delete/interpolate_redirect/" % self.author.pk + "/edit/author/%s/delete/interpolate_redirect/" % self.author.pk ) self.assertEqual(res.status_code, 302) - self.assertRedirects(res, "/edit/authors/create/?deleted=%d" % self.author.pk) + self.assertRedirects(res, "/edit/authors/create/?deleted=%s" % self.author.pk) self.assertQuerySetEqual(Author.objects.all(), []) # Also test with escaped chars in URL a = Author.objects.create( @@ -422,14 +422,14 @@ class DeleteViewTests(TestCase): self.assertRedirects(res, "/%C3%A9dit/authors/create/?deleted={}".format(a.pk)) def test_delete_with_special_properties(self): - res = self.client.get("/edit/author/%d/delete/special/" % self.author.pk) + res = self.client.get("/edit/author/%s/delete/special/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertEqual(res.context["object"], self.author) self.assertEqual(res.context["thingy"], self.author) self.assertNotIn("author", res.context) self.assertTemplateUsed(res, "generic_views/confirm_delete.html") - res = self.client.post("/edit/author/%d/delete/special/" % self.author.pk) + res = self.client.post("/edit/author/%s/delete/special/" % self.author.pk) self.assertEqual(res.status_code, 302) self.assertRedirects(res, "/list/authors/") self.assertQuerySetEqual(Author.objects.all(), []) @@ -437,29 +437,29 @@ class DeleteViewTests(TestCase): def test_delete_without_redirect(self): msg = "No URL to redirect to. Provide a success_url." with self.assertRaisesMessage(ImproperlyConfigured, msg): - self.client.post("/edit/author/%d/delete/naive/" % self.author.pk) + self.client.post("/edit/author/%s/delete/naive/" % self.author.pk) def test_delete_with_form_as_post(self): - res = self.client.get("/edit/author/%d/delete/form/" % self.author.pk) + res = self.client.get("/edit/author/%s/delete/form/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertEqual(res.context["object"], self.author) self.assertEqual(res.context["author"], self.author) self.assertTemplateUsed(res, "generic_views/author_confirm_delete.html") res = self.client.post( - "/edit/author/%d/delete/form/" % self.author.pk, data={"confirm": True} + "/edit/author/%s/delete/form/" % self.author.pk, data={"confirm": True} ) self.assertEqual(res.status_code, 302) self.assertRedirects(res, "/list/authors/") self.assertSequenceEqual(Author.objects.all(), []) def test_delete_with_form_as_post_with_validation_error(self): - res = self.client.get("/edit/author/%d/delete/form/" % self.author.pk) + res = self.client.get("/edit/author/%s/delete/form/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertEqual(res.context["object"], self.author) self.assertEqual(res.context["author"], self.author) self.assertTemplateUsed(res, "generic_views/author_confirm_delete.html") - res = self.client.post("/edit/author/%d/delete/form/" % self.author.pk) + res = self.client.post("/edit/author/%s/delete/form/" % self.author.pk) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.context_data["form"].errors), 2) self.assertEqual( |
