Skip to content

Commit

Permalink
Merge pull request #363 from rgeo/activerecord-tests
Browse files Browse the repository at this point in the history
Test Against ActiveRecord
  • Loading branch information
keithdoggett committed May 16, 2023
2 parents 69f89aa + c60c70b commit 8c3d9dd
Show file tree
Hide file tree
Showing 27 changed files with 1,374 additions and 988 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ Gemfile.lock
/travis/*.lock
/test/database_local.yml
.idea
debug.log
/test/db/*
23 changes: 23 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ Make sure the tests pass:

`bundle exec rake`

Run tests with a specific ActiveRecord version:

```sh
AR_VERSION=7.0.1 bundle install
AR_VERSION=7.0.1 bundle exec rake test
```

To run a specific test, use the `POSTGIS_TEST_FILES` environment variable:

`POSTGIS_TEST_FILES=test/cases/ddl_test.rb bundle exec rake`

If you are testing a feature against the ActiveRecord test suite run:

`bundle exec rake test:activerecord`

Files can be specified with the `AR_TEST_FILES` environment variable:

`AR_TEST_FILES=test/cases/adapters/postgresql/*_test.rb bundle exec rake test:activerecord`

To test with both local and ActiveRecord tests, run:

`bundle exec rake test:all`

Run tests against the test gemfiles:

run `rake appraisal` or run the tests manually:
Expand Down
33 changes: 33 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,36 @@ 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
return ENV["AR_VERSION"] if ENV["AR_VERSION"]

require "uri"
require "yaml"
require "net/http"

# read gemspec to get activerecord version spec
# pull all activerecord versions from rubygems
# find the newest version that matches the version from our gemspec
gs = Bundler.load_gemspec("activerecord-postgis-adapter.gemspec")
ar_dep = gs.dependencies.find { |d| d.name == "activerecord" }

uri = URI("https://rubygems.org/api/v1/versions/activerecord.yaml")
res = Net::HTTP.get_response(uri)
versions = YAML.safe_load(res.body)
ver = versions.find { |v| ar_dep.match?("activerecord", v["number"]) }

raise Bundler::GemNotFound, "No matching Activerecord version found for #{ar_dep.requirement}" unless ver

ver["number"]
end

# Need to install for tests
gem "rails", github: "rails/rails", tag: "v#{activerecord_version}"

group :development do
# Gems used by the ActiveRecord test suite
gem "bcrypt"
gem "mocha"
gem "sqlite3"
end
41 changes: 38 additions & 3 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
require "bundler/gem_tasks"
require "rake/testtask"
require_relative "test/rake_helper"

task default: [:test]
task test: "test:postgis"

Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.test_files = %w(test/**/*_test.rb)
Rake::TestTask.new(:test_postgis) do |t|
t.libs << postgis_test_load_paths
t.test_files = postgis_test_files
t.verbose = false
end

Rake::TestTask.new(:test_activerecord) do |t|
t.libs << postgis_test_load_paths
t.test_files = activerecord_test_files
t.verbose = false
end

Rake::TestTask.new(:test_all) do |t|
t.libs << postgis_test_load_paths
t.test_files = all_test_files
t.verbose = false
end

# We invoke the tests from here so we can add environment varaible(s)
# necessary for ActiveRecord tests. TestTask.new runs its block
# regardless of whether it has been invoked or not, so environment
# variables cannot be set in there if they're only needed for specific
# tests.
namespace :test do
task :postgis do
Rake::Task["test_postgis"].invoke
end

task :activerecord do
ENV["ARCONN"] = "postgis"
Rake::Task["test_activerecord"].invoke
end

task :all do
ENV["ARCONN"] = "postgis"
Rake::Task["test_all"].invoke
end
end
5 changes: 4 additions & 1 deletion activerecord-postgis-adapter.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require "./lib/active_record/connection_adapters/postgis/version"
require_relative "lib/active_record/connection_adapters/postgis/version"

Gem::Specification.new do |spec|
spec.name = "activerecord-postgis-adapter"
Expand All @@ -25,6 +25,9 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "minitest", "~> 5.4"
spec.add_development_dependency "mocha", "~> 1.1"
spec.add_development_dependency "benchmark-ips", "~> 2.9.1"
spec.add_development_dependency "rubocop", "~> 1.50"

spec.metadata = {
"rubygems_mfa_required" => "true"
}
Expand Down
20 changes: 20 additions & 0 deletions lib/active_record/connection_adapters/postgis/oid/date_time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module ActiveRecord
module ConnectionAdapters
module PostGIS
module OID
module DateTime
protected

# Uses PostGIS instead of PostgreSQLAdapter
def real_type_unless_aliased(real_type)
ActiveRecord::ConnectionAdapters::PostGISAdapter.datetime_type == real_type ? :datetime : real_type
end
end

PostgreSQL::OID::DateTime.prepend(DateTime)
end
end
end
end
1 change: 1 addition & 0 deletions lib/active_record/connection_adapters/postgis_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require "active_record/connection_adapters/postgis/spatial_column"
require "active_record/connection_adapters/postgis/arel_tosql"
require "active_record/connection_adapters/postgis/oid/spatial"
require "active_record/connection_adapters/postgis/oid/date_time"
require "active_record/connection_adapters/postgis/type" # has to be after oid/*
require "active_record/connection_adapters/postgis/create_connection"
# :startdoc:
Expand Down
120 changes: 0 additions & 120 deletions test/attributes_test.rb

This file was deleted.

0 comments on commit 8c3d9dd

Please sign in to comment.