Skip to content

Commit de127ee

Browse files
committed
Fix tests
1 parent 893449c commit de127ee

File tree

2 files changed

+123
-131
lines changed

2 files changed

+123
-131
lines changed

filter_test.go

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"testing"
55

66
"github.com/stretchr/testify/assert"
7+
"github.com/stretchr/testify/require"
78
"gorm.io/gorm"
89
"gorm.io/gorm/clause"
910
"gorm.io/gorm/schema"
@@ -449,36 +450,27 @@ func TestFilterScopeWithAlreadyExistingJoin(t *testing.T) {
449450
"FROM": {
450451
Name: "FROM",
451452
Expression: clause.From{
452-
Joins: []clause.Join{
453-
{
454-
Type: clause.LeftJoin,
455-
Table: clause.Table{
456-
Name: "filter_test_relations",
457-
Alias: "Relation",
458-
},
459-
ON: clause.Where{
460-
Exprs: []clause.Expression{
461-
clause.Eq{
462-
Column: clause.Column{
463-
Table: clause.CurrentTable,
464-
Name: "id",
465-
},
466-
Value: clause.Column{
467-
Table: "Relation",
468-
Name: "parent_id",
469-
},
470-
},
471-
clause.Expr{SQL: "id > ?", Vars: []any{0}},
472-
},
473-
},
474-
},
475-
},
453+
Joins: []clause.Join{},
476454
},
477455
},
478456
}
479457
assert.Equal(t, expected["FROM"], db.Statement.Clauses["FROM"])
480458
assert.Equal(t, expected["WHERE"], db.Statement.Clauses["WHERE"])
481-
assert.Empty(t, db.Statement.Joins)
459+
require.Len(t, db.Statement.Joins, 1)
460+
j := db.Statement.Joins[0]
461+
assert.Equal(t, "Relation", j.Name)
462+
assert.Equal(t, clause.LeftJoin, j.JoinType)
463+
assert.Empty(t, j.Omits)
464+
if assert.Len(t, j.Conds, 1) {
465+
assert.Equal(t, clause.Where{Exprs: []clause.Expression{clause.Expr{SQL: "id > ?", Vars: []any{0}}}}, j.Conds[0].(*gorm.DB).Statement.Clauses["WHERE"].Expression)
466+
}
467+
assert.Empty(t, j.Selects)
468+
expectedOn := &clause.Where{
469+
Exprs: []clause.Expression{
470+
clause.Expr{SQL: "id > ?", Vars: []any{0}},
471+
},
472+
}
473+
assert.Equal(t, expectedOn, j.On)
482474
}
483475

484476
func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
@@ -509,12 +501,12 @@ func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
509501
Name: "FROM",
510502
Expression: clause.From{
511503
Joins: []clause.Join{
512-
{
513-
Expression: clause.NamedExpr{
514-
SQL: `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`,
515-
Vars: []any{0},
516-
},
517-
},
504+
// {
505+
// Expression: clause.NamedExpr{
506+
// SQL: `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`,
507+
// Vars: []any{0},
508+
// },
509+
// },
518510
},
519511
},
520512
},
@@ -529,7 +521,17 @@ func TestFilterScopeWithAlreadyExistingRawJoin(t *testing.T) {
529521
},
530522
}
531523
assert.Equal(t, expected, db.Statement.Clauses)
532-
assert.Empty(t, db.Statement.Joins)
524+
525+
require.Len(t, db.Statement.Joins, 1)
526+
j := db.Statement.Joins[0]
527+
assert.Equal(t, `LEFT JOIN filter_test_relations AS "Relation" ON id > ?`, j.Name)
528+
assert.Equal(t, clause.LeftJoin, j.JoinType)
529+
assert.Empty(t, j.Omits)
530+
assert.Empty(t, j.Selects)
531+
if assert.Len(t, j.Conds, 1) {
532+
assert.Equal(t, 0, j.Conds[0])
533+
}
534+
assert.Nil(t, j.On)
533535
}
534536

535537
type FilterTestModelComputedRelation struct {

settings_test.go

Lines changed: 89 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77

88
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
910
"gorm.io/driver/sqlite"
1011
"gorm.io/gorm"
1112
"gorm.io/gorm/clause"
@@ -1510,7 +1511,7 @@ func TestSettingsSelectWithExistingJoin(t *testing.T) {
15101511
}),
15111512
PerPage: typeutil.NewUndefined(15),
15121513
}
1513-
db := openDryRunDB(t)
1514+
db := openDryRunDB(t).Debug()
15141515

