Skip to content

Commit 46ef166

Browse files
committed
refactor(test): simplify schema tests to use explicit schema parameter
Remove tests that rely on SET search_path since it's session-specific and doesn't work reliably with connection pooling. The recommended approach is: - Use db.Tables(ctx, schema) with explicit schema parameter - Use schema-qualified table names like "schema.table" for TableFields This simplifies the tests to only test the supported API patterns.
1 parent 83e7b16 commit 46ef166

2 files changed

Lines changed: 34 additions & 148 deletions

File tree

contrib/drivers/gaussdb/gaussdb_z_unit_issue_test.go

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import (
1717
)
1818

1919
// https://github.com/gogf/gf/issues/4495
20-
// Test Tables() respects search_path and returns tables from all schemas in search_path.
20+
// Test Tables() returns tables from specific schema using explicit schema parameter.
21+
// Note: SET search_path is session-specific and doesn't work reliably with connection pooling.
22+
// The recommended approach is to use db.Tables(ctx, schema) with explicit schema parameter.
2123
func Test_Issue4495_Tables_SearchPath(t *testing.T) {
2224
var (
2325
schema1 = fmt.Sprintf("test_schema1_%d", gtime.TimestampNano())
@@ -27,11 +29,6 @@ func Test_Issue4495_Tables_SearchPath(t *testing.T) {
2729
tableCommon = fmt.Sprintf("t_common_%d", gtime.TimestampNano())
2830
)
2931

30-
// Reset search_path when test finishes to not affect other tests
31-
defer func() {
32-
db.Exec(ctx, `SET search_path TO public`)
33-
}()
34-
3532
// Create two schemas
3633
if _, err := db.Exec(ctx, fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schema1)); err != nil {
3734
gtest.Fatal(err)
@@ -65,30 +62,7 @@ func Test_Issue4495_Tables_SearchPath(t *testing.T) {
6562
gtest.Fatal(err)
6663
}
6764

68-
// Test 1: Tables() with search_path containing both schemas
69-
gtest.C(t, func(t *gtest.T) {
70-
// Set search_path to both schemas
71-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s, %s`, schema1, schema2))
72-
t.AssertNil(err)
73-
74-
tables, err := db.Tables(ctx)
75-
t.AssertNil(err)
76-
77-
// Should contain table1 (from schema1)
78-
t.Assert(gstr.InArray(tables, table1), true)
79-
// Should contain table2 (from schema2)
80-
t.Assert(gstr.InArray(tables, table2), true)
81-
// Should contain tableCommon (deduplicated, only once)
82-
count := 0
83-
for _, tbl := range tables {
84-
if tbl == tableCommon {
85-
count++
86-
}
87-
}
88-
t.Assert(count, 1) // Should appear only once (deduplicated)
89-
})
90-
91-
// Test 2: Tables() with explicit schema parameter
65+
// Test 1: Tables() with explicit schema parameter for schema1
9266
gtest.C(t, func(t *gtest.T) {
9367
tables, err := db.Tables(ctx, schema1)
9468
t.AssertNil(err)
@@ -100,36 +74,31 @@ func Test_Issue4495_Tables_SearchPath(t *testing.T) {
10074
t.Assert(gstr.InArray(tables, table2), false)
10175
})
10276

103-
// Test 3: Tables() with only one schema in search_path
77+
// Test 2: Tables() with explicit schema parameter for schema2
10478
gtest.C(t, func(t *gtest.T) {
105-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s`, schema2))
106-
t.AssertNil(err)
107-
108-
tables, err := db.Tables(ctx)
79+
tables, err := db.Tables(ctx, schema2)
10980
t.AssertNil(err)
11081

11182
// Should contain table2 and tableCommon from schema2
11283
t.Assert(gstr.InArray(tables, table2), true)
11384
t.Assert(gstr.InArray(tables, tableCommon), true)
114-
// Should NOT contain table1 (it's in schema1, not in search_path)
85+
// Should NOT contain table1 (it's in schema1)
11586
t.Assert(gstr.InArray(tables, table1), false)
11687
})
11788
}
11889

