Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
116 lines (96 loc) · 2.81 KB
/
Copy pathexample_test.go
File metadata and controls
116 lines (96 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package testdb_test
import (
"context"
"fmt"
"github.com/bashhack/testdb"
"github.com/bashhack/testdb/postgres"
)
// Example_new demonstrates using testdb.New with a custom initializer
// for full control over database initialization.
func Example_new() {
t := newExampleT()
defer t.done()
provider := &postgres.PostgresProvider{}
initializer := &postgres.PoolInitializer{}
db, err := testdb.New(t, provider, initializer,
testdb.WithMigrator(postgres.SQLFiles("./testdata/postgres/schema")),
testdb.WithDBPrefix("myapp_test"),
)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer func() {
if err := db.Close(); err != nil {
fmt.Printf("Failed to close database: %v\n", err)
}
}()
// DSN is always available
fmt.Println("Database created successfully")
if err := db.RunMigrations(context.Background()); err != nil {
fmt.Printf("Migration error: %v\n", err)
return
}
fmt.Println("Migrations completed")
// Output:
// Database created successfully
// Migrations completed
}
// Example_dsnOnly demonstrates using testdb to get just a DSN
// without automatic connection initialization.
func Example_dsnOnly() {
t := newExampleT()
defer t.done()
provider := &postgres.PostgresProvider{}
// Pass nil initializer to skip connection initialization
db, err := testdb.New(t, provider, nil,
testdb.WithMigrator(postgres.SQLFiles("./testdata/postgres/schema")),
)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer func() {
if err := db.Close(); err != nil {
fmt.Printf("Failed to close database: %v\n", err)
}
}()
// Use the DSN with your own connection logic
dsn := db.DSN
_ = dsn // Connect with your preferred client
fmt.Println("DSN created successfully")
// Output:
// DSN created successfully
}
// Example_customInitializer shows implementing a custom initializer
// for integration with ORMs or custom database types.
func Example_customInitializer() {
t := newExampleT()
defer t.done()
provider := &postgres.PostgresProvider{}
initializer := &exampleInitializer{}
db, err := testdb.New(t, provider, initializer,
testdb.WithMigrator(postgres.SQLFiles("./testdata/postgres/schema")),
)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer func() {
if err := db.Close(); err != nil {
fmt.Printf("Failed to close database: %v\n", err)
}
}()
// Type assert to your custom type
// myDB := db.Entity().(*myapp.DB)
fmt.Println("Custom initializer setup complete")
// Output:
// Custom initializer setup complete
}
// exampleInitializer is a custom initializer for demonstration.
type exampleInitializer struct{}
func (e *exampleInitializer) InitializeTestDatabase(ctx context.Context, dsn string) (any, error) {
// Your custom initialization logic here
// For example: return gorm.Open(postgres.Open(dsn), &gorm.Config{})
return nil, nil
}