feat: null_safety test
This commit is contained in:
26
tests/models/testdata.go
Normal file
26
tests/models/testdata.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package models
|
||||
|
||||
type Order struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Status string
|
||||
ProductTypeId uint
|
||||
ProductType ProductType
|
||||
ProductAmount uint
|
||||
Description string
|
||||
CustomerId uint `gorm:"null;foreignKey:CustomerId;"` // want "Null safety error in \"Order\" model, field \"CustomerId\": column nullable policy doesn't match to tag nullable policy"
|
||||
Customer Customer
|
||||
CreatedAt int64 `gorm:"autoCreateTime"`
|
||||
DeadlineDate int64
|
||||
}
|
||||
|
||||
type ProductType struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Name string
|
||||
}
|
||||
|
||||
type Customer struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Title string
|
||||
Contact string
|
||||
Orders []Order `gorm:"foreignKey:CustomerId"`
|
||||
}
|
||||
12
tests/null_safety_test.go
Normal file
12
tests/null_safety_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"golang.org/x/tools/go/analysis/analysistest"
|
||||
"gormlint/nullSafetyCheck"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNullSafety(t *testing.T) {
|
||||
t.Parallel()
|
||||
analysistest.Run(t, analysistest.TestData(), nullSafetyCheck.NullSafetyAnalyzer, "null_safety")
|
||||
}
|
||||
29
tests/testdata/src/null_safety/negative.go
vendored
Normal file
29
tests/testdata/src/null_safety/negative.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package null_safety
|
||||
|
||||
type Order1 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Description string
|
||||
// not nullable - not nullable
|
||||
CustomerId uint `gorm:"not null;foreignKey:CustomerId;"`
|
||||
}
|
||||
|
||||
type Order2 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Description string
|
||||
// nullable - nullable
|
||||
CustomerId *uint `gorm:"null;foreignKey:CustomerId;"`
|
||||
}
|
||||
|
||||
type Order3 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
// nullable - unspecified
|
||||
Status *string
|
||||
Description string
|
||||
}
|
||||
|
||||
type Order4 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
// not nullable - unspecified
|
||||
Status *string
|
||||
Description string
|
||||
}
|
||||
29
tests/testdata/src/null_safety/positive.go
vendored
Normal file
29
tests/testdata/src/null_safety/positive.go
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
package null_safety
|
||||
|
||||
type Order5 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Description string
|
||||
// not nullable - nullable
|
||||
CustomerId uint `gorm:"null;foreignKey:CustomerId;"` // want "Null safety error in \"Order5\" model, field \"CustomerId\": column nullable policy doesn't match to tag nullable policy"
|
||||
}
|
||||
|
||||
type Order6 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Description string
|
||||
// nullable - not nullable
|
||||
CustomerId *uint `gorm:"not null;foreignKey:CustomerId;"` // want "Null safety error in \"Order6\" model, field \"CustomerId\": column nullable policy doesn't match to tag nullable policy"
|
||||
}
|
||||
|
||||
type Order7 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Description string
|
||||
// not nullable - not nullable, nullable
|
||||
CustomerId uint `gorm:"not null;foreignKey:CustomerId;null;"` // want "Null safety error: tags \"null\" and \"not null\" are specified at one field"
|
||||
}
|
||||
|
||||
type Order8 struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Description string
|
||||
// nullable - not nullable, nullable
|
||||
CustomerId *uint `gorm:"not null;foreignKey:CustomerId;null;"` // want "Null safety error: tags \"null\" and \"not null\" are specified at one field"
|
||||
}
|
||||
Reference in New Issue
Block a user