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"
2017-08-03 03:38:07 +02:00
"github.com/NyaaPantsu/nyaa/models"
2017-07-02 16:54:55 +02:00
msg "github.com/NyaaPantsu/nyaa/utils/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 { } ) {
2017-07-21 01:36:25 +02:00
config . Configpaths [ 1 ] = path . Join ( ".." , ".." , config . Configpaths [ 1 ] )
config . Configpaths [ 0 ] = path . Join ( ".." , ".." , config . Configpaths [ 0 ] )
2017-07-10 14:11:05 +02:00
config . Reload ( )
2017-08-03 03:38:07 +02:00
config . Get ( ) . DBType = models . SqliteType
config . Get ( ) . DBParams = ":memory:?cache=shared&mode=memory"
models . ORM , _ = models . GormInit ( models . DefaultLogger )
2017-06-20 02:06:07 +02:00
return
} ( )
type TestForm struct {
2017-07-01 02:59:39 +02:00
DefaultVal int ` validate:"default=3,required" `
ConfirmVal string ` validate:"eqfield=ConfirmeVal,min=7,max=8,required" `
ConfirmeVal string ` validate:"required" `
2017-07-01 19:44:36 +02:00
Optional string
2017-06-20 02:06:07 +02:00
}
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 ( )
2017-07-01 19:44:36 +02:00
testform . DefaultVal , testform . ConfirmVal , testform . ConfirmeVal , testform . Optional = 1 , "testingl" , "testingl" , "test"
2017-06-20 02:06:07 +02:00
ValidateForm ( & testform , messages )
if messages . HasErrors ( ) {
t . Errorf ( "Errors when parsing valid form %v\n with errors %v" , testform , messages . GetAllErrors ( ) )
}
messages . ClearAllErrors ( )
2017-07-01 19:44:36 +02:00
testform . Optional = ""
ValidateForm ( & testform , messages )
if messages . HasErrors ( ) {
t . Errorf ( "Errors when testing an empty optional field in form %v\n with errors %v" , testform , messages . GetAllErrors ( ) )
}
messages . ClearAllErrors ( )
2017-06-20 02:06:07 +02:00
testform . ConfirmVal = "test"
testform . ConfirmeVal = "test"
ValidateForm ( & testform , messages )
2017-07-01 02:59:39 +02:00
if len ( messages . GetErrors ( "ConfirmVal" ) ) == 0 {
2017-06-20 02:06:07 +02:00
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 )
2017-07-01 02:59:39 +02:00
if len ( messages . GetErrors ( "ConfirmVal" ) ) == 0 {
2017-06-20 02:06:07 +02:00
t . Errorf ( "No errors on equal test when parsing invalid form: %v" , testform )
}
messages . ClearAllErrors ( )
testform . ConfirmVal , testform . ConfirmeVal = "" , "testing"
ValidateForm ( & testform , messages )
2017-07-01 02:59:39 +02:00
if len ( messages . GetErrors ( "ConfirmVal" ) ) == 0 {
2017-06-20 02:06:07 +02:00
t . Errorf ( "No errors on needed test when parsing invalid form: %v" , testform )
}
messages . ClearAllErrors ( )
testform . ConfirmVal , testform . ConfirmeVal = "azertyuid" , "azertyuid"
ValidateForm ( & testform , messages )
2017-07-01 02:59:39 +02:00
if len ( messages . GetErrors ( "ConfirmVal" ) ) == 0 {
2017-06-20 02:06:07 +02:00
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 )
}
}
}