- Add github.com/jackc/pgx/v5 v5.9.1 dependency - Add golang-migrate pgx/v5 driver - Create migrations/postgres/0001_initial_schema.up.sql with SERIAL PRIMARY KEY - Create migrations/postgres/0001_initial_schema.down.sql - Rename RunMigrations to RunSQLiteMigrations in migrate.go - Add RunPostgresMigrations with pgxmigrate driver and 'pgx5' name - Update export_test.go to use RunSQLiteMigrations (go vet compliance)
47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package diunwebhook
|
|
|
|
import "database/sql"
|
|
|
|
// NewTestServer constructs a Server with a fresh in-memory SQLite database.
|
|
// Each call returns an isolated server -- tests do not share state.
|
|
func NewTestServer() (*Server, error) {
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := RunSQLiteMigrations(db); err != nil {
|
|
return nil, err
|
|
}
|
|
store := NewSQLiteStore(db)
|
|
return NewServer(store, ""), nil
|
|
}
|
|
|
|
// NewTestServerWithSecret constructs a Server with webhook authentication enabled.
|
|
func NewTestServerWithSecret(secret string) (*Server, error) {
|
|
db, err := sql.Open("sqlite", ":memory:")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := RunSQLiteMigrations(db); err != nil {
|
|
return nil, err
|
|
}
|
|
store := NewSQLiteStore(db)
|
|
return NewServer(store, secret), nil
|
|
}
|
|
|
|
// TestUpsertEvent calls UpsertEvent on the server's store (for test setup).
|
|
func (s *Server) TestUpsertEvent(event DiunEvent) error {
|
|
return s.store.UpsertEvent(event)
|
|
}
|
|
|
|
// TestGetUpdates calls GetUpdates on the server's store (for test assertions).
|
|
func (s *Server) TestGetUpdates() (map[string]UpdateEntry, error) {
|
|
return s.store.GetUpdates()
|
|
}
|
|
|
|
// TestGetUpdatesMap is a convenience wrapper that returns the map without error.
|
|
func (s *Server) TestGetUpdatesMap() map[string]UpdateEntry {
|
|
m, _ := s.store.GetUpdates()
|
|
return m
|
|
}
|