Fix postgres errors when running tests #245

Merged
moritz merged 1 commit from fix-test-postgres-errors into main 2025-12-03 14:51:09 +01:00
Collaborator

Description of the implemented changes

The changes were:

  • Bugfixing
  • New Feature
  • Breaking Change
  • Refactoring

By reusing postgres connections when running tests, we avoid running out of them.

What has been changed?

  1. Phoenix.Ecto.SQL.Sandbox Plug (lib/mv_web/endpoint.ex:42-45)
  if Application.compile_env(:mv, :sql_sandbox) do
    plug Phoenix.Ecto.SQL.Sandbox
  end

What it does:

  • This is a Plug that intercepts every HTTP request during tests
  • It reads sandbox metadata from the connection's private storage (:ecto_sandbox key)
  • When it finds this metadata (the owner PID), it calls Ecto.Adapters.SQL.Sandbox.allow/3
    to grant the request process and all processes it spawns (including LiveView processes)
    permission to use the same database connection as the test
  • This solves the problem where LiveView processes are started by the Phoenix LiveView
    supervisor (not linked to the test process), so they would normally try to checkout their
    own connections
  1. Configuration Flag (config/test.exs:49-50)

config :mv, :sql_sandbox, true

What it does:

  • Sets a compile-time configuration flag that enables the Phoenix.Ecto.SQL.Sandbox plug
    only in test environment
  • Using Application.compile_env/2 ensures this is checked at compile time for better
    performance
  • Prevents the sandbox plug from running in dev/production environments

Definition of Done

Code Quality

  • No new technical depths
  • Linting passed
  • Documentation is added were needed

Accessibility

  • New elements are properly defined with html-tags
  • Colour contrast follows WCAG criteria
  • Aria labels are added when needed
  • Everything is accessible by keyboard
  • Tab-Order is comprehensible
  • All interactive elements have a visible focus

Testing

  • Tests for new code are written
  • All tests pass
  • axe-core dev tools show no critical or major issues

Additional Notes

## Description of the implemented changes The changes were: - [ ] Bugfixing - [ ] New Feature - [ ] Breaking Change - [x] Refactoring By reusing postgres connections when running tests, we avoid running out of them. ## What has been changed? 1. Phoenix.Ecto.SQL.Sandbox Plug (lib/mv_web/endpoint.ex:42-45) ```elixir if Application.compile_env(:mv, :sql_sandbox) do plug Phoenix.Ecto.SQL.Sandbox end ``` What it does: - This is a Plug that intercepts every HTTP request during tests - It reads sandbox metadata from the connection's private storage (:ecto_sandbox key) - When it finds this metadata (the owner PID), it calls Ecto.Adapters.SQL.Sandbox.allow/3 to grant the request process and all processes it spawns (including LiveView processes) permission to use the same database connection as the test - This solves the problem where LiveView processes are started by the Phoenix LiveView supervisor (not linked to the test process), so they would normally try to checkout their own connections 2. Configuration Flag (config/test.exs:49-50) config :mv, :sql_sandbox, true What it does: - Sets a compile-time configuration flag that enables the Phoenix.Ecto.SQL.Sandbox plug only in test environment - Using Application.compile_env/2 ensures this is checked at compile time for better performance - Prevents the sandbox plug from running in dev/production environments ## Definition of Done ### Code Quality - [x] No new technical depths - [x] Linting passed - [ ] Documentation is added were needed ### Accessibility - [ ] New elements are properly defined with html-tags - [ ] Colour contrast follows WCAG criteria - [ ] Aria labels are added when needed - [ ] Everything is accessible by keyboard - [ ] Tab-Order is comprehensible - [ ] All interactive elements have a visible focus ### Testing - [ ] Tests for new code are written - [x] All tests pass - [ ] axe-core dev tools show no critical or major issues ## Additional Notes <!--- Add any additional information for the reviewers here -->
rafael self-assigned this 2025-12-03 12:35:11 +01:00
rafael added 1 commit 2025-12-03 12:35:13 +01:00
Fix postgres errors when running tests
All checks were successful
continuous-integration/drone/push Build is passing
ceb8da8eec
rafael added this to the Sprint 9: 20.11 - 11.12 project 2025-12-03 12:35:14 +01:00
requested review from moritz 2025-12-03 14:43:37 +01:00
moritz force-pushed fix-test-postgres-errors from ceb8da8eec to e9bcb25d20 2025-12-03 14:44:24 +01:00 Compare
moritz force-pushed fix-test-postgres-errors from e9bcb25d20 to dfff2486b5 2025-12-03 14:48:13 +01:00 Compare
moritz approved these changes 2025-12-03 14:50:15 +01:00
moritz merged commit 82f1a65b85 into main 2025-12-03 14:51:09 +01:00
moritz deleted branch fix-test-postgres-errors 2025-12-03 14:51:10 +01:00
Sign in to join this conversation.
No description provided.