...

Source file src/github.com/cybertec-postgresql/pgwatch/v3/internal/sources/resolver_test.go

Documentation: github.com/cybertec-postgresql/pgwatch/v3/internal/sources

     1  package sources_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
    11  	testcontainers "github.com/testcontainers/testcontainers-go"
    12  	"github.com/testcontainers/testcontainers-go/modules/postgres"
    13  	"github.com/testcontainers/testcontainers-go/wait"
    14  )
    15  
    16  func TestMonitoredDatabase_ResolveDatabasesFromPostgres(t *testing.T) {
    17  	pgContainer, err := postgres.Run(ctx,
    18  		ImageName,
    19  		postgres.WithDatabase("mydatabase"),
    20  		testcontainers.WithWaitStrategy(
    21  			wait.ForLog("database system is ready to accept connections").
    22  				WithOccurrence(2).
    23  				WithStartupTimeout(5*time.Second)),
    24  	)
    25  	require.NoError(t, err)
    26  	defer func() { assert.NoError(t, pgContainer.Terminate(ctx)) }()
    27  
    28  	// Create a new MonitoredDatabase instance
    29  	md := sources.Source{}
    30  	md.Name = "continuous"
    31  	md.Kind = sources.SourcePostgresContinuous
    32  	md.ConnStr, err = pgContainer.ConnectionString(ctx)
    33  	assert.NoError(t, err)
    34  
    35  	// Call the ResolveDatabasesFromPostgres method
    36  	dbs, err := sources.ResolveDatabasesFromPostgres(md)
    37  	assert.NoError(t, err)
    38  	assert.True(t, len(dbs) == 2) //postgres and mydatabase
    39  
    40  	// check the "continuous_mydatabase"
    41  	db := dbs.GetMonitoredDatabase(md.Name + "_mydatabase")
    42  	assert.NotNil(t, db)
    43  	assert.Equal(t, "mydatabase", db.GetDatabaseName())
    44  
    45  	//check unexpected database
    46  	db = dbs.GetMonitoredDatabase(md.Name + "_unexpected")
    47  	assert.Nil(t, db)
    48  }
    49