Skip to content

Commit 65e344f

Browse files
j7nw4rclaude
andcommitted
test(postgrest): add comprehensive unit tests
Add 19 unit tests covering: - Default configuration values - Image name and tag - Port constant - All builder methods (postgres_connection, db_schemas, db_anon_role, jwt_secret, jwt_role_claim_key, openapi_mode, max_rows, pre_request, log_level, tag, env) - Builder method chaining - Image trait methods (expose_ports, ready_conditions) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 44b77f3 commit 65e344f

File tree

1 file changed

+181
-7
lines changed

1 file changed

+181
-7
lines changed

src/postgrest.rs

Lines changed: 181 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,7 @@ impl Default for PostgREST {
221221
env_vars.insert("PGRST_DB_ANON_ROLE".to_string(), "anon".to_string());
222222

223223
// Server configuration
224-
env_vars.insert(
225-
"PGRST_SERVER_PORT".to_string(),
226-
POSTGREST_PORT.to_string(),
227-
);
224+
env_vars.insert("PGRST_SERVER_PORT".to_string(), POSTGREST_PORT.to_string());
228225
env_vars.insert("PGRST_SERVER_HOST".to_string(), "0.0.0.0".to_string());
229226

230227
Self {
@@ -270,12 +267,13 @@ impl Image for PostgREST {
270267
#[cfg(feature = "postgrest")]
271268
mod tests {
272269
use super::*;
270+
use testcontainers_modules::testcontainers::Image;
273271

274272
#[test]
275-
fn test_postgrest_default() {
273+
fn test_default_configuration() {
276274
let postgrest = PostgREST::default();
277-
assert_eq!(postgrest.name(), NAME);
278-
assert_eq!(postgrest.tag(), TAG);
275+
276+
// Verify essential env vars are set
279277
assert_eq!(
280278
postgrest.env_vars.get("PGRST_DB_SCHEMAS"),
281279
Some(&"public".to_string())
@@ -294,8 +292,184 @@ mod tests {
294292
);
295293
}
296294

295+
#[test]
296+
fn test_name_returns_correct_image() {
297+
let postgrest = PostgREST::default();
298+
assert_eq!(postgrest.name(), "postgrest/postgrest");
299+
}
300+
301+
#[test]
302+
fn test_tag_returns_correct_version() {
303+
let postgrest = PostgREST::default();
304+
assert_eq!(postgrest.tag(), "v12.2.3");
305+
}
306+
297307
#[test]
298308
fn test_postgrest_port_constant() {
299309
assert_eq!(POSTGREST_PORT, 3000);
300310
}
311+
312+
#[test]
313+
fn test_with_postgres_connection() {
314+
let postgrest =
315+
PostgREST::default().with_postgres_connection("postgres://user:pass@localhost:5432/db");
316+
assert_eq!(
317+
postgrest.env_vars.get("PGRST_DB_URI"),
318+
Some(&"postgres://user:pass@localhost:5432/db".to_string())
319+
);
320+
}
321+
322+
#[test]
323+
fn test_with_db_schemas() {
324+
let postgrest = PostgREST::default().with_db_schemas("public,api,private");
325+
assert_eq!(
326+
postgrest.env_vars.get("PGRST_DB_SCHEMAS"),
327+
Some(&"public,api,private".to_string())
328+
);
329+
}
330+
331+
#[test]
332+
fn test_with_db_anon_role() {
333+
let postgrest = PostgREST::default().with_db_anon_role("web_anon");
334+
assert_eq!(
335+
postgrest.env_vars.get("PGRST_DB_ANON_ROLE"),
336+
Some(&"web_anon".to_string())
337+
);
338+
}
339+
340+
#[test]
341+
fn test_with_jwt_secret() {
342+
let postgrest = PostgREST::default().with_jwt_secret("my-super-secret-jwt-key");
343+
assert_eq!(
344+
postgrest.env_vars.get("PGRST_JWT_SECRET"),
345+
Some(&"my-super-secret-jwt-key".to_string())
346+
);
347+
}
348+
349+
#[test]
350+
fn test_with_jwt_role_claim_key() {
351+
let postgrest = PostgREST::default().with_jwt_role_claim_key(".app_metadata.role");
352+
assert_eq!(
353+
postgrest.env_vars.get("PGRST_JWT_ROLE_CLAIM_KEY"),
354+
Some(&".app_metadata.role".to_string())
355+
);
356+
}
357+
358+
#[test]
359+
fn test_with_openapi_mode() {
360+
let postgrest = PostgREST::default().with_openapi_mode("follow-privileges");
361+
assert_eq!(
362+
postgrest.env_vars.get("PGRST_OPENAPI_MODE"),
363+
Some(&"follow-privileges".to_string())
364+
);
365+
}
366+
367+
#[test]
368+
fn test_with_max_rows() {
369+
let postgrest = PostgREST::default().with_max_rows(1000);
370+
assert_eq!(
371+
postgrest.env_vars.get("PGRST_DB_MAX_ROWS"),
372+
Some(&"1000".to_string())
373+
);
374+
}
375+
376+
#[test]
377+
fn test_with_pre_request() {
378+
let postgrest = PostgREST::default().with_pre_request("auth.check_request");
379+
assert_eq!(
380+
postgrest.env_vars.get("PGRST_DB_PRE_REQUEST"),
381+
Some(&"auth.check_request".to_string())
382+
);
383+
}
384+
385+
#[test]
386+
fn test_with_log_level() {
387+
let postgrest = PostgREST::default().with_log_level("warn");
388+
assert_eq!(
389+
postgrest.env_vars.get("PGRST_LOG_LEVEL"),
390+
Some(&"warn".to_string())
391+
);
392+
}
393+
394+
#[test]
395+
fn test_with_tag_overrides_default() {
396+
let postgrest = PostgREST::default().with_tag("v11.0.0");
397+
assert_eq!(postgrest.tag(), "v11.0.0");
398+
}
399+
400+
#[test]
401+
fn test_with_env_adds_custom_variable() {
402+
let postgrest = PostgREST::default()
403+
.with_env("CUSTOM_VAR", "custom_value")
404+
.with_env("ANOTHER_VAR", "another_value");
405+
406+
assert_eq!(
407+
postgrest.env_vars.get("CUSTOM_VAR"),
408+
Some(&"custom_value".to_string())
409+
);
410+
assert_eq!(
411+
postgrest.env_vars.get("ANOTHER_VAR"),
412+
Some(&"another_value".to_string())
413+
);
414+
}
415+
416+
#[test]
417+
fn test_builder_method_chaining() {
418+
let postgrest = PostgREST::default()
419+
.with_postgres_connection("postgres://user:pass@localhost:5432/db")
420+
.with_db_schemas("api")
421+
.with_db_anon_role("web_anon")
422+
.with_jwt_secret("secret")
423+
.with_max_rows(500)
424+
.with_log_level("info")
425+
.with_tag("v11.0.0");
426+
427+
assert_eq!(
428+
postgrest.env_vars.get("PGRST_DB_URI"),
429+
Some(&"postgres://user:pass@localhost:5432/db".to_string())
430+
);
431+
assert_eq!(
432+
postgrest.env_vars.get("PGRST_DB_SCHEMAS"),
433+
Some(&"api".to_string())
434+
);
435+
assert_eq!(
436+
postgrest.env_vars.get("PGRST_DB_ANON_ROLE"),
437+
Some(&"web_anon".to_string())
438+
);
439+
assert_eq!(
440+
postgrest.env_vars.get("PGRST_JWT_SECRET"),
441+
Some(&"secret".to_string())
442+
);
443+
assert_eq!(
444+
postgrest.env_vars.get("PGRST_DB_MAX_ROWS"),
445+
Some(&"500".to_string())
446+
);
447+
assert_eq!(
448+
postgrest.env_vars.get("PGRST_LOG_LEVEL"),
449+
Some(&"info".to_string())
450+
);
451+
assert_eq!(postgrest.tag(), "v11.0.0");
452+
}
453+
454+
#[test]
455+
fn test_new_creates_default_instance() {
456+
let postgrest = PostgREST::new();
457+
assert_eq!(postgrest.name(), NAME);
458+
assert_eq!(postgrest.tag(), TAG);
459+
}
460+
461+
#[test]
462+
fn test_expose_ports() {
463+
let postgrest = PostgREST::default();
464+
let ports = postgrest.expose_ports();
465+
assert_eq!(ports.len(), 1);
466+
assert_eq!(ports[0], ContainerPort::Tcp(3000));
467+
}
468+
469+
#[test]
470+
fn test_ready_conditions() {
471+
let postgrest = PostgREST::default();
472+
let conditions = postgrest.ready_conditions();
473+
assert_eq!(conditions.len(), 1);
474+
}
301475
}

0 commit comments

Comments
 (0)