Bugfixes and tests

Changes:

* ported tests from web2py, except for validation ones (in TODOs)
* several bugfixes