Skip to content

Commit

Permalink
Add coverage testing
Browse files Browse the repository at this point in the history
Add a small spec suite for code not covered by Sequel's tests,
to get to 100% line/branch coverage of the Ruby code.  Add nocov
markers around code for older versions of Sequel.
  • Loading branch information
jeremyevans committed Jul 7, 2022
1 parent 0fac9b1 commit a481897
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
/tmp
/lib/*.so
*.gem
*.rbc
/coverage
13 changes: 10 additions & 3 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,16 @@ variables to specify the shared library and header directories.

== Running the specs

sequel_pg doesn't ship with it's own specs. It's designed to
replace a part of Sequel, so it just uses Sequel's specs.
Specifically, the spec_postgres rake task from Sequel.
sequel_pg is designed to replace a part of Sequel, so it shold be tested
using Sequel's specs (the spec_postgres rake task). There is a spec_cov
task that assumes you have Sequel checked out at ../sequel, and uses a
small spec suite for parts of sequel_pg not covered by Sequel's specs.
It sets the SEQUEL_PG_STREAM environment variable when running Sequel's
specs, make sure that spec/spec_config.rb in Sequel is set to connect
to PostgreSQL and use the following additional settings:

DB.extension(:pg_streaming)
DB.stream_all_queries = true

== Reporting issues/bugs

Expand Down
18 changes: 16 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require "rake"
require "rake/clean"

CLEAN.include %w'**.rbc rdoc'
CLEAN.include %w'**.rbc rdoc coverage'

desc "Do a full cleaning"
task :distclean do
Expand All @@ -19,3 +18,18 @@ begin
Rake::ExtensionTask.new('sequel_pg')
rescue LoadError
end

# This assumes you have sequel checked out in ../sequel, and that
# spec_postgres is setup to run Sequel's PostgreSQL specs.
desc "Run Sequel's tests with coverage"
task :spec_cov=>:compile do
ENV['RUBYLIB'] = "#{__dir__}/lib:#{ENV['RUBYLIB']}"
ENV['RUBYOPT'] = "-r #{__dir__}/spec/coverage_helper.rb #{ENV['RUBYOPT']}"
ENV['SIMPLECOV_COMMAND_NAME'] = "sequel_pg"
sh %'#{FileUtils::RUBY} -I ../sequel/lib spec/sequel_pg_spec.rb'

ENV['RUBYOPT'] = "-I lib -r sequel -r sequel/extensions/pg_array #{ENV['RUBYOPT']}"
ENV['SEQUEL_PG_STREAM'] = "1"
ENV['SIMPLECOV_COMMAND_NAME'] = "sequel"
sh %'cd ../sequel && #{FileUtils::RUBY} spec/adapter_spec.rb postgres'
end
6 changes: 6 additions & 0 deletions lib/sequel/extensions/pg_streaming.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
# :nocov:
unless Sequel::Postgres.respond_to?(:supports_streaming?)
raise LoadError, "either sequel_pg not loaded, or an old version of sequel_pg loaded"
end
unless Sequel::Postgres.supports_streaming?
raise LoadError, "streaming is not supported by the version of libpq in use"
end
# :nocov:

# Database methods necessary to support streaming. You should load this extension
# into your database object:
Expand Down Expand Up @@ -73,11 +75,13 @@ def send_prepared_statement(ps_name, args)

private

# :nocov:
unless Sequel::Postgres::Adapter.method_defined?(:send_query_params)
def send_query_params(*args)
send_query(*args)
end
end
# :nocov:

if Sequel::Database.instance_methods.map(&:to_s).include?('log_connection_yield')
# If using single row mode, send the query instead of executing it.
Expand All @@ -93,6 +97,7 @@ def execute_query(sql, args)
end
end
else
# :nocov:
def execute_query(sql, args)
if @single_row_mode
@single_row_mode = false
Expand All @@ -104,6 +109,7 @@ def execute_query(sql, args)
super
end
end
# :nocov:
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/sequel_pg/sequel_pg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,13 @@ def as_hash(key_column, value_column = nil, opts = Sequel::OPTS)
end
end

# :nocov:
unless Sequel::Dataset.method_defined?(:as_hash)
# Handle previous versions of Sequel that use to_hash instead of as_hash
alias to_hash as_hash
remove_method :as_hash
end
# :nocov:

# In the case where both arguments given, use an optimized version.
def to_hash_groups(key_column, value_column = nil, opts = Sequel::OPTS)
Expand Down Expand Up @@ -120,8 +122,10 @@ def optimize_model_load?
# pg_array extension previously loaded

class Sequel::Postgres::PGArray::Creator
# :nocov:
# Avoid method redefined verbose warning
alias call call if method_defined?(:call)
# :nocov:

# Override Creator to use sequel_pg's C-based parser instead of the pure ruby parser.
def call(string)
Expand Down
10 changes: 10 additions & 0 deletions spec/coverage_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'simplecov'

SimpleCov.start do
enable_coverage :branch
command_name ENV['SIMPLECOV_COMMAND_NAME']
root File.dirname(__dir__)
add_filter "/spec/"
add_group('Missing'){|src| src.covered_percent < 100}
add_group('Covered'){|src| src.covered_percent == 100}
end
41 changes: 41 additions & 0 deletions spec/sequel_pg_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
gem 'minitest'
ENV['MT_NO_PLUGINS'] = '1' # Work around stupid autoloading of plugins
require 'minitest/global_expectations/autorun'

require 'sequel/core'

Sequel.extension :pg_array
db = Sequel.connect(ENV['SEQUEL_PG_SPEC_URL'] || 'postgres:///?user=sequel_test')
db.extension :pg_streaming
Sequel::Deprecation.output = false

describe 'sequel_pg' do
it "should support deprecated optimized_model_load methods" do
db.optimize_model_load.must_equal true
db.optimize_model_load = false
db.optimize_model_load.must_equal true

ds = db.dataset
ds.optimize_model_load.must_equal true
proc{ds.optimize_model_load = false}.must_raise FrozenError
ds.with_optimize_model_load(false).optimize_model_load.must_equal false
end

it "should have working Sequel::Postgres::PGArray::Creator#call" do
Sequel::Postgres::PGArray::Creator.new('text').call('{1}').must_equal ["1"]
end

it "should raise for map with symbol and block" do
proc{db.dataset.map(:x){}}.must_raise Sequel::Error
end

it "should support paged_each with and without streaming" do
a = []
db.select(Sequel.as(1, :v)).paged_each{|row| a << row}
a.must_equal [{:v=>1}]

a = []
db.select(Sequel.as(1, :v)).stream.paged_each{|row| a << row}
a.must_equal [{:v=>1}]
end
end

0 comments on commit a481897

Please sign in to comment.