<s> from __future__ import unicode_literals <EOL> from django import forms , http <EOL> from django . conf import settings <EOL> from django . db import models <EOL> from django . test import TestCase <EOL> from django . template . response import TemplateResponse <EOL> from django . utils . importlib import import_module <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . formtools . wizard . views import ( WizardView , <EOL> SessionWizardView , <EOL> CookieWizardView ) <EOL> class DummyRequest ( http . HttpRequest ) : <EOL> def __init__ ( self , POST = None ) : <EOL> super ( DummyRequest , self ) . __init__ ( ) <EOL> self . method = "<STR_LIT:POST>" if POST else "<STR_LIT:GET>" <EOL> if POST is not None : <EOL> self . POST . update ( POST ) <EOL> self . session = { } <EOL> self . _dont_enforce_csrf_checks = True <EOL> def get_request ( * args , ** kwargs ) : <EOL> request = DummyRequest ( * args , ** kwargs ) <EOL> engine = import_module ( settings . SESSION_ENGINE ) <EOL> request . session = engine . SessionStore ( None ) <EOL> return request <EOL> class Step1 ( forms . Form ) : <EOL> name = forms . CharField ( ) <EOL> class Step2 ( forms . Form ) : <EOL> name = forms . CharField ( ) <EOL> class Step3 ( forms . Form ) : <EOL> data = forms . CharField ( ) <EOL> class CustomKwargsStep1 ( Step1 ) : <EOL> def __init__ ( self , test = None , * args , ** kwargs ) : <EOL> self . test = test <EOL> return super ( CustomKwargsStep1 , self ) . __init__ ( * args , ** kwargs ) <EOL> class TestModel ( models . Model ) : <EOL> name = models . CharField ( max_length = <NUM_LIT:100> ) <EOL> class Meta : <EOL> app_label = '<STR_LIT>' <EOL> class TestModelForm ( forms . ModelForm ) : <EOL> class Meta : <EOL> model = TestModel <EOL> fields = '<STR_LIT>' <EOL> TestModelFormSet = forms . models . modelformset_factory ( TestModel , form = TestModelForm , extra = <NUM_LIT:2> , <EOL> fields = '<STR_LIT>' ) <EOL> class TestWizard ( WizardView ) : <EOL> storage_name = '<STR_LIT>' <EOL> def dispatch ( self , request , * args , ** kwargs ) : <EOL> response = super ( TestWizard , self ) . dispatch ( request , * args , ** kwargs ) <EOL> return response , self <EOL> def get_form_kwargs ( self , step , * args , ** kwargs ) : <EOL> kwargs = super ( TestWizard , self ) . get_form_kwargs ( step , * args , ** kwargs ) <EOL> if step == '<STR_LIT>' : <EOL> kwargs [ '<STR_LIT:test>' ] = True <EOL> return kwargs <EOL> class TestWizardWithInitAttrs ( TestWizard ) : <EOL> form_list = [ Step1 , Step2 ] <EOL> condition_dict = { '<STR_LIT>' : True } <EOL> initial_dict = { '<STR_LIT:start>' : { '<STR_LIT:name>' : '<STR_LIT>' } } <EOL> instance_dict = { '<STR_LIT:start>' : User ( ) } <EOL> class FormTests ( TestCase ) : <EOL> def test_form_init ( self ) : <EOL> testform = TestWizard . get_initkwargs ( [ Step1 , Step2 ] ) <EOL> self . assertEqual ( testform [ '<STR_LIT>' ] , { '<STR_LIT:0>' : Step1 , '<STR_LIT:1>' : Step2 } ) <EOL> testform = TestWizard . get_initkwargs ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> self . assertEqual ( <EOL> testform [ '<STR_LIT>' ] , { '<STR_LIT:start>' : Step1 , '<STR_LIT>' : Step2 } ) <EOL> testform = TestWizard . get_initkwargs ( [ Step1 , Step2 , ( '<STR_LIT>' , Step3 ) ] ) <EOL> self . assertEqual ( <EOL> testform [ '<STR_LIT>' ] , { '<STR_LIT:0>' : Step1 , '<STR_LIT:1>' : Step2 , '<STR_LIT>' : Step3 } ) <EOL> testform = TestWizardWithInitAttrs . get_initkwargs ( ) <EOL> self . assertEqual ( testform [ '<STR_LIT>' ] , { '<STR_LIT:0>' : Step1 , '<STR_LIT:1>' : Step2 } ) <EOL> def test_first_step ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( [ Step1 , Step2 ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . steps . current , '<STR_LIT:0>' ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . steps . current , '<STR_LIT:start>' ) <EOL> def test_persistence ( self ) : <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> request = get_request ( { '<STR_LIT>' : '<STR_LIT:start>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . steps . current , '<STR_LIT:start>' ) <EOL> instance . storage . current_step = '<STR_LIT>' <EOL> testform2 = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> request . POST = { '<STR_LIT>' : '<STR_LIT>' } <EOL> response , instance = testform2 ( request ) <EOL> self . assertEqual ( instance . steps . current , '<STR_LIT>' ) <EOL> def test_form_condition ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( <EOL> [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) , ( '<STR_LIT>' , Step3 ) ] , <EOL> condition_dict = { '<STR_LIT>' : True } ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_next_step ( ) , '<STR_LIT>' ) <EOL> testform = TestWizard . as_view ( <EOL> [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) , ( '<STR_LIT>' , Step3 ) ] , <EOL> condition_dict = { '<STR_LIT>' : False } ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_next_step ( ) , '<STR_LIT>' ) <EOL> testform = TestWizardWithInitAttrs . as_view ( <EOL> [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) , ( '<STR_LIT>' , Step3 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_next_step ( ) , '<STR_LIT>' ) <EOL> def test_form_kwargs ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , <EOL> ( '<STR_LIT>' , CustomKwargsStep1 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_form_kwargs ( '<STR_LIT:start>' ) , { } ) <EOL> self . assertEqual ( instance . get_form_kwargs ( '<STR_LIT>' ) , { '<STR_LIT:test>' : True } ) <EOL> self . assertEqual ( instance . get_form ( '<STR_LIT>' ) . test , True ) <EOL> def test_form_prefix ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_form_prefix ( ) , '<STR_LIT:start>' ) <EOL> self . assertEqual ( instance . get_form_prefix ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> def test_form_initial ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] , <EOL> initial_dict = { '<STR_LIT:start>' : { '<STR_LIT:name>' : '<STR_LIT>' } } ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_form_initial ( '<STR_LIT:start>' ) , { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( instance . get_form_initial ( '<STR_LIT>' ) , { } ) <EOL> testform = TestWizardWithInitAttrs . as_view ( <EOL> [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( instance . get_form_initial ( '<STR_LIT:start>' ) , { '<STR_LIT:name>' : '<STR_LIT>' } ) <EOL> self . assertEqual ( instance . get_form_initial ( '<STR_LIT>' ) , { } ) <EOL> def test_form_instance ( self ) : <EOL> request = get_request ( ) <EOL> the_instance = TestModel ( ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , TestModelForm ) , ( '<STR_LIT>' , Step2 ) ] , <EOL> instance_dict = { '<STR_LIT:start>' : the_instance } ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( <EOL> instance . get_form_instance ( '<STR_LIT:start>' ) , <EOL> the_instance ) <EOL> self . assertEqual ( <EOL> instance . get_form_instance ( '<STR_LIT>' ) , <EOL> None ) <EOL> testform = TestWizardWithInitAttrs . as_view ( <EOL> [ ( '<STR_LIT:start>' , TestModelForm ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( <EOL> instance . get_form_instance ( '<STR_LIT:start>' ) , <EOL> TestWizardWithInitAttrs . instance_dict [ '<STR_LIT:start>' ] ) <EOL> def test_formset_instance ( self ) : <EOL> request = get_request ( ) <EOL> the_instance1 , created = TestModel . objects . get_or_create ( <EOL> name = '<STR_LIT>' ) <EOL> the_instance2 , created = TestModel . objects . get_or_create ( <EOL> name = '<STR_LIT>' ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , TestModelFormSet ) , ( '<STR_LIT>' , Step2 ) ] , <EOL> instance_dict = { '<STR_LIT:start>' : TestModel . objects . filter ( name = '<STR_LIT>' ) } ) <EOL> response , instance = testform ( request ) <EOL> self . assertEqual ( list ( instance . get_form_instance ( '<STR_LIT:start>' ) ) , [ the_instance1 ] ) <EOL> self . assertEqual ( instance . get_form_instance ( '<STR_LIT>' ) , None ) <EOL> self . assertEqual ( instance . get_form ( ) . initial_form_count ( ) , <NUM_LIT:1> ) <EOL> def test_done ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> response , instance = testform ( request ) <EOL> self . assertRaises ( NotImplementedError , instance . done , None ) <EOL> def test_revalidation ( self ) : <EOL> request = get_request ( ) <EOL> testform = TestWizard . as_view ( [ ( '<STR_LIT:start>' , Step1 ) , ( '<STR_LIT>' , Step2 ) ] ) <EOL> response , instance = testform ( request ) <EOL> instance . render_done ( None ) <EOL> self . assertEqual ( instance . storage . current_step , '<STR_LIT:start>' ) <EOL> class SessionFormTests ( TestCase ) : <EOL> def test_init ( self ) : <EOL> request = get_request ( ) <EOL> testform = SessionWizardView . as_view ( [ ( '<STR_LIT:start>' , Step1 ) ] ) <EOL> self . assertTrue ( isinstance ( testform ( request ) , TemplateResponse ) ) <EOL> class CookieFormTests ( TestCase ) : <EOL> def test_init ( self ) : <EOL> request = get_request ( ) <EOL> testform = CookieWizardView . as_view ( [ ( '<STR_LIT:start>' , Step1 ) ] ) <EOL> self . assertTrue ( isinstance ( testform ( request ) , TemplateResponse ) ) </s>
