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

Test Against ActiveRecord #363

Merged
merged 7 commits into from
May 16, 2023
Merged

Test Against ActiveRecord #363

merged 7 commits into from
May 16, 2023

Conversation

keithdoggett
Copy link
Member

@keithdoggett keithdoggett commented Mar 28, 2022

Fixes #358

Add functionality to test the PostGIS adapter against the full ActiveRecord test suite for Postgres.

  • Moved all tests to test/cases and namespaced them with module PostGIS to avoid potential conflicts.
  • Added a script to the Gemfile that pulls the matching Rails version to the ActiveRecord version specified in the gemspec so we can access the tests.
  • Added some methods to rake_helper that help with getting the libs and tests from ActiveRecord.
  • Changed test/database.yml to be in an ActiveRecord test compatible format.
  • Add TestTasks to the Rakefile that allow testing on just local PostGIS tests, ActiveRecord tests, or both. By default it is only local tests.
    • bundle exec rake test:postgis
    • bundle exec rake test:activerecord
    • bundle exec rake test:all

TODOs:

  • Figure out way to skip specific ActiveRecord tests if necessary
  • Open issue to request help with ActiveRecord compatibility
  • Have way to track ActiveRecord compatibility automatically?

Copy link
Member

@BuonOmo BuonOmo left a comment

Choose a reason for hiding this comment

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

Looks good overall, however I think using a test matrix would make a bit more sense, and there are some simplifications available I think :)

.gitignore Outdated Show resolved Hide resolved
CONTRIBUTING.md Outdated Show resolved Hide resolved
@@ -7,3 +7,34 @@ gem "pg", "~> 1.0", platform: :ruby
gem "activerecord-jdbcpostgresql-adapter", platform: :jruby
gem "ffi-geos", platform: :jruby
gem "byebug" if ENV["BYEBUG"]

def activerecord_version
Copy link
Member

Choose a reason for hiding this comment

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

This gets only one version, shouldn't we test against multiple rails versions ? For that, we could do as with ruby: take advantage of the test matrix in github ci.

In that case the version could simply be an env variable or a task argument

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, I can add a check for an environment variable as well.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not certain why having both is mandatory. Actually I think I am missing the point of this method :/

Copy link
Member Author

Choose a reason for hiding this comment

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

Gotcha. The point of this method is that for local development if we want to test against ActiveRecord, we need to add Rails to the bundle and fetch it from its git repo in order to access all of the tests. If we just use the ActiveRecord from Rubygems the test directory is not included, same with Rails from Rubygems.

So this method reads in our gemspec, finds what ActiveRecord version we've specified as a dependency, fetches all of the Rails releases from Rubygems, finds the highest matching version (i.e. ActiveRecord ~> 7.0.0 resolves to ActiveRecord=7.0.2.3 currently), and then pulls the corresponding Rails version from git since all releases are tagged on git as well.

Comment on lines +17 to +26
def load_postgis_specific_schema
original_stdout = $stdout
$stdout = StringIO.new

load "schema/postgis_specific_schema.rb"

ActiveRecord::FixtureSet.reset_cache
ensure
$stdout = original_stdout
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 this stdout stuff?

Copy link
Member Author

Choose a reason for hiding this comment

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

I just copied from this.

https://github.com/rails/rails/blob/86fd8d0143b1a0578b359f4b86fea94c718139ae/activerecord/test/cases/helper.rb#L194-L213

It's not a big deal if we don't include the stdout logic, but it outputs a lot of migration logs if we don't.

Comment on lines 17 to 26
def set_arconfig_env!
arconfig_file = File.expand_path(File.join(File.dirname(__FILE__), "database.yml"))
ENV["ARCONFIG"] = arconfig_file
end

config_load_paths!
set_arconfig_env!

def activerecord_test_files
if ENV["AR_TEST_FILES"]
Copy link
Member

Choose a reason for hiding this comment

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

why not a single TEST_FILES constant to have only one thing to remember ? :)

Copy link
Member Author

Choose a reason for hiding this comment

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

It's possible that we will have conflicting files which could be confusing for a user. For example test/cases/type_test.rb is defined in both postgis and ActiveRecord so it would run 2 test files if we only had one environment variable. Maybe it's not a big deal though.

test/rake_helper.rb Outdated Show resolved Hide resolved
Gemfile Outdated

group :development do
# Need to install for tests
gem "rails", git: "https://github.com/rails/rails.git", tag: "v#{activerecord_version}"
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
gem "rails", git: "https://github.com/rails/rails.git", tag: "v#{activerecord_version}"
gem "rails", github: "rails/rails", tag: "v#{activerecord_version}"

YAGNI but sexy ^^

Copy link
Member

@BuonOmo BuonOmo left a comment

Choose a reason for hiding this comment

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

LGTM!

@keithdoggett
Copy link
Member Author

Some ActiveRecord specific tests are still failing, but we can handle those in separate issues/PRs as most of the remaining failures will require us to override/re-write the original test from Rails.

@keithdoggett keithdoggett merged commit 8c3d9dd into master May 16, 2023
24 checks passed
@keithdoggett keithdoggett deleted the activerecord-tests branch May 16, 2023 12:48
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.

Test Against ActiveRecord
2 participants