Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/util/modelHelper/modelHelper_test.go
akuma06 2e53fa2603 Added tests on some package + simplified a little modelHelper (#1026)
Test on:
* Categories
* Util
* modelHelper

Added hum_name on PasswordConfirm
Removed Zlib <= No code is using it anymore
modelHelper now show translated errors
New Translation strings
2017-06-20 10:06:07 +10:00

81 lignes
2,6 Kio
Go

package modelHelper
import (
"net/http"
"path"
"testing"
"github.com/NyaaPantsu/nyaa/config"
msg "github.com/NyaaPantsu/nyaa/util/messages"
)
// run before config/parse.go:init()
var _ = func() (_ struct{}) {
config.ConfigPath = path.Join("..", "..", config.ConfigPath)
config.DefaultConfigPath = path.Join("..", "..", config.DefaultConfigPath)
config.Parse()
return
}()
type TestForm struct {
DefaultVal int `form:"default" default:"3" notnull:"true"`
ConfirmVal string `form:"confirm" needed:"true" equalInput:"ConfirmeVal" len_min:"7" len_max:"8"`
ConfirmeVal string `form:"confirme" needed:"true"`
}
func TestValidateForm(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
messages := msg.GetMessages(req)
testform := TestForm{}
ValidateForm(&testform, messages)
if !messages.HasErrors() {
t.Errorf("No errors when parsing empty invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.DefaultVal, testform.ConfirmVal, testform.ConfirmeVal = 1, "testingl", "testingl"
ValidateForm(&testform, messages)
if messages.HasErrors() {
t.Errorf("Errors when parsing valid form %v\n with errors %v", testform, messages.GetAllErrors())
}
messages.ClearAllErrors()
testform.ConfirmVal = "test"
testform.ConfirmeVal = "test"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
t.Errorf("No errors on minimal length test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "testing", "testind"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
t.Errorf("No errors on equal test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "", "testing"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
t.Errorf("No errors on needed test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "azertyuid", "azertyuid"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
t.Errorf("No errors on maximal length test when parsing invalid form %v", testform)
}
messages.ClearAllErrors()
testform.DefaultVal = 0
ValidateForm(&testform, messages)
if testform.DefaultVal == 0 {
t.Errorf("Default value are not assigned on int with notnull specified: %v", testform)
}
messages.ClearAllErrors()
testform.DefaultVal = 1
ValidateForm(&testform, messages)
if testform.DefaultVal != 1 {
t.Errorf("Default value are assigned on int with non null value: %v", testform)
}
messages.ClearAllErrors()
}