<s> import pretend <EOL> import pytest <EOL> from warehouse . forms import Form , DBForm , StopValidation <EOL> def _raiser ( exc ) : <EOL> raise exc <EOL> class TestForm : <EOL> def test_empty_form_no_errors ( self ) : <EOL> form = Form ( ) <EOL> assert form . errors == { } <EOL> def test_errors_is_cached ( self ) : <EOL> form = Form ( ) <EOL> assert form . errors == { } <EOL> form . _form_errors . append ( "<STR_LIT>" ) <EOL> assert form . errors == { } <EOL> form . _errors = None <EOL> assert form . errors == { "<STR_LIT>" : [ "<STR_LIT>" ] } <EOL> def test_form_level_validation_no_validators ( self ) : <EOL> class TestForm ( Form ) : <EOL> pass <EOL> form = TestForm ( ) <EOL> assert form . validate ( ) <EOL> assert form . errors == { } <EOL> def test_form_level_validation_full_validate ( self ) : <EOL> class TestForm ( Form ) : <EOL> @ pretend . call_recorder <EOL> def full_validate ( self ) : <EOL> pass <EOL> form = TestForm ( ) <EOL> assert form . validate ( ) <EOL> assert form . errors == { } <EOL> assert form . full_validate . calls == [ pretend . call ( form ) ] <EOL> def test_form_level_validation_full_validate_fails ( self ) : <EOL> class TestForm ( Form ) : <EOL> @ pretend . call_recorder <EOL> def full_validate ( self ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> form = TestForm ( ) <EOL> assert not form . validate ( ) <EOL> assert form . errors == { "<STR_LIT>" : [ "<STR_LIT>" ] } <EOL> assert form . full_validate . calls == [ pretend . call ( form ) ] <EOL> @ pytest . mark . parametrize ( <EOL> "<STR_LIT>" , <EOL> [ <EOL> [ ] , <EOL> [ lambda f : None ] <EOL> ] , <EOL> ) <EOL> def test_form_level_validation_meta_works ( self , validator_funcs ) : <EOL> validator_funcs = [ pretend . call_recorder ( v ) for v in validator_funcs ] <EOL> class TestForm ( Form ) : <EOL> class Meta : <EOL> validators = validator_funcs <EOL> form = TestForm ( ) <EOL> assert form . validate ( ) <EOL> assert form . errors == { } <EOL> for v in validator_funcs : <EOL> assert v . calls == [ pretend . call ( form ) ] <EOL> @ pytest . mark . parametrize ( <EOL> ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) , <EOL> [ <EOL> ( <EOL> [ <EOL> lambda f : _raiser ( ValueError ( "<STR_LIT>" ) ) , <EOL> lambda f : None , <EOL> lambda f : _raiser ( ValueError ( "<STR_LIT>" ) ) , <EOL> lambda f : _raiser ( StopValidation ( "<STR_LIT>" ) ) , <EOL> lambda f : _raiser ( ValueError ( "<STR_LIT>" ) ) , <EOL> ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] , <EOL> <NUM_LIT:3> , <EOL> ) , <EOL> ( <EOL> [ <EOL> lambda f : _raiser ( ValueError ( "<STR_LIT>" ) ) , <EOL> lambda f : None , <EOL> lambda f : _raiser ( ValueError ( "<STR_LIT>" ) ) , <EOL> lambda f : _raiser ( StopValidation ) , <EOL> lambda f : _raiser ( ValueError ( "<STR_LIT>" ) ) , <EOL> ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" ] , <EOL> <NUM_LIT:3> , <EOL> ) , <EOL> ] , <EOL> ) <EOL> def test_form_level_validation_meta_fails ( self , validator_funcs , errors , <EOL> stop_after ) : <EOL> validator_funcs = [ pretend . call_recorder ( v ) for v in validator_funcs ] <EOL> class TestForm ( Form ) : <EOL> class Meta : <EOL> validators = validator_funcs <EOL> form = TestForm ( ) <EOL> assert not form . validate ( ) <EOL> assert form . errors == { "<STR_LIT>" : errors } <EOL> for i , v in enumerate ( validator_funcs ) : <EOL> assert v . calls == [ pretend . call ( form ) ] <EOL> if i >= stop_after : <EOL> break <EOL> class TestDBForm : <EOL> def test_form_requires_db ( self ) : <EOL> with pytest . raises ( TypeError ) : <EOL> DBForm ( ) <EOL> def test_form_accepts_db ( self ) : <EOL> db = pretend . stub ( ) <EOL> form = DBForm ( db = db ) <EOL> assert form . db is db </s>
