@@ -49,5 +49,203 @@ describe(
4949
5050 await assert . rejects ( postgresMonitor . check ( monitor , heartbeat , { } ) , regex ) ;
5151 } ) ;
52+
53+ test ( "check() sets status to UP when custom query returns single value" , async ( ) => {
54+ // The default timeout of 30 seconds might not be enough for the container to start
55+ const postgresContainer = await new PostgreSqlContainer ( "postgres:latest" )
56+ . withStartupTimeout ( 60000 )
57+ . start ( ) ;
58+
59+ const postgresMonitor = new PostgresMonitorType ( ) ;
60+ const monitor = {
61+ databaseConnectionString : postgresContainer . getConnectionUri ( ) ,
62+ databaseQuery : "SELECT 42" ,
63+ conditions : "[]" ,
64+ } ;
65+
66+ const heartbeat = {
67+ msg : "" ,
68+ status : PENDING ,
69+ } ;
70+
71+ try {
72+ await postgresMonitor . check ( monitor , heartbeat , { } ) ;
73+ assert . strictEqual ( heartbeat . status , UP , `Expected status ${ UP } but got ${ heartbeat . status } ` ) ;
74+ } finally {
75+ await postgresContainer . stop ( ) ;
76+ }
77+ } ) ;
78+ test ( "check() sets status to UP when custom query result meets condition" , async ( ) => {
79+ const postgresContainer = await new PostgreSqlContainer ( "postgres:latest" )
80+ . withStartupTimeout ( 60000 )
81+ . start ( ) ;
82+
83+ const postgresMonitor = new PostgresMonitorType ( ) ;
84+ const monitor = {
85+ databaseConnectionString : postgresContainer . getConnectionUri ( ) ,
86+ databaseQuery : "SELECT 42 AS value" ,
87+ conditions : JSON . stringify ( [
88+ {
89+ type : "expression" ,
90+ andOr : "and" ,
91+ variable : "result" ,
92+ operator : "equals" ,
93+ value : "42" ,
94+ } ,
95+ ] ) ,
96+ } ;
97+
98+ const heartbeat = {
99+ msg : "" ,
100+ status : PENDING ,
101+ } ;
102+
103+ try {
104+ await postgresMonitor . check ( monitor , heartbeat , { } ) ;
105+ assert . strictEqual ( heartbeat . status , UP , `Expected status ${ UP } but got ${ heartbeat . status } ` ) ;
106+ } finally {
107+ await postgresContainer . stop ( ) ;
108+ }
109+ } ) ;
110+ test ( "check() rejects when custom query result does not meet condition" , async ( ) => {
111+ const postgresContainer = await new PostgreSqlContainer ( "postgres:latest" )
112+ . withStartupTimeout ( 60000 )
113+ . start ( ) ;
114+
115+ const postgresMonitor = new PostgresMonitorType ( ) ;
116+ const monitor = {
117+ databaseConnectionString : postgresContainer . getConnectionUri ( ) ,
118+ databaseQuery : "SELECT 99 AS value" ,
119+ conditions : JSON . stringify ( [
120+ {
121+ type : "expression" ,
122+ andOr : "and" ,
123+ variable : "result" ,
124+ operator : "equals" ,
125+ value : "42" ,
126+ } ,
127+ ] ) ,
128+ } ;
129+
130+ const heartbeat = {
131+ msg : "" ,
132+ status : PENDING ,
133+ } ;
134+
135+ try {
136+ await assert . rejects (
137+ postgresMonitor . check ( monitor , heartbeat , { } ) ,
138+ new Error ( "Query result did not meet the specified conditions (99)" )
139+ ) ;
140+ assert . strictEqual ( heartbeat . status , PENDING , `Expected status should not be ${ heartbeat . status } ` ) ;
141+ } finally {
142+ await postgresContainer . stop ( ) ;
143+ }
144+ } ) ;
145+ test ( "check() rejects when query returns no results with conditions" , async ( ) => {
146+ const postgresContainer = await new PostgreSqlContainer ( "postgres:latest" )
147+ . withStartupTimeout ( 60000 )
148+ . start ( ) ;
149+
150+ const postgresMonitor = new PostgresMonitorType ( ) ;
151+ const monitor = {
152+ databaseConnectionString : postgresContainer . getConnectionUri ( ) ,
153+ databaseQuery : "SELECT 1 WHERE 1 = 0" ,
154+ conditions : JSON . stringify ( [
155+ {
156+ type : "expression" ,
157+ andOr : "and" ,
158+ variable : "result" ,
159+ operator : "equals" ,
160+ value : "1" ,
161+ } ,
162+ ] ) ,
163+ } ;
164+
165+ const heartbeat = {
166+ msg : "" ,
167+ status : PENDING ,
168+ } ;
169+
170+ try {
171+ await assert . rejects (
172+ postgresMonitor . check ( monitor , heartbeat , { } ) ,
173+ new Error ( "Database connection/query failed: Query returned no results" )
174+ ) ;
175+ assert . strictEqual ( heartbeat . status , PENDING , `Expected status should not be ${ heartbeat . status } ` ) ;
176+ } finally {
177+ await postgresContainer . stop ( ) ;
178+ }
179+ } ) ;
180+ test ( "check() rejects when query returns multiple rows with conditions" , async ( ) => {
181+ const postgresContainer = await new PostgreSqlContainer ( "postgres:latest" )
182+ . withStartupTimeout ( 60000 )
183+ . start ( ) ;
184+
185+ const postgresMonitor = new PostgresMonitorType ( ) ;
186+ const monitor = {
187+ databaseConnectionString : postgresContainer . getConnectionUri ( ) ,
188+ databaseQuery : "SELECT 1 UNION ALL SELECT 2" ,
189+ conditions : JSON . stringify ( [
190+ {
191+ type : "expression" ,
192+ andOr : "and" ,
193+ variable : "result" ,
194+ operator : "equals" ,
195+ value : "1" ,
196+ } ,
197+ ] ) ,
198+ } ;
199+
200+ const heartbeat = {
201+ msg : "" ,
202+ status : PENDING ,
203+ } ;
204+
205+ try {
206+ await assert . rejects (
207+ postgresMonitor . check ( monitor , heartbeat , { } ) ,
208+ new Error ( "Database connection/query failed: Multiple values were found, expected only one value" )
209+ ) ;
210+ assert . strictEqual ( heartbeat . status , PENDING , `Expected status should not be ${ heartbeat . status } ` ) ;
211+ } finally {
212+ await postgresContainer . stop ( ) ;
213+ }
214+ } ) ;
215+ test ( "check() rejects when query returns multiple columns with conditions" , async ( ) => {
216+ const postgresContainer = await new PostgreSqlContainer ( "postgres:latest" )
217+ . withStartupTimeout ( 60000 )
218+ . start ( ) ;
219+
220+ const postgresMonitor = new PostgresMonitorType ( ) ;
221+ const monitor = {
222+ databaseConnectionString : postgresContainer . getConnectionUri ( ) ,
223+ databaseQuery : "SELECT 1 AS col1, 2 AS col2" ,
224+ conditions : JSON . stringify ( [
225+ {
226+ type : "expression" ,
227+ andOr : "and" ,
228+ variable : "result" ,
229+ operator : "equals" ,
230+ value : "1" ,
231+ } ,
232+ ] ) ,
233+ } ;
234+
235+ const heartbeat = {
236+ msg : "" ,
237+ status : PENDING ,
238+ } ;
239+
240+ try {
241+ await assert . rejects (
242+ postgresMonitor . check ( monitor , heartbeat , { } ) ,
243+ new Error ( "Database connection/query failed: Multiple columns were found, expected only one value" )
244+ ) ;
245+ assert . strictEqual ( heartbeat . status , PENDING , `Expected status should not be ${ heartbeat . status } ` ) ;
246+ } finally {
247+ await postgresContainer . stop ( ) ;
248+ }
249+ } ) ;
52250 }
53251) ;
0 commit comments