MCPcopy
hub / github.com/go-gorm/gorm / TestComplexScopes

Function TestComplexScopes

tests/scopes_test.go:76–129  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

74}
75
76func TestComplexScopes(t *testing.T) {
77 tests := []struct {
78 name string
79 queryFn func(tx *gorm.DB) *gorm.DB
80 expected string
81 }{
82 {
83 name: "depth_1",
84 queryFn: func(tx *gorm.DB) *gorm.DB {
85 return tx.Scopes(
86 func(d *gorm.DB) *gorm.DB { return d.Where("a = 1") },
87 func(d *gorm.DB) *gorm.DB {
88 return d.Where(DB.Or("b = 2").Or("c = 3"))
89 },
90 ).Find(&Language{})
91 },
92 expected: `SELECT * FROM "languages" WHERE a = 1 AND (b = 2 OR c = 3)`,
93 }, {
94 name: "depth_1_pre_cond",
95 queryFn: func(tx *gorm.DB) *gorm.DB {
96 return tx.Where("z = 0").Scopes(
97 func(d *gorm.DB) *gorm.DB { return d.Where("a = 1") },
98 func(d *gorm.DB) *gorm.DB {
99 return d.Or(DB.Where("b = 2").Or("c = 3"))
100 },
101 ).Find(&Language{})
102 },
103 expected: `SELECT * FROM "languages" WHERE z = 0 AND a = 1 OR (b = 2 OR c = 3)`,
104 }, {
105 name: "depth_2",
106 queryFn: func(tx *gorm.DB) *gorm.DB {
107 return tx.Scopes(
108 func(d *gorm.DB) *gorm.DB { return d.Model(&Language{}) },
109 func(d *gorm.DB) *gorm.DB {
110 return d.
111 Or(DB.Scopes(
112 func(d *gorm.DB) *gorm.DB { return d.Where("a = 1") },
113 func(d *gorm.DB) *gorm.DB { return d.Where("b = 2") },
114 )).
115 Or("c = 3")
116 },
117 func(d *gorm.DB) *gorm.DB { return d.Where("d = 4") },
118 ).Find(&Language{})
119 },
120 expected: `SELECT * FROM "languages" WHERE d = 4 OR c = 3 OR (a = 1 AND b = 2)`,
121 },
122 }
123
124 for _, test := range tests {
125 t.Run(test.name, func(t *testing.T) {
126 assertEqualSQL(t, test.expected, DB.ToSQL(test.queryFn))
127 })
128 }
129}

Callers

nothing calls this directly

Calls 7

assertEqualSQLFunction · 0.85
ModelMethod · 0.80
ToSQLMethod · 0.80
FindMethod · 0.65
ScopesMethod · 0.65
WhereMethod · 0.65
OrMethod · 0.65

Tested by

no test coverage detected