Skip to content

Commit c6400f0

Browse files
feat: cleanup tests
1 parent dfda82a commit c6400f0

File tree

3 files changed

+22
-52
lines changed

3 files changed

+22
-52
lines changed

connection_string_test.go

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,26 +177,22 @@ func TestReplaceDatabaseInConnectionStringErrorCases(t *testing.T) {
177177
dbName string
178178

179179
expected string
180-
}{
181-
{
182-
name: "malformed URL uses fallback",
183-
connStr: "postgres://user:pass@[invalid-ipv6:5432/postgres", // Invalid IPv6.
184-
dbName: "testdb",
185-
expected: "postgres://user:pass@[invalid-ipv6:5432/testdb", // Fallback replacement.
186-
},
187-
{
188-
name: "DSN without dbname falls through to fallback",
189-
connStr: "host=localhost user=postgres port=5432", // No dbname.
190-
dbName: "testdb",
191-
expected: "host=localhost user=postgres port=5432/testdb", // Fallback.
192-
},
193-
{
194-
name: "empty connection string",
195-
connStr: "",
196-
dbName: "testdb",
197-
expected: "/testdb", // Fallback behavior.
198-
},
199-
}
180+
}{{
181+
name: "malformed URL uses fallback",
182+
connStr: "postgres://user:pass@[invalid-ipv6:5432/postgres", // Invalid IPv6.
183+
dbName: "testdb",
184+
expected: "postgres://user:pass@[invalid-ipv6:5432/testdb", // Fallback replacement.
185+
}, {
186+
name: "DSN without dbname falls through to fallback",
187+
connStr: "host=localhost user=postgres port=5432", // No dbname.
188+
dbName: "testdb",
189+
expected: "host=localhost user=postgres port=5432/testdb", // Fallback.
190+
}, {
191+
name: "empty connection string",
192+
connStr: "",
193+
dbName: "testdb",
194+
expected: "/testdb", // Fallback behavior.
195+
}}
200196

201197
for _, test := range tests {
202198
c.Run(test.name, func(c *qt.C) {

mock_test.go

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (m *sharedMockDatabaseConnection) ExecContext(ctx context.Context, query st
3434
databases := m.provider.getDatabases()
3535
if databases[dbName] {
3636
mu.Unlock()
37-
return nil, fmt.Errorf("database \"%s\" already exists", dbName)
37+
return nil, fmt.Errorf("database %q already exists", dbName)
3838
}
3939
databases[dbName] = true
4040
mu.Unlock()
@@ -43,10 +43,10 @@ func (m *sharedMockDatabaseConnection) ExecContext(ctx context.Context, query st
4343
parts := strings.Fields(query)
4444
var dbName string
4545
if len(parts) >= 5 && parts[2] == "IF" && parts[3] == "EXISTS" {
46-
// Handle "DROP DATABASE IF EXISTS dbname"
46+
// Handle "DROP DATABASE IF EXISTS dbname".
4747
dbName = strings.Trim(parts[4], `"`)
4848
} else if len(parts) >= 3 {
49-
// Handle "DROP DATABASE dbname"
49+
// Handle "DROP DATABASE dbname".
5050
dbName = strings.Trim(parts[2], `"`)
5151
}
5252
if dbName != "" {
@@ -56,9 +56,9 @@ func (m *sharedMockDatabaseConnection) ExecContext(ctx context.Context, query st
5656
if databases[dbName] {
5757
delete(databases, dbName)
5858
} else if !(len(parts) >= 5 && parts[2] == "IF" && parts[3] == "EXISTS") {
59-
// Only error if it's not an "IF EXISTS" query
59+
// Only error if it's not an "IF EXISTS" query.
6060
mu.Unlock()
61-
return nil, fmt.Errorf("database \"%s\" does not exist", dbName)
61+
return nil, fmt.Errorf("database %q does not exist", dbName)
6262
}
6363
mu.Unlock()
6464
}
@@ -68,32 +68,6 @@ func (m *sharedMockDatabaseConnection) ExecContext(ctx context.Context, query st
6868

6969
// QueryRowContext implements pgdbtemplate.DatabaseConnection.QueryRowContext.
7070
func (m *sharedMockDatabaseConnection) QueryRowContext(ctx context.Context, query string, args ...any) pgdbtemplate.Row {
71-
if strings.Contains(query, "SELECT datname FROM pg_database WHERE NOT datistemplate") {
72-
var dbs []any
73-
mu := m.provider.getMutex()
74-
mu.RLock()
75-
databases := m.provider.getDatabases()
76-
for db, exists := range databases {
77-
if exists && db != "template0" && db != "template1" {
78-
dbs = append(dbs, db)
79-
}
80-
}
81-
mu.RUnlock()
82-
return &sharedMockRow{data: dbs}
83-
}
84-
if strings.Contains(query, "SELECT datname FROM pg_database WHERE datistemplate") {
85-
var dbs []any
86-
mu := m.provider.getMutex()
87-
mu.RLock()
88-
databases := m.provider.getDatabases()
89-
for db, exists := range databases {
90-
if exists && (db == "template0" || db == "template1") {
91-
dbs = append(dbs, db)
92-
}
93-
}
94-
mu.RUnlock()
95-
return &sharedMockRow{data: dbs}
96-
}
9771
if strings.Contains(query, "SELECT TRUE FROM pg_database WHERE datname") {
9872
// Handle both parameterized and literal queries
9973
if len(args) > 0 {

template_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ func TestTemplateManagerCleanupErrorPaths(t *testing.T) {
338338
err = tm.Cleanup(ctx)
339339
c.Assert(err, qt.ErrorMatches, "(?s).*drop error.*drop error.*")
340340

341-
// Note: Using mock provider, no real databases created - cleanup not needed
341+
// Note: Using mock provider, no real databases created - cleanup not needed.
342342
}
343343

344344
// TestTemplateManagerDropTestDatabaseErrorPaths tests error handling in DropTestDatabase

0 commit comments

Comments
 (0)