Add schema tests

This commit is contained in:
Jinzhu 2018-02-25 15:00:44 +08:00
parent bf0cec734f
commit af5536108a
3 changed files with 117 additions and 13 deletions

View File

@ -18,7 +18,7 @@ type Relationship struct {
AssociationJointableForeignkey []string
}
func buildToOneRel(field *Field, sourceSchema *Schema) error {
func buildToOneRel(field *Field, sourceSchema *Schema) {
var (
// user has one profile, associationType is User, profile use UserID as foreign key
// user belongs to profile, associationType is Profile, user use ProfileID as foreign key
@ -94,7 +94,7 @@ func buildToOneRel(field *Field, sourceSchema *Schema) error {
associationForeignKeys = []string{getPrimaryPrimaryField(sourceSchema.PrimaryFields).DBName}
}
} else if len(foreignKeys) != len(associationForeignKeys) {
return errors.New("invalid foreign keys, should have same length")
sourceSchema.ParseErrors = append(sourceSchema.ParseErrors, errors.New("invalid foreign keys, should have same length"))
}
}
@ -114,7 +114,7 @@ func buildToOneRel(field *Field, sourceSchema *Schema) error {
if len(relationship.ForeignKey) != 0 {
relationship.Kind = "has_one"
field.Relationship = relationship
return nil
return
}
}
@ -154,7 +154,7 @@ func buildToOneRel(field *Field, sourceSchema *Schema) error {
associationForeignKeys = []string{getPrimaryPrimaryField(destSchema.PrimaryFields).DBName}
}
} else if len(foreignKeys) != len(associationForeignKeys) {
return errors.New("invalid foreign keys, should have same length")
sourceSchema.ParseErrors = append(sourceSchema.ParseErrors, errors.New("invalid foreign keys, should have same length"))
}
}
@ -177,10 +177,10 @@ func buildToOneRel(field *Field, sourceSchema *Schema) error {
field.Relationship = relationship
}
}
return nil
return
}
func buildToManyRel(field *Field, sourceSchema *Schema) error {
func buildToManyRel(field *Field, sourceSchema *Schema) {
var (
relationship = &Relationship{}
elemType = field.StructField.Type
@ -321,7 +321,7 @@ func buildToManyRel(field *Field, sourceSchema *Schema) error {
associationForeignKeys = []string{getPrimaryPrimaryField(sourceSchema.PrimaryFields).DBName}
}
} else if len(foreignKeys) != len(associationForeignKeys) {
return errors.New("invalid foreign keys, should have same length")
sourceSchema.ParseErrors = append(sourceSchema.ParseErrors, errors.New("invalid foreign keys, should have same length"))
}
}
@ -345,5 +345,5 @@ func buildToManyRel(field *Field, sourceSchema *Schema) error {
} else {
field.IsNormal = true
}
return nil
return
}

View File

@ -1,20 +1,92 @@
package schema
import (
"database/sql"
"testing"
"github.com/davecgh/go-spew/spew"
"time"
)
type MyStruct struct {
ID int
Int uint
IntPointer *uint
String string
StringPointer *string
Time time.Time
TimePointer *time.Time
NullInt64 sql.NullInt64
}
type BelongsTo struct {
ID int
Name string
}
type BelongTo struct {
type HasOne struct {
ID int
MyStructID uint
}
type HasMany struct {
ID int
MyStructID uint
Name string
}
type Many2Many struct {
ID int
Name string
}
func TestParseSchema(t *testing.T) {
Schema := ParseSchema(&MyStruct{})
spew.Dump(Schema)
ParseSchema(&MyStruct{})
}
func TestParseBelongsToRel(t *testing.T) {
type MyStruct struct {
ID int
Name string
BelongsTo BelongsTo
}
ParseSchema(&MyStruct{})
}
func TestParseHasOneRel(t *testing.T) {
type MyStruct struct {
ID int
Name string
HasOne HasOne
}
ParseSchema(&MyStruct{})
}
func TestParseHasManyRel(t *testing.T) {
type MyStruct struct {
ID int
Name string
HasMany []HasMany
}
ParseSchema(&MyStruct{})
}
func TestParseManyToManyRel(t *testing.T) {
type MyStruct struct {
ID int
Name string
HasMany []HasMany
}
ParseSchema(&MyStruct{})
}
func TestEmbeddedStruct(t *testing.T) {
}
func TestCustomizePrimaryKey(t *testing.T) {
}
func TestCompositePrimaryKeys(t *testing.T) {
}

32
schema/utils_test.go Normal file
View File

@ -0,0 +1,32 @@
package schema
import (
"testing"
gorm "github.com/jinzhu/gorm.bak"
)
func TestToDBName(t *testing.T) {
var maps = map[string]string{
"": "",
"X": "x",
"ThisIsATest": "this_is_a_test",
"PFAndESI": "pf_and_esi",
"AbcAndJkl": "abc_and_jkl",
"EmployeeID": "employee_id",
"SKU_ID": "sku_id",
"FieldX": "field_x",
"HTTPAndSMTP": "http_and_smtp",
"HTTPServerHandlerForURLID": "http_server_handler_for_url_id",
"UUID": "uuid",
"HTTPURL": "http_url",
"HTTP_URL": "http_url",
"ThisIsActuallyATestSoWeMayBeAbleToUseThisCodeInGormPackageAlsoIdCanBeUsedAtTheEndAsID": "this_is_actually_a_test_so_we_may_be_able_to_use_this_code_in_gorm_package_also_id_can_be_used_at_the_end_as_id",
}
for key, value := range maps {
if gorm.ToDBName(key) != value {
t.Errorf("%v ToDBName should equal %v, but got %v", key, value, gorm.ToDBName(key))
}
}
}