How to run Rails 7 tests or RSpec without recreating test DBs

69 views Asked by At

Edited to reframe the question.

I have Rails 7.0 applications that query databases of other business applications. We have no control over the schemas, and have read-only access to these databases. Application uses several such databases, with prod and non-prod environments.

Local DB is used for applications' own housekeeping. It would be acceptable if that DB must be recreated. (Raw DDL probably required.)

One DB connection is established in the usual way by Rails. Additional connections are established like:

class ExtraDbBase < ApplicationRecord
  self.abstract_class = true
  establish_connection CONST_SET_BY_INITIALIZER_FROM_YAML
end

Will Rails tests attempt to recreate these secondary databases? We could craft seed data and get good test coverage if the primary database is the only one that is recreated.

The main functionality of the application:

  • analysis and reporting based on existing data from vendor applications
  • retrieve data from multiple databases and REST API endpoints
  • submit data to vendor system using REST API calls

By the nature of the application, integration tests should succeed with the data as it exists. In the final analysis, the application has to cope with data as it exists in the vendor's database.

I have investigated potential workarounds, including the possibility that Rails has a proper feature or configuration setting that would let us run tests without attempting to recreate test db schemas.

Would it be sufficient to comment out

ActiveRecord::Migration.maintain_test_schema!

in spec/rails_helper.rb?

Other posts suggest monkey-patches, or attempt to clear/bypass Rake tasks. These are old suggestions that smell bad.

Also looking into using RSpec through Pry, if rake can be avoided that way.

Is there a way to run Rails tests or use RSpec with specified test database(s), stipulating:

  • It would be acceptable if only the primary DB must be recreated.
  • We do not want to (and cannot) write to at least one of the databases used.
  • It is not necessary to reference all configured DBs to get valuable test coverage, as long as we can launch the test.
  • Meaningful test data based on vendor data model would be non-trivial to craft, high effort relative to its value during tests.
  • May not have access to same RDBMS as vendor system.
  • Would need to use db-specific DDL where features are not (well-)supported in ActiveRecord.
  • Need to use raw SQL or db-specific features in models for vendor DB.
0

There are 0 answers