Skip to content

Commit

Permalink
Get back to 100% branch coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Dec 2, 2020
1 parent ef922c0 commit 21ab4ce
Show file tree
Hide file tree
Showing 18 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/sequel/dataset/prepared_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ def run(&block)
when :insert_pk
fetch_rows(prepared_sql){|r| return r.values.first}
when Array
# :nocov:
case prepared_type[0]
# :nocov:
when :map, :as_hash, :to_hash, :to_hash_groups
public_send(*prepared_type, &block)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/dataset/sql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,9 @@ def complex_expression_arg_pairs_append(sql, args, &block)
# operators unsupported by some databases. Used by adapters for databases
# that don't support the operators natively.
def complex_expression_emulate_append(sql, op, args)
# :nocov:
case op
# :nocov:
when :%
complex_expression_arg_pairs_append(sql, args){|a, b| Sequel.function(:MOD, a, b)}
when :>>
Expand Down
4 changes: 2 additions & 2 deletions lib/sequel/extensions/date_arithmetic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ def date_add_sql_append(sql, da)
end
when :mssql, :h2, :access, :sqlanywhere
units = case db_type
when :mssql, :sqlanywhere
MSSQL_DURATION_UNITS
when :h2
H2_DURATION_UNITS
when :access
ACCESS_DURATION_UNITS
else
MSSQL_DURATION_UNITS
end
each_valid_interval_unit(h, units) do |value, sql_unit|
expr = Sequel.function(:DATEADD, sql_unit, value, expr)
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/extensions/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def method_missing(method_sym, *args, &block)
# Allow calling private methods for backwards compatibility
@db.send(method_sym, *args, &block)
end
# :nocov:
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
# :nocov:

# This object responds to all methods the database responds to.
def respond_to_missing?(meth, include_private)
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/extensions/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def method_missing(method, *args, &block)
raise(Sequel::Error, "method #{method.inspect} did not return a dataset") unless @dataset.is_a?(Dataset)
self
end
# :nocov:
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
# :nocov:
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/sequel/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,9 @@ def plugin(plugin, *args, &block)

m.configure(self, *args, &block) if m.respond_to?(:configure)
end
# :nocov:
ruby2_keywords(:plugin) if respond_to?(:ruby2_keywords, true)
# :nocov:

# Returns primary key attribute hash. If using a composite primary key
# value such be an array with values for each primary key in the correct
Expand Down Expand Up @@ -757,7 +759,9 @@ def def_model_dataset_method(meth)
else
define_singleton_method(meth){|*args, &block| dataset.public_send(meth, *args, &block)}
end
# :nocov:
singleton_class.send(:ruby2_keywords, meth) if respond_to?(:ruby2_keywords, true)
# :nocov:
end

# Get the schema from the database, fall back on checking the columns
Expand Down
4 changes: 4 additions & 0 deletions lib/sequel/model/plugins.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ module Plugins
def self.def_dataset_methods(mod, meths)
Array(meths).each do |meth|
mod.class_eval("def #{meth}(*args, &block); dataset.#{meth}(*args, &block) end", __FILE__, __LINE__)
# :nocov:
mod.send(:ruby2_keywords, meth) if respond_to?(:ruby2_keywords, true)
# :nocov:
end
end

Expand Down Expand Up @@ -141,6 +143,8 @@ def self._define_sequel_method_arg_numbers(callable)
keyword = :required
when :key, :keyrest
keyword ||= true
else
raise Error, "invalid arg_type passed to _define_sequel_method_arg_numbers: #{arg_type}"
end
end
arity = callable.arity
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/plugins/association_proxies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ def method_missing(meth, *args, &block)
end
v.public_send(meth, *args, &block)
end
# :nocov:
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
# :nocov:
end

module ClassMethods
Expand Down
3 changes: 2 additions & 1 deletion lib/sequel/plugins/constraint_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ def constraint_validation_like_to_regexp(arg, case_insensitive)
'%'
when '%'
'.*'
when '_'
else
#when '_'
'.'
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/sequel/plugins/pg_array_associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,9 @@ module DatasetMethods
def many_to_pg_array_association_filter_expression(op, ref, obj)
pk = ref.qualify(model.table_name, ref.primary_key)
key = ref[:key]
# :nocov:
expr = case obj
# :nocov:
when Sequel::Model
if (assoc_pks = obj.get_column_value(key)) && !assoc_pks.empty?
Sequel[pk=>assoc_pks.to_a]
Expand All @@ -540,7 +542,9 @@ def many_to_pg_array_association_filter_expression(op, ref, obj)
# Support filtering by pg_array_to_many associations using a subquery.
def pg_array_to_many_association_filter_expression(op, ref, obj)
key = ref.qualify(model.table_name, ref[:key_column])
# :nocov:
expr = case obj
# :nocov:
when Sequel::Model
if pkv = obj.get_column_value(ref.primary_key_method)
Sequel.pg_array_op(key).contains(Sequel.pg_array([pkv], ref.array_type))
Expand Down
2 changes: 2 additions & 0 deletions lib/sequel/plugins/pg_auto_constraint_validations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def check_pg_constraint_error(ds)
messages = model.pg_auto_constraint_validations_messages

unless override
# :nocov:
case e
# :nocov:
when Sequel::NotNullConstraintViolation
if column = info[:column]
add_pg_constraint_validation_error([m.call(column)], messages[:not_null])
Expand Down
4 changes: 4 additions & 0 deletions spec/core/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2741,6 +2741,7 @@ def db.views(*) end
it "should handle converting many default formats" do
db = Sequel::Database.new
p = lambda{|d,t| db.send(:column_schema_to_ruby_default, d, t)}
p['any', :unknown].must_be_nil
p[nil, :integer].must_be_nil
p[1, :integer].must_equal 1
p['1', :integer].must_equal 1
Expand All @@ -2758,6 +2759,7 @@ def db.views(*) end
p['false', :boolean].must_equal false
p["'t'", :boolean].must_equal true
p["'f'", :boolean].must_equal false
p["'x'", :boolean].must_be_nil
p["'a'", :string].must_equal 'a'
p["'a'", :blob].must_equal Sequel.blob('a')
p["'a'", :blob].must_be_kind_of(Sequel::SQL::Blob)
Expand Down Expand Up @@ -2948,6 +2950,8 @@ def database_exception_sqlstate(exception, opts={})
proc{@db.get(:a)}.must_raise(Sequel::CheckConstraintViolation)
@db.sql_state = '40001'
proc{@db.get(:a)}.must_raise(Sequel::SerializationFailure)
@db.sql_state = '41245'
proc{@db.get(:a)}.must_raise(Sequel::DatabaseError)
def @db.database_specific_error_class_from_sqlstate(_) end
(@db.get(:a) rescue $!.class).must_equal(Sequel::DatabaseError)
@db.sql_state = nil
Expand Down
5 changes: 5 additions & 0 deletions spec/core/dataset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2892,6 +2892,11 @@ def supports_cte_in_subselect?; false end
@d.order(:c, :d).last.must_equal(:s=>'SELECT * FROM test ORDER BY c DESC, d DESC LIMIT 1')
@d.order(Sequel.desc(:e), :f).last.must_equal(:s=>'SELECT * FROM test ORDER BY e ASC, f DESC LIMIT 1')
end

it "should raise an error for multiple arguments" do
proc{@d.first(1, :z=>10)}.must_raise Sequel::Error
proc{@d.last(1, :z=>10)}.must_raise Sequel::Error
end
end

describe "Dataset #first!" do
Expand Down
5 changes: 5 additions & 0 deletions spec/extensions/columns_introspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
@db.sqls.length.must_equal 0
end

it "should handle LiteralStrings in FROM tables by issuing a query" do
@ds.from(Sequel.lit('x')).columns.must_equal []
@db.sqls.must_equal ["SELECT * FROM x LIMIT 1"]
end

it "should handle selecting * from a single subselect with no joins without a database query if the subselect's columns can be handled" do
@ds.select(:x).from_self.columns.must_equal [:x]
@db.sqls.length.must_equal 0
Expand Down
2 changes: 1 addition & 1 deletion spec/extensions/dataset_associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def @Track.name; 'Track' end
@Track.plugin :many_through_many
@Artist.plugin :pg_array_associations
@Tag.plugin :pg_array_associations
@Artist.one_to_many :albums, :class=>@Album
@Artist.one_to_many :albums, :class=>@Album, :dataset_associations_join=>true
@Artist.one_to_one :first_album, :class=>@Album
@Album.many_to_one :artist, :class=>@Artist
@Album.many_to_many :tags, :class=>@Tag
Expand Down
4 changes: 4 additions & 0 deletions spec/extensions/datetime_parse_to_time_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ def supports_timestamp_usecs?; false end
Sequel.database_timezone = :utc
Sequel.database_to_application_timestamp("2017-03-26 02:30:00").getutc.hour.must_equal 2
Sequel.database_to_application_timestamp("2017-03-12 02:30:00").getutc.hour.must_equal 2

Sequel.application_timezone = :utc
Sequel.database_to_application_timestamp("2017-03-26 02:30:00").getutc.hour.must_equal 2
Sequel.database_to_application_timestamp("2017-03-12 02:30:00").getutc.hour.must_equal 2
end

it "should handle an database timezone of :utc when literalizing values" do
Expand Down
6 changes: 6 additions & 0 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,12 @@ def supports_distinct_on?; true; end
@Album.filter(:b_tags=>@Tag.load(:id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT albums_tags.album_id FROM tags INNER JOIN albums_tags ON (albums_tags.tag_id = tags.id) WHERE ((name = 'B') AND (albums_tags.album_id IS NOT NULL) AND (tags.id = 3))))"
end

it "should not use an eager limit strategy if the strategy is not specified and the model defaults to not using one" do
@Album.default_eager_limit_strategy = nil
@Album.one_to_many :l_tracks2, :clone=>:l_tracks
@Album.filter(:l_tracks2=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id = 5))))"
end

it "should be able to filter on one_to_many associations with :limit" do
@Album.filter(:l_tracks=>@Track.load(:id=>5, :album_id=>3)).sql.must_equal "SELECT * FROM albums WHERE (albums.id IN (SELECT tracks.album_id FROM tracks WHERE ((tracks.album_id IS NOT NULL) AND (tracks.id IN (SELECT id FROM (SELECT tracks.id, row_number() OVER (PARTITION BY tracks.album_id) AS x_sequel_row_number_x FROM tracks) AS t1 WHERE (x_sequel_row_number_x <= 10))) AND (tracks.id = 5))))"
end
Expand Down
6 changes: 6 additions & 0 deletions spec/model/plugins_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,12 @@ def one; dataset.send(:cache_set, :foo, 1) end
proc{@scope.send(m1, 1)}.must_raise ArgumentError
end

it "should raise if an invalid arg type is used" do
o = Object.new
def o.parameters; [[:foo]] end
proc{Sequel::Plugins.send(:_define_sequel_method_arg_numbers, o)}.must_raise Sequel::Error
end

if RUBY_VERSION > '2.1'
it "should raise for required keyword arguments for expected_arity 0 or 1" do
proc{eval("Sequel::Plugins.def_sequel_method(@m, 'x', 0){|b:| [b, 1]}", binding)}.must_raise Sequel::Error
Expand Down

0 comments on commit 21ab4ce

Please sign in to comment.