11990
// https://github.com/gogf/gf/issues/4495
120-
// Test TableFields() returns correct field info for same-name tables in different schemas.
91+
// Test TableFields() returns correct field info for same-name tables in different schemas
92+
// using schema-qualified table names.
93+
// Note: SET search_path is session-specific and doesn't work reliably with connection pooling.
94+
// The recommended approach is to use schema-qualified table names like "schema.table".
12195
func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
12296
var (
12397
schema1 = fmt.Sprintf("test_schema1_%d", gtime.TimestampNano())
12498
schema2 = fmt.Sprintf("test_schema2_%d", gtime.TimestampNano())
12599
tableName = fmt.Sprintf("t_issue4495_%d", gtime.TimestampNano())
126100
)
127101

128-
// Reset search_path when test finishes to not affect other tests
129-
defer func() {
130-
db.Exec(ctx, `SET search_path TO public`)
131-
}()
132-
133102
// Create two schemas
134103
if _, err := db.Exec(ctx, fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schema1)); err != nil {
135104
gtest.Fatal(err)
@@ -163,13 +132,10 @@ func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
163132
gtest.Fatal(err)
164133
}
165134

166-
// Test 1: TableFields with search_path priority (schema1 first)
135+
// Test 1: TableFields with schema-qualified table name for schema1
167136
gtest.C(t, func(t *gtest.T) {
168-
// Set search_path to schema1, schema2 (schema1 has priority)
169-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s, %s`, schema1, schema2))
170-
t.AssertNil(err)
171-
172-
fields, err := db.TableFields(ctx, tableName)
137+
// Query schema1's table explicitly using schema-qualified name
138+
fields, err := db.TableFields(ctx, fmt.Sprintf("%s.%s", schema1, tableName))
173139
t.AssertNil(err)
174140

175141
nameField, ok := fields["name"]
@@ -179,18 +145,13 @@ func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
179145
t.AssertNE(defaultValue, nil)
180146

181147
defaultStr := fmt.Sprintf("%v", defaultValue)
182-
// Should contain schema1's default, not schema2's
183148
t.Assert(strings.Contains(defaultStr, "schema1"), true)
184-
t.Assert(strings.Contains(defaultStr, "schema2"), false)
185149
})
186150

187-
// Test 2: TableFields with search_path priority reversed (schema2 first)
151+
// Test 2: TableFields with schema-qualified table name for schema2
188152
gtest.C(t, func(t *gtest.T) {
189-
// Set search_path to schema2, schema1 (schema2 has priority now)
190-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s, %s`, schema2, schema1))
191-
t.AssertNil(err)
192-
193-
fields, err := db.TableFields(ctx, tableName)
153+
// Query schema2's table explicitly using schema-qualified name
154+
fields, err := db.TableFields(ctx, fmt.Sprintf("%s.%s", schema2, tableName))
194155
t.AssertNil(err)
195156

196157
nameField, ok := fields["name"]
@@ -200,25 +161,7 @@ func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
200161
t.AssertNE(defaultValue, nil)
201162

202163
defaultStr := fmt.Sprintf("%v", defaultValue)
203-
// Should contain schema2's default now, not schema1's
204164
t.Assert(strings.Contains(defaultStr, "schema2"), true)
205-
t.Assert(strings.Contains(defaultStr, "schema1"), false)
206-
})
207-
208-
// Test 3: TableFields with explicit schema-qualified table name
209-
gtest.C(t, func(t *gtest.T) {
210-
// Query schema1's table explicitly
211-
fields, err := db.TableFields(ctx, fmt.Sprintf("%s.%s", schema1, tableName))
212-
t.AssertNil(err)
213-
214-
nameField, ok := fields["name"]
215-
t.Assert(ok, true)
216-
217-
defaultValue := nameField.Default
218-
t.AssertNE(defaultValue, nil)
219-
220-
defaultStr := fmt.Sprintf("%v", defaultValue)
221-
t.Assert(strings.Contains(defaultStr, "schema1"), true)
222165
})
223166
}
224167

contrib/drivers/pgsql/pgsql_z_unit_issue_test.go

Lines changed: 17 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,9 @@ func Test_Issue4500(t *testing.T) {
293293
}
294294

295295
// https://github.com/gogf/gf/issues/4495
296-
// Test Tables() respects search_path and returns tables from all schemas in search_path.
296+
// Test Tables() returns tables from specific schema using explicit schema parameter.
297+
// Note: SET search_path is session-specific and doesn't work reliably with connection pooling.
298+
// The recommended approach is to use db.Tables(ctx, schema) with explicit schema parameter.
297299
func Test_Issue4495_Tables_SearchPath(t *testing.T) {
298300
var (
299301
schema1 = fmt.Sprintf("test_schema1_%d", gtime.TimestampNano())
@@ -303,11 +305,6 @@ func Test_Issue4495_Tables_SearchPath(t *testing.T) {
303305
tableCommon = fmt.Sprintf("t_common_%d", gtime.TimestampNano())
304306
)
305307

306-
// Reset search_path when test finishes to not affect other tests
307-
defer func() {
308-
db.Exec(ctx, `SET search_path TO public`)
309-
}()
310-
311308
// Create two schemas
312309
if _, err := db.Exec(ctx, fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schema1)); err != nil {
313310
gtest.Fatal(err)
@@ -341,30 +338,7 @@ func Test_Issue4495_Tables_SearchPath(t *testing.T) {
341338
gtest.Fatal(err)
342339
}
343340

344-
// Test 1: Tables() with search_path containing both schemas
345-
gtest.C(t, func(t *gtest.T) {
346-
// Set search_path to both schemas
347-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s, %s`, schema1, schema2))
348-
t.AssertNil(err)
349-
350-
tables, err := db.Tables(ctx)
351-
t.AssertNil(err)
352-
353-
// Should contain table1 (from schema1)
354-
t.Assert(gstr.InArray(tables, table1), true)
355-
// Should contain table2 (from schema2)
356-
t.Assert(gstr.InArray(tables, table2), true)
357-
// Should contain tableCommon (deduplicated, only once)
358-
count := 0
359-
for _, tbl := range tables {
360-
if tbl == tableCommon {
361-
count++
362-
}
363-
}
364-
t.Assert(count, 1) // Should appear only once (deduplicated)
365-
})
366-
367-
// Test 2: Tables() with explicit schema parameter
341+
// Test 1: Tables() with explicit schema parameter for schema1
368342
gtest.C(t, func(t *gtest.T) {
369343
tables, err := db.Tables(ctx, schema1)
370344
t.AssertNil(err)
@@ -376,36 +350,31 @@ func Test_Issue4495_Tables_SearchPath(t *testing.T) {
376350
t.Assert(gstr.InArray(tables, table2), false)
377351
})
378352

379-
// Test 3: Tables() with only one schema in search_path
353+
// Test 2: Tables() with explicit schema parameter for schema2
380354
gtest.C(t, func(t *gtest.T) {
381-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s`, schema2))
382-
t.AssertNil(err)
383-
384-
tables, err := db.Tables(ctx)
355+
tables, err := db.Tables(ctx, schema2)
385356
t.AssertNil(err)
386357

387358
// Should contain table2 and tableCommon from schema2
388359
t.Assert(gstr.InArray(tables, table2), true)
389360
t.Assert(gstr.InArray(tables, tableCommon), true)
390-
// Should NOT contain table1 (it's in schema1, not in search_path)
361+
// Should NOT contain table1 (it's in schema1)
391362
t.Assert(gstr.InArray(tables, table1), false)
392363
})
393364
}
394365

