Skip to content

Commit

Permalink
Improved custom object identifier test
Browse files Browse the repository at this point in the history
This provides an example for implementors and ensures that failing to
use the custom class would cause a test failure.
  • Loading branch information
acdha committed Jan 2, 2017
1 parent f5cf421 commit 176bc54
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 7 additions & 1 deletion test_haystack/core/custom_identifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

from __future__ import absolute_import, division, print_function, unicode_literals

import hashlib

def get_identifier_method(key):
"""
Custom get_identifier method used for testing the
setting HAYSTACK_IDENTIFIER_MODULE
"""
return key

if hasattr(key, 'get_custom_haystack_id'):
return key.get_custom_haystack_id()
else:
key_bytes = key.encode('utf-8')
return hashlib.md5(key_bytes).hexdigest()
14 changes: 13 additions & 1 deletion test_haystack/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,20 @@ def test_get_identifier(self):

@override_settings(HAYSTACK_IDENTIFIER_METHOD='test_haystack.core.custom_identifier.get_identifier_method')
def test_haystack_identifier_method(self):
# The custom implementation returns the MD-5 hash of the key value by
# default:
get_identifier = _lookup_identifier_method()
self.assertEqual(get_identifier('a.b.c'), 'a.b.c')
self.assertEqual(get_identifier('a.b.c'),
'553f764f7b436175c0387e22b4a19213')

# … but it also supports a custom override mechanism which would
# definitely fail with the default implementation:
class custom_id_class(object):
def get_custom_haystack_id(self):
return 'CUSTOM'

self.assertEqual(get_identifier(custom_id_class()),
'CUSTOM')

@override_settings(HAYSTACK_IDENTIFIER_METHOD='test_haystack.core.custom_identifier.not_there')
def test_haystack_identifier_method_bad_path(self):
Expand Down

0 comments on commit 176bc54

Please sign in to comment.