mirror of
https://github.com/django/django.git
synced 2025-10-24 06:06:09 +00:00
Refs #33476 -- Reformatted code with Black.
This commit is contained in:
committed by
Mariusz Felisiak
parent
f68fa8b45d
commit
9c19aff7c7
@@ -9,10 +9,14 @@ class BasicCustomPKTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.dan = Employee.objects.create(
|
||||
employee_code=123, first_name="Dan", last_name="Jones",
|
||||
employee_code=123,
|
||||
first_name="Dan",
|
||||
last_name="Jones",
|
||||
)
|
||||
cls.fran = Employee.objects.create(
|
||||
employee_code=456, first_name="Fran", last_name="Bones",
|
||||
employee_code=456,
|
||||
first_name="Fran",
|
||||
last_name="Bones",
|
||||
)
|
||||
cls.business = Business.objects.create(name="Sears")
|
||||
cls.business.employees.add(cls.dan, cls.fran)
|
||||
@@ -22,46 +26,48 @@ class BasicCustomPKTests(TestCase):
|
||||
Both pk and custom attribute_name can be used in filter and friends
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.filter(pk=123), [
|
||||
Employee.objects.filter(pk=123),
|
||||
[
|
||||
"Dan Jones",
|
||||
],
|
||||
str
|
||||
str,
|
||||
)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.filter(employee_code=123), [
|
||||
Employee.objects.filter(employee_code=123),
|
||||
[
|
||||
"Dan Jones",
|
||||
],
|
||||
str
|
||||
str,
|
||||
)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.filter(pk__in=[123, 456]), [
|
||||
Employee.objects.filter(pk__in=[123, 456]),
|
||||
[
|
||||
"Fran Bones",
|
||||
"Dan Jones",
|
||||
],
|
||||
str
|
||||
str,
|
||||
)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.all(), [
|
||||
Employee.objects.all(),
|
||||
[
|
||||
"Fran Bones",
|
||||
"Dan Jones",
|
||||
],
|
||||
str
|
||||
str,
|
||||
)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Business.objects.filter(name="Sears"), [
|
||||
"Sears"
|
||||
],
|
||||
lambda b: b.name
|
||||
Business.objects.filter(name="Sears"), ["Sears"], lambda b: b.name
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
Business.objects.filter(pk="Sears"), [
|
||||
Business.objects.filter(pk="Sears"),
|
||||
[
|
||||
"Sears",
|
||||
],
|
||||
lambda b: b.name
|
||||
lambda b: b.name,
|
||||
)
|
||||
|
||||
def test_querysets_related_name(self):
|
||||
@@ -69,17 +75,19 @@ class BasicCustomPKTests(TestCase):
|
||||
Custom pk doesn't affect related_name based lookups
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
self.business.employees.all(), [
|
||||
self.business.employees.all(),
|
||||
[
|
||||
"Fran Bones",
|
||||
"Dan Jones",
|
||||
],
|
||||
str
|
||||
str,
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
self.fran.business_set.all(), [
|
||||
self.fran.business_set.all(),
|
||||
[
|
||||
"Sears",
|
||||
],
|
||||
lambda b: b.name
|
||||
lambda b: b.name,
|
||||
)
|
||||
|
||||
def test_querysets_relational(self):
|
||||
@@ -87,14 +95,16 @@ class BasicCustomPKTests(TestCase):
|
||||
Queries across tables, involving primary key
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.filter(business__name="Sears"), [
|
||||
Employee.objects.filter(business__name="Sears"),
|
||||
[
|
||||
"Fran Bones",
|
||||
"Dan Jones",
|
||||
],
|
||||
str,
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.filter(business__pk="Sears"), [
|
||||
Employee.objects.filter(business__pk="Sears"),
|
||||
[
|
||||
"Fran Bones",
|
||||
"Dan Jones",
|
||||
],
|
||||
@@ -102,23 +112,26 @@ class BasicCustomPKTests(TestCase):
|
||||
)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Business.objects.filter(employees__employee_code=123), [
|
||||
Business.objects.filter(employees__employee_code=123),
|
||||
[
|
||||
"Sears",
|
||||
],
|
||||
lambda b: b.name
|
||||
lambda b: b.name,
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
Business.objects.filter(employees__pk=123), [
|
||||
Business.objects.filter(employees__pk=123),
|
||||
[
|
||||
"Sears",
|
||||
],
|
||||
lambda b: b.name,
|
||||
)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Business.objects.filter(employees__first_name__startswith="Fran"), [
|
||||
Business.objects.filter(employees__first_name__startswith="Fran"),
|
||||
[
|
||||
"Sears",
|
||||
],
|
||||
lambda b: b.name
|
||||
lambda b: b.name,
|
||||
)
|
||||
|
||||
def test_get(self):
|
||||
@@ -146,7 +159,9 @@ class BasicCustomPKTests(TestCase):
|
||||
# Or we can use the real attribute name for the primary key:
|
||||
self.assertEqual(e.employee_code, 123)
|
||||
|
||||
with self.assertRaisesMessage(AttributeError, "'Employee' object has no attribute 'id'"):
|
||||
with self.assertRaisesMessage(
|
||||
AttributeError, "'Employee' object has no attribute 'id'"
|
||||
):
|
||||
e.id
|
||||
|
||||
def test_in_bulk(self):
|
||||
@@ -156,9 +171,12 @@ class BasicCustomPKTests(TestCase):
|
||||
emps = Employee.objects.in_bulk([123, 456])
|
||||
self.assertEqual(emps[123], self.dan)
|
||||
|
||||
self.assertEqual(Business.objects.in_bulk(["Sears"]), {
|
||||
"Sears": self.business,
|
||||
})
|
||||
self.assertEqual(
|
||||
Business.objects.in_bulk(["Sears"]),
|
||||
{
|
||||
"Sears": self.business,
|
||||
},
|
||||
)
|
||||
|
||||
def test_save(self):
|
||||
"""
|
||||
@@ -169,11 +187,12 @@ class BasicCustomPKTests(TestCase):
|
||||
fran.save()
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Employee.objects.filter(last_name="Jones"), [
|
||||
Employee.objects.filter(last_name="Jones"),
|
||||
[
|
||||
"Dan Jones",
|
||||
"Fran Jones",
|
||||
],
|
||||
str
|
||||
str,
|
||||
)
|
||||
|
||||
|
||||
@@ -189,7 +208,7 @@ class CustomPKTests(TestCase):
|
||||
|
||||
def test_unicode_pk(self):
|
||||
# Primary key may be Unicode string.
|
||||
Business.objects.create(name='jaźń')
|
||||
Business.objects.create(name="jaźń")
|
||||
|
||||
def test_unique_pk(self):
|
||||
# The primary key must also be unique, so trying to create a new object
|
||||
@@ -199,12 +218,12 @@ class CustomPKTests(TestCase):
|
||||
)
|
||||
with self.assertRaises(IntegrityError):
|
||||
with transaction.atomic():
|
||||
Employee.objects.create(employee_code=123, first_name="Fred", last_name="Jones")
|
||||
Employee.objects.create(
|
||||
employee_code=123, first_name="Fred", last_name="Jones"
|
||||
)
|
||||
|
||||
def test_zero_non_autoincrement_pk(self):
|
||||
Employee.objects.create(
|
||||
employee_code=0, first_name="Frank", last_name="Jones"
|
||||
)
|
||||
Employee.objects.create(employee_code=0, first_name="Frank", last_name="Jones")
|
||||
employee = Employee.objects.get(pk=0)
|
||||
self.assertEqual(employee.employee_code, 0)
|
||||
|
||||
@@ -224,7 +243,7 @@ class CustomPKTests(TestCase):
|
||||
# SQLite lets objects be saved with an empty primary key, even though an
|
||||
# integer is expected. So we can't check for an error being raised in that
|
||||
# case for SQLite. Remove it from the suite for this next bit.
|
||||
@skipIfDBFeature('supports_unspecified_pk')
|
||||
@skipIfDBFeature("supports_unspecified_pk")
|
||||
def test_required_pk(self):
|
||||
# The primary key must be specified, so an error is raised if you
|
||||
# try to create an object without it.
|
||||
@@ -236,7 +255,7 @@ class CustomPKTests(TestCase):
|
||||
obj = CustomAutoFieldModel.objects.create()
|
||||
self.assertIsInstance(obj.id, MyWrapper)
|
||||
|
||||
@skipUnlessDBFeature('can_return_rows_from_bulk_insert')
|
||||
@skipUnlessDBFeature("can_return_rows_from_bulk_insert")
|
||||
def test_auto_field_subclass_bulk_create(self):
|
||||
obj = CustomAutoFieldModel()
|
||||
CustomAutoFieldModel.objects.bulk_create([obj])
|
||||
|
||||
Reference in New Issue
Block a user