Skip to content

Commit

Permalink
Update GORM
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Nov 2, 2022
1 parent 4b41b42 commit 901baf2
Show file tree
Hide file tree
Showing 10 changed files with 806 additions and 367 deletions.
117 changes: 100 additions & 17 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gorm/utils/tests"
)

func TestFilterWhere(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "name", Args: []string{"val1"}}
db = filter.Where(db, "name = ?", "val1")
expected := map[string]clause.Clause{
Expand All @@ -28,7 +27,7 @@ func TestFilterWhere(t *testing.T) {
}

func TestFilterWhereOr(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "name", Args: []string{"val1"}, Or: true}
db = filter.Where(db, "name = ?", "val1")
expected := map[string]clause.Clause{
Expand All @@ -49,9 +48,10 @@ func TestFilterWhereOr(t *testing.T) {
}

func TestFilterScope(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "notacolumn", Args: []string{"val1"}, Operator: Operators["$eq"]}
schema := &schema.Schema{
DBNames: []string{"name"},
FieldsByDBName: map[string]*schema.Field{
"name": {Name: "Name", DBName: "name"},
},
Expand All @@ -64,7 +64,8 @@ func TestFilterScope(t *testing.T) {

filter.Field = "name"

db = db.Scopes(filter.Scope(&Settings{}, schema)).Find(nil)
results := []map[string]interface{}{}
db = db.Scopes(filter.Scope(&Settings{}, schema)).Find(results)
expected := map[string]clause.Clause{
"WHERE": {
Name: "WHERE",
Expand All @@ -81,6 +82,7 @@ func TestFilterScope(t *testing.T) {
func TestFilterScopeBlacklisted(t *testing.T) {
filter := &Filter{Field: "name", Args: []string{"val1"}, Operator: Operators["$eq"]}
schema := &schema.Schema{
DBNames: []string{"name"},
FieldsByDBName: map[string]*schema.Field{
"name": {Name: "Name"},
},
Expand Down Expand Up @@ -111,7 +113,7 @@ type FilterTestModel struct {
}

func TestFilterScopeWithJoin(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand All @@ -120,6 +122,7 @@ func TestFilterScopeWithJoin(t *testing.T) {
return
}

db.DryRun = true
db = db.Model(&results).Scopes(filter.Scope(&Settings{}, schema)).Find(&results)
expected := map[string]clause.Clause{
"WHERE": {
Expand Down Expand Up @@ -158,12 +161,22 @@ func TestFilterScopeWithJoin(t *testing.T) {
},
},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{
Columns: []clause.Column{
{Table: "filter_test_models", Name: "name"},
{Table: "filter_test_models", Name: "id"},
},
},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
assert.Nil(t, db.Error)
}

func TestFilterScopeWithJoinBlacklistedRelation(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand All @@ -184,7 +197,7 @@ func TestFilterScopeWithJoinBlacklistedRelation(t *testing.T) {
}

func TestFilterScopeWithJoinHasMany(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand All @@ -200,7 +213,7 @@ func TestFilterScopeWithJoinHasMany(t *testing.T) {
}

func TestFilterScopeWithJoinInvalidModel(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand All @@ -214,7 +227,7 @@ func TestFilterScopeWithJoinInvalidModel(t *testing.T) {
}

func TestFilterScopeWithJoinNestedRelation(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.NestedRelation.field", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand All @@ -236,6 +249,14 @@ func TestFilterScopeWithJoinNestedRelation(t *testing.T) {
},
},
},
"FROM": {
Name: "FROM",
Expression: clause.From{},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{},
},
}
assert.Equal(t, expected, conditionTx.Statement.Clauses)

Expand Down Expand Up @@ -290,12 +311,21 @@ func TestFilterScopeWithJoinNestedRelation(t *testing.T) {
},
},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{
Columns: []clause.Column{
{Table: "filter_test_models", Name: "name"},
{Table: "filter_test_models", Name: "id"},
},
},
},
}
assert.Equal(t, expected, joinTx.Statement.Clauses)
}

func TestFilterScopeWithJoinDontDuplicate(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
settings := &Settings{}
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}
filter2 := &Filter{Field: "Relation.id", Args: []string{"0"}, Operator: Operators["$gt"]}
Expand Down Expand Up @@ -348,12 +378,21 @@ func TestFilterScopeWithJoinDontDuplicate(t *testing.T) {
},
},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{
Columns: []clause.Column{
{Table: "filter_test_models", Name: "name"},
{Table: "filter_test_models", Name: "id"},
},
},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
}

func TestFilterScopeWithAlreadyExistingJoin(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand Down Expand Up @@ -409,13 +448,23 @@ func TestFilterScopeWithAlreadyExistingJoin(t *testing.T) {
},
},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{
Columns: []clause.Column{
{Raw: true, Name: "`Relation`.`name` AS `Relation__name`"},
{Raw: true, Name: "`Relation`.`id` AS `Relation__id`"},
{Raw: true, Name: "`Relation`.`parent_id` AS `Relation__parent_id`"},
},
},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
assert.Empty(t, db.Statement.Joins)
}

func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.name", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModel{}
Expand All @@ -441,12 +490,28 @@ func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
"FROM": {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{}, // The join is in Statement.Joins
Joins: []clause.Join{
{
Expression: clause.NamedExpr{
SQL: `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`,
Vars: []interface{}{0},
},
},
},
},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{
Columns: []clause.Column{
{Table: "filter_test_models", Name: "name"},
{Table: "filter_test_models", Name: "id"},
},
},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
assert.NotEmpty(t, db.Statement.Joins)
assert.Empty(t, db.Statement.Joins)
}

type FilterTestModelComputedRelation struct {
Expand All @@ -464,7 +529,7 @@ type FilterTestModelComputed struct {
}

func TestFilterScopeComputed(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "computed", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModelComputed{}
Expand All @@ -483,12 +548,20 @@ func TestFilterScopeComputed(t *testing.T) {
},
},
},
"FROM": {
Name: "FROM",
Expression: clause.From{},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
}

func TestFilterScopeComputedRelation(t *testing.T) {
db, _ := gorm.Open(&tests.DummyDialector{}, nil)
db := openDryRunDB(t)
filter := &Filter{Field: "Relation.computed", Args: []string{"val1"}, Operator: Operators["$eq"]}

results := []*FilterTestModelComputed{}
Expand Down Expand Up @@ -535,6 +608,16 @@ func TestFilterScopeComputedRelation(t *testing.T) {
},
},
},
"SELECT": {
Name: "SELECT",
Expression: clause.Select{
Columns: []clause.Column{
{Table: "filter_test_model_computeds", Name: "name"},
{Table: "filter_test_model_computeds", Name: "computed"}, // Should not be problematic that it is added automatically by Gorm since we force only selectable fields all he time.
{Table: "filter_test_model_computeds", Name: "id"},
},
},
},
}
assert.Equal(t, expected, db.Statement.Clauses)
}
12 changes: 7 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,20 @@ module goyave.dev/filter
go 1.17

require (
github.com/stretchr/testify v1.7.0
gorm.io/gorm v1.23.10
goyave.dev/goyave/v4 v4.2.0
github.com/stretchr/testify v1.8.1
gorm.io/driver/sqlite v1.4.3
gorm.io/gorm v1.24.1
goyave.dev/goyave/v4 v4.4.5
)

require (
github.com/Code-Hex/uniseg v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 901baf2

Please sign in to comment.