Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional ActiveRecord config #2038

Closed
wants to merge 1 commit into from
Closed

Conversation

Jack12816
Copy link

- What is it good for

This PR ships a solution for #1876.

- What I did

I added a new option use_active_record to the RSpec config. This option is also added to the generator for new spec/rails_helper.rb files. The config option is read on the FixtureSupport module before the ActiveRecord loading happens. Tests with and without this setting were added in the case a database connection is not established.

- A picture of a cute animal (not mandatory but encouraged)

download

spec/rspec/rails/matchers/has_spec.rb Outdated Show resolved Hide resolved
lib/rspec/rails/fixture_support.rb Show resolved Hide resolved
next unless Kernel.const_defined? host
Connections.extended(Kernel.const_get(host))
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Author

@Jack12816 Jack12816 Oct 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the spec.

    context "without database available" do
       before { clear_active_record_connection }
       after { establish_active_record_connection }
       context "with use_active_record set to true" do

I wrote a spec which tests exactly this behavior.

establish_active_record_connection takes care of creating AR connection, but when it runs for the first time the models are not yet defined and later in the middle of the suite the connection is dropped which leads to the fact that the db tables are missing and need to be migrated again.

Maybe switching to a sqlite file will be a better option, then the initial migrations stay in place after dropping the connections.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be done in the specs necessary and not globally.

spec/support/ar_classes.rb Outdated Show resolved Hide resolved
@fables-tales
Copy link
Member

@Jack12816 any chance that you take a look at the travis build and get it to a passing state?

Thank you!

@mrgordon
Copy link

This is a workaround for ActiveStorage bugs. Ideally it should be fixed there instead of adding new code to rspec-rails but I'd support this to save people time if we don't think they'll fix it

@JonRowe
Copy link
Member

JonRowe commented Mar 26, 2019

@mrgordon If you'd like to use this as a base and fix it up I'm sure @Jack12816 won't mind, its gone rather stale. Please note the existing feedback still needs taking into account and a build passing.

@benoittgt
Copy link
Member

@mrgordon you said

This is a workaround for ActiveStorage bugs

Do you have more infos? Maybe it is fixed and we don't need this patch anymore? Especially with rspec-rails 4

@mrgordon
Copy link

I have no other info. Just seems logical that ActiveStorage should either not require ActiveRecord at all or should automatically require it if necessary so it functions. Instead there is an unstated (?) / unclear dependency that is only noticed if you remove ActiveRecord because things break.

@benoittgt
Copy link
Member

@mrgordon thanks

@Jack12816 Do you think you can rebase.

@Jack12816 Jack12816 force-pushed the master branch 6 times, most recently from 9cde8aa to 85e23f3 Compare May 24, 2019 12:18
@Jack12816
Copy link
Author

@benoittgt I've rebased the branch and did some changes to make it work with Rails 3.

spec/rspec/rails/fixture_support_spec.rb Outdated Show resolved Hide resolved
spec/rspec/rails/fixture_support_spec.rb Outdated Show resolved Hide resolved
@pirj
Copy link
Member

pirj commented Jan 8, 2020

@Jack12816 are you up to wrap this up?
Just in case, Rails 3 support has been dropped.

@pirj
Copy link
Member

pirj commented Feb 4, 2020

@Jack12816 a friendly reminder.

@Jack12816 Jack12816 force-pushed the master branch 3 times, most recently from e0d8a8b to 562940e Compare February 5, 2020 09:35
Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
@Jack12816
Copy link
Author

@pirj I've rebased it to upstream master and now all tests passing.

Copy link
Member

@JonRowe JonRowe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had actually started looking at this myself to pick up the slack and I'm not convinced this solution works as I'm not convinced the specs work. I'd like to see a test that fails without modifying the code, and without modifying the global specs.

next unless Kernel.const_defined? host
Connections.extended(Kernel.const_get(host))
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should only be done in the specs necessary and not globally.

end
end

establish_active_record_connection
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be removed, we don't want every other test having a database connection.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but it's not a subject of the PR. Actually all tests require a database connection due to the *Model classes at ar_classes.rb. When disabling AR support on the specs overall, everything is broken. Disabling the database connection on all tests sounds like a bigger refactoring.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually all tests require a database connection due to the *Model classes at ar_classes.rb. When disabling AR support on the specs overall, everything is broken.

Currently there is no database configured so I'm not sure thats true, if your changes cause things to fail your changes are at fault, applying a global config to the specs is not a valid fix.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently there is no database configured so I'm not sure thats true

https://github.com/rspec/rspec-rails/blob/master/spec/support/ar_classes.rb#L1-L19

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm ok, well make it fail with that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have in_sub_process helpers if you need to change something, but I don't want global configuration changes for just this spec, it shouldn't be necessary.


after { RSpec.configuration.use_active_record = true }

include_examples "unrelated example does not raise"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This spec does nothing to demonstrate that the support works when active record is available but not configured, you should be able to demonstrate the issue without adding database support as its supposed to be broken without database support.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I don't get your point. When use_active_record is set to true and no AR connection is available, it raises an ActiveRecord::ConnectionNotEstablished. When use_active_record is set to false and no AR connection is available it does not raise. Could you explain in detail what you have in mind for another test?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its artificial because you have configured a database then manually cleared it.

The default is no configuration, the changes adding config should be removed and these spec should demonstrate that requiring' active_record' causes an error, and that setting use_active_record false prevents it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I've been trying to find time to work on this myself because I believe strongly that adding database configuration for all the specs is not the way to go! :)

@JonRowe
Copy link
Member

JonRowe commented Mar 13, 2020

Thanks for your hard work getting this started, closing in favour of #2266, would you be able to check that works for you? Once green I can ship as a beta if you'd like, this is one of the last things we'd like included in 4.0.0 final.

@JonRowe JonRowe closed this Mar 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants