Extracting the appstruct logic into a separate function is one way to go, though it still would be useful to have a way to conveniently create peppercorn POST values from appstructs. Especially for cases where the view logic is quite simple boilerplate (validate the form, submit the resulting appstruct somewhere and do a redirect) separating every view function into 2 could be avoided (e.g. register() and do_register(), with the latter being appstruct-only logic).
On Tuesday, December 23, 2014 6:57:22 AM UTC+1, Randall Leeds wrote: > > On Mon, Dec 22, 2014 at 1:59 PM, pyramidX <[email protected] > <javascript:>> wrote: > >> If my view uses this pattern for working with forms >> >> appstruct = deform_form.validate(request.POST.items()) >> >> Then to unit test it I do something like this >> >> post = collections.OrderedDict([ >> ('somefield', 'somevalue'), >> ('__start__', 'somedate:mapping'), >> ('year', '1111'), >> ('month', '11'), >> ('day', '11'), >> ('__end__', 'somedate:mapping'), >> ])) >> request = testing.DummyRequest(post=post) >> response = views.someview(request) >> >> With more complex forms this test code gets messy and less readable with >> __start__, __end__, :sequence, :mapping all over the place. >> >> I've skimmed through the code for peppercorn/deform/pyramid and I can >> find ways to turn the POSTed values into an appstruct, but I can't find any >> way to turn an appstruct into these peppercorn POST values. The closest >> hints I could find is https://github.com/Pylons/deform/search?q=__start__ >> and https://github.com/Pylons/deform/search?q=start_mapping >> >> Is there a way to do something like >> >> post = some_function({ >> 'somefield': 'somevalue', >> 'somedate': some_date >> }) >> request = testing.DummyRequest(post=post) >> response = views.someview(request) >> >> which would be equivalent to the first example? If not would it be a >> worthy feature request for deform or peppercorn? >> > > You may simply want to extract any non-trivial logic out into functions > that take the appstruct. You can then test most of that logic without > touching deform. Or use the mock library to mock the schema so you can test > that the view does the right thing when `validate` returns a valid > appstruct or raises an exception. > -- You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/pylons-discuss. For more options, visit https://groups.google.com/d/optout.
