Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
System-Glitch committed Nov 13, 2024
1 parent 893449c commit de127ee
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 131 deletions.
66 changes: 34 additions & 32 deletions filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
Expand Down Expand Up @@ -449,36 +450,27 @@ func TestFilterScopeWithAlreadyExistingJoin(t *testing.T) {
"FROM": {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{
{
Type: clause.LeftJoin,
Table: clause.Table{
Name: "filter_test_relations",
Alias: "Relation",
},
ON: clause.Where{
Exprs: []clause.Expression{
clause.Eq{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "id",
},
Value: clause.Column{
Table: "Relation",
Name: "parent_id",
},
},
clause.Expr{SQL: "id > ?", Vars: []any{0}},
},
},
},
},
Joins: []clause.Join{},
},
},
}
assert.Equal(t, expected["FROM"], db.Statement.Clauses["FROM"])
assert.Equal(t, expected["WHERE"], db.Statement.Clauses["WHERE"])
assert.Empty(t, db.Statement.Joins)
require.Len(t, db.Statement.Joins, 1)
j := db.Statement.Joins[0]
assert.Equal(t, "Relation", j.Name)
assert.Equal(t, clause.LeftJoin, j.JoinType)
assert.Empty(t, j.Omits)
if assert.Len(t, j.Conds, 1) {
assert.Equal(t, clause.Where{Exprs: []clause.Expression{clause.Expr{SQL: "id > ?", Vars: []any{0}}}}, j.Conds[0].(*gorm.DB).Statement.Clauses["WHERE"].Expression)
}
assert.Empty(t, j.Selects)
expectedOn := &clause.Where{
Exprs: []clause.Expression{
clause.Expr{SQL: "id > ?", Vars: []any{0}},
},
}
assert.Equal(t, expectedOn, j.On)
}

func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
Expand Down Expand Up @@ -509,12 +501,12 @@ func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{
{
Expression: clause.NamedExpr{
SQL: `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`,
Vars: []any{0},
},
},
// {
// Expression: clause.NamedExpr{
// SQL: `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`,
// Vars: []any{0},
// },
// },
},
},
},
Expand All @@ -529,7 +521,17 @@ func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
},
}
assert.Equal(t, expected, db.Statement.Clauses)
assert.Empty(t, db.Statement.Joins)

require.Len(t, db.Statement.Joins, 1)
j := db.Statement.Joins[0]
assert.Equal(t, `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`, j.Name)
assert.Equal(t, clause.LeftJoin, j.JoinType)
assert.Empty(t, j.Omits)
assert.Empty(t, j.Selects)
if assert.Len(t, j.Conds, 1) {
assert.Equal(t, 0, j.Conds[0])
}
assert.Nil(t, j.On)
}

type FilterTestModelComputedRelation struct {
Expand Down
188 changes: 89 additions & 99 deletions settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -1510,7 +1511,7 @@ func TestSettingsSelectWithExistingJoin(t *testing.T) {
}),
PerPage: typeutil.NewUndefined(15),
}
db := openDryRunDB(t)
db := openDryRunDB(t).Debug()

// We manually join a relation with a condition.
// We expect this join to not be removed nor duplicated, with the condition kept and the fields selected.
Expand Down Expand Up @@ -1538,30 +1539,7 @@ func TestSettingsSelectWithExistingJoin(t *testing.T) {
"FROM": {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{
{
Type: clause.LeftJoin,
Table: clause.Table{
Name: "test_scope_relations",
Alias: "Relation",
},
ON: clause.Where{
Exprs: []clause.Expression{
clause.Eq{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "relation_id",
},
Value: clause.Column{
Table: "Relation",
Name: "id",
},
},
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
},
},
},
},
Joins: []clause.Join{},
},
},
"LIMIT": {
Expand All @@ -1587,7 +1565,21 @@ func TestSettingsSelectWithExistingJoin(t *testing.T) {
},
}
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
assert.Empty(t, paginator.DB.Statement.Joins)
require.Len(t, paginator.DB.Statement.Joins, 1)
j := paginator.DB.Statement.Joins[0]
assert.Equal(t, "Relation", j.Name)
assert.Equal(t, clause.LeftJoin, j.JoinType)
assert.Equal(t, []string{"*"}, j.Omits)
if assert.Len(t, j.Conds, 1) {
assert.Equal(t, clause.Where{Exprs: []clause.Expression{clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}}}}, j.Conds[0].(*gorm.DB).Statement.Clauses["WHERE"].Expression)
}
assert.Empty(t, j.Selects)
expectedOn := &clause.Where{
Exprs: []clause.Expression{
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
},
}
assert.Equal(t, expectedOn, j.On)
}

