From 607ff4efa31cd0c2217ed021dc939ffddad89c97 Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Mon, 15 Apr 2019 12:18:08 +0200 Subject: [PATCH] Refs #30254 -- Added tests for Model.__hash__() inheritance. --- tests/basic/tests.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/basic/tests.py b/tests/basic/tests.py index 909f3049b9..328ddc31f1 100644 --- a/tests/basic/tests.py +++ b/tests/basic/tests.py @@ -2,7 +2,7 @@ import threading from datetime import datetime, timedelta from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist -from django.db import DEFAULT_DB_ALIAS, DatabaseError, connections +from django.db import DEFAULT_DB_ALIAS, DatabaseError, connections, models from django.db.models.manager import BaseManager from django.db.models.query import EmptyQuerySet, QuerySet from django.test import ( @@ -357,6 +357,23 @@ class ModelTest(TestCase): # hash) hash(Article()) + def test_missing_hash_not_inherited(self): + class NoHash(models.Model): + def __eq__(self, other): + return super.__eq__(other) + + with self.assertRaisesMessage(TypeError, "unhashable type: 'NoHash'"): + hash(NoHash(id=1)) + + def test_specified_parent_hash_inherited(self): + class ParentHash(models.Model): + def __eq__(self, other): + return super.__eq__(other) + + __hash__ = models.Model.__hash__ + + self.assertEqual(hash(ParentHash(id=1)), 1) + def test_delete_and_access_field(self): # Accessing a field after it's deleted from a model reloads its value. pub_date = datetime.now()