github.com/etecs-ru/gnomock@v0.13.2/preset/postgres/README.md (about) 1 # Gnomock Postgres 2 3 Gnomock Postgres is a [Gnomock](https://github.com/orlangure/gnomock) preset for running tests against a real Postgres 4 database, without mocks. 5 6 ```go 7 package postgres_test 8 9 import ( 10 "database/sql" 11 "fmt" 12 13 "github.com/orlangure/gnomock" 14 "github.com/orlangure/gnomock/preset/postgres" 15 ) 16 17 func ExamplePreset() { 18 queries := ` 19 create table t(a int); 20 insert into t (a) values (1); 21 insert into t (a) values (2); 22 ` 23 query := `insert into t (a) values (3);` 24 p := postgres.Preset( 25 postgres.WithUser("gnomock", "gnomick"), 26 postgres.WithDatabase("mydb"), 27 postgres.WithQueries(queries, query), 28 ) 29 30 container, err := gnomock.Start(p) 31 if err != nil { 32 panic(err) 33 } 34 35 defer func() { _ = gnomock.Stop(container) }() 36 37 connStr := fmt.Sprintf( 38 "host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", 39 container.Host, container.DefaultPort(), 40 "gnomock", "gnomick", "mydb", 41 ) 42 43 db, err := sql.Open("postgres", connStr) 44 if err != nil { 45 panic(err) 46 } 47 48 var max, avg, min, count float64 49 50 rows := db.QueryRow("select max(a), avg(a), min(a), count(a) from t") 51 52 err = rows.Scan(&max, &avg, &min, &count) 53 if err != nil { 54 panic("can't query the database: " + err.Error()) 55 } 56 57 fmt.Println("max", 3) 58 fmt.Println("avg", 2) 59 fmt.Println("min", 1) 60 fmt.Println("count", 3) 61 62 // Output: 63 // max 3 64 // avg 2 65 // min 1 66 // count 3 67 } 68 ```