15151516
// We manually join a relation with a condition.
15161517
// We expect this join to not be removed nor duplicated, with the condition kept and the fields selected.
@@ -1538,30 +1539,7 @@ func TestSettingsSelectWithExistingJoin(t *testing.T) {
15381539
"FROM": {
15391540
Name: "FROM",
15401541
Expression: clause.From{
1541-
Joins: []clause.Join{
1542-
{
1543-
Type: clause.LeftJoin,
1544-
Table: clause.Table{
1545-
Name: "test_scope_relations",
1546-
Alias: "Relation",
1547-
},
1548-
ON: clause.Where{
1549-
Exprs: []clause.Expression{
1550-
clause.Eq{
1551-
Column: clause.Column{
1552-
Table: clause.CurrentTable,
1553-
Name: "relation_id",
1554-
},
1555-
Value: clause.Column{
1556-
Table: "Relation",
1557-
Name: "id",
1558-
},
1559-
},
1560-
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
1561-
},
1562-
},
1563-
},
1564-
},
1542+
Joins: []clause.Join{},
15651543
},
15661544
},
15671545
"LIMIT": {
@@ -1587,7 +1565,21 @@ func TestSettingsSelectWithExistingJoin(t *testing.T) {
15871565
},
15881566
}
15891567
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
1590-
assert.Empty(t, paginator.DB.Statement.Joins)
1568+
require.Len(t, paginator.DB.Statement.Joins, 1)
1569+
j := paginator.DB.Statement.Joins[0]
1570+
assert.Equal(t, "Relation", j.Name)
1571+
assert.Equal(t, clause.LeftJoin, j.JoinType)
1572+
assert.Equal(t, []string{"*"}, j.Omits)
1573+
if assert.Len(t, j.Conds, 1) {
1574+
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)
1575+
}
1576+
assert.Empty(t, j.Selects)
1577+
expectedOn := &clause.Where{
1578+
Exprs: []clause.Expression{
1579+
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
1580+
},
1581+
}
1582+
assert.Equal(t, expectedOn, j.On)
15911583
}
15921584

15931585
type TestScopeRelationWithComputed struct {
@@ -1638,29 +1630,7 @@ func TestSettingsSelectWithExistingJoinAndComputed(t *testing.T) {
16381630
"FROM": {
16391631
Name: "FROM",
16401632
Expression: clause.From{
1641-
Joins: []clause.Join{
1642-
{
1643-
Type: clause.LeftJoin,
1644-
Table: clause.Table{
1645-
Name: "test_scope_relation_with_computeds",
1646-
Alias: "Relation",
1647-
},
1648-
ON: clause.Where{
1649-
Exprs: []clause.Expression{
1650-
clause.Eq{
1651-
Column: clause.Column{
1652-
Table: clause.CurrentTable,
1653-
Name: "relation_id",
1654-
},
1655-
Value: clause.Column{
1656-
Table: "Relation",
1657-
Name: "id",
1658-
},
1659-
},
1660-
},
1661-
},
1662-
},
1663-
},
1633+
Joins: []clause.Join{},
16641634
},
16651635
},
16661636
"LIMIT": {
@@ -1687,7 +1657,26 @@ func TestSettingsSelectWithExistingJoinAndComputed(t *testing.T) {
16871657
},
16881658
}
16891659
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
1690-
assert.Empty(t, paginator.DB.Statement.Joins)
1660+
expectedSelect := []string{
1661+
"`Relation`.`a` `Relation__a`",
1662+
"`Relation`.`b` `Relation__b`",
1663+
"(UPPER(`Relation`.b)) `Relation__c`",
1664+
"`Relation`.`id` `Relation__id`",
1665+
"`test_scope_model_with_computeds`.`name`",
1666+
"`test_scope_model_with_computeds`.`email`",
1667+
"(UPPER(`test_scope_model_with_computeds`.name)) `computed`",
1668+
"`test_scope_model_with_computeds`.`id`",
1669+
"`test_scope_model_with_computeds`.`relation_id`",
1670+
}
1671+
assert.Equal(t, expectedSelect, paginator.DB.Statement.Selects)
1672+
require.Len(t, paginator.DB.Statement.Joins, 1)
1673+
j := paginator.DB.Statement.Joins[0]
1674+
assert.Equal(t, "Relation", j.Name)
1675+
assert.Equal(t, clause.LeftJoin, j.JoinType)
1676+
assert.Equal(t, []string{"*"}, j.Omits)
1677+
assert.Empty(t, j.Conds)
1678+
assert.Empty(t, j.Selects)
1679+
assert.Empty(t, j.On)
16911680
}
16921681

16931682
func TestSettingsSelectWithExistingJoinAndComputedOmit(t *testing.T) {
@@ -1724,29 +1713,7 @@ func TestSettingsSelectWithExistingJoinAndComputedOmit(t *testing.T) {
17241713
"FROM": {
17251714
Name: "FROM",
17261715
Expression: clause.From{
1727-
Joins: []clause.Join{
1728-
{
1729-
Type: clause.LeftJoin,
1730-
Table: clause.Table{
1731-
Name: "test_scope_relation_with_computeds",
1732-
Alias: "Relation",
1733-
},
1734-
ON: clause.Where{
1735-
Exprs: []clause.Expression{
1736-
clause.Eq{
1737-
Column: clause.Column{
1738-
Table: clause.CurrentTable,
1739-
Name: "relation_id",
1740-
},
1741-
Value: clause.Column{
1742-
Table: "Relation",
1743-
Name: "id",
1744-
},
1745-
},
1746-
},
1747-
},
1748-
},
1749-
},
1716+
Joins: []clause.Join{},
17501717
},
17511718
},
17521719
"LIMIT": {
@@ -1772,7 +1739,27 @@ func TestSettingsSelectWithExistingJoinAndComputedOmit(t *testing.T) {
17721739
},
17731740
}
17741741
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
1775-
assert.Empty(t, paginator.DB.Statement.Joins)
1742+
expectedSelect := []string{
1743+
"`Relation`.`a` `Relation__a`",
1744+
"`Relation`.`b` `Relation__b`",
1745+
"`Relation`.`id` `Relation__id`",
1746+
"`test_scope_model_with_computeds`.`name`",
1747+
"`test_scope_model_with_computeds`.`email`",
1748+
"(UPPER(`test_scope_model_with_computeds`.name)) `computed`",
1749+
"`test_scope_model_with_computeds`.`id`",
1750+
"`test_scope_model_with_computeds`.`relation_id`",
1751+
}
1752+
assert.Equal(t, expectedSelect, paginator.DB.Statement.Selects)
1753+
require.Len(t, paginator.DB.Statement.Joins, 1)
1754+
j := paginator.DB.Statement.Joins[0]
1755+
assert.Equal(t, "Relation", j.Name)
1756+
assert.Equal(t, clause.LeftJoin, j.JoinType)
1757+
assert.Equal(t, []string{"*"}, j.Omits)
1758+
if assert.Len(t, j.Conds, 1) {
1759+
assert.Equal(t, []string{"c"}, j.Conds[0].(*gorm.DB).Statement.Omits)
1760+
}
1761+
assert.Empty(t, j.Selects)
1762+
assert.Empty(t, j.On)
17761763
}
17771764

17781765
func TestSettingsSelectWithExistingJoinAndComputedWithoutFiltering(t *testing.T) {
@@ -1795,30 +1782,7 @@ func TestSettingsSelectWithExistingJoinAndComputedWithoutFiltering(t *testing.T)
17951782
"FROM": {
17961783
Name: "FROM",
17971784
Expression: clause.From{
1798-
Joins: []clause.Join{
1799-
{
1800-
Type: clause.LeftJoin,
1801-
Table: clause.Table{
1802-
Name: "test_scope_relation_with_computeds",
1803-
Alias: "Relation",
1804-
},
1805-
ON: clause.Where{
1806-
Exprs: []clause.Expression{
1807-
clause.Eq{
1808-
Column: clause.Column{
1809-
Table: clause.CurrentTable,
1810-
Name: "relation_id",
1811-
},
1812-
Value: clause.Column{
1813-
Table: "Relation",
1814-
Name: "id",
1815-
},
1816-
},
1817-
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
1818-
},
1819-
},
1820-
},
1821-
},
1785+
Joins: []clause.Join{},
18221786
},
18231787
},
18241788
"LIMIT": {
@@ -1845,7 +1809,33 @@ func TestSettingsSelectWithExistingJoinAndComputedWithoutFiltering(t *testing.T)
18451809
},
18461810
}
18471811
assert.Equal(t, expected, paginator.DB.Statement.Clauses)
1848-
assert.Empty(t, paginator.DB.Statement.Joins)
1812+
expectedSelect := []string{
1813+
"`Relation`.`a` `Relation__a`",
1814+
"`Relation`.`b` `Relation__b`",
1815+
"(UPPER(`Relation`.b)) `Relation__c`",
1816+
"`Relation`.`id` `Relation__id`",
1817+
"`test_scope_model_with_computeds`.`name`",
1818+
"`test_scope_model_with_computeds`.`email`",
1819+
"(UPPER(`test_scope_model_with_computeds`.name)) `computed`",
1820+
"`test_scope_model_with_computeds`.`id`",
1821+
"`test_scope_model_with_computeds`.`relation_id`",
1822+
}
1823+
assert.Equal(t, expectedSelect, paginator.DB.Statement.Selects)
1824+
require.Len(t, paginator.DB.Statement.Joins, 1)
1825+
j := paginator.DB.Statement.Joins[0]
1826+
assert.Equal(t, "Relation", j.Name)
1827+
assert.Equal(t, clause.LeftJoin, j.JoinType)
1828+
assert.Equal(t, []string{"*"}, j.Omits)
1829+
if assert.Len(t, j.Conds, 1) {
1830+
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)
1831+
}
1832+
assert.Empty(t, j.Selects)
1833+
expectedOn := &clause.Where{
1834+
Exprs: []clause.Expression{
1835+
clause.Expr{SQL: "Relation.id > ?", Vars: []any{0}},
1836+
},
1837+
}
1838+
assert.Equal(t, expectedOn, j.On)
18491839
}
18501840

18511841
func TestSettingsDefaultSort(t *testing.T) {

0 commit comments

Comments
 (0)