def test_can_provide_custom_override_to_cache_key_through_model_admin():
    class AdminWithCustomCacheKey(object):
        def admin_caching_key(self, obj):
            return 'Foo:Bar:9'

    ck = CacheKey(model_admin=AdminWithCustomCacheKey(), result=Group(pk=55))
    assert ck.result_key() == 'Foo:Bar:9'
    assert ck.key == \
        '{}.AdminWithCustomCacheKey-auth.Group-Foo:Bar:9'.format(
            AdminWithCustomCacheKey.__module__)
def test_when_model_admin_is_not_provided_it_is_derived_from_admin_registry():
    session_obj = Session()
    ck_explicit_admin = CacheKey(result=Group(), model_admin=session_obj)
    assert ck_explicit_admin.model_admin == session_obj

    ck_derived_admin = CacheKey(result=Group())
    assert ck_derived_admin.model_admin == site._registry[Group]

    orig_admin = site._registry[Group]
    try:
        admin_obj = object()
        site._registry[Group] = admin_obj
        ck_derived_admin = CacheKey(result=Group())
        assert ck_derived_admin.model_admin == site._registry[Group]
    finally:
        site._registry[Group] = orig_admin
def test_when_all_language_is_deactivated(settings):
    settings.USE_I18N = True
    settings.USE_L10N = True
    with translation_being('en'):
        translation.deactivate_all()
        ck = CacheKey(result=Group(pk=1))
        prefix = ck.i18n_l10n_prefix
        assert prefix == '.'
def test_key_is_i18n_l10n_aware_if_settings_enabled(settings, language, i18n,
                                                    l10n, expected_key_prefix):
    settings.USE_I18N = i18n
    settings.USE_L10N = l10n
    with translation_being(language):
        ck = CacheKey(result=Group(pk=1))
        assert ck.i18n_l10n_prefix == expected_key_prefix
        assert ck.key.startswith(expected_key_prefix)
        if expected_key_prefix:
            assert ck.key.startswith('{}.'.format(expected_key_prefix))
def test_cache_key_is_derived_from_admin_and_obj_by_default(
        admin_cls, result, expected_key):
    ck = CacheKey(model_admin=admin_cls(), result=result)
    assert ck.result_key() == '{}'.format(result.pk)
    assert ck.key == expected_key