This is a prelimenary work
Showing how we can remove services, preventing cyclic imports and lessing the number of imports.
Now db is in models. Db and models are highly tightened, according to go standards, you should put them in the same package.
In models, there are folders separating the different methods used to modify the models. For example, if you want to create a user, you have to use /models (for the user struct) and /models/user (for creating a user.
However, if you want to delete a torrent, you just have to import /models and do torrent.Delete(definitely bool).
By the way packages in models are the plural name of a model. For example, you have torrent.go for a torrent model and its package torrents for db stuff related functions (Find, Create, Some helpers)
2017-06-29 00:44:07 +02:00
package validator
2017-06-20 02:06:07 +02:00
import (
"net/http"
"path"
"testing"
"github.com/NyaaPantsu/nyaa/config"
msg "github.com/NyaaPantsu/nyaa/util/messages"
2017-06-28 13:42:38 +02:00
"github.com/gin-gonic/gin"
2017-06-20 02:06:07 +02:00
)
// 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 ) {
2017-07-01 01:25:11 +02:00
t . Parallel ( )
2017-06-20 02:06:07 +02:00
req , err := http . NewRequest ( "GET" , "/" , nil )
if err != nil {
t . Fatal ( err )
}
2017-06-28 13:42:38 +02:00
c := & gin . Context { Request : req }
messages := msg . GetMessages ( c )
2017-06-20 02:06:07 +02:00
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 ( )
}
2017-07-01 01:25:11 +02:00
func TestIsUTFLetterNumeric ( t * testing . T ) {
t . Parallel ( )
var tests = [ ] struct {
param string
expected bool
} {
{ "\n" , false } ,
{ "\r" , false } ,
{ "Ⅸ" , true } ,
{ "" , true } ,
{ " fooo " , false } ,
{ "abc!!!" , false } ,
{ "abc1" , true } ,
{ "abc〩" , true } ,
{ "abc" , true } ,
{ "소주" , true } ,
{ "ABC" , true } ,
{ "FoObAr" , true } ,
{ "소aBC" , true } ,
{ "소" , true } ,
{ "달기&Co." , false } ,
{ "〩Hours" , true } ,
{ "\ufff0" , false } ,
{ "\u0070" , true } , //UTF-8(ASCII): p
{ "\u0026" , false } , //UTF-8(ASCII): &
{ "\u0030" , true } , //UTF-8(ASCII): 0
{ "123" , true } ,
{ "0123" , true } ,
{ "-00123" , false } ,
{ "0" , true } ,
{ "-0" , false } ,
{ "123.123" , false } ,
{ " " , false } ,
{ "." , false } ,
{ "-1¾" , false } ,
{ "1¾" , true } ,
{ "〥〩" , true } ,
{ "모자" , true } ,
{ "ix" , true } ,
{ "۳۵۶۰" , true } ,
{ "1--" , false } ,
{ "1-1" , false } ,
{ "-" , false } ,
{ "--" , false } ,
{ "1++" , false } ,
{ "1+1" , false } ,
{ "+" , false } ,
{ "++" , false } ,
{ "+1" , false } ,
}
for _ , test := range tests {
actual := IsUTFLetterNumeric ( test . param )
if actual != test . expected {
t . Errorf ( "Expected IsUTFLetterNumeric(%q) to be %v, got %v" , test . param , test . expected , actual )
}
}
}