feat: many2many relations check
This commit is contained in:
12
tests/relations_check_test.go
Normal file
12
tests/relations_check_test.go
Normal file
@@ -0,0 +1,12 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"golang.org/x/tools/go/analysis/analysistest"
|
||||
"gormlint/relationsCheck"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRelationsCheck(t *testing.T) {
|
||||
t.Parallel()
|
||||
analysistest.Run(t, analysistest.TestData(), relationsCheck.RelationsAnalyzer, "relations_check")
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package references_check
|
||||
|
||||
// TODO: add test with annotations on back-references
|
||||
|
||||
type WorkArea struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Workshop Workshop `gorm:"foreignKey:WorkshopId;references:Id;"`
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package references_check
|
||||
|
||||
// TODO: add test with annotations on back-references
|
||||
|
||||
type User struct {
|
||||
Name string
|
||||
CompanyID string
|
||||
|
||||
16
tests/testdata/src/relations_check/negative.go
vendored
Normal file
16
tests/testdata/src/relations_check/negative.go
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
package relations_check
|
||||
|
||||
type Library struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Books []*Book `gorm:"many2many:library_book;"`
|
||||
}
|
||||
|
||||
type Book struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Libraries []*Library `gorm:"many2many:library_book;"`
|
||||
}
|
||||
|
||||
type Employee struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Subordinates []*Employee `gorm:"many2many:employee_subordinates;"` // self-reference
|
||||
}
|
||||
21
tests/testdata/src/relations_check/positive.go
vendored
Normal file
21
tests/testdata/src/relations_check/positive.go
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
package relations_check
|
||||
|
||||
type Student struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Courses []Course `gorm:"many2many:student_courses;"`
|
||||
}
|
||||
|
||||
type Course struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Students []Course `gorm:"many2many:student_courses;"` // want "Invalid type `Course` in M2M relation \\(use \\[\\]\\*Student or self-reference\\)"
|
||||
}
|
||||
|
||||
type Author struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Articles []Article `gorm:"many2many:author_articles;"`
|
||||
}
|
||||
|
||||
type Article struct {
|
||||
Id uint `gorm:"primaryKey"`
|
||||
Authors Author `gorm:"many2many:author_articles;"` // want "M2M relation `author_articles` with bad type `Author` \\(should be a slice\\)"
|
||||
}
|
||||
Reference in New Issue
Block a user