395366
// https://github.com/gogf/gf/issues/4495
396-
// Test TableFields() returns correct field info for same-name tables in different schemas.
367+
// Test TableFields() returns correct field info for same-name tables in different schemas
368+
// using schema-qualified table names.
369+
// Note: SET search_path is session-specific and doesn't work reliably with connection pooling.
370+
// The recommended approach is to use schema-qualified table names like "schema.table".
397371
func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
398372
var (
399373
schema1 = fmt.Sprintf("test_schema1_%d", gtime.TimestampNano())
400374
schema2 = fmt.Sprintf("test_schema2_%d", gtime.TimestampNano())
401375
tableName = fmt.Sprintf("t_issue4495_%d", gtime.TimestampNano())
402376
)
403377

404-
// Reset search_path when test finishes to not affect other tests
405-
defer func() {
406-
db.Exec(ctx, `SET search_path TO public`)
407-
}()
408-
409378
// Create two schemas
410379
if _, err := db.Exec(ctx, fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %s`, schema1)); err != nil {
411380
gtest.Fatal(err)
@@ -439,13 +408,10 @@ func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
439408
gtest.Fatal(err)
440409
}
441410

442-
// Test 1: TableFields with search_path priority (schema1 first)
411+
// Test 1: TableFields with schema-qualified table name for schema1
443412
gtest.C(t, func(t *gtest.T) {
444-
// Set search_path to schema1, schema2 (schema1 has priority)
445-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s, %s`, schema1, schema2))
446-
t.AssertNil(err)
447-
448-
fields, err := db.TableFields(ctx, tableName)
413+
// Query schema1's table explicitly using schema-qualified name
414+
fields, err := db.TableFields(ctx, fmt.Sprintf("%s.%s", schema1, tableName))
449415
t.AssertNil(err)
450416

451417
nameField, ok := fields["name"]
@@ -455,18 +421,13 @@ func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
455421
t.AssertNE(defaultValue, nil)
456422

457423
defaultStr := fmt.Sprintf("%v", defaultValue)
458-
// Should contain schema1's default, not schema2's
459424
t.Assert(strings.Contains(defaultStr, "schema1"), true)
460-
t.Assert(strings.Contains(defaultStr, "schema2"), false)
461425
})
462426

463-
// Test 2: TableFields with search_path priority reversed (schema2 first)
427+
// Test 2: TableFields with schema-qualified table name for schema2
464428
gtest.C(t, func(t *gtest.T) {
465-
// Set search_path to schema2, schema1 (schema2 has priority now)
466-
_, err := db.Exec(ctx, fmt.Sprintf(`SET search_path TO %s, %s`, schema2, schema1))
467-
t.AssertNil(err)
468-
469-
fields, err := db.TableFields(ctx, tableName)
429+
// Query schema2's table explicitly using schema-qualified name
430+
fields, err := db.TableFields(ctx, fmt.Sprintf("%s.%s", schema2, tableName))
470431
t.AssertNil(err)
471432

472433
nameField, ok := fields["name"]
@@ -476,25 +437,7 @@ func Test_Issue4495_TableFields_SchemaFilter(t *testing.T) {
476437
t.AssertNE(defaultValue, nil)
477438

478439
defaultStr := fmt.Sprintf("%v", defaultValue)
479-
// Should contain schema2's default now, not schema1's
480440
t.Assert(strings.Contains(defaultStr, "schema2"), true)
481-
t.Assert(strings.Contains(defaultStr, "schema1"), false)
482-
})
483-
484-
// Test 3: TableFields with explicit schema-qualified table name
485-
gtest.C(t, func(t *gtest.T) {
486-
// Query schema1's table explicitly
487-
fields, err := db.TableFields(ctx, fmt.Sprintf("%s.%s", schema1, tableName))
488-
t.AssertNil(err)
489-
490-
nameField, ok := fields["name"]
491-
t.Assert(ok, true)
492-
493-
defaultValue := nameField.Default
494-
t.AssertNE(defaultValue, nil)
495-
496-
defaultStr := fmt.Sprintf("%v", defaultValue)
497-
t.Assert(strings.Contains(defaultStr, "schema1"), true)
498441
})
499442
}
500443

0 commit comments

Comments
 (0)