-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: for postgres and jenkins postgres setup
- Loading branch information
Showing
5 changed files
with
151 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package persistence | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
_ "github.com/jackc/pgx/v5/stdlib" // Blank import to register the postgres driver | ||
) | ||
|
||
// var dbUrlTemplate = "postgres://postgres@localhost:%s/%s?sslmode=disable" | ||
var dbUrlTemplate = "postgres://harshjain@localhost:%s/%s?sslmode=disable" | ||
|
||
func ResetDefaultTestPostgresDB(dropDBUrl string) error { | ||
db, err := sql.Open("postgres", dropDBUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
deletePrevConnectionsSql := ` | ||
SELECT pid, pg_terminate_backend(pid) | ||
FROM pg_stat_activity | ||
WHERE datname in ('template1', 'postgres') AND pid <> pg_backend_pid();` | ||
_, err = db.Exec(deletePrevConnectionsSql) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec("DROP DATABASE IF EXISTS postgres;") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = db.Exec("CREATE DATABASE postgres;") | ||
return err | ||
} | ||
|
||
func NewMockPgDB() *sql.DB { | ||
mockPgDBPort := os.Getenv("TEST_DB_PORT") | ||
|
||
// | ||
dropDBUrl := fmt.Sprintf(dbUrlTemplate, mockPgDBPort, "template1") | ||
fmt.Println(dropDBUrl) | ||
if err := ResetDefaultTestPostgresDB(dropDBUrl); err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
mockDBUrl := fmt.Sprintf(dbUrlTemplate, mockPgDBPort, "postgres") | ||
db, err := sql.Open("pgx", mockDBUrl) | ||
if err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
return db | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//go:build include_postgres_tests | ||
// +build include_postgres_tests | ||
|
||
package postgres | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/waku-org/go-waku/waku/persistence" | ||
) | ||
|
||
func TestQueries(t *testing.T) { | ||
db := persistence.NewMockPgDB() | ||
|
||
queries, err := NewQueries("test_queries", db) | ||
require.NoError(t, err) | ||
|
||
query := queries.Delete() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Exists() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Get() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Put() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Query() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Prefix() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Limit() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.Offset() | ||
require.NotEmpty(t, query) | ||
|
||
query = queries.GetSize() | ||
require.NotEmpty(t, query) | ||
} | ||
|
||
func TestCreateTable(t *testing.T) { | ||
db := persistence.NewMockPgDB() | ||
|
||
err := CreateTable(db, "test_create_table") | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters