@@ -15,45 +15,15 @@ import (
1515 "github.com/cybertec-postgresql/pg_timetable/internal/log"
1616 "github.com/cybertec-postgresql/pg_timetable/internal/pgengine"
1717 "github.com/cybertec-postgresql/pg_timetable/internal/scheduler"
18+ "github.com/cybertec-postgresql/pg_timetable/internal/testutils"
1819)
1920
20- // this instance used for all engine tests
21- var pge * pgengine.PgEngine
22-
23- var cmdOpts * config.CmdOptions = config .NewCmdOptions ("--clientname=pgengine_unit_test" , "--connstr=postgresql://scheduler:somestrong@localhost/timetable" )
24-
25- // SetupTestCaseEx allows to configure the test case before execution
26- func SetupTestCaseEx (t * testing.T , fc func (c * config.CmdOptions )) func (t * testing.T ) {
27- fc (cmdOpts )
28- return SetupTestCase (t )
29- }
30-
31- // SetupTestCase used to connect and to initialize test PostgreSQL database
32- func SetupTestCase (t * testing.T ) func (t * testing.T ) {
33- t .Helper ()
34- timeout := time .After (30 * time .Second )
35- done := make (chan bool )
36- go func () {
37- pge , _ = pgengine .New (context .Background (), * cmdOpts , log .Init (config.LoggingOpts {LogLevel : "error" }))
38- done <- true
39- }()
40- select {
41- case <- timeout :
42- t .Fatal ("Cannot connect and initialize test database in time" )
43- case <- done :
44- }
45- return func (t * testing.T ) {
46- _ , _ = pge .ConfigDb .Exec (context .Background (), "DROP SCHEMA IF EXISTS timetable CASCADE" )
47- pge .ConfigDb .Close ()
48- t .Log ("Test schema dropped" )
49- }
50- }
51-
5221func TestInitAndTestConfigDBConnection (t * testing.T ) {
53- teardownTestCase := SetupTestCase (t )
54- defer teardownTestCase ( t )
22+ container , cleanup := testutils . SetupPostgresContainer (t )
23+ defer cleanup ( )
5524
5625 ctx := context .Background ()
26+ pge := container .Engine
5727
5828 require .NotNil (t , pge .ConfigDb , "ConfigDB should be initialized" )
5929
@@ -105,8 +75,10 @@ func TestInitAndTestConfigDBConnection(t *testing.T) {
10575 t .Run ("Check connection closing" , func (t * testing.T ) {
10676 pge .Finalize ()
10777 assert .Nil (t , pge .ConfigDb , "Connection isn't closed properly" )
108- // reinit connection to execute teardown actions
109- pge , _ = pgengine .New (context .Background (), * cmdOpts , log .Init (config.LoggingOpts {LogLevel : "error" }))
78+ // reinit connection to execute teardown actions - create new container
79+ newContainer , newCleanup := testutils .SetupPostgresContainer (t )
80+ defer newCleanup ()
81+ pge = newContainer .Engine
11082 })
11183
11284}
@@ -120,10 +92,11 @@ func TestFailedConnect(t *testing.T) {
12092}
12193
12294func TestSchedulerFunctions (t * testing.T ) {
123- teardownTestCase := SetupTestCase (t )
124- defer teardownTestCase ( t )
95+ container , cleanup := testutils . SetupPostgresContainer (t )
96+ defer cleanup ( )
12597
12698 ctx := context .Background ()
99+ pge := container .Engine
127100
128101 t .Run ("Check DeleteChainConfig function" , func (t * testing.T ) {
129102 assert .Equal (t , false , pge .DeleteChain (ctx , 0 ), "Should not delete in clean database" )
@@ -180,10 +153,11 @@ func TestSchedulerFunctions(t *testing.T) {
180153}
181154
182155func TestGetRemoteDBTransaction (t * testing.T ) {
183- teardownTestCase := SetupTestCase (t )
184- defer teardownTestCase ( t )
156+ container , cleanup := testutils . SetupPostgresContainer (t )
157+ defer cleanup ( )
185158 ctx := context .Background ()
186- remoteDb , err := pge .GetRemoteDBConnection (context .Background (), cmdOpts .ConnStr )
159+ pge := container .Engine
160+ remoteDb , err := pge .GetRemoteDBConnection (context .Background (), container .ConnStr )
187161 defer pge .FinalizeDBConnection (ctx , remoteDb )
188162 require .NoError (t , err , "remoteDB should be initialized" )
189163 require .NotNil (t , remoteDb , "remoteDB should be initialized" )
@@ -196,12 +170,13 @@ func TestGetRemoteDBTransaction(t *testing.T) {
196170}
197171
198172func TestSamplesScripts (t * testing.T ) {
199- teardownTestCase := SetupTestCase (t )
200- defer teardownTestCase ( t )
173+ container , cleanup := testutils . SetupPostgresContainer (t )
174+ defer cleanup ( )
201175
202176 files , err := os .ReadDir ("../../samples" )
203177 assert .NoError (t , err , "Cannot read samples directory" )
204178 l := log .Init (config.LoggingOpts {LogLevel : "error" })
179+ pge := container .Engine
205180 for _ , f := range files {
206181 ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
207182 defer cancel ()
0 commit comments