<s> from __future__ import unicode_literals <EOL> import random <EOL> from django . contrib . auth . models import AnonymousUser , Group , User <EOL> from django . db import connection <EOL> from django . test import RequestFactory <EOL> from django . test . utils import override_settings <EOL> import mock <EOL> import waffle <EOL> from test_app import views <EOL> from waffle . middleware import WaffleMiddleware <EOL> from waffle . models import Flag , Sample , Switch <EOL> from waffle . tests . base import TestCase <EOL> def get ( ** kw ) : <EOL> request = RequestFactory ( ) . get ( '<STR_LIT>' , data = kw ) <EOL> request . user = AnonymousUser ( ) <EOL> return request <EOL> def process_request ( request , view ) : <EOL> response = view ( request ) <EOL> return WaffleMiddleware ( ) . process_response ( request , response ) <EOL> class WaffleTests ( TestCase ) : <EOL> def test_persist_active_flag ( self ) : <EOL> Flag . objects . create ( name = '<STR_LIT>' , percent = '<STR_LIT>' ) <EOL> request = get ( ) <EOL> request . COOKIES [ '<STR_LIT>' ] = '<STR_LIT:True>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' in response . cookies <EOL> self . assertEqual ( '<STR_LIT:True>' , response . cookies [ '<STR_LIT>' ] . value ) <EOL> def test_persist_inactive_flag ( self ) : <EOL> Flag . objects . create ( name = '<STR_LIT>' , percent = '<STR_LIT>' ) <EOL> request = get ( ) <EOL> request . COOKIES [ '<STR_LIT>' ] = '<STR_LIT:False>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' in response . cookies <EOL> self . assertEqual ( '<STR_LIT:False>' , response . cookies [ '<STR_LIT>' ] . value ) <EOL> def test_no_set_unused_flag ( self ) : <EOL> """<STR_LIT>""" <EOL> request = get ( ) <EOL> request . COOKIES [ '<STR_LIT>' ] = '<STR_LIT:True>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_superuser ( self ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , superusers = True ) <EOL> request = get ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> superuser = User ( username = '<STR_LIT:foo>' , is_superuser = True ) <EOL> request . user = superuser <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> non_superuser = User ( username = '<STR_LIT:bar>' , is_superuser = False ) <EOL> non_superuser . save ( ) <EOL> request . user = non_superuser <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_staff ( self ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , staff = True ) <EOL> request = get ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> staff = User ( username = '<STR_LIT:foo>' , is_staff = True ) <EOL> request . user = staff <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> non_staff = User ( username = '<STR_LIT:foo>' , is_staff = False ) <EOL> non_staff . save ( ) <EOL> request . user = non_staff <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_languages ( self ) : <EOL> Flag . objects . create ( name = '<STR_LIT>' , languages = '<STR_LIT>' ) <EOL> request = get ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> request . LANGUAGE_CODE = '<STR_LIT>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> request . LANGUAGE_CODE = '<STR_LIT>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> def test_user ( self ) : <EOL> """<STR_LIT>""" <EOL> user = User . objects . create ( username = '<STR_LIT:foo>' ) <EOL> flag = Flag . objects . create ( name = '<STR_LIT>' ) <EOL> flag . users . add ( user ) <EOL> request = get ( ) <EOL> request . user = user <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> request . user = User . objects . create ( username = '<STR_LIT>' ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_group ( self ) : <EOL> """<STR_LIT>""" <EOL> group = Group . objects . create ( name = '<STR_LIT:foo>' ) <EOL> user = User . objects . create ( username = '<STR_LIT:bar>' ) <EOL> user . groups . add ( group ) <EOL> flag = Flag . objects . create ( name = '<STR_LIT>' ) <EOL> flag . groups . add ( group ) <EOL> request = get ( ) <EOL> request . user = user <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> request . user = User ( username = '<STR_LIT>' ) <EOL> request . user . save ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_authenticated ( self ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , authenticated = True ) <EOL> request = get ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> request . user = User ( username = '<STR_LIT:foo>' ) <EOL> assert request . user . is_authenticated ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_everyone_on ( self ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , everyone = True ) <EOL> request = get ( ) <EOL> request . COOKIES [ '<STR_LIT>' ] = '<STR_LIT:False>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> request . user = User ( username = '<STR_LIT:foo>' ) <EOL> assert request . user . is_authenticated ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_everyone_off ( self ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , everyone = False , <EOL> authenticated = True ) <EOL> request = get ( ) <EOL> request . COOKIES [ '<STR_LIT>' ] = '<STR_LIT:True>' <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> request . user = User ( username = '<STR_LIT:foo>' ) <EOL> assert request . user . is_authenticated ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> assert '<STR_LIT>' not in response . cookies <EOL> def test_percent ( self ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , percent = '<STR_LIT>' ) <EOL> request = get ( ) <EOL> response = process_request ( request , views . flag_in_view ) <EOL> assert '<STR_LIT>' in response . cookies <EOL> @ mock . patch . object ( random , '<STR_LIT>' ) <EOL> def test_reroll ( self , uniform ) : <EOL> """<STR_LIT>""" <EOL> Flag . objects . create ( name = '<STR_LIT>' , percent = '<STR_LIT>' ) <EOL> request = get ( ) <EOL> assert not hasattr ( request , '<STR_LIT>' ) <EOL> uniform . return_value = '<STR_LIT>' <EOL> assert waffle . flag_is_active ( request , '<STR_LIT>' ) <EOL> assert hasattr ( request , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in request . waffles <EOL> assert request . waffles [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> uniform . return_value = '<STR_LIT>' <EOL> assert waffle . flag_is_active ( request , '<STR_LIT>' ) <EOL> assert request . waffles [ '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> def test_undefined ( self ) : <EOL> """<STR_LIT>""" <EOL> request = get ( ) <EOL> assert not waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> @ override_settings ( WAFFLE_FLAG_DEFAULT = True ) <EOL> def test_undefined_default ( self ) : <EOL> """<STR_LIT>""" <EOL> request = get ( ) <EOL> assert waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> @ override_settings ( WAFFLE_OVERRIDE = True ) <EOL> def test_override ( self ) : <EOL> request = get ( foo = '<STR_LIT:1>' ) <EOL> Flag . objects . create ( name = '<STR_LIT:foo>' ) <EOL> assert waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> def test_testing_flag ( self ) : <EOL> Flag . objects . create ( name = '<STR_LIT:foo>' , testing = True ) <EOL> request = get ( dwft_foo = '<STR_LIT:1>' ) <EOL> assert waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> assert '<STR_LIT:foo>' in request . waffle_tests <EOL> assert request . waffle_tests [ '<STR_LIT:foo>' ] <EOL> request = get ( dwft_foo = '<STR_LIT:0>' ) <EOL> request . COOKIES [ '<STR_LIT>' ] = '<STR_LIT:True>' <EOL> assert not waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> assert '<STR_LIT:foo>' in request . waffle_tests <EOL> assert not request . waffle_tests [ '<STR_LIT:foo>' ] <EOL> def test_testing_disabled_flag ( self ) : <EOL> Flag . objects . create ( name = '<STR_LIT:foo>' ) <EOL> request = get ( dwft_foo = '<STR_LIT:1>' ) <EOL> assert not waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> assert not hasattr ( request , '<STR_LIT>' ) <EOL> request = get ( dwft_foo = '<STR_LIT:0>' ) <EOL> assert not waffle . flag_is_active ( request , '<STR_LIT:foo>' ) <EOL> assert not hasattr ( request , '<STR_LIT>' ) <EOL> def test_set_then_unset_testing_flag ( self ) : <EOL> Flag . objects . create ( name = '<STR_LIT>' , testing = True ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> response = self . client . get ( '<STR_LIT>' ) <EOL> self . assertEqual ( b'<STR_LIT>' , response . content ) <EOL> class SwitchTests ( TestCase ) : <EOL> def test_switch_active ( self ) : <EOL> switch = Switch . objects . create ( name = '<STR_LIT>' , active = True ) <EOL> assert waffle . switch_is_active ( switch . name ) <EOL> def test_switch_inactive ( self ) : <EOL> switch = Switch . objects . create ( name = '<STR_LIT>' , active = False ) <EOL> assert not waffle . switch_is_active ( switch . name ) <EOL> def test_switch_active_from_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> switch = Switch . objects . create ( name = '<STR_LIT>' , active = True ) <EOL> assert waffle . switch_is_active ( switch . name ) <EOL> queries = len ( connection . queries ) <EOL> assert waffle . switch_is_active ( switch . name ) <EOL> self . assertEqual ( queries , len ( connection . queries ) , '<STR_LIT>' ) <EOL> def test_switch_inactive_from_cache ( self ) : <EOL> """<STR_LIT>""" <EOL> switch = Switch . objects . create ( name = '<STR_LIT>' , active = False ) <EOL> assert not waffle . switch_is_active ( switch . name ) <EOL> queries = len ( connection . queries ) <EOL> assert not waffle . switch_is_active ( switch . name ) <EOL> self . assertEqual ( queries , len ( connection . queries ) , '<STR_LIT>' ) <EOL> def test_undefined ( self ) : <EOL> assert not waffle . switch_is_active ( '<STR_LIT:foo>' ) <EOL> @ override_settings ( WAFFLE_SWITCH_DEFAULT = True ) <EOL> def test_undefined_default ( self ) : <EOL> assert waffle . switch_is_active ( '<STR_LIT:foo>' ) <EOL> @ override_settings ( DEBUG = True ) <EOL> def test_no_query ( self ) : <EOL> """<STR_LIT>""" <EOL> assert not Switch . objects . filter ( name = '<STR_LIT:foo>' ) . exists ( ) <EOL> queries = len ( connection . queries ) <EOL> assert not waffle . switch_is_active ( '<STR_LIT:foo>' ) <EOL> assert len ( connection . queries ) > queries , '<STR_LIT>' <EOL> queries = len ( connection . queries ) <EOL> assert not waffle . switch_is_active ( '<STR_LIT:foo>' ) <EOL> self . assertEqual ( queries , len ( connection . queries ) , '<STR_LIT>' ) <EOL> class SampleTests ( TestCase ) : <EOL> def test_sample_100 ( self ) : <EOL> sample = Sample . objects . create ( name = '<STR_LIT>' , percent = '<STR_LIT>' ) <EOL> assert waffle . sample_is_active ( sample . name ) <EOL> def test_sample_0 ( self ) : <EOL> sample = Sample . objects . create ( name = '<STR_LIT>' , percent = '<STR_LIT>' ) <EOL> assert not waffle . sample_is_active ( sample . name ) <EOL> def test_undefined ( self ) : <EOL> assert not waffle . sample_is_active ( '<STR_LIT:foo>' ) <EOL> @ override_settings ( WAFFLE_SAMPLE_DEFAULT = True ) <EOL> def test_undefined_default ( self ) : <EOL> assert waffle . sample_is_active ( '<STR_LIT:foo>' ) </s>
