<s> from __future__ import unicode_literals <EOL> from django . test import LiveServerTestCase , TestCase <EOL> from django . test . utils import override_settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . management import call_command <EOL> from django . test . utils import override_settings <EOL> from django . contrib . auth . models import User <EOL> from django . core . cache import caches <EOL> from django . db import IntegrityError <EOL> from django . template import defaultfilters <EOL> from decimal import Decimal <EOL> from dynamic_preferences . serializers import * <EOL> from dynamic_preferences . registries import user_preferences_registry , global_preferences_registry <EOL> from dynamic_preferences . models import UserPreferenceModel , GlobalPreferenceModel <EOL> from dynamic_preferences . managers import PreferencesManager <EOL> from dynamic_preferences import exceptions <EOL> from dynamic_preferences . forms import global_preference_form_builder , user_preference_form_builder <EOL> from dynamic_preferences . preferences import EMPTY_SECTION , Section <EOL> from . types import * <EOL> from . test_app . dynamic_preferences_registry import * <EOL> class BaseTest ( object ) : <EOL> def tearDown ( self ) : <EOL> caches [ '<STR_LIT:default>' ] . clear ( ) <EOL> class TestTutorial ( BaseTest , TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . henri = User ( <EOL> username = "<STR_LIT>" , password = "<STR_LIT:test>" , email = "<STR_LIT>" ) <EOL> self . henri . save ( ) <EOL> def test_quickstart ( self ) : <EOL> global_preferences = global_preferences_registry . manager ( ) <EOL> self . assertEqual ( global_preferences [ '<STR_LIT>' ] , False ) <EOL> global_preferences [ '<STR_LIT>' ] = True <EOL> self . assertEqual ( global_preferences [ '<STR_LIT>' ] , True ) <EOL> self . assertEqual ( GlobalPreferenceModel . objects . get ( <EOL> section = "<STR_LIT:user>" , name = "<STR_LIT>" ) . value , True ) <EOL> self . assertEqual ( self . henri . preferences [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . henri . preferences [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . assertEqual ( self . henri . preferences [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( UserPreferenceModel . objects . get ( <EOL> section = "<STR_LIT>" , name = "<STR_LIT>" , instance = self . henri ) . value , '<STR_LIT>' ) <EOL> class TestModels ( BaseTest , TestCase ) : <EOL> def test_preference_order_match_register_call ( self ) : <EOL> expected = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> self . assertEqual ( [ p . name for p in global_preferences_registry . preferences ( ) ] [ : <NUM_LIT:4> ] , expected ) <EOL> def test_adding_user_create_default_preferences ( self ) : <EOL> u = User ( username = "<STR_LIT>" ) <EOL> u . save ( ) <EOL> self . assertEqual ( <EOL> len ( u . preferences ) , len ( user_preferences_registry . preferences ( ) ) ) <EOL> def test_global_preferences_manager_get ( self ) : <EOL> global_preferences = global_preferences_registry . manager ( ) <EOL> self . assertEqual ( global_preferences [ '<STR_LIT>' ] , False ) <EOL> def test_global_preferences_manager_set ( self ) : <EOL> global_preferences = global_preferences_registry . manager ( ) <EOL> global_preferences [ '<STR_LIT>' ] = True <EOL> self . assertEqual ( global_preferences [ '<STR_LIT>' ] , True ) <EOL> def test_can_cache_single_preference ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> v = manager [ '<STR_LIT>' ] <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> v = manager [ '<STR_LIT>' ] <EOL> v = manager [ '<STR_LIT>' ] <EOL> v = manager [ '<STR_LIT>' ] <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_can_bypass_cache_in_get ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> manager [ '<STR_LIT>' ] <EOL> with self . assertNumQueries ( <NUM_LIT:3> ) : <EOL> manager [ '<STR_LIT>' ] <EOL> manager [ '<STR_LIT>' ] <EOL> manager [ '<STR_LIT>' ] <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } , DEBUG = True ) <EOL> def test_can_bypass_cache_in_get_all ( self ) : <EOL> from django . db import connection <EOL> manager = global_preferences_registry . manager ( ) <EOL> queries_before = len ( connection . queries ) <EOL> manager . all ( ) <EOL> manager_queries = len ( connection . queries ) - queries_before <EOL> manager . all ( ) <EOL> self . assertGreater ( len ( connection . queries ) , manager_queries ) <EOL> def test_can_cache_all_preferences ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> manager . all ( ) <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> manager . all ( ) <EOL> manager . all ( ) <EOL> manager . all ( ) <EOL> def test_global_preferences_manager_by_name ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> self . assertEqual ( manager . by_name ( ) [ '<STR_LIT>' ] , manager [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( manager . all ( ) ) , len ( manager . by_name ( ) ) ) <EOL> def test_global_preferences_manager_by_name ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> self . assertEqual ( manager . by_name ( ) [ '<STR_LIT>' ] , manager [ '<STR_LIT>' ] ) <EOL> self . assertEqual ( len ( manager . all ( ) ) , len ( manager . by_name ( ) ) ) <EOL> def test_cache_invalidate_on_save ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> model_instance = manager . create_db_pref ( section = None , name = '<STR_LIT>' , value = False ) <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> assert not manager [ '<STR_LIT>' ] <EOL> manager [ '<STR_LIT>' ] <EOL> model_instance . value = True <EOL> model_instance . save ( ) <EOL> with self . assertNumQueries ( <NUM_LIT:0> ) : <EOL> assert manager [ '<STR_LIT>' ] <EOL> manager [ '<STR_LIT>' ] <EOL> class TestDynamicPreferences ( BaseTest , TestCase ) : <EOL> def setUp ( self ) : <EOL> self . test_user = User ( <EOL> username = "<STR_LIT:test>" , password = "<STR_LIT:test>" , email = "<STR_LIT>" ) <EOL> self . test_user . save ( ) <EOL> def test_cannot_instanciate_preference_or_section_with_invalid_name ( self ) : <EOL> invalid_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for n in invalid_names : <EOL> with self . assertRaises ( ValueError ) : <EOL> Section ( n ) <EOL> with self . assertRaises ( ValueError ) : <EOL> class P ( IntegerPreference ) : <EOL> name = n <EOL> P ( ) <EOL> def test_manager_is_attached_to_each_referenced_instance ( self ) : <EOL> self . assertTrue ( isinstance ( self . test_user . preferences , PreferencesManager ) ) <EOL> def test_preference_is_saved_to_database ( self ) : <EOL> self . test_user . preferences [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> test_user_pref1 = UserPreferenceModel . objects . get ( <EOL> section = "<STR_LIT:test>" , name = "<STR_LIT>" , instance = self . test_user ) <EOL> self . assertEqual ( self . test_user . preferences [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> def test_per_instance_preference_stay_unique_in_db ( self ) : <EOL> self . test_user . preferences [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> duplicate = UserPreferenceModel ( <EOL> section = "<STR_LIT:test>" , name = "<STR_LIT>" , instance = self . test_user ) <EOL> with self . assertRaises ( IntegrityError ) : <EOL> duplicate . save ( ) <EOL> def test_preference_value_set_to_default ( self ) : <EOL> pref = user_preferences_registry . get ( "<STR_LIT>" , "<STR_LIT:test>" ) <EOL> value = self . test_user . preferences [ '<STR_LIT>' ] <EOL> self . assertEqual ( pref . default , value ) <EOL> instance = UserPreferenceModel . objects . get ( <EOL> section = "<STR_LIT:test>" , name = "<STR_LIT>" , instance = self . test_user ) <EOL> def test_preference_model_manager_to_dict ( self ) : <EOL> manager = global_preferences_registry . manager ( ) <EOL> call_command ( '<STR_LIT>' , verbosity = <NUM_LIT:1> , interactive = False ) <EOL> expected = { <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT>' : <NUM_LIT:100> , <EOL> u'<STR_LIT>' : <NUM_LIT> , <EOL> u'<STR_LIT>' : None , <EOL> u'<STR_LIT>' : False } <EOL> self . assertDictEqual ( manager . all ( ) , expected ) <EOL> def test_user_preference_model_manager_to_dict ( self ) : <EOL> user = self . test_user <EOL> expected = { <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : True , <EOL> u'<STR_LIT>' : '<STR_LIT:C>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : False , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> u'<STR_LIT>' : u'<STR_LIT>' , <EOL> } <EOL> self . assertEqual ( <EOL> user . preferences . all ( ) , expected ) <EOL> class TestPreferenceObjects ( BaseTest , TestCase ) : <EOL> def test_can_get_to_string_notation ( self ) : <EOL> pref = global_preferences_registry . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( pref . identifier ( ) , '<STR_LIT>' ) <EOL> def test_preference_requires_default_value ( self ) : <EOL> with self . assertRaises ( exceptions . MissingDefault ) : <EOL> preference = NoDefault ( ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_default_accepts_callable ( self ) : <EOL> class P ( IntPreference ) : <EOL> def get_default ( self ) : <EOL> return <NUM_LIT:4> <EOL> self . assertEqual ( P ( ) . get ( '<STR_LIT:default>' ) , <NUM_LIT:4> ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_getter ( self ) : <EOL> class PNoGetter ( IntPreference ) : <EOL> default = <NUM_LIT:1> <EOL> help_text = '<STR_LIT>' <EOL> class PGetter ( IntPreference ) : <EOL> def get_default ( self ) : <EOL> return <NUM_LIT:1> <EOL> def get_help_text ( self ) : <EOL> return '<STR_LIT>' <EOL> p_no_getter = PNoGetter ( ) <EOL> p_getter = PGetter ( ) <EOL> for attribute , expected in [ ( '<STR_LIT:default>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , '<STR_LIT>' ) ] : <EOL> self . assertEqual ( p_no_getter . get ( attribute ) , expected ) <EOL> self . assertEqual ( p_getter . get ( attribute ) , expected ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_field ( self ) : <EOL> class P ( IntPreference ) : <EOL> default = <NUM_LIT:1> <EOL> verbose_name = '<STR_LIT:P>' <EOL> p = P ( ) <EOL> self . assertEqual ( p . field . initial , <NUM_LIT:1> ) <EOL> self . assertEqual ( p . field . label , '<STR_LIT:P>' ) <EOL> self . assertEqual ( p . field . __class__ , forms . IntegerField ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_boolean_field_class_instantiation ( self ) : <EOL> class P ( BooleanPreference ) : <EOL> default = False <EOL> preference = P ( ) <EOL> self . assertEqual ( preference . field . initial , False ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_char_field_class_instantiation ( self ) : <EOL> class P ( StringPreference ) : <EOL> default = "<STR_LIT>" <EOL> preference = P ( ) <EOL> self . assertEqual ( preference . field . initial , "<STR_LIT>" ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_longstring_preference_widget ( self ) : <EOL> class P ( LongStringPreference ) : <EOL> default = "<STR_LIT>" <EOL> preference = P ( ) <EOL> self . assertTrue ( isinstance ( preference . field . widget , forms . Textarea ) ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_decimal_preference ( self ) : <EOL> class P ( DecimalPreference ) : <EOL> default = Decimal ( '<STR_LIT>' ) <EOL> preference = P ( ) <EOL> self . assertEqual ( preference . field . initial , Decimal ( '<STR_LIT>' ) ) <EOL> @ override_settings ( DYNAMIC_PREFERENCES = { '<STR_LIT>' : False } ) <EOL> def test_float_preference ( self ) : <EOL> class P ( FloatPreference ) : <EOL> default = <NUM_LIT> <EOL> preference = P ( ) <EOL> self . assertEqual ( preference . field . initial , <NUM_LIT> ) <EOL> self . assertNotEqual ( preference . field . initial , <NUM_LIT> ) <EOL> self . assertNotEqual ( preference . field . initial , <NUM_LIT> ) <EOL> class TestRegistry ( BaseTest , TestCase ) : <EOL> def test_can_retrieve_preference_using_dotted_notation ( self ) : <EOL> registration_allowed = global_preferences_registry . get ( <EOL> name = "<STR_LIT>" , section = "<STR_LIT:user>" ) <EOL> dotted_result = global_preferences_registry . get ( <EOL> "<STR_LIT>" ) <EOL> self . assertEqual ( registration_allowed , dotted_result ) <EOL> def test_can_register_and_retrieve_preference_with_section_none ( self ) : <EOL> no_section_pref = global_preferences_registry . get ( name = "<STR_LIT>" ) <EOL> self . assertEqual ( no_section_pref . section , EMPTY_SECTION ) <EOL> class TestSerializers ( BaseTest , TestCase ) : <EOL> def test_boolean_serialization ( self ) : <EOL> s = BooleanSerializer <EOL> self . assertEqual ( s . serialize ( True ) , "<STR_LIT:True>" ) <EOL> self . assertEqual ( s . serialize ( False ) , "<STR_LIT:False>" ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( '<STR_LIT>' ) <EOL> def test_boolean_deserialization ( self ) : <EOL> s = BooleanSerializer <EOL> for v in s . true : <EOL> self . assertEqual ( s . deserialize ( v ) , True ) <EOL> for v in s . false : <EOL> self . assertEqual ( s . deserialize ( v ) , False ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . deserialize ( "<STR_LIT>" ) <EOL> def test_int_serialization ( self ) : <EOL> s = IntSerializer <EOL> self . assertEqual ( s . serialize ( <NUM_LIT:1> ) , "<STR_LIT:1>" ) <EOL> self . assertEqual ( s . serialize ( <NUM_LIT> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( - <NUM_LIT> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( <NUM_LIT:0> ) , "<STR_LIT:0>" ) <EOL> self . assertEqual ( s . serialize ( <NUM_LIT> ) , "<STR_LIT>" ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( "<STR_LIT>" ) <EOL> def test_decimal_serialization ( self ) : <EOL> s = DecimalSerializer <EOL> self . assertEqual ( s . serialize ( Decimal ( "<STR_LIT:1>" ) ) , "<STR_LIT:1>" ) <EOL> self . assertEqual ( s . serialize ( Decimal ( "<STR_LIT>" ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( Decimal ( "<STR_LIT>" ) ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( Decimal ( "<STR_LIT>" ) ) , "<STR_LIT>" ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( "<STR_LIT>" ) <EOL> def test_float_serialization ( self ) : <EOL> s = FloatSerializer <EOL> self . assertEqual ( s . serialize ( <NUM_LIT:1.0> ) , "<STR_LIT:1.0>" ) <EOL> self . assertEqual ( s . serialize ( - <NUM_LIT:1.0> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( - <NUM_LIT> ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( <NUM_LIT> ) , "<STR_LIT>" ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( "<STR_LIT>" ) <EOL> def test_float_deserialization ( self ) : <EOL> s = FloatSerializer <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT:1.0>" ) , float ( "<STR_LIT:1.0>" ) ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , float ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , float ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , float ( "<STR_LIT>" ) ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( "<STR_LIT>" ) <EOL> def test_int_deserialization ( self ) : <EOL> s = DecimalSerializer <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT:1>" ) , Decimal ( "<STR_LIT:1>" ) ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , Decimal ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , Decimal ( "<STR_LIT>" ) ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , Decimal ( "<STR_LIT>" ) ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( "<STR_LIT>" ) <EOL> def test_string_serialization ( self ) : <EOL> s = StringSerializer <EOL> self . assertEqual ( s . serialize ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . serialize ( <EOL> "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> kwargs = { "<STR_LIT>" : True , } <EOL> self . assertEqual ( <EOL> s . serialize ( <EOL> "<STR_LIT>" , ** kwargs ) , <EOL> defaultfilters . force_escape ( <EOL> "<STR_LIT>" ) <EOL> ) <EOL> with self . assertRaises ( s . exception ) : <EOL> s . serialize ( ( '<STR_LIT:I>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:A>' , '<STR_LIT>' ) ) <EOL> def test_string_deserialization ( self ) : <EOL> s = StringSerializer <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . deserialize ( "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> self . assertEqual ( s . deserialize ( <EOL> "<STR_LIT>" ) , "<STR_LIT>" ) <EOL> kwargs = { "<STR_LIT>" : True , } <EOL> self . assertEqual ( <EOL> s . deserialize ( <EOL> s . serialize ( "<STR_LIT>" , ** kwargs ) ) , <EOL> defaultfilters . force_escape ( <EOL> "<STR_LIT>" ) <EOL> ) <EOL> class TestViews ( BaseTest , LiveServerTestCase ) : <EOL> def setUp ( self ) : <EOL> self . henri = User ( <EOL> username = "<STR_LIT>" , password = "<STR_LIT:test>" , email = "<STR_LIT>" ) <EOL> self . henri . set_password ( '<STR_LIT:test>' ) <EOL> self . henri . save ( ) <EOL> self . admin = User ( <EOL> username = "<STR_LIT>" , email = "<STR_LIT>" , is_superuser = True , is_staff = True ) <EOL> self . admin . set_password ( '<STR_LIT:test>' ) <EOL> self . admin . save ( ) <EOL> def test_can_build_global_preference_form ( self ) : <EOL> form = global_preference_form_builder ( <EOL> preferences = [ '<STR_LIT>' , "<STR_LIT>" ] ) ( ) <EOL> self . assertEqual ( len ( form . fields ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( <EOL> form . fields [ '<STR_LIT>' ] . initial , False ) <EOL> def test_can_build_preference_form_from_sections ( self ) : <EOL> form = global_preference_form_builder ( section = '<STR_LIT:test>' ) ( ) <EOL> self . assertEqual ( len ( form . fields ) , <NUM_LIT:3> ) <EOL> def test_can_build_global_preference_form_from_sections ( self ) : <EOL> form = global_preference_form_builder ( section = '<STR_LIT:test>' ) ( ) <EOL> self . assertEqual ( len ( form . fields ) , <NUM_LIT:3> ) <EOL> def test_can_build_user_preference_form_from_sections ( self ) : <EOL> form = user_preference_form_builder ( <EOL> instance = self . admin , section = '<STR_LIT:test>' ) ( ) <EOL> self . assertEqual ( len ( form . fields ) , <NUM_LIT:4> ) <EOL> def test_global_preference_view_requires_staff_member ( self ) : <EOL> url = reverse ( "<STR_LIT>" ) <EOL> response = self . client . get ( url , follow = True ) <EOL> self . assertRedirects ( response , "<STR_LIT>" ) <EOL> self . client . login ( username = '<STR_LIT>' , password = "<STR_LIT:test>" ) <EOL> response = self . client . get ( url ) <EOL> self . assertRedirects ( response , "<STR_LIT>" ) <EOL> self . client . login ( username = '<STR_LIT>' , password = "<STR_LIT:test>" ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( self . admin . is_authenticated ( ) , True ) <EOL> self . assertEqual ( response . status_code , <NUM_LIT:200> ) <EOL> def test_global_preference_view_display_form ( self ) : <EOL> url = reverse ( "<STR_LIT>" ) <EOL> self . client . login ( username = '<STR_LIT>' , password = "<STR_LIT:test>" ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] . fields ) , <NUM_LIT:8> ) <EOL> self . assertEqual ( <EOL> response . context [ '<STR_LIT>' ] , global_preferences_registry ) <EOL> def test_global_preference_filters_by_section ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = "<STR_LIT:test>" ) <EOL> url = reverse ( <EOL> "<STR_LIT>" , kwargs = { "<STR_LIT>" : '<STR_LIT:user>' } ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( len ( response . context [ '<STR_LIT>' ] . fields ) , <NUM_LIT:3> ) <EOL> def test_preference_are_updated_on_form_submission ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = "<STR_LIT:test>" ) <EOL> url = reverse ( <EOL> "<STR_LIT>" , kwargs = { "<STR_LIT>" : '<STR_LIT:user>' } ) <EOL> response = self . client . post ( url , { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : True , <EOL> "<STR_LIT>" : <NUM_LIT:12> } ) <EOL> self . assertEqual ( GlobalPreferenceModel . objects . get ( <EOL> section = "<STR_LIT:user>" , name = "<STR_LIT>" ) . value , <NUM_LIT> ) <EOL> self . assertEqual ( GlobalPreferenceModel . objects . get ( <EOL> section = "<STR_LIT:user>" , name = "<STR_LIT>" ) . value , True ) <EOL> self . assertEqual ( GlobalPreferenceModel . objects . get ( <EOL> section = "<STR_LIT:user>" , name = "<STR_LIT>" ) . value , <NUM_LIT:12> ) <EOL> def test_user_preference_form_is_bound_with_current_user ( self ) : <EOL> self . client . login ( username = '<STR_LIT>' , password = "<STR_LIT:test>" ) <EOL> self . assertEqual ( UserPreferenceModel . objects . get_or_create ( <EOL> instance = self . henri , section = "<STR_LIT>" , name = '<STR_LIT>' ) [ <NUM_LIT:0> ] . value , '<STR_LIT>' ) <EOL> self . assertEqual ( UserPreferenceModel . objects . get_or_create ( <EOL> instance = self . henri , section = "<STR_LIT>" , name = '<STR_LIT>' ) [ <NUM_LIT:0> ] . value , True ) <EOL> url = reverse ( <EOL> "<STR_LIT>" , kwargs = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> response = self . client . post ( <EOL> url , { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : False } ) <EOL> self . assertEqual ( self . henri . preferences [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertEqual ( self . henri . preferences [ '<STR_LIT>' ] , False ) <EOL> def test_template_gets_global_preferences_via_template_processor ( self ) : <EOL> global_preferences = global_preferences_registry . manager ( ) <EOL> url = reverse ( "<STR_LIT>" ) <EOL> response = self . client . get ( url ) <EOL> self . assertEqual ( <EOL> response . context [ '<STR_LIT>' ] , global_preferences . all ( ) ) </s>
