Fix SQL regeneration
Improve tests