2015-01-07 00:16:35 +00:00
|
|
|
from django.apps import apps
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
|
2015-01-07 00:16:35 +00:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2019-08-20 07:54:41 +00:00
|
|
|
from django.db.models import CharField, Field, ForeignObjectRel, ManyToManyField
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.db.models.options import EMPTY_RELATION_TREE, IMMUTABLE_WARNING
|
2015-04-17 21:38:20 +00:00
|
|
|
from django.test import SimpleTestCase
|
2015-01-07 00:16:35 +00:00
|
|
|
|
2015-02-03 20:13:43 +00:00
|
|
|
from .models import (
|
2015-01-28 12:35:27 +00:00
|
|
|
AbstractPerson,
|
|
|
|
BasePerson,
|
|
|
|
Child,
|
|
|
|
CommonAncestor,
|
|
|
|
FirstParent,
|
|
|
|
Person,
|
|
|
|
ProxyPerson,
|
|
|
|
Relating,
|
|
|
|
Relation,
|
|
|
|
SecondParent,
|
2015-02-03 20:13:43 +00:00
|
|
|
)
|
2015-01-07 00:16:35 +00:00
|
|
|
from .results import TEST_RESULTS
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class OptionsBaseTests(SimpleTestCase):
|
2015-01-07 00:16:35 +00:00
|
|
|
def _map_related_query_names(self, res):
|
|
|
|
return tuple((o.name, m) for o, m in res)
|
|
|
|
|
|
|
|
def _map_names(self, res):
|
|
|
|
return tuple((f.name, m) for f, m in res)
|
|
|
|
|
|
|
|
def _model(self, current_model, field):
|
|
|
|
model = field.model._meta.concrete_model
|
|
|
|
return None if model == current_model else model
|
|
|
|
|
|
|
|
def _details(self, current_model, relation):
|
2019-03-02 11:19:21 +00:00
|
|
|
direct = isinstance(relation, (Field, GenericForeignKey))
|
2015-01-07 00:16:35 +00:00
|
|
|
model = relation.model._meta.concrete_model
|
|
|
|
if model == current_model:
|
|
|
|
model = None
|
|
|
|
|
|
|
|
field = relation if direct else relation.field
|
2016-03-10 17:21:25 +00:00
|
|
|
return (
|
|
|
|
relation,
|
|
|
|
model,
|
|
|
|
direct,
|
|
|
|
bool(field.many_to_many),
|
|
|
|
) # many_to_many can be None
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetFieldsTests(OptionsBaseTests):
|
|
|
|
def test_get_fields_is_immutable(self):
|
|
|
|
msg = IMMUTABLE_WARNING % "get_fields()"
|
|
|
|
for _ in range(2):
|
|
|
|
# Running unit test twice to ensure both non-cached and cached result
|
|
|
|
# are immutable.
|
|
|
|
fields = Person._meta.get_fields()
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
|
|
|
fields += ["errors"]
|
|
|
|
|
|
|
|
|
2015-04-26 21:05:50 +00:00
|
|
|
class LabelTests(OptionsBaseTests):
|
|
|
|
def test_label(self):
|
|
|
|
for model, expected_result in TEST_RESULTS["labels"].items():
|
|
|
|
self.assertEqual(model._meta.label, expected_result)
|
|
|
|
|
|
|
|
def test_label_lower(self):
|
|
|
|
for model, expected_result in TEST_RESULTS["lower_labels"].items():
|
|
|
|
self.assertEqual(model._meta.label_lower, expected_result)
|
|
|
|
|
|
|
|
|
2015-01-07 00:16:35 +00:00
|
|
|
class DataTests(OptionsBaseTests):
|
|
|
|
def test_fields(self):
|
|
|
|
for model, expected_result in TEST_RESULTS["fields"].items():
|
|
|
|
fields = model._meta.fields
|
|
|
|
self.assertEqual([f.attname for f in fields], expected_result)
|
|
|
|
|
|
|
|
def test_local_fields(self):
|
2016-01-23 16:47:07 +00:00
|
|
|
def is_data_field(f):
|
2016-03-10 17:21:25 +00:00
|
|
|
return isinstance(f, Field) and not f.many_to_many
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
for model, expected_result in TEST_RESULTS["local_fields"].items():
|
|
|
|
fields = model._meta.local_fields
|
|
|
|
self.assertEqual([f.attname for f in fields], expected_result)
|
|
|
|
for f in fields:
|
|
|
|
self.assertEqual(f.model, model)
|
|
|
|
self.assertTrue(is_data_field(f))
|
|
|
|
|
|
|
|
def test_local_concrete_fields(self):
|
|
|
|
for model, expected_result in TEST_RESULTS["local_concrete_fields"].items():
|
|
|
|
fields = model._meta.local_concrete_fields
|
|
|
|
self.assertEqual([f.attname for f in fields], expected_result)
|
|
|
|
for f in fields:
|
|
|
|
self.assertIsNotNone(f.column)
|
|
|
|
|
|
|
|
|
|
|
|
class M2MTests(OptionsBaseTests):
|
|
|
|
def test_many_to_many(self):
|
|
|
|
for model, expected_result in TEST_RESULTS["many_to_many"].items():
|
|
|
|
fields = model._meta.many_to_many
|
|
|
|
self.assertEqual([f.attname for f in fields], expected_result)
|
|
|
|
for f in fields:
|
|
|
|
self.assertTrue(f.many_to_many and f.is_relation)
|
|
|
|
|
|
|
|
def test_many_to_many_with_model(self):
|
|
|
|
for model, expected_result in TEST_RESULTS["many_to_many_with_model"].items():
|
|
|
|
models = [self._model(model, field) for field in model._meta.many_to_many]
|
|
|
|
self.assertEqual(models, expected_result)
|
|
|
|
|
|
|
|
|
|
|
|
class RelatedObjectsTests(OptionsBaseTests):
|
2016-01-23 16:47:07 +00:00
|
|
|
def key_name(self, r):
|
|
|
|
return r[0]
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
def test_related_objects(self):
|
|
|
|
result_key = "get_all_related_objects_with_model"
|
|
|
|
for model, expected in TEST_RESULTS[result_key].items():
|
|
|
|
objects = [
|
|
|
|
(field, self._model(model, field))
|
|
|
|
for field in model._meta.get_fields()
|
|
|
|
if field.auto_created and not field.concrete
|
|
|
|
]
|
2015-10-07 19:29:20 +00:00
|
|
|
self.assertEqual(
|
|
|
|
sorted(self._map_related_query_names(objects), key=self.key_name),
|
|
|
|
sorted(expected, key=self.key_name),
|
|
|
|
)
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
def test_related_objects_local(self):
|
|
|
|
result_key = "get_all_related_objects_with_model_local"
|
|
|
|
for model, expected in TEST_RESULTS[result_key].items():
|
|
|
|
objects = [
|
|
|
|
(field, self._model(model, field))
|
|
|
|
for field in model._meta.get_fields(include_parents=False)
|
|
|
|
if field.auto_created and not field.concrete
|
|
|
|
]
|
2015-10-07 19:29:20 +00:00
|
|
|
self.assertEqual(
|
|
|
|
sorted(self._map_related_query_names(objects), key=self.key_name),
|
|
|
|
sorted(expected, key=self.key_name),
|
|
|
|
)
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
def test_related_objects_include_hidden(self):
|
|
|
|
result_key = "get_all_related_objects_with_model_hidden"
|
|
|
|
for model, expected in TEST_RESULTS[result_key].items():
|
|
|
|
objects = [
|
|
|
|
(field, self._model(model, field))
|
|
|
|
for field in model._meta.get_fields(include_hidden=True)
|
|
|
|
if field.auto_created and not field.concrete
|
|
|
|
]
|
|
|
|
self.assertEqual(
|
|
|
|
sorted(self._map_names(objects), key=self.key_name),
|
|
|
|
sorted(expected, key=self.key_name),
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_related_objects_include_hidden_local_only(self):
|
|
|
|
result_key = "get_all_related_objects_with_model_hidden_local"
|
|
|
|
for model, expected in TEST_RESULTS[result_key].items():
|
|
|
|
objects = [
|
|
|
|
(field, self._model(model, field))
|
|
|
|
for field in model._meta.get_fields(
|
|
|
|
include_hidden=True, include_parents=False
|
|
|
|
)
|
|
|
|
if field.auto_created and not field.concrete
|
|
|
|
]
|
|
|
|
self.assertEqual(
|
|
|
|
sorted(self._map_names(objects), key=self.key_name),
|
|
|
|
sorted(expected, key=self.key_name),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-03-20 17:10:55 +00:00
|
|
|
class PrivateFieldsTests(OptionsBaseTests):
|
|
|
|
def test_private_fields(self):
|
|
|
|
for model, expected_names in TEST_RESULTS["private_fields"].items():
|
|
|
|
objects = model._meta.private_fields
|
2017-06-01 23:08:59 +00:00
|
|
|
self.assertEqual(sorted(f.name for f in objects), sorted(expected_names))
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GetFieldByNameTests(OptionsBaseTests):
|
|
|
|
def test_get_data_field(self):
|
|
|
|
field_info = self._details(Person, Person._meta.get_field("data_abstract"))
|
|
|
|
self.assertEqual(field_info[1:], (BasePerson, True, False))
|
|
|
|
self.assertIsInstance(field_info[0], CharField)
|
|
|
|
|
|
|
|
def test_get_m2m_field(self):
|
|
|
|
field_info = self._details(Person, Person._meta.get_field("m2m_base"))
|
|
|
|
self.assertEqual(field_info[1:], (BasePerson, True, True))
|
2019-08-20 07:54:41 +00:00
|
|
|
self.assertIsInstance(field_info[0], ManyToManyField)
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
def test_get_related_object(self):
|
|
|
|
field_info = self._details(
|
|
|
|
Person, Person._meta.get_field("relating_baseperson")
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2015-01-07 00:16:35 +00:00
|
|
|
self.assertEqual(field_info[1:], (BasePerson, False, False))
|
2019-08-20 07:54:41 +00:00
|
|
|
self.assertIsInstance(field_info[0], ForeignObjectRel)
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
def test_get_related_m2m(self):
|
|
|
|
field_info = self._details(Person, Person._meta.get_field("relating_people"))
|
|
|
|
self.assertEqual(field_info[1:], (None, False, True))
|
2019-08-20 07:54:41 +00:00
|
|
|
self.assertIsInstance(field_info[0], ForeignObjectRel)
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
def test_get_generic_relation(self):
|
|
|
|
field_info = self._details(
|
|
|
|
Person, Person._meta.get_field("generic_relation_base")
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2015-01-07 00:16:35 +00:00
|
|
|
self.assertEqual(field_info[1:], (None, True, False))
|
|
|
|
self.assertIsInstance(field_info[0], GenericRelation)
|
|
|
|
|
2015-02-03 20:12:28 +00:00
|
|
|
def test_get_fields_only_searches_forward_on_apps_not_ready(self):
|
2015-01-07 00:16:35 +00:00
|
|
|
opts = Person._meta
|
|
|
|
# If apps registry is not ready, get_field() searches over only
|
|
|
|
# forward fields.
|
2015-02-10 17:15:54 +00:00
|
|
|
opts.apps.models_ready = False
|
2015-01-07 00:16:35 +00:00
|
|
|
try:
|
|
|
|
# 'data_abstract' is a forward field, and therefore will be found
|
|
|
|
self.assertTrue(opts.get_field("data_abstract"))
|
|
|
|
msg = (
|
2015-02-03 20:12:28 +00:00
|
|
|
"Person has no field named 'relating_baseperson'. The app "
|
2015-01-14 18:40:31 +00:00
|
|
|
"cache isn't ready yet, so if this is an auto-created related "
|
2015-02-03 20:12:28 +00:00
|
|
|
"field, it won't be available yet."
|
2015-01-07 00:16:35 +00:00
|
|
|
)
|
2015-02-03 20:12:28 +00:00
|
|
|
# 'data_abstract' is a reverse field, and will raise an exception
|
2015-01-07 00:16:35 +00:00
|
|
|
with self.assertRaisesMessage(FieldDoesNotExist, msg):
|
2015-02-03 20:12:28 +00:00
|
|
|
opts.get_field("relating_baseperson")
|
2015-01-07 00:16:35 +00:00
|
|
|
finally:
|
2015-02-10 17:15:54 +00:00
|
|
|
opts.apps.models_ready = True
|
2015-01-07 00:16:35 +00:00
|
|
|
|
|
|
|
|
2024-02-18 21:49:25 +00:00
|
|
|
class VerboseNameRawTests(SimpleTestCase):
|
|
|
|
def test_string(self):
|
|
|
|
# Clear cached property.
|
|
|
|
Relation._meta.__dict__.pop("verbose_name_raw", None)
|
|
|
|
self.assertEqual(Relation._meta.verbose_name_raw, "relation")
|
|
|
|
|
|
|
|
def test_gettext(self):
|
|
|
|
Person._meta.__dict__.pop("verbose_name_raw", None)
|
|
|
|
self.assertEqual(Person._meta.verbose_name_raw, "Person")
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class RelationTreeTests(SimpleTestCase):
|
2015-01-07 00:16:35 +00:00
|
|
|
all_models = (Relation, AbstractPerson, BasePerson, Person, ProxyPerson, Relating)
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
apps.clear_cache()
|
|
|
|
|
|
|
|
def test_clear_cache_clears_relation_tree(self):
|
|
|
|
# The apps.clear_cache is setUp() should have deleted all trees.
|
|
|
|
# Exclude abstract models that are not included in the Apps registry
|
|
|
|
# and have no cache.
|
|
|
|
all_models_with_cache = (m for m in self.all_models if not m._meta.abstract)
|
|
|
|
for m in all_models_with_cache:
|
|
|
|
self.assertNotIn("_relation_tree", m._meta.__dict__)
|
|
|
|
|
|
|
|
def test_first_relation_tree_access_populates_all(self):
|
|
|
|
# On first access, relation tree should have populated cache.
|
|
|
|
self.assertTrue(self.all_models[0]._meta._relation_tree)
|
|
|
|
|
|
|
|
# AbstractPerson does not have any relations, so relation_tree
|
|
|
|
# should just return an EMPTY_RELATION_TREE.
|
|
|
|
self.assertEqual(AbstractPerson._meta._relation_tree, EMPTY_RELATION_TREE)
|
|
|
|
|
|
|
|
# All the other models should already have their relation tree
|
|
|
|
# in the internal __dict__ .
|
|
|
|
all_models_but_abstractperson = (
|
|
|
|
m for m in self.all_models if m is not AbstractPerson
|
|
|
|
)
|
|
|
|
for m in all_models_but_abstractperson:
|
|
|
|
self.assertIn("_relation_tree", m._meta.__dict__)
|
|
|
|
|
|
|
|
def test_relations_related_objects(self):
|
|
|
|
# Testing non hidden related objects
|
|
|
|
self.assertEqual(
|
2017-06-01 23:08:59 +00:00
|
|
|
sorted(
|
|
|
|
field.related_query_name()
|
|
|
|
for field in Relation._meta._relation_tree
|
2024-02-16 22:08:46 +00:00
|
|
|
if not field.remote_field.field.remote_field.hidden
|
2017-06-01 23:08:59 +00:00
|
|
|
),
|
2015-01-07 00:16:35 +00:00
|
|
|
sorted(
|
|
|
|
[
|
2015-02-16 07:49:19 +00:00
|
|
|
"fk_abstract_rel",
|
|
|
|
"fk_base_rel",
|
|
|
|
"fk_concrete_rel",
|
|
|
|
"fo_abstract_rel",
|
|
|
|
"fo_base_rel",
|
|
|
|
"fo_concrete_rel",
|
|
|
|
"m2m_abstract_rel",
|
|
|
|
"m2m_base_rel",
|
|
|
|
"m2m_concrete_rel",
|
2015-01-07 00:16:35 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
2015-01-07 00:16:35 +00:00
|
|
|
)
|
|
|
|
# Testing hidden related objects
|
|
|
|
self.assertEqual(
|
2017-06-01 23:08:59 +00:00
|
|
|
sorted(
|
|
|
|
field.related_query_name() for field in BasePerson._meta._relation_tree
|
|
|
|
),
|
2015-01-07 00:16:35 +00:00
|
|
|
sorted(
|
|
|
|
[
|
2020-12-30 02:22:17 +00:00
|
|
|
"+",
|
|
|
|
"_model_meta_relating_basepeople_hidden_+",
|
2015-09-11 23:33:12 +00:00
|
|
|
"BasePerson_following_abstract+",
|
2020-12-30 02:22:17 +00:00
|
|
|
"BasePerson_following_abstract+",
|
2015-09-11 23:33:12 +00:00
|
|
|
"BasePerson_following_base+",
|
|
|
|
"BasePerson_following_base+",
|
|
|
|
"BasePerson_friends_abstract+",
|
|
|
|
"BasePerson_friends_abstract+",
|
|
|
|
"BasePerson_friends_base+",
|
|
|
|
"BasePerson_friends_base+",
|
|
|
|
"BasePerson_m2m_abstract+",
|
|
|
|
"BasePerson_m2m_base+",
|
|
|
|
"Relating_basepeople+",
|
|
|
|
"Relating_basepeople_hidden+",
|
|
|
|
"followers_abstract",
|
|
|
|
"followers_base",
|
|
|
|
"friends_abstract_rel_+",
|
|
|
|
"friends_base_rel_+",
|
|
|
|
"person",
|
|
|
|
"relating_basepeople",
|
|
|
|
"relating_baseperson",
|
2015-01-07 00:16:35 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
),
|
|
|
|
)
|
2015-01-07 00:16:35 +00:00
|
|
|
self.assertEqual(
|
2022-02-03 19:24:19 +00:00
|
|
|
[
|
2015-01-07 00:16:35 +00:00
|
|
|
field.related_query_name()
|
|
|
|
for field in AbstractPerson._meta._relation_tree
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
|
|
|
[],
|
2015-01-07 00:16:35 +00:00
|
|
|
)
|
2015-02-03 20:13:43 +00:00
|
|
|
|
|
|
|
|
2024-02-23 22:50:09 +00:00
|
|
|
class AllParentsTests(SimpleTestCase):
|
|
|
|
def test_all_parents(self):
|
|
|
|
self.assertEqual(CommonAncestor._meta.all_parents, ())
|
|
|
|
self.assertEqual(FirstParent._meta.all_parents, (CommonAncestor,))
|
|
|
|
self.assertEqual(SecondParent._meta.all_parents, (CommonAncestor,))
|
2015-02-03 20:13:43 +00:00
|
|
|
self.assertEqual(
|
2024-02-23 22:50:09 +00:00
|
|
|
Child._meta.all_parents,
|
|
|
|
(FirstParent, SecondParent, CommonAncestor),
|
2015-02-03 20:13:43 +00:00
|
|
|
)
|
2017-06-05 12:27:55 +00:00
|
|
|
|
2024-02-23 22:50:09 +00:00
|
|
|
def test_get_parent_list(self):
|
|
|
|
self.assertEqual(Child._meta.get_parent_list(), list(Child._meta.all_parents))
|
|
|
|
|
2017-06-05 12:27:55 +00:00
|
|
|
|
|
|
|
class PropertyNamesTests(SimpleTestCase):
|
|
|
|
def test_person(self):
|
2017-06-04 21:58:24 +00:00
|
|
|
# Instance only descriptors don't appear in _property_names.
|
2020-08-02 20:20:30 +00:00
|
|
|
self.assertEqual(BasePerson().test_instance_only_descriptor, 1)
|
|
|
|
with self.assertRaisesMessage(AttributeError, "Instance only"):
|
|
|
|
AbstractPerson.test_instance_only_descriptor
|
2017-06-05 12:27:55 +00:00
|
|
|
self.assertEqual(
|
|
|
|
AbstractPerson._meta._property_names, frozenset(["pk", "test_property"])
|
2022-02-03 19:24:19 +00:00
|
|
|
)
|
2019-07-24 06:42:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ReturningFieldsTests(SimpleTestCase):
|
|
|
|
def test_pk(self):
|
|
|
|
self.assertEqual(Relation._meta.db_returning_fields, [Relation._meta.pk])
|
2020-08-02 20:20:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AbstractModelTests(SimpleTestCase):
|
|
|
|
def test_abstract_model_not_instantiated(self):
|
|
|
|
msg = "Abstract models cannot be instantiated."
|
|
|
|
with self.assertRaisesMessage(TypeError, msg):
|
|
|
|
AbstractPerson()
|