type TestScopeRelationWithComputed struct {
Expand Down Expand Up @@ -1638,29 +1630,7 @@ func TestSettingsSelectWithExistingJoinAndComputed(t *testing.T) {
"FROM": {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{
{
Type: clause.LeftJoin,
Table: clause.Table{
Name: "test_scope_relation_with_computeds",
Alias: "Relation",
},
ON: clause.Where{
Exprs: []clause.Expression{
clause.Eq{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "relation_id",
},
Value: clause.Column{
Table: "Relation",
Name: "id",
},
},
},
},
},
},
Joins: []clause.Join{},
},
},
"LIMIT": {
Expand All @@ -1687,7 +1657,26 @@ func TestSettingsSelectWithExistingJoinAndComputed(t *testing.T) {
},
}
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
assert.Empty(t, paginator.DB.Statement.Joins)
expectedSelect := []string{
"`Relation`.`a` `Relation__a`",
"`Relation`.`b` `Relation__b`",
"(UPPER(`Relation`.b)) `Relation__c`",
"`Relation`.`id` `Relation__id`",
"`test_scope_model_with_computeds`.`name`",
"`test_scope_model_with_computeds`.`email`",
"(UPPER(`test_scope_model_with_computeds`.name)) `computed`",
"`test_scope_model_with_computeds`.`id`",
"`test_scope_model_with_computeds`.`relation_id`",
}
assert.Equal(t, expectedSelect, paginator.DB.Statement.Selects)
require.Len(t, paginator.DB.Statement.Joins, 1)
j := paginator.DB.Statement.Joins[0]
assert.Equal(t, "Relation", j.Name)
assert.Equal(t, clause.LeftJoin, j.JoinType)
assert.Equal(t, []string{"*"}, j.Omits)
assert.Empty(t, j.Conds)
assert.Empty(t, j.Selects)
assert.Empty(t, j.On)
}

func TestSettingsSelectWithExistingJoinAndComputedOmit(t *testing.T) {
Expand Down Expand Up @@ -1724,29 +1713,7 @@ func TestSettingsSelectWithExistingJoinAndComputedOmit(t *testing.T) {
"FROM": {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{
{
Type: clause.LeftJoin,
Table: clause.Table{
Name: "test_scope_relation_with_computeds",
Alias: "Relation",
},
ON: clause.Where{
Exprs: []clause.Expression{
clause.Eq{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "relation_id",
},
Value: clause.Column{
Table: "Relation",
Name: "id",
},
},
},
},
},
},
Joins: []clause.Join{},
},
},
"LIMIT": {
Expand All @@ -1772,7 +1739,27 @@ func TestSettingsSelectWithExistingJoinAndComputedOmit(t *testing.T) {
},
}
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
assert.Empty(t, paginator.DB.Statement.Joins)
expectedSelect := []string{
"`Relation`.`a` `Relation__a`",
"`Relation`.`b` `Relation__b`",
"`Relation`.`id` `Relation__id`",
"`test_scope_model_with_computeds`.`name`",
"`test_scope_model_with_computeds`.`email`",
"(UPPER(`test_scope_model_with_computeds`.name)) `computed`",
"`test_scope_model_with_computeds`.`id`",
"`test_scope_model_with_computeds`.`relation_id`",
}
assert.Equal(t, expectedSelect, paginator.DB.Statement.Selects)
require.Len(t, paginator.DB.Statement.Joins, 1)
j := paginator.DB.Statement.Joins[0]
assert.Equal(t, "Relation", j.Name)
assert.Equal(t, clause.LeftJoin, j.JoinType)
assert.Equal(t, []string{"*"}, j.Omits)
if assert.Len(t, j.Conds, 1) {
assert.Equal(t, []string{"c"}, j.Conds[0].(*gorm.DB).Statement.Omits)
}
assert.Empty(t, j.Selects)
assert.Empty(t, j.On)
}

func TestSettingsSelectWithExistingJoinAndComputedWithoutFiltering(t *testing.T) {
Expand All @@ -1795,30 +1782,7 @@ func TestSettingsSelectWithExistingJoinAndComputedWithoutFiltering(t *testing.T)
"FROM": {
Name: "FROM",
Expression: clause.From{
Joins: []clause.Join{
{
Type: clause.LeftJoin,
Table: clause.Table{
Name: "test_scope_relation_with_computeds",
Alias: "Relation",
},
ON: clause.Where{
Exprs: []clause.Expression{
clause.Eq{
Column: clause.Column{
Table: clause.CurrentTable,
Name: "relation_id",
},
Value: clause.Column{
Table: "Relation",
Name: "id",
},
},
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
},
},
},
},
Joins: []clause.Join{},
},
},
"LIMIT": {
Expand All @@ -1845,7 +1809,33 @@ func TestSettingsSelectWithExistingJoinAndComputedWithoutFiltering(t *testing.T)
},
}
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
assert.Empty(t, paginator.DB.Statement.Joins)
expectedSelect := []string{
"`Relation`.`a` `Relation__a`",
"`Relation`.`b` `Relation__b`",
"(UPPER(`Relation`.b)) `Relation__c`",
"`Relation`.`id` `Relation__id`",
"`test_scope_model_with_computeds`.`name`",
"`test_scope_model_with_computeds`.`email`",
"(UPPER(`test_scope_model_with_computeds`.name)) `computed`",
"`test_scope_model_with_computeds`.`id`",
"`test_scope_model_with_computeds`.`relation_id`",
}
assert.Equal(t, expectedSelect, paginator.DB.Statement.Selects)
require.Len(t, paginator.DB.Statement.Joins, 1)
j := paginator.DB.Statement.Joins[0]
assert.Equal(t, "Relation", j.Name)
assert.Equal(t, clause.LeftJoin, j.JoinType)
assert.Equal(t, []string{"*"}, j.Omits)
if assert.Len(t, j.Conds, 1) {
assert.Equal(t, clause.Where{Exprs: []clause.Expression{clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}}}}, j.Conds[0].(*gorm.DB).Statement.Clauses["WHERE"].Expression)
}
assert.Empty(t, j.Selects)
expectedOn := &clause.Where{
Exprs: []clause.Expression{
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
},
}
assert.Equal(t, expectedOn, j.On)
}

func TestSettingsDefaultSort(t *testing.T) {
Expand Down

0 comments on commit de127ee

Please sign in to comment.