From 626fa28878b1167d20d2419d68d561297390510a Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Mon, 8 Jul 2013 09:35:08 +1000 Subject: [PATCH] Fixed #13813 -- Comparison of DatabaseWrappers doesn't raise errors. Patch from Lukasz Balcerzak. --- django/db/backends/__init__.py | 4 +++- tests/db_backends/__init__.py | 0 tests/db_backends/tests.py | 36 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/db_backends/__init__.py create mode 100644 tests/db_backends/tests.py diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index a1fd923faf..28a4a48a9f 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -70,7 +70,9 @@ class BaseDatabaseWrapper(object): self._thread_ident = thread.get_ident() def __eq__(self, other): - return self.alias == other.alias + if isinstance(other, BaseDatabaseWrapper): + return self.alias == other.alias + return NotImplemented def __ne__(self, other): return not self == other diff --git a/tests/db_backends/__init__.py b/tests/db_backends/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/db_backends/tests.py b/tests/db_backends/tests.py new file mode 100644 index 0000000000..052ee87254 --- /dev/null +++ b/tests/db_backends/tests.py @@ -0,0 +1,36 @@ +from django.test import TestCase +from django.db.backends import BaseDatabaseWrapper + + +class DummyDatabaseWrapper(BaseDatabaseWrapper): + pass + + +class DummyObject(object): + alias = None + + +class DbBackendTests(TestCase): + def test_compare_db_wrapper_with_another_object(self): + wrapper = BaseDatabaseWrapper({}) + self.assertFalse(wrapper == 'not-a-db-wrapper') + + def test_compare_db_wrapper_with_another_object_with_alias(self): + wrapper = BaseDatabaseWrapper({}) + obj = DummyObject() + obj.alias = wrapper.alias = 'foobar' + self.assertFalse(wrapper == obj) + + def test_negate_compare_db_wrapper_with_another_object(self): + wrapper = BaseDatabaseWrapper({}) + self.assertTrue(wrapper != 'not-a-db-wrapper') + + def test_compare_db_wrappers(self): + wrapper1 = DummyDatabaseWrapper({}) + wrapper2 = BaseDatabaseWrapper({}) + + wrapper1.alias = wrapper2.alias = 'foo' + self.assertTrue(wrapper1 == wrapper2) + + wrapper1.alias = 'bar' + self.assertFalse(wrapper1 == wrapper2)