Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions django/db/models/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1416,11 +1416,14 @@ def values(self, *fields, **expressions):
def values_list(self, *fields, flat=False, named=False):
if flat and named:
raise TypeError("'flat' and 'named' can't be used together.")
if flat and len(fields) > 1:
raise TypeError(
"'flat' is not valid when values_list is called with more than one "
"field."
)
if flat:
if len(fields) > 1:
raise TypeError(
"'flat' is not valid when values_list is called with more than one "
"field."
)
elif not fields:
fields = [self.model._meta.concrete_fields[0].attname]

field_names = {f: False for f in fields if not hasattr(f, "resolve_expression")}
_fields = []
Expand Down
5 changes: 4 additions & 1 deletion django/test/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def _init_worker(
used_aliases=None,
):
"""
Switch to databases dedicated to this worker.
Switch to databases dedicated to this worker and run system checks.

This helper lives at module-level because of the multiprocessing module's
requirements.
Expand All @@ -463,6 +463,9 @@ def _init_worker(
process_setup(*process_setup_args)
django.setup()
setup_test_environment(debug=debug_mode)
call_command(
"check", stdout=io.StringIO(), stderr=io.StringIO(), databases=used_aliases
)

db_aliases = used_aliases if used_aliases is not None else connections
for alias in db_aliases:
Expand Down
50 changes: 50 additions & 0 deletions tests/lookup/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,25 @@ def test_in_bulk_not_model_iterable(self):
def test_values(self):
# values() returns a list of dictionaries instead of object instances,
# and you can specify which fields you want to retrieve.
self.assertSequenceEqual(
Article.objects.filter(id__in=(self.a5.id, self.a6.id)).values(),
[
{
"id": self.a5.id,
"headline": "Article 5",
"pub_date": datetime(2005, 8, 1, 9, 0),
"author_id": self.au2.id,
"slug": "a5",
},
{
"id": self.a6.id,
"headline": "Article 6",
"pub_date": datetime(2005, 8, 1, 8, 0),
"author_id": self.au2.id,
"slug": "a6",
},
],
)
self.assertSequenceEqual(
Article.objects.values("headline"),
[
Expand Down Expand Up @@ -522,6 +541,37 @@ def test_values_list(self):
# returned as a list of tuples, rather than a list of dictionaries.
# Within each tuple, the order of the elements is the same as the order
# of fields in the values_list() call.
self.assertSequenceEqual(
Article.objects.filter(id__in=(self.a5.id, self.a6.id)).values_list(),
[
(
self.a5.id,
"Article 5",
datetime(2005, 8, 1, 9, 0),
self.au2.id,
"a5",
),
(
self.a6.id,
"Article 6",
datetime(2005, 8, 1, 8, 0),
self.au2.id,
"a6",
),
],
)
self.assertSequenceEqual(
Article.objects.values_list(flat=True),
[
self.a5.id,
self.a6.id,
self.a4.id,
self.a2.id,
self.a3.id,
self.a7.id,
self.a1.id,
],
)
self.assertSequenceEqual(
Article.objects.values_list("headline"),
[
Expand Down
7 changes: 7 additions & 0 deletions tests/test_runner/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ class Person(models.Model):
last_name = models.CharField(max_length=20)
friends = models.ManyToManyField("self")

system_check_run_count = 0

@classmethod
def check(cls, *args, **kwargs):
cls.system_check_run_count += 1
return super().check(**kwargs)


# A set of models that use a non-abstract inherited 'through' model.
class ThroughBase(models.Model):
Expand Down
5 changes: 5 additions & 0 deletions tests/test_runner/test_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from django.test import SimpleTestCase
from django.test.runner import ParallelTestSuite, RemoteTestResult

from . import models

try:
import tblib.pickling_support
except ImportError:
Expand Down Expand Up @@ -48,6 +50,9 @@ def test_subtest(self):
with self.subTest(index=i):
self.assertEqual(i, i)

def test_system_checks(self):
self.assertEqual(models.Person.system_check_run_count, 1)


class SampleFailingSubtest(SimpleTestCase):
# This method name doesn't begin with "test" to prevent test discovery
Expand Down
Loading