diff --git a/lib/rom/plugins/relation/sql/auto_restrictions.rb b/lib/rom/plugins/relation/sql/auto_restrictions.rb index f90cb89d4..6a764fa8b 100644 --- a/lib/rom/plugins/relation/sql/auto_restrictions.rb +++ b/lib/rom/plugins/relation/sql/auto_restrictions.rb @@ -48,7 +48,7 @@ def self.restriction_methods(schema) next if index.partial? attributes = index.to_a - meth_name = :"by_#{ attributes.map(&:name).join("_and_") }" + meth_name = :"by_#{attributes.map(&:name).join("_and_")}" next if generated.include?(meth_name) diff --git a/lib/rom/plugins/relation/sql/default_views.rb b/lib/rom/plugins/relation/sql/default_views.rb index 4226b9615..618d7ff80 100644 --- a/lib/rom/plugins/relation/sql/default_views.rb +++ b/lib/rom/plugins/relation/sql/default_views.rb @@ -29,8 +29,8 @@ def self.define_default_views!(target, schema) target.class_eval <<-RUBY, __FILE__, __LINE__ + 1 undef :by_pk if method_defined?(:by_pk) - def by_pk(#{schema.primary_key.map(&:name).join(', ')}) - where(#{schema.primary_key.map { |attr| "schema.canonical[:#{attr.name}] => #{attr.name}" }.join(', ')}) + def by_pk(#{schema.primary_key.map(&:name).join(", ")}) + where(#{schema.primary_key.map { |attr| "schema.canonical[:#{attr.name}] => #{attr.name}" }.join(", ")}) end RUBY else diff --git a/lib/rom/plugins/relation/sql/postgres/explain.rb b/lib/rom/plugins/relation/sql/postgres/explain.rb index 63789d87c..69e263996 100644 --- a/lib/rom/plugins/relation/sql/postgres/explain.rb +++ b/lib/rom/plugins/relation/sql/postgres/explain.rb @@ -32,7 +32,7 @@ def explain(format: :text, **options) query = "EXPLAIN (#{explain_value}) #{dataset.sql}" - rows = dataset.with_sql(query).map(:'QUERY PLAN') + rows = dataset.with_sql(query).map(:"QUERY PLAN") case format when :json diff --git a/lib/rom/plugins/relation/sql/postgres/full_text_search.rb b/lib/rom/plugins/relation/sql/postgres/full_text_search.rb index b0734a90d..db4cee783 100644 --- a/lib/rom/plugins/relation/sql/postgres/full_text_search.rb +++ b/lib/rom/plugins/relation/sql/postgres/full_text_search.rb @@ -13,23 +13,24 @@ module FullTextSearch # By default, searching for the inclusion of any of the terms in any of the cols. # # @example - # posts.full_text_search([:title, :content], 'apples', language: 'english') # => Relation which match the 'apples' phrase - # - # @option :headline [String] Append a expression to the selected columns aliased to headline that contains an extract of the matched text. + # # => Relation which match the 'apples' phrase + # posts.full_text_search([:title, :content], 'apples', language: 'english') # + # @option :headline [String] Append a expression to the selected columns aliased to headline that contains + # an extract of the matched text. # @option :language [String] The language to use for the search (default: 'simple') - # - # @option :plain [Boolean] Whether a plain search should be used (default: false). In this case, terms should be a single string, and it will do a search where cols contains all of the words in terms. This ignores search operators in terms. - # - # @option :phrase [Boolean] Similar to :plain, but also adding an ILIKE filter to ensure that returned rows also include the exact phrase used. - # + # @option :plain [Boolean] Whether a plain search should be used (default: false). In this case, terms + # should be a single string, and it will do a search where cols contains all of the words in terms. + # This ignores search operators in terms. + # @option :phrase [Boolean] Similar to :plain, but also adding an ILIKE filter to ensure that returned rows + # also include the exact phrase used. # @option :rank [Boolean] Set to true to order by the rank, so that closer matches are returned first. - # - # @option :to_tsquery [Symbol] Can be set to :plain or :phrase to specify the function to use to convert the terms to a ts_query. - # - # @option :tsquery [Boolean] Specifies the terms argument is already a valid SQL expression returning a tsquery, and can be used directly in the query. - # - # @option :tsvector [Boolean] Specifies the cols argument is already a valid SQL expression returning a tsvector, and can be used directly in the query. + # @option :to_tsquery [Symbol] Can be set to :plain or :phrase to specify the function to use to convert + # the terms to a ts_query. + # @option :tsquery [Boolean] Specifies the terms argument is already a valid SQL expression returning + # a tsquery, and can be used directly in the query. + # @option :tsvector [Boolean] Specifies the cols argument is already a valid SQL expression returning + # a tsvector, and can be used directly in the query. # # @return [Relation] # @@ -48,6 +49,7 @@ def full_text_search(*args, &block) ROM.plugins do adapter(:sql) do - register :pg_full_text_search, ROM::Plugins::Relation::SQL::Postgres::FullTextSearch, type: :relation + register :pg_full_text_search, ROM::Plugins::Relation::SQL::Postgres::FullTextSearch, +type: :relation end end diff --git a/lib/rom/plugins/relation/sql/postgres/streaming.rb b/lib/rom/plugins/relation/sql/postgres/streaming.rb index 186167694..a43184dbd 100644 --- a/lib/rom/plugins/relation/sql/postgres/streaming.rb +++ b/lib/rom/plugins/relation/sql/postgres/streaming.rb @@ -13,7 +13,7 @@ module Streaming class StreamingNotSupportedError < StandardError; end - def self.apply(target, **opts) + def self.apply(target, **_opts) conn = registry.gateways[target.config.component.gateway].connection return unless conn.database_type.to_sym == :postgres diff --git a/lib/rom/sql/associations/many_to_many.rb b/lib/rom/sql/associations/many_to_many.rb index d5f9ad978..342b4e527 100644 --- a/lib/rom/sql/associations/many_to_many.rb +++ b/lib/rom/sql/associations/many_to_many.rb @@ -17,10 +17,10 @@ def call(target: self.target) schema = if left.schema.key?(foreign_key) - if target != self.target - target.schema.merge(join_schema) - else + if target == self.target left.schema.uniq.project(*columns) + else + target.schema.merge(join_schema) end else target_schema @@ -50,7 +50,7 @@ def join(type, source = self.source, target = self.target) # @api public def join_keys - { source_attr => target_attr } + {source_attr => target_attr} end # @api public diff --git a/lib/rom/sql/associations/self_ref.rb b/lib/rom/sql/associations/self_ref.rb index 0265475d4..b3d15b332 100644 --- a/lib/rom/sql/associations/self_ref.rb +++ b/lib/rom/sql/associations/self_ref.rb @@ -11,7 +11,7 @@ def self.included(klass) # @api public def join_keys - { source_attr => target_attr } + {source_attr => target_attr} end # @api public diff --git a/lib/rom/sql/attribute_aliasing.rb b/lib/rom/sql/attribute_aliasing.rb index da6e6e06d..260f125c4 100644 --- a/lib/rom/sql/attribute_aliasing.rb +++ b/lib/rom/sql/attribute_aliasing.rb @@ -19,8 +19,7 @@ def aliased(alias_name) sql_expr: alias_sql_expr(sql_expr, new_alias_name) ) end - alias as aliased - + alias_method :as, :aliased # Return true if this attribute is an aliased projection # @@ -46,7 +45,7 @@ def aliased(alias_name) # # @api private def aliased_projection? - self.meta[:sql_expr].is_a?(Sequel::SQL::AliasedExpression) + meta[:sql_expr].is_a?(Sequel::SQL::AliasedExpression) end private diff --git a/lib/rom/sql/commands/create.rb b/lib/rom/sql/commands/create.rb index c802758d3..e20ffa6ac 100644 --- a/lib/rom/sql/commands/create.rb +++ b/lib/rom/sql/commands/create.rb @@ -60,10 +60,11 @@ def multi_insert(tuples) # Yields tuples for insertion or return an enumerator # # @api private - def with_input_tuples(tuples) + def with_input_tuples(tuples, &block) input_tuples = Array([tuples]).flatten(1).map return input_tuples unless block_given? - input_tuples.each { |tuple| yield(tuple) } + + input_tuples.each(&block) end end end diff --git a/lib/rom/sql/commands/update.rb b/lib/rom/sql/commands/update.rb index 34a897c1b..f8e7a71a7 100644 --- a/lib/rom/sql/commands/update.rb +++ b/lib/rom/sql/commands/update.rb @@ -52,10 +52,11 @@ def primary_key # Yields tuples for insertion or return an enumerator # # @api private - def with_input_tuples(tuples) + def with_input_tuples(tuples, &block) input_tuples = Array([tuples]).flatten(1).map return input_tuples unless block_given? - input_tuples.each { |tuple| yield(tuple) } + + input_tuples.each(&block) end end end diff --git a/lib/rom/sql/extensions/active_support_notifications.rb b/lib/rom/sql/extensions/active_support_notifications.rb index cb94f08aa..8ce4528fe 100644 --- a/lib/rom/sql/extensions/active_support_notifications.rb +++ b/lib/rom/sql/extensions/active_support_notifications.rb @@ -24,4 +24,4 @@ def instrumentation_name end end -Sequel::Database.send(:prepend, ROM::SQL::ActiveSupportInstrumentation) +Sequel::Database.prepend ROM::SQL::ActiveSupportInstrumentation diff --git a/lib/rom/sql/extensions/postgres/commands.rb b/lib/rom/sql/extensions/postgres/commands.rb index c25978250..bc3867cbe 100644 --- a/lib/rom/sql/extensions/postgres/commands.rb +++ b/lib/rom/sql/extensions/postgres/commands.rb @@ -93,23 +93,23 @@ class Upsert < SQL::Commands::Create # @!attribute [r] constraint # @return [Symbol] the name of the constraint expected to be violated - option :constraint, default: -> { self.config.constraint } + option :constraint, default: -> { config.constraint } # @!attribute [r] conflict_target # @return [Object] the column or expression to handle a violation on - option :conflict_target, default: -> { self.config.conflict_target } + option :conflict_target, default: -> { config.conflict_target } # @!attribute [r] conflict_where # @return [Object] the index filter, when using a partial index to determine uniqueness - option :conflict_where, default: -> { self.config.conflict_where } + option :conflict_where, default: -> { config.conflict_where } # @!attribute [r] update_statement # @return [Object] the update statement which will be executed in case of a violation - option :update_statement, default: -> { self.config.update_statement } + option :update_statement, default: -> { config.update_statement } # @!attribute [r] update_where # @return [Object] the WHERE clause to be added to the update - option :update_where, default: -> { self.config.update_where } + option :update_where, default: -> { config.update_where } # Tries to insert provided tuples and do an update (or nothing) # when the inserted record violates a unique constraint and hence diff --git a/lib/rom/sql/extensions/postgres/type_builder.rb b/lib/rom/sql/extensions/postgres/type_builder.rb index 1c1da3cc4..62f6288e9 100644 --- a/lib/rom/sql/extensions/postgres/type_builder.rb +++ b/lib/rom/sql/extensions/postgres/type_builder.rb @@ -6,17 +6,14 @@ module Postgres class TypeBuilder < Schema::TypeBuilder defines :db_numeric_types, :db_type_mapping, :db_array_type_matcher - db_numeric_types %w[ - smallint integer bigint - decimal numeric real - double\ precision serial bigserial - ].to_set.freeze + db_numeric_types ["smallint", "integer", "bigint", "decimal", "numeric", "real", + "double precision", "serial", "bigserial"].to_set.freeze db_type_mapping( - "uuid" => Types::UUID, + "uuid" => Types::UUID, "money" => Types::Money, "bytea" => Types::Bytea, - "json" => Types::JSON, + "json" => Types::JSON, "jsonb" => Types::JSONB, "xml" => Types::XML, "inet" => Types::IPAddress, @@ -39,7 +36,7 @@ class TypeBuilder < Schema::TypeBuilder "ltree" => Types::LTree ).freeze - db_array_type_matcher "[]".freeze + db_array_type_matcher "[]" def map_pk_type(type, db_type, **options) if numeric?(type, db_type) diff --git a/lib/rom/sql/extensions/postgres/type_serializer.rb b/lib/rom/sql/extensions/postgres/type_serializer.rb index 9f1ded440..cdb4370ef 100644 --- a/lib/rom/sql/extensions/postgres/type_serializer.rb +++ b/lib/rom/sql/extensions/postgres/type_serializer.rb @@ -36,7 +36,7 @@ class TypeSerializer < ROM::SQL::TypeSerializer def call(type) super do if type.respond_to?(:primitive) && type.primitive.equal?(Array) - "#{ type.meta[:type] }[]" + "#{type.meta[:type]}[]" end end end diff --git a/lib/rom/sql/extensions/postgres/types/array.rb b/lib/rom/sql/extensions/postgres/types/array.rb index c5525326b..e6e929412 100644 --- a/lib/rom/sql/extensions/postgres/types/array.rb +++ b/lib/rom/sql/extensions/postgres/types/array.rb @@ -123,7 +123,7 @@ def get(type, expr, idx) end def any(_type, expr, value) - Attribute[SQL::Types::Bool].meta(sql_expr: { value => expr.pg_array.any }) + Attribute[SQL::Types::Bool].meta(sql_expr: {value => expr.pg_array.any}) end def contained_by(type, expr, other) diff --git a/lib/rom/sql/extensions/postgres/types/geometric.rb b/lib/rom/sql/extensions/postgres/types/geometric.rb index c2cde94bb..9f6e8cb88 100644 --- a/lib/rom/sql/extensions/postgres/types/geometric.rb +++ b/lib/rom/sql/extensions/postgres/types/geometric.rb @@ -37,7 +37,7 @@ module Types Point = Type("point") do SQL::Types.define(Values::Point) do input do |point| - "(#{ point.x },#{ point.y })" + "(#{point.x},#{point.y})" end output do |point| @@ -50,7 +50,7 @@ module Types Line = Type("line") do SQL::Types.define(Values::Line) do input do |line| - "{#{ line.a },#{ line.b },#{line.c}}" + "{#{line.a},#{line.b},#{line.c}}" end output do |line| @@ -63,7 +63,7 @@ module Types Circle = Type("circle") do SQL::Types.define(Values::Circle) do input do |circle| - "<(#{ circle.center.x },#{ circle.center.y }),#{ circle.radius }>" + "<(#{circle.center.x},#{circle.center.y}),#{circle.radius}>" end output do |circle| @@ -77,8 +77,8 @@ module Types Box = Type("box") do SQL::Types.define(Values::Box) do input do |box| - "((#{ box.upper_right.x },#{ box.upper_right.y }),"\ - "(#{ box.lower_left.x },#{ box.lower_left.y }))" + "((#{box.upper_right.x},#{box.upper_right.y}),"\ + "(#{box.lower_left.x},#{box.lower_left.y}))" end output do |box| @@ -93,8 +93,8 @@ module Types LineSegment = Type("lseg") do SQL::Types.define(Values::LineSegment) do input do |segment| - "[(#{ segment.begin.x },#{ segment.begin.y }),"\ - "(#{ segment.end.x },#{ segment.end.y })]" + "[(#{segment.begin.x},#{segment.begin.y}),"\ + "(#{segment.end.x},#{segment.end.y})]" end output do |segment| @@ -109,8 +109,8 @@ module Types Polygon = Type("polygon") do SQL::Types.define(::Array) do input do |points| - points_joined = points.map { |p| "(#{ p.x },#{ p.y })" }.join(",") - "(#{ points_joined })" + points_joined = points.map { |p| "(#{p.x},#{p.y})" }.join(",") + "(#{points_joined})" end output do |polygon| @@ -123,12 +123,12 @@ module Types Path = Type("path") do SQL::Types.define(Values::Path) do input do |path| - points_joined = path.to_a.map { |p| "(#{ p.x },#{ p.y })" }.join(",") + points_joined = path.to_a.map { |p| "(#{p.x},#{p.y})" }.join(",") if path.open? - "[#{ points_joined }]" + "[#{points_joined}]" else - "(#{ points_joined })" + "(#{points_joined})" end end diff --git a/lib/rom/sql/extensions/postgres/types/ltree.rb b/lib/rom/sql/extensions/postgres/types/ltree.rb index 8051d3fea..a30b2f215 100644 --- a/lib/rom/sql/extensions/postgres/types/ltree.rb +++ b/lib/rom/sql/extensions/postgres/types/ltree.rb @@ -122,8 +122,14 @@ module Types # # Translates to || # # # # @example - # # people.select { (ltree_tags + ROM::Types::Values::TreePath.new('Moscu')).as(:ltree_tags) }.where { name.is('Jade Doe') } - # # people.select { (ltree_tags + 'Moscu').as(:ltree_tags) }.where { name.is('Jade Doe') } + # # people.select { + # # (ltree_tags + ROM::Types::Values::TreePath.new('Moscu')).as(:ltree_tags) + # # }.where { name.is('Jade Doe') } + # # + # # people.select { + # # (ltree_tags + 'Moscu').as(:ltree_tags) + # # } + # # .where { name.is('Jade Doe') } # # # # @param [LTree, String] keys # # @@ -234,7 +240,8 @@ module LTreeMethods MATCH_ANY_LTEXTQUERY = ["(", " ?@ ", ")"].freeze def match(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: Sequel::SQL::BooleanExpression.new(:'~', expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: Sequel::SQL::BooleanExpression.new(:~, +expr, query)) end def match_any(_type, expr, query) @@ -262,31 +269,42 @@ def build_array_query(query, array_type = "lquery") include LTreeMethods def contain_any_ltextquery(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::MATCH_LTEXTQUERY, expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::MATCH_LTEXTQUERY, expr, query + )) end def contain_ancestor(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::ASCENDANT, expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::ASCENDANT, expr, query + )) end def contain_descendant(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::DESCENDANT, expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::DESCENDANT, expr, query + )) end def find_ancestor(_type, expr, query) - Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::FIND_ASCENDANT, expr, query)) + Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::FIND_ASCENDANT, +expr, query)) end def find_descendant(_type, expr, query) - Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::FIND_DESCENDANT, expr, query)) + Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::FIND_DESCENDANT, +expr, query)) end def match_any_lquery(_type, expr, query) - Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::MATCH_ANY_LQUERY, expr, query)) + Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::MATCH_ANY_LQUERY, +expr, query)) end def match_any_ltextquery(_type, expr, query) - Attribute[LTree].meta(sql_expr: custom_operator_expr(LTreeMethods::MATCH_ANY_LTEXTQUERY, expr, query)) + Attribute[LTree].meta(sql_expr: custom_operator_expr( + LTreeMethods::MATCH_ANY_LTEXTQUERY, expr, query + )) end end @@ -294,25 +312,35 @@ def match_any_ltextquery(_type, expr, query) include LTreeMethods def match_ltextquery(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::MATCH_LTEXTQUERY, expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::MATCH_LTEXTQUERY, expr, query + )) end def contain_descendant(_type, expr, query) array = build_array_query(query, "ltree") - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::DESCENDANT, expr, array)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::DESCENDANT, expr, array + )) end def descendant(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::DESCENDANT, expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::DESCENDANT, expr, query + )) end def contain_ascendant(_type, expr, query) array = build_array_query(query, "ltree") - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::ASCENDANT, expr, array)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::ASCENDANT, expr, array + )) end def ascendant(_type, expr, query) - Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr(LTreeMethods::ASCENDANT, expr, query)) + Attribute[SQL::Types::Bool].meta(sql_expr: custom_operator_expr( + LTreeMethods::ASCENDANT, expr, query + )) end def +(_type, expr, other) @@ -322,7 +350,8 @@ def +(_type, expr, other) else ROM::Types::Values::TreePath.new(other) end - Attribute[LTree].meta(sql_expr: Sequel::SQL::StringExpression.new(:'||', expr, other_value.to_s)) + Attribute[LTree].meta(sql_expr: Sequel::SQL::StringExpression.new(:"||", expr, +other_value.to_s)) end end end diff --git a/lib/rom/sql/extensions/postgres/types/range.rb b/lib/rom/sql/extensions/postgres/types/range.rb index 5ae9df498..843c89ec5 100644 --- a/lib/rom/sql/extensions/postgres/types/range.rb +++ b/lib/rom/sql/extensions/postgres/types/range.rb @@ -9,10 +9,10 @@ module SQL module Postgres module Values Range = ::Struct.new(:lower, :upper, :bounds) do - PAREN_LEFT = "(".freeze - PAREN_RIGHT = ")".freeze + PAREN_LEFT = "(" + PAREN_RIGHT = ")" - def initialize(lower, upper, bounds = :'[)') + def initialize(lower, upper, bounds = :"[)") super end @@ -38,10 +38,10 @@ module Types int8range: Sequel::Postgres::PGRange::Parser.new( "int8range", SQL::Types::Coercible::Integer ), - numrange: Sequel::Postgres::PGRange::Parser.new( + numrange: Sequel::Postgres::PGRange::Parser.new( "numrange", SQL::Types::Coercible::Integer ), - tsrange: Sequel::Postgres::PGRange::Parser.new( + tsrange: Sequel::Postgres::PGRange::Parser.new( "tsrange", ::Time.method(:parse) ), tstzrange: Sequel::Postgres::PGRange::Parser.new( @@ -67,8 +67,8 @@ def self.range_read_type(name) Values::Range.new( pg_range.begin, pg_range.end, - [pg_range.exclude_begin? ? :'(' : :'[', - pg_range.exclude_end? ? :')' : :']'] + [pg_range.exclude_begin? ? :"(" : :"[", + pg_range.exclude_end? ? :")" : :"]"] .join("").to_sym ) end @@ -79,10 +79,10 @@ def self.range(name, read_type) Type(name) do type = SQL::Types.Nominal(Values::Range).constructor do |range| format("%s%s,%s%s", - range.exclude_begin? ? :'(' : :'[', + range.exclude_begin? ? :"(" : :"[", range.lower, range.upper, - range.exclude_end? ? :')' : :']') + range.exclude_end? ? :")" : :"]") end type.meta(read: read_type) diff --git a/lib/rom/sql/function.rb b/lib/rom/sql/function.rb index 5b77c1cb3..2daf7857b 100644 --- a/lib/rom/sql/function.rb +++ b/lib/rom/sql/function.rb @@ -20,9 +20,9 @@ def frame_limit(value) when :end then "UNBOUNDED FOLLOWING" else if value > 0 - "#{ value } FOLLOWING" + "#{value} FOLLOWING" else - "#{ value.abs } PRECEDING" + "#{value.abs} PRECEDING" end end end @@ -34,13 +34,14 @@ def frame_limit(value) WINDOW_FRAMES = Hash.new do |cache, frame| type = frame.key?(:rows) ? "ROWS" : "RANGE" bounds = frame[:rows] || frame[:range] - cache[frame] = "#{ type } BETWEEN #{ frame_limit(bounds[0]) } AND #{ frame_limit(bounds[1]) }" + cache[frame] = + "#{type} BETWEEN #{frame_limit(bounds[0])} AND #{frame_limit(bounds[1])}" end WINDOW_FRAMES[nil] = nil - WINDOW_FRAMES[:all] = WINDOW_FRAMES[rows: [:start, :end]] - WINDOW_FRAMES[:rows] = WINDOW_FRAMES[rows: [:start, :current]] - WINDOW_FRAMES[range: :current] = WINDOW_FRAMES[range: [:current, :current]] + WINDOW_FRAMES[:all] = WINDOW_FRAMES[rows: %i[start end]] + WINDOW_FRAMES[:rows] = WINDOW_FRAMES[rows: %i[start current]] + WINDOW_FRAMES[range: :current] = WINDOW_FRAMES[range: %i[current current]] # Return a new attribute with an alias # @@ -104,7 +105,7 @@ def qualified?(_table_alias = nil) # @api public def is(other) ::ROM::SQL::Attribute[::ROM::SQL::Types::Bool].meta( - sql_expr: ::Sequel::SQL::BooleanExpression.new(:'=', func, other) + sql_expr: ::Sequel::SQL::BooleanExpression.new(:"=", func, other) ) end @@ -209,7 +210,7 @@ def case(mapping) def filter(condition = Undefined, &block) if block conditions = schema.restriction(&block) - conditions = conditions & condition unless condition.equal?(Undefined) + conditions &= condition unless condition.equal?(Undefined) else conditions = condition end diff --git a/lib/rom/sql/gateway.rb b/lib/rom/sql/gateway.rb index 0dbd220d5..34b0b6325 100644 --- a/lib/rom/sql/gateway.rb +++ b/lib/rom/sql/gateway.rb @@ -161,15 +161,15 @@ def dataset?(name) # Create a table using the configured connection # # @api public - def create_table(*args, &block) - connection.create_table(*args, &block) + def create_table(...) + connection.create_table(...) end # Drops a table # # @api public - def drop_table(*args, &block) - connection.drop_table(*args, &block) + def drop_table(...) + connection.drop_table(...) end # Returns a list of datasets inferred from table names diff --git a/lib/rom/sql/mapper_compiler.rb b/lib/rom/sql/mapper_compiler.rb index f6105735b..1efbc532f 100644 --- a/lib/rom/sql/mapper_compiler.rb +++ b/lib/rom/sql/mapper_compiler.rb @@ -9,7 +9,7 @@ def visit_attribute(node) name, _, meta_options = node if meta_options[:wrapped] - [extract_wrapped_name(node), from: meta_options[:alias]] + [extract_wrapped_name(node), {from: meta_options[:alias]}] else [name] end diff --git a/lib/rom/sql/migration/migrator.rb b/lib/rom/sql/migration/migrator.rb index 726bf66d4..9b6f3e258 100644 --- a/lib/rom/sql/migration/migrator.rb +++ b/lib/rom/sql/migration/migrator.rb @@ -16,8 +16,8 @@ module Migration class Migrator extend Initializer - DEFAULT_PATH = "db/migrate".freeze - VERSION_FORMAT = "%Y%m%d%H%M%S".freeze + DEFAULT_PATH = "db/migrate" + VERSION_FORMAT = "%Y%m%d%H%M%S" DEFAULT_INFERRER = Schema::Inferrer.new.suppress_errors.freeze param :connection @@ -44,7 +44,7 @@ def migration(&block) # @api private def create_file(name, version = generate_version, **options) sequence = options[:sequence] ? "%03d" % options[:sequence] : nil - filename = "#{ version }#{ sequence }_#{ name }.rb" + filename = "#{version}#{sequence}_#{name}.rb" content = options[:content] || migration_file_content path = options[:path] || self.path dirname = Pathname(path) diff --git a/lib/rom/sql/migration/runner.rb b/lib/rom/sql/migration/runner.rb index 7bba43b9f..0b0c2180f 100644 --- a/lib/rom/sql/migration/runner.rb +++ b/lib/rom/sql/migration/runner.rb @@ -68,9 +68,8 @@ def alter_table(diff) when SchemaDiff::AttributeChanged if attribute.type_changed? from, to = attribute.current.unwrap, attribute.target.unwrap - raise UnsupportedConversion.new( - "Don't know how to convert #{from.inspect} to #{to.inspect}" - ) + raise UnsupportedConversion, +"Don't know how to convert #{from.inspect} to #{to.inspect}" end if attribute.nullability_changed? diff --git a/lib/rom/sql/migration/schema_diff.rb b/lib/rom/sql/migration/schema_diff.rb index dcce2559b..1bc1da2fc 100644 --- a/lib/rom/sql/migration/schema_diff.rb +++ b/lib/rom/sql/migration/schema_diff.rb @@ -46,7 +46,6 @@ class TableCreated < TableDiff end class TableAltered < TableDiff - option :attribute_changes, default: -> { EMPTY_ARRAY } option :index_changes, default: -> { EMPTY_ARRAY } @@ -135,10 +134,10 @@ def name class IndexAdded < IndexDiff def options options = {} - options[:name] = index.name if !index.name.nil? + options[:name] = index.name unless index.name.nil? options[:unique] = true if index.unique? - options[:type] = index.type if !index.type.nil? - options[:where] = index.predicate if !index.predicate.nil? + options[:type] = index.type unless index.type.nil? + options[:where] = index.predicate unless index.predicate.nil? options end end @@ -146,7 +145,7 @@ def options class IndexRemoved < IndexDiff def options options = {} - options[:name] = index.name if !index.name.nil? + options[:name] = index.name unless index.name.nil? options end end diff --git a/lib/rom/sql/migration/writer.rb b/lib/rom/sql/migration/writer.rb index 96acf4d01..a14d6bd4d 100644 --- a/lib/rom/sql/migration/writer.rb +++ b/lib/rom/sql/migration/writer.rb @@ -7,8 +7,8 @@ module SQL module Migration # @api private class Writer - MIGRATION_BEGIN = "ROM::SQL.migration do\n change do".freeze - MIGRATION_END = "\n end\nend\n".freeze + MIGRATION_BEGIN = "ROM::SQL.migration do\n change do" + MIGRATION_END = "\n end\nend\n" attr_reader :yield_migration @@ -36,11 +36,11 @@ def write(operations, buffer, indent) buffer << indent << op.to_s << " " write_arguments(buffer, args) - if !nested.empty? - buffer << " do" - write(nested, buffer, indent + " ") - buffer << indent << "end" - end + next if nested.empty? + + buffer << " do" + write(nested, buffer, indent + " ") + buffer << indent << "end" end end @@ -61,7 +61,7 @@ def migration_name(op) create_or_alter, args = op table_name = args[0] - "#{create_or_alter.to_s.sub('_table', '')}_#{table_name}" + "#{create_or_alter.to_s.sub("_table", "")}_#{table_name}" end end end diff --git a/lib/rom/sql/plugin/associates.rb b/lib/rom/sql/plugin/associates.rb index 203185e32..38ac83d4c 100644 --- a/lib/rom/sql/plugin/associates.rb +++ b/lib/rom/sql/plugin/associates.rb @@ -16,7 +16,7 @@ class AssociateOptions def initialize(name, relation, opts) @name = name @assoc = relation.associations[name] - @opts = { assoc: assoc, keys: assoc.join_keys } + @opts = {assoc: assoc, keys: assoc.join_keys} @opts.update(parent: opts[:parent]) if opts[:parent] end @@ -25,7 +25,7 @@ def after? end def to_hash - { associate: opts } + {associate: opts} end end @@ -35,7 +35,7 @@ def self.included(klass) extend ClassMethods include InstanceMethods - setting :associations, default: Hash.new, reader: true + setting :associations, default: {}, reader: true option :associations, default: -> { self.class.associations } @@ -89,16 +89,17 @@ def build(relation, **options) associate_options = command.associations.map { |(name, opts)| next if configured_assocs.include?(name) + AssociateOptions.new(name, relation, opts) }.compact before_hooks = associate_options.reject(&:after?).map(&:to_hash) after_hooks = associate_options.select(&:after?).map(&:to_hash) - command. - with(configured_associations: configured_assocs + associate_options.map(&:name)). - before(*before_hooks). - after(*after_hooks) + command + .with(configured_associations: configured_assocs + associate_options.map(&:name)) + .before(*before_hooks) + .after(*after_hooks) end # Set command to associate tuples with a parent tuple using provided keys diff --git a/lib/rom/sql/plugin/pagination.rb b/lib/rom/sql/plugin/pagination.rb index e3f1fe034..123762e2d 100644 --- a/lib/rom/sql/plugin/pagination.rb +++ b/lib/rom/sql/plugin/pagination.rb @@ -102,7 +102,7 @@ def at(dataset, current_page, per_page = self.per_page) per_page = per_page.to_i self.class.new( - dataset.offset((current_page-1)*per_page).limit(per_page), + dataset.offset((current_page - 1) * per_page).limit(per_page), current_page: current_page, per_page: per_page ) end diff --git a/lib/rom/sql/relation.rb b/lib/rom/sql/relation.rb index c1971b528..79d37b04b 100644 --- a/lib/rom/sql/relation.rb +++ b/lib/rom/sql/relation.rb @@ -69,18 +69,36 @@ def assoc(name) # Open a database transaction # # @param [Hash] opts - # @option opts [Boolean] :auto_savepoint Automatically use a savepoint for Database#transaction calls inside this transaction block. - # @option opts [Symbol] :isolation The transaction isolation level to use for this transaction, should be :uncommitted, :committed, :repeatable, or :serializable, used if given and the database/adapter supports customizable transaction isolation levels. - # @option opts [Integer] :num_retries The number of times to retry if the :retry_on option is used. The default is 5 times. Can be set to nil to retry indefinitely, but that is not recommended. - # @option opts [Proc] :before_retry Proc to execute before rertrying if the :retry_on option is used. Called with two arguments: the number of retry attempts (counting the current one) and the error the last attempt failed with. - # @option opts [String] :prepare A string to use as the transaction identifier for a prepared transaction (two-phase commit), if the database/adapter supports prepared transactions. - # @option opts [Class] :retry_on An exception class or array of exception classes for which to automatically retry the transaction. Can only be set if not inside an existing transaction. Note that this should not be used unless the entire transaction block is idempotent, as otherwise it can cause non-idempotent behavior to execute multiple times. - # @option opts [Symbol] :rollback Can the set to :reraise to reraise any Sequel::Rollback exceptions raised, or :always to always rollback even if no exceptions occur (useful for testing). - # @option opts [Symbol] :server The server to use for the transaction. Set to :default, :read_only, or whatever symbol you used in the connect string when naming your servers. - # @option opts [Boolean] :savepoint Whether to create a new savepoint for this transaction, only respected if the database/adapter supports savepoints. By default Sequel will reuse an existing transaction, so if you want to use a savepoint you must use this option. If the surrounding transaction uses :auto_savepoint, you can set this to false to not use a savepoint. If the value given for this option is :only, it will only create a savepoint if it is inside a transacation. - # @option opts [Boolean] :deferrable **PG 9.1+ only** If present, set to DEFERRABLE if true or NOT DEFERRABLE if false. + # @option opts [Boolean] :auto_savepoint Automatically use a savepoint for Database#transaction calls inside + # this transaction block. + # @option opts [Symbol] :isolation The transaction isolation level to use for this transaction, should be + # :uncommitted, :committed, :repeatable, or :serializable, used if given and the database/adapter supports + # customizable transaction isolation levels. + # @option opts [Integer] :num_retries The number of times to retry if the :retry_on option is used. The default is + # 5 times. Can be set to nil to retry indefinitely, but that is not recommended. + # @option opts [Proc] :before_retry Proc to execute before rertrying if the :retry_on option is used. Called with + # two arguments: the number of retry attempts (counting the current one) and the error the last attempt failed + # with. + # @option opts [String] :prepare A string to use as the transaction identifier for a prepared transaction + # (two-phase commit), if the database/adapter supports prepared transactions. + # @option opts [Class] :retry_on An exception class or array of exception classes for which to automatically retry + # the transaction. Can only be set if not inside an existing transaction. Note that this should not be used + # unless the entire transaction block is idempotent, as otherwise it can cause non-idempotent behavior to + # execute multiple times. + # @option opts [Symbol] :rollback Can the set to :reraise to reraise any Sequel::Rollback exceptions raised, or + # :always to always rollback even if no exceptions occur (useful for testing). + # @option opts [Symbol] :server The server to use for the transaction. Set to :default, :read_only, or whatever + # symbol you used in the connect string when naming your servers. + # @option opts [Boolean] :savepoint Whether to create a new savepoint for this transaction, only respected if the + # database/adapter supports savepoints. By default Sequel will reuse an existing transaction, so if you want to + # use a savepoint you must use this option. If the surrounding transaction uses :auto_savepoint, you can set + # this to false to not use a savepoint. If the value given for this option is :only, it will only create + # a savepoint if it is inside a transacation. + # @option opts [Boolean] :deferrable **PG 9.1+ only** If present, set to DEFERRABLE if true or NOT DEFERRABLE if + # false. # @option opts [Boolean] :read_only **PG only** If present, set to READ ONLY if true or READ WRITE if false. - # @option opts [Symbol] :synchronous **PG only** if non-nil, set synchronous_commit appropriately. Valid values true, :on, false, :off, :local (9.1+), and :remote_write (9.2+). + # @option opts [Symbol] :synchronous **PG only** if non-nil, set synchronous_commit appropriately. Valid values + # true, :on, false, :off, :local (9.1+), and :remote_write (9.2+). # # @yield [t] Transaction # diff --git a/lib/rom/sql/relation/reading.rb b/lib/rom/sql/relation/reading.rb index b0b1abfd3..29dda17a7 100644 --- a/lib/rom/sql/relation/reading.rb +++ b/lib/rom/sql/relation/reading.rb @@ -11,18 +11,18 @@ class Relation < ROM::Relation # @api public module Reading # Row-level lock modes - ROW_LOCK_MODES = Hash.new(update: "FOR UPDATE".freeze).update( + ROW_LOCK_MODES = Hash.new(update: "FOR UPDATE").update( # https://www.postgresql.org/docs/current/static/sql-select.html#SQL-FOR-UPDATE-SHARE postgres: { - update: "FOR UPDATE".freeze, - no_key_update: "FOR NO KEY UPDATE".freeze, - share: "FOR SHARE".freeze, - key_share: "FOR KEY SHARE".freeze + update: "FOR UPDATE", + no_key_update: "FOR NO KEY UPDATE", + share: "FOR SHARE", + key_share: "FOR KEY SHARE" }, # https://dev.mysql.com/doc/refman/5.7/en/innodb-locking-reads.html mysql: { - update: "FOR UPDATE".freeze, - share: "LOCK IN SHARE MODE".freeze + update: "FOR UPDATE", + share: "LOCK IN SHARE MODE" } ).freeze @@ -235,8 +235,8 @@ def rename(options) # @return [Relation] # # @api public - def select(*args, &block) - schema.project(*args, &block).(self) + def select(...) + schema.project(...).(self) end alias_method :project, :select @@ -247,8 +247,8 @@ def select(*args, &block) # @return [Relation] # # @api public - def select_append(*args, &block) - schema.merge(schema.canonical.project(*args, &block)).(self) + def select_append(...) + schema.merge(schema.canonical.project(...)).(self) end # Returns a copy of the relation with a SQL DISTINCT clause. @@ -814,8 +814,10 @@ def select_group(*args, &block) # # @param [Hash] options Options for union # @option options [Symbol] :alias Use the given value as the #from_self alias - # @option options [TrueClass, FalseClass] :all Set to true to use UNION ALL instead of UNION, so duplicate rows can occur - # @option options [TrueClass, FalseClass] :from_self Set to false to not wrap the returned dataset in a #from_self, use with care. + # @option options [TrueClass, FalseClass] :all Set to true to use UNION ALL instead of UNION, so duplicate rows + # can occur + # @option options [TrueClass, FalseClass] :from_self Set to false to not wrap the returned dataset in a + # #from_self, use with care. # # @returRelation] # @@ -827,7 +829,7 @@ def union(relation, options = EMPTY_HASH, &block) # confusing ways. same_relation = name == relation.name alias_name = same_relation ? name : "#{name.to_sym}__#{relation.name.to_sym}" - opts = { alias: alias_name.to_sym, **options } + opts = {alias: alias_name.to_sym, **options} new_schema = schema.qualified(opts[:alias]) new_schema.(new(dataset.__send__(__method__, relation.dataset, opts, &block))) @@ -848,8 +850,8 @@ def union(relation, options = EMPTY_HASH, &block) # @return [TrueClass, FalseClass] # # @api public - def exist?(*args, &block) - !where(*args, &block).limit(1).count.zero? + def exist?(...) + !where(...).limit(1).count.zero? end # Return if a restricted relation has 0 tuples @@ -1050,7 +1052,7 @@ def lock_clause(mode: :update, skip_locked: false, of: nil, wait: nil) stmt << " OF " << Array(of).join(", ") if of if skip_locked - raise ArgumentError, "SKIP LOCKED cannot be used with (NO)WAIT clause" if !wait.nil? + raise ArgumentError, "SKIP LOCKED cannot be used with (NO)WAIT clause" unless wait.nil? stmt << " SKIP LOCKED" else @@ -1101,7 +1103,7 @@ def __join__(type, other, join_cond = EMPTY_HASH, opts = EMPTY_HASH, &block) join_cond = JoinDSL.new(schema).(&block) if other.name.aliaz - join_opts = { table_alias: other.name.aliaz } + join_opts = {table_alias: other.name.aliaz} else join_opts = EMPTY_HASH end @@ -1111,7 +1113,8 @@ def __join__(type, other, join_cond = EMPTY_HASH, opts = EMPTY_HASH, &block) associations[other.name.key].join(type, self, other) end else - raise ArgumentError, "+other+ must be either a symbol or a relation, #{other.class} given" + raise ArgumentError, +"+other+ must be either a symbol or a relation, #{other.class} given" end end end diff --git a/lib/rom/sql/relation/writing.rb b/lib/rom/sql/relation/writing.rb index 4aec53d5d..40f503ee2 100644 --- a/lib/rom/sql/relation/writing.rb +++ b/lib/rom/sql/relation/writing.rb @@ -37,8 +37,8 @@ def upsert(*args, &block) # @return [Hash] Inserted tuple # # @api public - def insert(*args, &block) - dataset.insert(*args, &block) + def insert(...) + dataset.insert(...) end # Multi insert tuples into relation @@ -51,8 +51,8 @@ def insert(*args, &block) # @return [Array] A list of executed SQL statements # # @api public - def multi_insert(*args, &block) - dataset.multi_insert(*args, &block) + def multi_insert(...) + dataset.multi_insert(...) end # Update tuples in the relation @@ -64,8 +64,8 @@ def multi_insert(*args, &block) # @return [Integer] Number of updated rows # # @api public - def update(*args, &block) - dataset.update(*args, &block) + def update(...) + dataset.update(...) end # Delete tuples from the relation @@ -78,8 +78,8 @@ def update(*args, &block) # @return [Integer] Number of deleted tuples # # @api public - def delete(*args, &block) - dataset.delete(*args, &block) + def delete(...) + dataset.delete(...) end # Insert tuples from other relation @@ -119,7 +119,9 @@ def import(other, options = EMPTY_HASH) else columns = other.schema.map { |a| a.alias || a.name } keys = columns.map(&:to_sym) - dataset.import(columns, other.to_a.map { |record| record.to_h.values_at(*keys) }, options) + dataset.import(columns, other.to_a.map { |record| + record.to_h.values_at(*keys) + }, options) end end end diff --git a/lib/rom/sql/schema.rb b/lib/rom/sql/schema.rb index 20ab7d82b..1c6b83f70 100644 --- a/lib/rom/sql/schema.rb +++ b/lib/rom/sql/schema.rb @@ -140,7 +140,7 @@ def joined # # @api public def call(relation) - relation.new(relation.dataset.select(*self.qualified_projection), schema: self) + relation.new(relation.dataset.select(*qualified_projection), schema: self) end # Return an empty schema diff --git a/lib/rom/sql/schema/attributes_inferrer.rb b/lib/rom/sql/schema/attributes_inferrer.rb index 7365121d1..bb952dfe5 100644 --- a/lib/rom/sql/schema/attributes_inferrer.rb +++ b/lib/rom/sql/schema/attributes_inferrer.rb @@ -12,7 +12,7 @@ class AttributesInferrer defines :type_builders - CONSTRAINT_DB_TYPE = "add_constraint".freeze + CONSTRAINT_DB_TYPE = "add_constraint" option :type_builder diff --git a/lib/rom/sql/schema/inferrer.rb b/lib/rom/sql/schema/inferrer.rb index 3db47db9b..ebb312850 100644 --- a/lib/rom/sql/schema/inferrer.rb +++ b/lib/rom/sql/schema/inferrer.rb @@ -50,7 +50,9 @@ def infer_from_database(gateway, schema, attributes:, **rest) foreign_keys = foreign_keys_from_database(gateway, schema, idx) {**rest, - attributes: attributes.map { |attr| mark_fk(mark_indexed(attr, indexes), foreign_keys) }, + attributes: attributes.map { |attr| + mark_fk(mark_indexed(attr, indexes), foreign_keys) + }, foreign_keys: foreign_keys, indexes: indexes} end @@ -96,18 +98,18 @@ def foreign_keys_from_database(gateway, schema, attributes) # @api private def indexes_from_attributes(attributes) - attributes. - select(&:indexed?). - map { |attr| SQL::Index.new([attr.unwrap]) }. - to_set + attributes + .select(&:indexed?) + .map { |attr| SQL::Index.new([attr.unwrap]) } + .to_set end # @api private def foreign_keys_from_attributes(attributes) - attributes. - select(&:foreign_key?). - map { |attr| SQL::ForeignKey.new([attr.unwrap], attr.target) }. - to_set + attributes + .select(&:foreign_key?) + .map { |attr| SQL::ForeignKey.new([attr.unwrap], attr.target) } + .to_set end # @api private diff --git a/lib/rom/sql/schema/type_builder.rb b/lib/rom/sql/schema/type_builder.rb index 9bdf63db3..5227362e0 100644 --- a/lib/rom/sql/schema/type_builder.rb +++ b/lib/rom/sql/schema/type_builder.rb @@ -21,7 +21,7 @@ def self.[](db_type) defines :ruby_type_mapping, :numeric_pk_type - DECIMAL_REGEX = /(?:decimal|numeric)\((\d+)(?:,\s*(\d+))?\)/.freeze + DECIMAL_REGEX = /(?:decimal|numeric)\((\d+)(?:,\s*(\d+))?\)/ ruby_type_mapping( integer: Types::Integer, @@ -66,7 +66,7 @@ def map_pk_type(_ruby_type, _db_type, **) def map_type(ruby_type, db_type, **kw) type = self.class.ruby_type_mapping[ruby_type] - if db_type.is_a?(String) && db_type.include?("numeric") || db_type.include?("decimal") + if (db_type.is_a?(String) && db_type.include?("numeric")) || db_type.include?("decimal") map_decimal_type(db_type) elsif db_type.is_a?(String) && db_type.include?("char") && kw[:max_length] type.meta(limit: kw[:max_length]) diff --git a/lib/rom/sql/tasks/migration_tasks.rake b/lib/rom/sql/tasks/migration_tasks.rake index 59dd6a03b..af1c8b843 100644 --- a/lib/rom/sql/tasks/migration_tasks.rake +++ b/lib/rom/sql/tasks/migration_tasks.rake @@ -76,7 +76,7 @@ namespace :db do end desc "Create a migration (parameters: NAME, VERSION)" - task :create_migration, [:name, :version] => :rom_configuration do |_, args| + task :create_migration, %i[name version] => :rom_configuration do |_, args| name, version = args.values_at(:name, :version) if name.nil? diff --git a/lib/rom/sql/type_extensions.rb b/lib/rom/sql/type_extensions.rb index 09898d464..6ef130deb 100644 --- a/lib/rom/sql/type_extensions.rb +++ b/lib/rom/sql/type_extensions.rb @@ -36,7 +36,9 @@ def register(type, &block) mod = Module.new(&block) ctx = Object.new.extend(mod) - functions = mod.public_instance_methods.each_with_object({}) { |m, ms| ms[m] = ctx.method(m) } + functions = mod.public_instance_methods.each_with_object({}) { |m, ms| + ms[m] = ctx.method(m) + } extensions[db_type] = (extensions[db_type] || {}).merge(functions) end end diff --git a/lib/rom/sql/type_serializer.rb b/lib/rom/sql/type_serializer.rb index adc193225..4dbdd0647 100644 --- a/lib/rom/sql/type_serializer.rb +++ b/lib/rom/sql/type_serializer.rb @@ -29,18 +29,18 @@ def self.[](db_type) Types::Date => "date", Types::Bool => "boolean", Types::Decimal => "numeric", - Types::Float => "float", + Types::Float => "float" ) def call(type) return type.meta[:db_type] if type.meta[:db_type] - meta = type.meta[:read] ? { read: type.meta[:read] } : EMPTY_HASH + meta = type.meta[:read] ? {read: type.meta[:read]} : EMPTY_HASH self.class.mapping.fetch(type.with(meta: meta)) { if block_given? yield(type) - end or raise "Cannot serialize #{ type }" + end or raise "Cannot serialize #{type}" } end end diff --git a/lib/rom/types/values.rb b/lib/rom/types/values.rb index 6e9428f6f..64028af3b 100644 --- a/lib/rom/types/values.rb +++ b/lib/rom/types/values.rb @@ -6,7 +6,7 @@ module ROM module Types module Values class TreePath < ::Struct.new(:value, :separator) - DEFAULT_SEPARATOR = ".".freeze + DEFAULT_SEPARATOR = "." # @api public def self.new(value, separator = DEFAULT_SEPARATOR) diff --git a/spec/extensions/postgres/attribute/array_spec.rb b/spec/extensions/postgres/attribute/array_spec.rb index 422695ae8..cb5837990 100644 --- a/spec/extensions/postgres/attribute/array_spec.rb +++ b/spec/extensions/postgres/attribute/array_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Attribute / PG array", :postgres do subject(:relation) { relations[:pg_arrays] } @@ -25,7 +27,7 @@ tuples = relation.to_a - expect(tuples).to eql([{ numbers: [3, 1, 2] }]) + expect(tuples).to eql([{numbers: [3, 1, 2]}]) expect(tuples[0][:numbers]).to be_instance_of(Array) end @@ -39,11 +41,11 @@ end it "loads an array with json hashes" do - relation.command(:create).call(meta: [{ one: "1", two: "2" }]) + relation.command(:create).call(meta: [{one: "1", two: "2"}]) tuples = relation.to_a - expect(tuples).to eql([{ meta: [{ "one" => "1", "two" => "2" }] }]) + expect(tuples).to eql([{meta: [{"one" => "1", "two" => "2"}]}]) expect(tuples[0][:meta]).to be_instance_of(Array) expect(tuples[0][:meta][0]).to be_instance_of(Hash) diff --git a/spec/extensions/postgres/attribute/range_spec.rb b/spec/extensions/postgres/attribute/range_spec.rb index 54ce7508c..cf669a8a2 100644 --- a/spec/extensions/postgres/attribute/range_spec.rb +++ b/spec/extensions/postgres/attribute/range_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Attribute", :postgres do include_context "database setup" @@ -27,42 +29,42 @@ def create_ranges_table(db_type, values) it "restrict by contains (`@>`)" do expect(rel.where(pg_ranges[:range].contain(ref_value)).to_a) - .to eql([{ name: "containing" }]) + .to eql([{name: "containing"}]) end it "restrict by contained_by (`<@`)" do expect(rel.where(pg_ranges[:range].contained_by(ref_value)).to_a) - .to eql([{ name: "contained" }, { name: "empty" }]) + .to eql([{name: "contained"}, {name: "empty"}]) end it "restrict by strict left of (`<<`)" do expect(rel.where(pg_ranges[:range].left_of(ref_value)).to_a) - .to eql([{ name: "left" }]) + .to eql([{name: "left"}]) end it "restrict by strict right of (`>>`)" do expect(rel.where(pg_ranges[:range].right_of(ref_value)).to_a) - .to eql([{ name: "right" }]) + .to eql([{name: "right"}]) end it "restrict by does not extend to the right of (`&<`)" do expect(rel.where(pg_ranges[:range].ends_before(ref_value)).to_a) - .to eql([{ name: "contained" }, { name: "left" }]) + .to eql([{name: "contained"}, {name: "left"}]) end it "restrict by does not extend to the left of (`&>`)" do expect(rel.where(pg_ranges[:range].starts_after(ref_value)).to_a) - .to eql([{ name: "contained" }, { name: "right" }]) + .to eql([{name: "contained"}, {name: "right"}]) end it "restrict by overlapping (`&&`)" do expect(rel.where(pg_ranges[:range].overlap(ref_value)).to_a) - .to eql([{ name: "containing" }, { name: "contained" }]) + .to eql([{name: "containing"}, {name: "contained"}]) end it "restrict by adjacent to (`-|-`)" do expect(rel.where(pg_ranges[:range].adjacent_to(ref_value)).to_a) - .to eql([{ name: "right" }]) + .to eql([{name: "right"}]) end describe "functions" do @@ -87,15 +89,15 @@ def create_ranges_table(db_type, values) end it "lower infinity" do - expect(rel.where { range.lower_inf }.to_a).to eql([{ name: "left" }]) + expect(rel.where { range.lower_inf }.to_a).to eql([{name: "left"}]) end it "upper infinity" do - expect(rel.where { range.upper_inf }.to_a).to eql([{ name: "right" }]) + expect(rel.where { range.upper_inf }.to_a).to eql([{name: "right"}]) end it "is empty" do - expect(rel.where { range.is_empty }.to_a).to eql([{ name: "empty" }]) + expect(rel.where { range.is_empty }.to_a).to eql([{name: "empty"}]) end end end @@ -115,11 +117,11 @@ def create_ranges_table(db_type, values) let(:values) do { - containing: range_value.new(3, 9, :'[]'), - contained: range_value.new(5, 7, :'[)'), - empty: range_value.new(0, 0, :'()'), - left: range_value.new(nil, 3, :'(]'), - right: range_value.new(8, nil, :'()') + containing: range_value.new(3, 9, :[]), + contained: range_value.new(5, 7, :"[)"), + empty: range_value.new(0, 0, :"()"), + left: range_value.new(nil, 3, :"(]"), + right: range_value.new(8, nil, :"()") } end @@ -136,19 +138,19 @@ def create_ranges_table(db_type, values) containing: range_value.new( Time.parse("2017-09-25 03:00:00"), Time.parse("2017-09-25 09:00:00"), - :'[]' + :[] ), contained: range_value.new( Time.parse("2017-09-25 05:00:00"), Time.parse("2017-09-25 07:00:00"), - :'[)' + :"[)" ), empty: range_value.new( Time.parse("2017-09-25 00:00:00"), - Time.parse("2017-09-25 00:00:00"), :'()' + Time.parse("2017-09-25 00:00:00"), :"()" ), - left: range_value.new(nil, Time.parse("2017-09-25 03:00:00"), :'(]'), - right: range_value.new(Time.parse("2017-09-25 08:00:00"), nil, :'()') + left: range_value.new(nil, Time.parse("2017-09-25 03:00:00"), :"(]"), + right: range_value.new(Time.parse("2017-09-25 08:00:00"), nil, :"()") } end @@ -165,20 +167,20 @@ def create_ranges_table(db_type, values) containing: range_value.new( Date.parse("2017-10-03"), Date.parse("2017-10-09"), - :'[]' + :[] ), contained: range_value.new( Date.parse("2017-10-05"), Date.parse("2017-10-07"), - :'[)' + :"[)" ), empty: range_value.new( Date.parse("2017-10-01"), Date.parse("2017-10-01"), - :'()' + :"()" ), - left: range_value.new(nil, Date.parse("2017-10-03"), :'[)'), - right: range_value.new(Date.parse("2017-10-08"), nil, :'()') + left: range_value.new(nil, Date.parse("2017-10-03"), :"[)"), + right: range_value.new(Date.parse("2017-10-08"), nil, :"()") } end diff --git a/spec/extensions/postgres/attribute_spec.rb b/spec/extensions/postgres/attribute_spec.rb index 681785093..631383693 100644 --- a/spec/extensions/postgres/attribute_spec.rb +++ b/spec/extensions/postgres/attribute_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Attribute", :postgres do include_context "database setup" @@ -13,8 +15,8 @@ let(:people) { relations[:people] } let(:create_person) { commands[:people][:create] } - %i(json jsonb).each do |type| - describe "using arrays in #{ type }" do + %i[json jsonb].each do |type| + describe "using arrays in #{type}" do before do conn.create_table :pg_people do primary_key :id @@ -27,104 +29,104 @@ define(:update) end - create_person.(name: "John Doe", fields: [{ name: "age", value: "30" }, - { name: "height", value: 180 }]) - create_person.(name: "Jade Doe", fields: [{ name: "age", value: "25" }]) + create_person.(name: "John Doe", fields: [{name: "age", value: "30"}, + {name: "height", value: 180}]) + create_person.(name: "Jade Doe", fields: [{name: "age", value: "25"}]) end it "fetches data from jsonb array by index" do - expect(people.select { [fields.get(1).as(:field)] }.where(name: "John Doe").one). - to eql(field: { "name" => "height", "value" => 180 }) + expect(people.select { [fields.get(1).as(:field)] }.where(name: "John Doe").one) + .to eql(field: {"name" => "height", "value" => 180}) end it "fetches data from jsonb array" do - expect(people.select { fields.get(1).get_text("value").as(:height) }.where(name: "John Doe").one). - to eql(height: "180") + expect(people.select { fields.get(1).get_text("value").as(:height) }.where(name: "John Doe").one) + .to eql(height: "180") end it "fetches data with path" do - expect(people.select(people[:fields].get_text("1", "value").as(:height)).to_a). - to eql([{ height: "180" }, { height: nil }]) + expect(people.select(people[:fields].get_text("1", "value").as(:height)).to_a) + .to eql([{height: "180"}, {height: nil}]) end if type == :jsonb it "allows to query jsonb by inclusion" do - expect(people.select(:name).where { fields.contain([value: "30"]) }.one). - to eql(name: "John Doe") + expect(people.select(:name).where { fields.contain([value: "30"]) }.one) + .to eql(name: "John Doe") end it "cat project result of contains" do - expect(people.select { fields.contain([value: "30"]).as(:contains) }.to_a). - to eql([{ contains: true }, { contains: false }]) + expect(people.select { fields.contain([value: "30"]).as(:contains) }.to_a) + .to eql([{contains: true}, {contains: false}]) end it "deletes key from result" do - expect(people.select { fields.delete(0).as(:result) }.limit(1).one). - to eq(result: ["name" => "height", "value" => 180]) + expect(people.select { fields.delete(0).as(:result) }.limit(1).one) + .to eq(result: ["name" => "height", "value" => 180]) end it "deletes by path" do - expect(people.select { fields.delete("0", "name").delete("1", "name").as(:result) }.limit(1).one). - to eq(result: [{ "value" => "30" }, { "value" => 180 }]) + expect(people.select { fields.delete("0", "name").delete("1", "name").as(:result) }.limit(1).one) + .to eq(result: [{"value" => "30"}, {"value" => 180}]) end it "concatenates JSON values" do - expect(people.select { (fields + [name: "height", value: 165]).as(:result) }.by_pk(2).one). - to eq(result: [{ "name" => "age", "value" => "25" }, - { "name" => "height", "value" => 165 }]) + expect(people.select { (fields + [name: "height", value: 165]).as(:result) }.by_pk(2).one) + .to eq(result: [{"name" => "age", "value" => "25"}, + {"name" => "height", "value" => 165}]) end end end - if type == :jsonb - describe "using maps in #{ type }" do - before do - conn.create_table :pg_people do - primary_key :id - String :name - column :data, type - end - - conf.commands(:people) do - define(:create) - define(:update) - end - - create_person.(name: "John Doe", data: { age: 30, height: 180 }) - create_person.(name: "Jade Doe", data: { age: 25 }) - end + next unless type == :jsonb - it "queries data by inclusion" do - expect(people.select(:name).where { data.contain(age: 30) }.one). - to eql(name: "John Doe") + describe "using maps in #{type}" do + before do + conn.create_table :pg_people do + primary_key :id + String :name + column :data, type end - it "queries data by left inclusion" do - expect(people.select(:name).where { data.contained_by(age: 25, foo: "bar") }.one). - to eql(name: "Jade Doe") + conf.commands(:people) do + define(:create) + define(:update) end - it "checks for key presence" do - expect(people.select { data.has_key("height").as(:there) }.to_a). - to eql([{ there: true }, { there: false }]) + create_person.(name: "John Doe", data: {age: 30, height: 180}) + create_person.(name: "Jade Doe", data: {age: 25}) + end - expect(people.select(:name).where { data.has_any_key("height", "width") }.one). - to eql(name: "John Doe") + it "queries data by inclusion" do + expect(people.select(:name).where { data.contain(age: 30) }.one) + .to eql(name: "John Doe") + end - expect(people.select(:name).where { data.has_all_keys("height", "age") }.one). - to eql(name: "John Doe") - end + it "queries data by left inclusion" do + expect(people.select(:name).where { data.contained_by(age: 25, foo: "bar") }.one) + .to eql(name: "Jade Doe") + end - it "concatenates JSON values" do - expect(people.select { data.merge(height: 165).as(:result) }.by_pk(2).one). - to eql(result: { "age" => 25, "height" => 165 }) - end + it "checks for key presence" do + expect(people.select { data.has_key("height").as(:there) }.to_a) + .to eql([{there: true}, {there: false}]) - it "deletes key from result" do - expect(people.select { data.delete("height").as(:result) }.to_a). - to eql([{ result: { "age" => 30 } }, - { result: { "age" => 25 } }]) - end + expect(people.select(:name).where { data.has_any_key("height", "width") }.one) + .to eql(name: "John Doe") + + expect(people.select(:name).where { data.has_all_keys("height", "age") }.one) + .to eql(name: "John Doe") + end + + it "concatenates JSON values" do + expect(people.select { data.merge(height: 165).as(:result) }.by_pk(2).one) + .to eql(result: {"age" => 25, "height" => 165}) + end + + it "deletes key from result" do + expect(people.select { data.delete("height").as(:result) }.to_a) + .to eql([{result: {"age" => 30}}, + {result: {"age" => 25}}]) end end end @@ -143,66 +145,66 @@ define(:update) end - create_person.(name: "John Doe", emails: %w(john@doe.com john@example.com), bigids: [84]) - create_person.(name: "Jade Doe", emails: %w(jade@hotmail.com), bigids: [42]) + create_person.(name: "John Doe", emails: %w[john@doe.com john@example.com], bigids: [84]) + create_person.(name: "Jade Doe", emails: %w[jade@hotmail.com], bigids: [42]) end it "filters by email inclusion" do - expect(people.select(:name).where { emails.contain(["john@doe.com"]) }.one). - to eql(name: "John Doe") + expect(people.select(:name).where { emails.contain(["john@doe.com"]) }.one) + .to eql(name: "John Doe") end it "coerces values so that PG does not complain" do - expect(people.select(:name).where { bigids.contain([84]) }.one). - to eql(name: "John Doe") + expect(people.select(:name).where { bigids.contain([84]) }.one) + .to eql(name: "John Doe") end it "fetches element by index" do - expect(people.select { [name, emails.get(2).as(:second_email)] }.to_a). - to eql([{ name: "John Doe", second_email: "john@example.com" }, - { name: "Jade Doe", second_email: nil }]) + expect(people.select { [name, emails.get(2).as(:second_email)] }.to_a) + .to eql([{name: "John Doe", second_email: "john@example.com"}, + {name: "Jade Doe", second_email: nil}]) end it "restricts with ANY" do - expect(people.select(:name).where { bigids.any(84)}.one). - to eql(name: "John Doe") + expect(people.select(:name).where { bigids.any(84) }.one) + .to eql(name: "John Doe") end it "restricts by <@" do - expect(people.select(:name).where { bigids.contained_by((30..50).to_a) }.one). - to eql(name: "Jade Doe") + expect(people.select(:name).where { bigids.contained_by((30..50).to_a) }.one) + .to eql(name: "Jade Doe") end it "returns array length" do - expect(people.select { [name, emails.length.as(:size)] }.to_a). - to eql([{ name: "John Doe", size: 2 }, { name: "Jade Doe", size: 1 }]) + expect(people.select { [name, emails.length.as(:size)] }.to_a) + .to eql([{name: "John Doe", size: 2}, {name: "Jade Doe", size: 1}]) end it "restrict by overlapping with other array" do - expect(people.select(:name).where { emails.overlaps(%w(jade@hotmail.com)) }.one). - to eql(name: "Jade Doe") + expect(people.select(:name).where { emails.overlaps(%w[jade@hotmail.com]) }.one) + .to eql(name: "Jade Doe") - expect(people.select(:name).where { bigids.overlaps([42]) }.one). - to eql(name: "Jade Doe") + expect(people.select(:name).where { bigids.overlaps([42]) }.one) + .to eql(name: "Jade Doe") end it "removes element by value" do - expect(people.select { emails.remove_value("john@example.com").as(:emails) }.to_a). - to eq([{ emails: %w(john@doe.com) }, { emails: %w(jade@hotmail.com) }]) + expect(people.select { emails.remove_value("john@example.com").as(:emails) }.to_a) + .to eq([{emails: %w[john@doe.com]}, {emails: %w[jade@hotmail.com]}]) - expect(people.select(:name).where { bigids.remove_value(100).contain([42]) }.one). - to eql(name: "Jade Doe") + expect(people.select(:name).where { bigids.remove_value(100).contain([42]) }.one) + .to eql(name: "Jade Doe") end it "joins values" do - expect(people.select { emails.join(",").as(:emails) }.to_a). - to eql([{ emails: "john@doe.com,john@example.com" }, - { emails: "jade@hotmail.com" }]) + expect(people.select { emails.join(",").as(:emails) }.to_a) + .to eql([{emails: "john@doe.com,john@example.com"}, + {emails: "jade@hotmail.com"}]) end it "concatenates arrays" do - expect(people.select { (emails + %w(foo@bar.com)).as(:emails) }.where { name.is("Jade Doe") }.one). - to eq(emails: %w(jade@hotmail.com foo@bar.com)) + expect(people.select { (emails + %w[foo@bar.com]).as(:emails) }.where { name.is("Jade Doe") }.one) + .to eq(emails: %w[jade@hotmail.com foo@bar.com]) end end @@ -222,11 +224,11 @@ define(:update) end - create_person.(name: "John Wilkson",ltree_tags: ltree("Bottom"), parents_tags: [ltree("Top"), ltree("Top.Building")]) - create_person.(name: "John Wayne",ltree_tags: ltree("Bottom.Countries"), parents_tags: [ltree("Left"), ltree("Left.Parks")]) - create_person.(name: "John Fake",ltree_tags: ltree("Bottom.Cities"), parents_tags: [ltree("Top.Building.EmpireState"), ltree("Top.Building.EmpireState.381")]) - create_person.(name: "John Bros",ltree_tags: ltree("Bottom.Cities.Melbourne"), parents_tags: [ltree("Right.Cars.Ford"), ltree("Right.Cars.Nissan")]) - create_person.(name: "John Wick",ltree_tags: ltree("Bottom.Countries.Australia")) + create_person.(name: "John Wilkson", ltree_tags: ltree("Bottom"), parents_tags: [ltree("Top"), ltree("Top.Building")]) + create_person.(name: "John Wayne", ltree_tags: ltree("Bottom.Countries"), parents_tags: [ltree("Left"), ltree("Left.Parks")]) + create_person.(name: "John Fake", ltree_tags: ltree("Bottom.Cities"), parents_tags: [ltree("Top.Building.EmpireState"), ltree("Top.Building.EmpireState.381")]) + create_person.(name: "John Bros", ltree_tags: ltree("Bottom.Cities.Melbourne"), parents_tags: [ltree("Right.Cars.Ford"), ltree("Right.Cars.Nissan")]) + create_person.(name: "John Wick", ltree_tags: ltree("Bottom.Countries.Australia")) create_person.(name: "Jade Doe", ltree_tags: ltree("Bottom.Countries.Australia.Brasil")) end @@ -235,103 +237,102 @@ def ltree(label_path) end it "matches regular expression" do - expect(people.select(:name).where { ltree_tags.match("Bottom.Cities") }.one). - to eql(name: "John Fake") + expect(people.select(:name).where { ltree_tags.match("Bottom.Cities") }.one) + .to eql(name: "John Fake") end it "matches any lquery" do - expect(people.select(:name).where { ltree_tags.match_any(["Bottom", "Bottom.Cities.*"]) }.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Fake"}, {:name=>"John Bros"}]) + expect(people.select(:name).where { ltree_tags.match_any(["Bottom", "Bottom.Cities.*"]) }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Fake"}, {name: "John Bros"}]) end it "matches any lquery using string" do - expect(people.select(:name).where { ltree_tags.match_any("Bottom,Bottom.Cities.*") }.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Fake"}, {:name=>"John Bros"}]) + expect(people.select(:name).where { ltree_tags.match_any("Bottom,Bottom.Cities.*") }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Fake"}, {name: "John Bros"}]) end it "match ltextquery" do - expect(people.select(:name).where { ltree_tags.match_ltextquery("Countries & Brasil") }.one). - to eql(:name=>"Jade Doe") + expect(people.select(:name).where { ltree_tags.match_ltextquery("Countries & Brasil") }.one) + .to eql(name: "Jade Doe") end describe "concatenation" do it "concatenates ltrees" do - expect(people.select { (ltree_tags + ROM::Types::Values::TreePath.new("Moscu")).as(:ltree_tags) }.where { name.is("Jade Doe") }.one). - to eq(ltree_tags: ROM::Types::Values::TreePath.new("Bottom.Countries.Australia.Brasil.Moscu")) + expect(people.select { (ltree_tags + ROM::Types::Values::TreePath.new("Moscu")).as(:ltree_tags) }.where { name.is("Jade Doe") }.one) + .to eq(ltree_tags: ROM::Types::Values::TreePath.new("Bottom.Countries.Australia.Brasil.Moscu")) end it "concatenates strings" do - expect(people.select { (ltree_tags + "Moscu").as(:ltree_tags) }.where { name.is("Jade Doe") }.one). - to eq(ltree_tags: ROM::Types::Values::TreePath.new("Bottom.Countries.Australia.Brasil.Moscu")) + expect(people.select { (ltree_tags + "Moscu").as(:ltree_tags) }.where { name.is("Jade Doe") }.one) + .to eq(ltree_tags: ROM::Types::Values::TreePath.new("Bottom.Countries.Australia.Brasil.Moscu")) end end it "allow query by descendant of ltree" do - expect(people.select(:name).where { ltree_tags.descendant("Bottom.Countries") }.to_a). - to eql([{:name=>"John Wayne"}, {:name=>"John Wick"}, {:name=>"Jade Doe"}]) + expect(people.select(:name).where { ltree_tags.descendant("Bottom.Countries") }.to_a) + .to eql([{name: "John Wayne"}, {name: "John Wick"}, {name: "Jade Doe"}]) end it "allow query by any descendant contain in the array" do - expect(people.select(:name).where { ltree_tags.contain_descendant(["Bottom.Cities"]) }.to_a). - to eql([{:name=>"John Fake"}, {:name=>"John Bros"}]) + expect(people.select(:name).where { ltree_tags.contain_descendant(["Bottom.Cities"]) }.to_a) + .to eql([{name: "John Fake"}, {name: "John Bros"}]) end it "allow query by ascendant of ltree" do - expect(people.select(:name).where { ltree_tags.ascendant("Bottom.Countries.Australia.Brasil") }.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Wayne"}, {:name=>"John Wick"}, {:name=>"Jade Doe"}]) + expect(people.select(:name).where { ltree_tags.ascendant("Bottom.Countries.Australia.Brasil") }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Wayne"}, {name: "John Wick"}, {name: "Jade Doe"}]) end it "allow query by any ascendant contain in the array" do - expect(people.select(:name).where { ltree_tags.contain_ascendant(["Bottom.Cities"]) }.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Fake"}]) + expect(people.select(:name).where { ltree_tags.contain_ascendant(["Bottom.Cities"]) }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Fake"}]) end describe "Using with in array ltree[]" do it "contains any ltextquery" do - expect(people.select(:name).where { parents_tags.contain_any_ltextquery("Parks")}.to_a). - to eql([{:name=>"John Wayne"}]) + expect(people.select(:name).where { parents_tags.contain_any_ltextquery("Parks") }.to_a) + .to eql([{name: "John Wayne"}]) end it "contains any ancestor" do - expect(people.select(:name).where { parents_tags.contain_ancestor("Top.Building.EmpireState.381")}.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Fake"}]) + expect(people.select(:name).where { parents_tags.contain_ancestor("Top.Building.EmpireState.381") }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Fake"}]) end it "contains any descendant" do - expect(people.select(:name).where { parents_tags.contain_descendant("Left")}.to_a). - to eql([{:name=>"John Wayne"}]) + expect(people.select(:name).where { parents_tags.contain_descendant("Left") }.to_a) + .to eql([{name: "John Wayne"}]) end it "allow to filter by matching lquery" do - expect(people.select(:name).where { parents_tags.match("T*|Left.*")}.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Wayne"}, {:name=>"John Fake"}]) + expect(people.select(:name).where { parents_tags.match("T*|Left.*") }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Wayne"}, {name: "John Fake"}]) end it "allow to filter by matching any lquery conatin in the array" do - expect(people.select(:name).where { parents_tags.match_any(["Top.*", "Left.*"])}.to_a). - to eql([{:name=>"John Wilkson"}, {:name=>"John Wayne"}, {:name=>"John Fake"}]) + expect(people.select(:name).where { parents_tags.match_any(["Top.*", "Left.*"]) }.to_a) + .to eql([{name: "John Wilkson"}, {name: "John Wayne"}, {name: "John Fake"}]) end it "finds first array entry that is an ancestor of ltree" do - expect(people.select(:name).where { parents_tags.find_ancestor("Left.Parks").not(nil)}.to_a). - to eql([{:name=>"John Wayne"}]) + expect(people.select(:name).where { parents_tags.find_ancestor("Left.Parks").not(nil) }.to_a) + .to eql([{name: "John Wayne"}]) end it "finds first array entry that is an descendant of ltree" do - expect(people.select(:name).where { parents_tags.find_descendant("Right.Cars").not(nil)}.to_a). - to eql([name: "John Bros"]) + expect(people.select(:name).where { parents_tags.find_descendant("Right.Cars").not(nil) }.to_a) + .to eql([name: "John Bros"]) end it "finds first array entry that is a match (lquery)" do - expect(people.select(:name).where { parents_tags.match_any_lquery("Right.*").not(nil)}.to_a). - to eql([{:name=>"John Bros"}]) + expect(people.select(:name).where { parents_tags.match_any_lquery("Right.*").not(nil) }.to_a) + .to eql([{name: "John Bros"}]) end it "finds first array entry that is a match (ltextquery)" do - expect(people.select(:name).where { parents_tags.match_any_ltextquery("EmpireState").not(nil)}.to_a). - to eql([{:name=>"John Fake"}]) + expect(people.select(:name).where { parents_tags.match_any_ltextquery("EmpireState").not(nil) }.to_a) + .to eql([{name: "John Fake"}]) end end - end end diff --git a/spec/extensions/postgres/integration_spec.rb b/spec/extensions/postgres/integration_spec.rb index 42227f8ff..19680743d 100644 --- a/spec/extensions/postgres/integration_spec.rb +++ b/spec/extensions/postgres/integration_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "PostgreSQL extension", :postgres do include_context "database setup" @@ -49,7 +51,7 @@ describe "using retrurning" do let(:create_person) { commands[:people][:create] } let(:update_person) { commands[:people][:update] } - let(:composite_relation) { people_relation >> -> r { r.map { |x| { name: x.fetch(:name).upcase } } } } + let(:composite_relation) { people_relation >> -> r { r.map { |x| {name: x.fetch(:name).upcase} } } } context "with pipeline" do it "works with create" do @@ -90,8 +92,8 @@ let(:people) { commands[:people] } it "reads jsonb values as plain hashes" do - people[:create].call(name: "John Doe", attributes: { foo: "bar" }) - expect(people_relation.to_a).to eql([id: 1, name: "John Doe", attributes: { "foo" => "bar" }]) + people[:create].call(name: "John Doe", attributes: {foo: "bar"}) + expect(people_relation.to_a).to eql([id: 1, name: "John Doe", attributes: {"foo" => "bar"}]) expect(people_relation.to_a[0][:attributes].class).to be(Hash) end diff --git a/spec/extensions/postgres/type_serializer_spec.rb b/spec/extensions/postgres/type_serializer_spec.rb index 7b1455434..7d9594cfa 100644 --- a/spec/extensions/postgres/type_serializer_spec.rb +++ b/spec/extensions/postgres/type_serializer_spec.rb @@ -1,12 +1,14 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::TypeSerializer[:postgres] do subject(:serializer) { ROM::SQL::TypeSerializer[:postgres] } it "serializes PG types" do types = { - "uuid" => ROM::SQL::Postgres::Types::UUID, + "uuid" => ROM::SQL::Postgres::Types::UUID, "money" => ROM::SQL::Postgres::Types::Money, "bytea" => ROM::SQL::Postgres::Types::Bytea, - "json" => ROM::SQL::Postgres::Types::JSON, + "json" => ROM::SQL::Postgres::Types::JSON, "jsonb" => ROM::SQL::Postgres::Types::JSONB, "hstore" => ROM::SQL::Postgres::Types::HStore, "inet" => ROM::SQL::Postgres::Types::IPAddress, diff --git a/spec/extensions/postgres/types_meta_spec.rb b/spec/extensions/postgres/types_meta_spec.rb index 9de1ea713..9249e139c 100644 --- a/spec/extensions/postgres/types_meta_spec.rb +++ b/spec/extensions/postgres/types_meta_spec.rb @@ -1,21 +1,23 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Postgres::Types" do it "has the correct meta db_type" do { "uuid" => ROM::SQL::Postgres::Types::UUID, "hstore" => ROM::SQL::Postgres::Types::HStore, - "bytea" => ROM::SQL::Postgres::Types::Bytea, + "bytea" => ROM::SQL::Postgres::Types::Bytea, "money" => ROM::SQL::Postgres::Types::Money, "xml" => ROM::SQL::Postgres::Types::XML, "inet" => ROM::SQL::Postgres::Types::IPAddress, "json" => ROM::SQL::Postgres::Types::JSON, "jsonb" => ROM::SQL::Postgres::Types::JSONB, "point" => ROM::SQL::Postgres::Types::Point, - "line" => ROM::SQL::Postgres::Types::Line, + "line" => ROM::SQL::Postgres::Types::Line, "circle" => ROM::SQL::Postgres::Types::Circle, - "box" => ROM::SQL::Postgres::Types::Box, - "lseg" => ROM::SQL::Postgres::Types::LineSegment, + "box" => ROM::SQL::Postgres::Types::Box, + "lseg" => ROM::SQL::Postgres::Types::LineSegment, "polygon" => ROM::SQL::Postgres::Types::Polygon, - "path" => ROM::SQL::Postgres::Types::Path, + "path" => ROM::SQL::Postgres::Types::Path, "integer[]" => ROM::SQL::Postgres::Types.Array("integer") }.each do |meta_type, type| expect(type.meta[:db_type]).to eq meta_type diff --git a/spec/extensions/postgres/types_spec.rb b/spec/extensions/postgres/types_spec.rb index 1e6c9a512..0ea2b6852 100644 --- a/spec/extensions/postgres/types_spec.rb +++ b/spec/extensions/postgres/types_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "securerandom" RSpec.describe "ROM::SQL::Postgres::Types" do @@ -5,7 +7,7 @@ describe "ROM::SQL::Types::PG::JSON" do it "coerces to pg json hash" do - input = { foo: "bar" } + input = {foo: "bar"} expect(ROM::SQL::Types::PG::JSON[input]).to eql(Sequel.pg_json(input)) end @@ -58,7 +60,7 @@ describe ROM::SQL::Types::PG::JSON do it "coerces to pg json hash" do - input = { foo: "bar" } + input = {foo: "bar"} output = described_class[input] expect(output).to be_instance_of(Sequel::Postgres::JSONHash) @@ -99,7 +101,7 @@ describe ROM::SQL::Types::PG::JSONB do it "coerces to pg jsonb hash" do - input = { foo: "bar" } + input = {foo: "bar"} output = described_class[input] expect(output).to be_instance_of(Sequel::Postgres::JSONBHash) @@ -170,7 +172,7 @@ end describe ROM::SQL::Types::PG::HStore do - let(:mapping) { Hash["hot" => "cold"] } + let(:mapping) { {"hot" => "cold"} } let(:read_type) { described_class.meta[:read] } it "covertss data to Sequel::Postgres::HStore" do @@ -241,7 +243,7 @@ describe ROM::SQL::Types::PG::Polygon do let(:first) { values::Point.new(8.5, 30.5) } - let(:second) {values::Point.new(7.5, 20.5) } + let(:second) { values::Point.new(7.5, 20.5) } let(:third) { values::Point.new(6.5, 10.5) } let(:polygon) { [first, second, third] } @@ -278,7 +280,7 @@ end describe ROM::SQL::Types::PG::Int4Range do - let(:range) { values::Range.new(1, 4, :'[)') } + let(:range) { values::Range.new(1, 4, :"[)") } it "serialize a range to a string" do expect(described_class[range]).to eql "[1,4)" @@ -286,19 +288,19 @@ it "read serialized format" do expect(described_class.meta[:read]["(42, 64]"]).to eql( - values::Range.new(42, 64, :'(]') + values::Range.new(42, 64, :"(]") ) end it "read an empty value" do expect(described_class.meta[:read]["empty"]).to eql( - values::Range.new(nil, nil, :'[]') + values::Range.new(nil, nil, :[]) ) end it "read an unbounded range" do expect(described_class.meta[:read]["(,)"]).to eql( - values::Range.new(nil, nil, :'()') + values::Range.new(nil, nil, :"()") ) end end diff --git a/spec/extensions/sqlite/types_spec.rb b/spec/extensions/sqlite/types_spec.rb index d150ed833..2d6cfa435 100644 --- a/spec/extensions/sqlite/types_spec.rb +++ b/spec/extensions/sqlite/types_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Types" do describe "ROM::SQL::Types::SQLite::Object" do let(:type) { ROM::SQL::Types::SQLite::Object } diff --git a/spec/fixtures/migrations/20150403090603_create_carrots.rb b/spec/fixtures/migrations/20150403090603_create_carrots.rb index f391e921d..fb413698e 100644 --- a/spec/fixtures/migrations/20150403090603_create_carrots.rb +++ b/spec/fixtures/migrations/20150403090603_create_carrots.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + ROM::SQL.migration do change do create_table :carrots do diff --git a/spec/integration/associations/many_to_many/custom_fks_spec.rb b/spec/integration/associations/many_to_many/custom_fks_spec.rb index e233ead13..741281c72 100644 --- a/spec/integration/associations/many_to_many/custom_fks_spec.rb +++ b/spec/integration/associations/many_to_many/custom_fks_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::ManyToMany, "#call" do include_context "users" before do - inferrable_relations.concat %i(puzzles puzzle_solvers) + inferrable_relations.concat %i[puzzles puzzle_solvers] end subject(:assoc) do @@ -59,18 +61,18 @@ it "prepares joined relations using custom FK" do relation = assoc.().order(puzzles[:text].qualified, puzzle_solvers[:solver_id].qualified) - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:puzzles, :id), - Sequel.qualify(:puzzles, :text), - Sequel.qualify(:puzzle_solvers, :solver_id)]) - - expect(relation.to_a). - to eql([ - { id: 1, solver_id: 2, text: "P1" }, - { id: 2, solver_id: 1, text: "P2" }, - { id: 2, solver_id: 2, text: "P2" }, - { id: 3, solver_id: 1, text: "P3" } - ]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:puzzles, :id), + Sequel.qualify(:puzzles, :text), + Sequel.qualify(:puzzle_solvers, :solver_id)]) + + expect(relation.to_a) + .to eql([ + {id: 1, solver_id: 2, text: "P1"}, + {id: 2, solver_id: 1, text: "P2"}, + {id: 2, solver_id: 2, text: "P2"}, + {id: 3, solver_id: 1, text: "P3"} + ]) end end end diff --git a/spec/integration/associations/many_to_many/from_view_spec.rb b/spec/integration/associations/many_to_many/from_view_spec.rb index 11a749cf0..9fc190189 100644 --- a/spec/integration/associations/many_to_many/from_view_spec.rb +++ b/spec/integration/associations/many_to_many/from_view_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::ManyToMany, "#call" do include_context "users" before do - inferrable_relations.concat %i(puzzles puzzle_solvers) + inferrable_relations.concat %i[puzzles puzzle_solvers] end subject(:assoc) do @@ -78,17 +80,17 @@ it "prepares joined relations using custom FK" do relation = assoc.().order(puzzles[:text].qualified, puzzle_solvers[:user_id].qualified) - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:puzzles, :id), - Sequel.qualify(:puzzles, :text), - Sequel.qualify(:puzzles, :solved), - Sequel.qualify(:puzzle_solvers, :user_id)]) - - expect(relation.to_a). - to eql([ - { id: 2, user_id: 1, solved: db_true, text: "P2" }, - { id: 2, user_id: 2, solved: db_true, text: "P2" } - ]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:puzzles, :id), + Sequel.qualify(:puzzles, :text), + Sequel.qualify(:puzzles, :solved), + Sequel.qualify(:puzzle_solvers, :user_id)]) + + expect(relation.to_a) + .to eql([ + {id: 2, user_id: 1, solved: db_true, text: "P2"}, + {id: 2, user_id: 2, solved: db_true, text: "P2"} + ]) end end end diff --git a/spec/integration/associations/many_to_many/self_ref_spec.rb b/spec/integration/associations/many_to_many/self_ref_spec.rb index 130fb469d..ad05b5876 100644 --- a/spec/integration/associations/many_to_many/self_ref_spec.rb +++ b/spec/integration/associations/many_to_many/self_ref_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::ManyToMany, "#call" do include_context "database setup" @@ -55,7 +57,7 @@ positions.insert(manager_id: jane, participant_id: fred) - expect(assoc.().to_a).to eql([{ id: 1, name: "Jane", participant_id: 2}]) + expect(assoc.().to_a).to eql([{id: 1, name: "Jane", participant_id: 2}]) end end end diff --git a/spec/integration/associations/many_to_many_spec.rb b/spec/integration/associations/many_to_many_spec.rb index 86f16e6ed..5e97810b3 100644 --- a/spec/integration/associations/many_to_many_spec.rb +++ b/spec/integration/associations/many_to_many_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::ManyToMany, helpers: true do include_context "users and tasks" @@ -41,10 +43,10 @@ it "prepares joined relations" do relation = assoc.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:tags, :id), - Sequel.qualify(:tags, :name), - Sequel.qualify(:task_tags, :task_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:tags, :id), + Sequel.qualify(:tags, :name), + Sequel.qualify(:task_tags, :task_id)]) expect(relation.to_a).to eql([id: 1, name: "important", task_id: 1]) end end @@ -57,10 +59,10 @@ it "prepares joined relations through other association" do relation = assoc.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:tags, :id), - Sequel.qualify(:tags, :name), - Sequel.qualify(:tasks, :user_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:tags, :id), + Sequel.qualify(:tags, :name), + Sequel.qualify(:tasks, :user_id)]) expect(relation.to_a).to eql([id: 1, name: "important", user_id: 2]) end end @@ -73,9 +75,9 @@ end it "maintains original relation" do - relation = tags. - select_append(tags[:name].as(:tag)). - eager_load(assoc).call(tasks.call) + relation = tags + .select_append(tags[:name].as(:tag)) + .eager_load(assoc).call(tasks.call) expect(relation.to_a).to eql([id: 1, tag: "important", name: "important", task_id: 1]) end @@ -84,22 +86,22 @@ conn[:tags].insert id: 2, name: "boring" conn[:task_tags].insert(tag_id: 2, task_id: 1) - relation = tags. - order(tags[:name].qualified). - eager_load(assoc).call(tasks.call) + relation = tags + .order(tags[:name].qualified) + .eager_load(assoc).call(tasks.call) - expect(relation.to_a). - to eql([ - { id: 2, name: "boring", task_id: 1 }, - { id: 1, name: "important", task_id: 1 } - ]) + expect(relation.to_a) + .to eql([ + {id: 2, name: "boring", task_id: 1}, + {id: 1, name: "important", task_id: 1} + ]) end end end context "with two associations pointing to the same target relation" do before do - inferrable_relations.concat %i(users_tasks) + inferrable_relations.concat %i[users_tasks] end before do diff --git a/spec/integration/associations/many_to_one/custom_fks_spec.rb b/spec/integration/associations/many_to_one/custom_fks_spec.rb index e93c21a75..f0565880b 100644 --- a/spec/integration/associations/many_to_one/custom_fks_spec.rb +++ b/spec/integration/associations/many_to_one/custom_fks_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Associations::ManyToOne, "#call" do include_context "database setup" before do - inferrable_relations.concat %i(destinations flights) + inferrable_relations.concat %i[destinations flights] end let(:assoc_from) { relations[:flights].associations[:from] } @@ -44,19 +46,19 @@ it "prepares joined relations using correct FKs based on association aliases" do relation = assoc_from.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:destinations, :id), - Sequel.qualify(:destinations, :name), - Sequel.qualify(:flights, :id).as(:flight_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:destinations, :id), + Sequel.qualify(:destinations, :name), + Sequel.qualify(:flights, :id).as(:flight_id)]) expect(relation.first).to eql(id: 1, name: "FROM", flight_id: 1) relation = assoc_to.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:destinations, :id), - Sequel.qualify(:destinations, :name), - Sequel.qualify(:flights, :id).as(:flight_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:destinations, :id), + Sequel.qualify(:destinations, :name), + Sequel.qualify(:flights, :id).as(:flight_id)]) expect(relation.first).to eql(id: 2, name: "TO", flight_id: 1) end diff --git a/spec/integration/associations/many_to_one/from_view_spec.rb b/spec/integration/associations/many_to_one/from_view_spec.rb index 435332889..deed20942 100644 --- a/spec/integration/associations/many_to_one/from_view_spec.rb +++ b/spec/integration/associations/many_to_one/from_view_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Associations::ManyToOne, "#call" do include_context "database setup" before do - inferrable_relations.concat %i(destinations flights) + inferrable_relations.concat %i[destinations flights] end let(:assoc_inter) { relations[:flights].associations[:inter_destination] } @@ -72,22 +74,22 @@ it "prepares joined relations using custom view in target relation" do relation = assoc_inter.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:destinations, :id), - Sequel.qualify(:destinations, :name), - Sequel.qualify(:destinations, :intermediate), - Sequel.qualify(:flights, :id).as(:flight_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:destinations, :id), + Sequel.qualify(:destinations, :name), + Sequel.qualify(:destinations, :intermediate), + Sequel.qualify(:flights, :id).as(:flight_id)]) expect(relation.first).to eql(id: 2, intermediate: db_true, name: "Intermediate", flight_id: 1) expect(relation.count).to be(1) relation = assoc_final.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:destinations, :id), - Sequel.qualify(:destinations, :name), - Sequel.qualify(:destinations, :intermediate), - Sequel.qualify(:flights, :id).as(:flight_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:destinations, :id), + Sequel.qualify(:destinations, :name), + Sequel.qualify(:destinations, :intermediate), + Sequel.qualify(:flights, :id).as(:flight_id)]) expect(relation.first).to eql(id: 1, intermediate: db_false, name: "Final", flight_id: 2) expect(relation.count).to be(1) diff --git a/spec/integration/associations/many_to_one/self_ref_spec.rb b/spec/integration/associations/many_to_one/self_ref_spec.rb index 12caa4746..88ca1ff51 100644 --- a/spec/integration/associations/many_to_one/self_ref_spec.rb +++ b/spec/integration/associations/many_to_one/self_ref_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Associations::ManyToOne, "#call" do diff --git a/spec/integration/associations/many_to_one_spec.rb b/spec/integration/associations/many_to_one_spec.rb index 4a3652397..416ce0d03 100644 --- a/spec/integration/associations/many_to_one_spec.rb +++ b/spec/integration/associations/many_to_one_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::ManyToOne, helpers: true do with_adapters do context "common name conventions" do @@ -32,20 +34,20 @@ it "prepares joined relations" do relation = assoc.(preload: false) - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:users, :id), - Sequel.qualify(:users, :name), - Sequel.qualify(:tasks, :id).as(:task_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:users, :id), + Sequel.qualify(:users, :name), + Sequel.qualify(:tasks, :id).as(:task_id)]) expect(relation.where(user_id: 1).one).to eql(id: 1, task_id: 2, name: "Jane") expect(relation.where(user_id: 2).one).to eql(id: 2, task_id: 1, name: "Joe") - expect(relation.to_a). - to eql([ - { id: 1, task_id: 2, name: "Jane" }, - { id: 2, task_id: 1, name: "Joe" } - ]) + expect(relation.to_a) + .to eql([ + {id: 1, task_id: 2, name: "Jane"}, + {id: 2, task_id: 1, name: "Joe"} + ]) end end @@ -53,21 +55,21 @@ it "preloads relation based on association" do relation = users.eager_load(assoc).call(tasks.call) - expect(relation.to_a).to eql([{ id: 1, name: "Jane" }, { id: 2, name: "Joe" }]) + expect(relation.to_a).to eql([{id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end it "maintains original relation" do users.accounts.insert(user_id: 2, number: "31", balance: 0) - relation = users. - join(:accounts, user_id: :id). - select_append(users.accounts[:number].as(:account_num)). - order(:account_num). - eager_load(assoc).call(tasks.call) + relation = users + .join(:accounts, user_id: :id) + .select_append(users.accounts[:number].as(:account_num)) + .order(:account_num) + .eager_load(assoc).call(tasks.call) - expect(relation.to_a). - to eql([{ id: 2, name: "Joe", account_num: "31" }, - { id: 1, name: "Jane", account_num: "42" }]) + expect(relation.to_a) + .to eql([{id: 2, name: "Joe", account_num: "31"}, + {id: 1, name: "Jane", account_num: "42"}]) end end end @@ -93,14 +95,14 @@ it "prepares joined relations" do relation = assoc.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:users, :id), - Sequel.qualify(:users, :name), - Sequel.qualify(:posts, :post_id)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:users, :id), + Sequel.qualify(:users, :name), + Sequel.qualify(:posts, :post_id)]) expect(relation.order(:id).to_a).to eql([ - { id: 1, name: "Jane", post_id: 2 }, - { id: 2, name: "Joe", post_id: 1 } + {id: 1, name: "Jane", post_id: 2}, + {id: 2, name: "Joe", post_id: 1} ]) expect(relation.where(author_id: 1).to_a).to eql( diff --git a/spec/integration/associations/one_to_many/custom_fks_spec.rb b/spec/integration/associations/one_to_many/custom_fks_spec.rb index fd1047997..9866b1a15 100644 --- a/spec/integration/associations/one_to_many/custom_fks_spec.rb +++ b/spec/integration/associations/one_to_many/custom_fks_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Associations::OneToMany, "#call" do include_context "users" before do - inferrable_relations.concat %i(puzzles) + inferrable_relations.concat %i[puzzles] end subject(:assoc) do @@ -42,11 +44,11 @@ it "prepares joined relations using custom FK" do relation = assoc.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:puzzles, :id), - Sequel.qualify(:puzzles, :author_id), - Sequel.qualify(:puzzles, :solver_id), - Sequel.qualify(:puzzles, :text)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:puzzles, :id), + Sequel.qualify(:puzzles, :author_id), + Sequel.qualify(:puzzles, :solver_id), + Sequel.qualify(:puzzles, :text)]) expect(relation.first).to eql(id: 2, author_id: 2, solver_id: 1, text: "P2") end diff --git a/spec/integration/associations/one_to_many/from_view_spec.rb b/spec/integration/associations/one_to_many/from_view_spec.rb index 1cac9f447..82f5fe764 100644 --- a/spec/integration/associations/one_to_many/from_view_spec.rb +++ b/spec/integration/associations/one_to_many/from_view_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Associations::OneToMany, "#call" do include_context "users" before do - inferrable_relations.concat %i(puzzles) + inferrable_relations.concat %i[puzzles] end subject(:assoc) do @@ -50,11 +52,11 @@ it "prepares joined relations using custom view" do relation = assoc.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:puzzles, :id), - Sequel.qualify(:puzzles, :user_id), - Sequel.qualify(:puzzles, :text), - Sequel.qualify(:puzzles, :solved)]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:puzzles, :id), + Sequel.qualify(:puzzles, :user_id), + Sequel.qualify(:puzzles, :text), + Sequel.qualify(:puzzles, :solved)]) expect(relation.count).to be(1) expect(relation.first).to eql(id: 2, user_id: 2, solved: db_true, text: "P2") diff --git a/spec/integration/associations/one_to_many/self_ref_spec.rb b/spec/integration/associations/one_to_many/self_ref_spec.rb index 9f710a8d0..bf376c807 100644 --- a/spec/integration/associations/one_to_many/self_ref_spec.rb +++ b/spec/integration/associations/one_to_many/self_ref_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Associations::OneToMany, "#call" do include_context "database setup" before do - inferrable_relations.concat %i(categories) + inferrable_relations.concat %i[categories] end subject(:assoc) do @@ -38,17 +40,17 @@ it "prepares joined relations using custom FK for a self-ref association" do relation = assoc.() - expect(relation.schema.map(&:to_sql_name)). - to eql([Sequel.qualify(:categories, :id), - Sequel.qualify(:categories, :parent_id), - Sequel.qualify(:categories, :name)]) - - expect(relation.to_a). - to eql([ - { id: 3, parent_id: 2, name: "C3" }, - { id: 4, parent_id: 1, name: "C4" }, - { id: 5, parent_id: 1, name: "C5" } - ]) + expect(relation.schema.map(&:to_sql_name)) + .to eql([Sequel.qualify(:categories, :id), + Sequel.qualify(:categories, :parent_id), + Sequel.qualify(:categories, :name)]) + + expect(relation.to_a) + .to eql([ + {id: 3, parent_id: 2, name: "C3"}, + {id: 4, parent_id: 1, name: "C4"}, + {id: 5, parent_id: 1, name: "C5"} + ]) end end end diff --git a/spec/integration/associations/one_to_many_spec.rb b/spec/integration/associations/one_to_many_spec.rb index f974550e4..84a9155d7 100644 --- a/spec/integration/associations/one_to_many_spec.rb +++ b/spec/integration/associations/one_to_many_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::OneToMany, helpers: true do include_context "users and tasks" @@ -26,8 +28,8 @@ describe "#associate" do it "merges FKs into tuples" do - child = { name: "Child" } - parent = { id: 312, name: "Parent "} + child = {name: "Child"} + parent = {id: 312, name: "Parent "} expect(assoc.associate(child, parent)).to eql(user_id: 312, name: "Child") end @@ -40,16 +42,16 @@ expect(relation.schema.map(&:name)).to eql(%i[id user_id title]) expect(relation.order(tasks[:id].qualified).to_a).to eql([ - { id: 1, user_id: 2, title: "Joe's task" }, - { id: 2, user_id: 1, title: "Jane's task" } + {id: 1, user_id: 2, title: "Joe's task"}, + {id: 2, user_id: 1, title: "Jane's task"} ]) expect(relation.where(user_id: 1).to_a).to eql([ - { id: 2, user_id: 1, title: "Jane's task" } + {id: 2, user_id: 1, title: "Jane's task"} ]) expect(relation.where(user_id: 2).to_a).to eql([ - { id: 1, user_id: 2, title: "Joe's task" } + {id: 1, user_id: 2, title: "Joe's task"} ]) end end @@ -59,27 +61,27 @@ relation = tasks.eager_load(assoc).call(users.call) expect(relation.to_a).to eql([ - { id: 1, user_id: 2, title: "Joe's task" }, - { id: 2, user_id: 1, title: "Jane's task" } + {id: 1, user_id: 2, title: "Joe's task"}, + {id: 2, user_id: 1, title: "Jane's task"} ]) end it "maintains original relation" do - relation = tasks. - join(:task_tags, tag_id: :id). - select_append(tasks.task_tags[:tag_id].qualified). - eager_load(assoc).call(users.call) + relation = tasks + .join(:task_tags, tag_id: :id) + .select_append(tasks.task_tags[:tag_id].qualified) + .eager_load(assoc).call(users.call) - expect(relation.to_a).to eql([{ id: 1, user_id: 2, title: "Joe's task", tag_id: 1 }]) + expect(relation.to_a).to eql([{id: 1, user_id: 2, title: "Joe's task", tag_id: 1}]) end it "respects custom order" do - relation = tasks. - order(tasks[:title].qualified). - eager_load(assoc).call(users.call) + relation = tasks + .order(tasks[:title].qualified) + .eager_load(assoc).call(users.call) - expect(relation.to_a). - to eql([{ id: 2, user_id: 1, title: "Jane's task" }, { id: 1, user_id: 2, title: "Joe's task" }]) + expect(relation.to_a) + .to eql([{id: 2, user_id: 1, title: "Jane's task"}, {id: 1, user_id: 2, title: "Joe's task"}]) end end end diff --git a/spec/integration/associations/one_to_one_spec.rb b/spec/integration/associations/one_to_one_spec.rb index d539996e2..3f8dfb430 100644 --- a/spec/integration/associations/one_to_one_spec.rb +++ b/spec/integration/associations/one_to_one_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::OneToOne, helpers: true do include_context "users" include_context "accounts" @@ -37,13 +39,13 @@ # TODO: this if clause should be removed when (and if) https://github.com/xerial/sqlite-jdbc/issues/112 # will be resolved. See https://github.com/rom-rb/rom-sql/issues/49 for details if jruby? && sqlite?(example) - expect(relation.to_a). - to eql([{ id: 1, user_id: 1, number: "42", balance: 10_000 }, - { id: 2, user_id: 1, number: "43", balance: -273.15 }]) + expect(relation.to_a) + .to eql([{id: 1, user_id: 1, number: "42", balance: 10_000}, + {id: 2, user_id: 1, number: "43", balance: -273.15}]) else - expect(relation.to_a). - to eql([{ id: 1, user_id: 1, number: "42", balance: 10_000.to_d }, - { id: 2, user_id: 1, number: "43", balance: -273.15.to_d }]) + expect(relation.to_a) + .to eql([{id: 1, user_id: 1, number: "42", balance: 10_000.to_d}, + {id: 2, user_id: 1, number: "43", balance: -273.15.to_d}]) end end end @@ -55,13 +57,13 @@ # TODO: this if caluse should be removed when (and if) https://github.com/xerial/sqlite-jdbc/issues/112 # will be resolved. See https://github.com/rom-rb/rom-sql/issues/49 for details if jruby? && sqlite?(example) - expect(relation.to_a). - to eql([{ id: 1, user_id: 1, number: "42", balance: 10_000 }, - { id: 2, user_id: 1, number: "43", balance: -273.15 }]) + expect(relation.to_a) + .to eql([{id: 1, user_id: 1, number: "42", balance: 10_000}, + {id: 2, user_id: 1, number: "43", balance: -273.15}]) else - expect(relation.to_a). - to eql([{ id: 1, user_id: 1, number: "42", balance: 10_000.to_d }, - { id: 2, user_id: 1, number: "43", balance: -273.15.to_d }]) + expect(relation.to_a) + .to eql([{id: 1, user_id: 1, number: "42", balance: 10_000.to_d}, + {id: 2, user_id: 1, number: "43", balance: -273.15.to_d}]) end end end diff --git a/spec/integration/associations/one_to_one_through_spec.rb b/spec/integration/associations/one_to_one_through_spec.rb index 41df9bca3..ed4a826ac 100644 --- a/spec/integration/associations/one_to_one_through_spec.rb +++ b/spec/integration/associations/one_to_one_through_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Associations::OneToOneThrough, helpers: true do include_context "users" include_context "accounts" diff --git a/spec/integration/auto_migrations/errors_spec.rb b/spec/integration/auto_migrations/errors_spec.rb index cba6772c0..0741075a4 100644 --- a/spec/integration/auto_migrations/errors_spec.rb +++ b/spec/integration/auto_migrations/errors_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres do include_context "database setup" diff --git a/spec/integration/auto_migrations/file_based_migrations_spec.rb b/spec/integration/auto_migrations/file_based_migrations_spec.rb index c52202a09..d117ffec0 100644 --- a/spec/integration/auto_migrations/file_based_migrations_spec.rb +++ b/spec/integration/auto_migrations/file_based_migrations_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres, :helpers, skip_tables: true do include_context "database setup" @@ -8,7 +10,7 @@ before { FileUtils.rm_rf(path) } after { FileUtils.rm_rf(path) } - let(:options) { { path: path } } + let(:options) { {path: path} } before do conn.drop_table?(:posts) @@ -17,7 +19,7 @@ end def migrations - Dir["#{ path }/*.rb"].sort.map do |path| + Dir["#{path}/*.rb"].sort.map do |path| [File.basename(path), File.read(path)] end end @@ -42,17 +44,17 @@ def migrations name, content = migrations[0] expect(name).to match(/\A\d+_create_users\.rb$/) - expect(content).to eql(<<-RUBY) -ROM::SQL.migration do - change do - create_table :users do - primary_key :id - column :name, "text", null: false - index :name, name: :unique_name, unique: true - index :name - end - end -end + expect(content).to eql(<<~RUBY) + ROM::SQL.migration do + change do + create_table :users do + primary_key :id + column :name, "text", null: false + index :name, name: :unique_name, unique: true + index :name + end + end + end RUBY end end @@ -86,19 +88,19 @@ def migrations name, content = migrations[0] expect(name).to match(/\A\d+_alter_users\.rb$/) - expect(content).to eql(<<-RUBY) -ROM::SQL.migration do - change do - alter_table :users do - drop_column :name - add_column :first_name, "text", null: false - add_column :last_name, "text", null: false - set_column_not_null :age - add_index [:first_name, :last_name], name: :unique_name, unique: true - end - end -end - RUBY + expect(content).to eql(<<~RUBY) + ROM::SQL.migration do + change do + alter_table :users do + drop_column :name + add_column :first_name, "text", null: false + add_column :last_name, "text", null: false + set_column_not_null :age + add_index [:first_name, :last_name], name: :unique_name, unique: true + end + end + end + RUBY end end @@ -131,15 +133,15 @@ def migrations name, content = migrations[1] expect(name).to match(/\A\d+_alter_posts\.rb$/) - expect(content).to eql(<<-RUBY) -ROM::SQL.migration do - change do - alter_table :posts do - add_foreign_key [:user_id], :users - end - end -end - RUBY + expect(content).to eql(<<~RUBY) + ROM::SQL.migration do + change do + alter_table :posts do + add_foreign_key [:user_id], :users + end + end + end + RUBY end end end diff --git a/spec/integration/auto_migrations/foreign_keys_spec.rb b/spec/integration/auto_migrations/foreign_keys_spec.rb index 42717ea79..1fc82c9f8 100644 --- a/spec/integration/auto_migrations/foreign_keys_spec.rb +++ b/spec/integration/auto_migrations/foreign_keys_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres, :helpers do include_context "database setup" @@ -48,10 +50,10 @@ gateway.auto_migrate!(conf, inline: true) expect(migrated_schema.foreign_keys.size).to eql(1) - expect(migrated_schema.foreign_keys.first). - to eql( - ROM::SQL::ForeignKey.new([posts[:user_id].unwrap], :users) - ) + expect(migrated_schema.foreign_keys.first) + .to eql( + ROM::SQL::ForeignKey.new([posts[:user_id].unwrap], :users) + ) end end diff --git a/spec/integration/auto_migrations/indexes_spec.rb b/spec/integration/auto_migrations/indexes_spec.rb index 53388de05..108f7e349 100644 --- a/spec/integration/auto_migrations/indexes_spec.rb +++ b/spec/integration/auto_migrations/indexes_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres, :helpers do include_context "database setup" @@ -60,12 +62,12 @@ def indexdef(index) ] ) - expect(migrated_schema.indexes.first). - to eql(ROM::SQL::Index.new( - [define_attribute(:name, :String, source: relation_name)], + expect(migrated_schema.indexes.first) + .to eql(ROM::SQL::Index.new( + [define_attribute(:name, :String, source: relation_name)], name: :unique_name, unique: true - )) + )) end end end @@ -92,7 +94,7 @@ def indexdef(index) expect(migrated_schema.attributes[1].name).to eql(:name) expect(migrated_schema.indexes.size).to eql(1) expect(name_index.name).to eql(:users_name_index) - expect(name_index.attributes.map(&:name)).to eql(%i(name)) + expect(name_index.attributes.map(&:name)).to eql(%i[name]) end it "supports custom names" do @@ -118,7 +120,7 @@ def indexdef(index) expect(migrated_schema.attributes[1].name).to eql(:name) expect(migrated_schema.indexes.size).to eql(1) expect(name_index.name).to eql(:custom_idx) - expect(name_index.attributes.map(&:name)).to eql(%i(name)) + expect(name_index.attributes.map(&:name)).to eql(%i[name]) end it "adds index to existing column" do @@ -143,7 +145,7 @@ def indexdef(index) name_index = migrated_schema.indexes.first expect(name_index.name).to eql(:users_name_index) - expect(name_index.attributes.map(&:name)).to eql(%i(name)) + expect(name_index.attributes.map(&:name)).to eql(%i[name]) expect(name_index).not_to be_unique end @@ -169,7 +171,7 @@ def indexdef(index) name_index = migrated_schema.indexes.first expect(name_index.name).to eql(:users_name_index) - expect(name_index.attributes.map(&:name)).to eql(%i(name)) + expect(name_index.attributes.map(&:name)).to eql(%i[name]) expect(name_index).to be_unique end @@ -193,8 +195,8 @@ def indexdef(index) gateway.auto_migrate!(conf, inline: true) - expect(indexdef("users_props_index")). - to eql("CREATE INDEX users_props_index ON public.users USING gin (props)") + expect(indexdef("users_props_index")) + .to eql("CREATE INDEX users_props_index ON public.users USING gin (props)") end it "supports partial indexes" do @@ -216,8 +218,8 @@ def indexdef(index) gateway.auto_migrate!(conf, inline: true) - expect(indexdef("long_names_only")). - to eql("CREATE INDEX long_names_only ON public.users USING btree (name) WHERE (length(name) > 10)") + expect(indexdef("long_names_only")) + .to eql("CREATE INDEX long_names_only ON public.users USING btree (name) WHERE (length(name) > 10)") end end end diff --git a/spec/integration/auto_migrations/managing_columns_spec.rb b/spec/integration/auto_migrations/managing_columns_spec.rb index 3a13859ca..969ffe1a9 100644 --- a/spec/integration/auto_migrations/managing_columns_spec.rb +++ b/spec/integration/auto_migrations/managing_columns_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres, :helpers do include_context "database setup" @@ -34,22 +36,21 @@ expect(attributes.map(&:to_ast)) .to eql([ - [:attribute, - [:id, - [:nominal, [Integer, {}]], - primary_key: true, source: :users, alias: nil]], - [:attribute, [:name, [:nominal, [String, {}]], source: :users, alias: nil]], - [:attribute, - [:email, - [:sum, - [[:constrained, - [[:nominal, [NilClass, {}]], - [:predicate, [:type?, [[:type, NilClass], [:input, ROM::Undefined]]]], - ]], - [:nominal, [String, {}]], - {}]], - source: :users, alias: nil]] - ]) + [:attribute, + [:id, + [:nominal, [Integer, {}]], + {primary_key: true, source: :users, alias: nil}]], + [:attribute, [:name, [:nominal, [String, {}]], {source: :users, alias: nil}]], + [:attribute, + [:email, + [:sum, + [[:constrained, + [[:nominal, [NilClass, {}]], + [:predicate, [:type?, [[:type, NilClass], [:input, ROM::Undefined]]]]]], + [:nominal, [String, {}]], + {}]], + {source: :users, alias: nil}]] + ]) end end @@ -65,21 +66,20 @@ expect(attributes[1].to_ast) .to eql( - [:attribute, [:name, [:nominal, [String, {}]], source: :users, alias: nil]] - ) + [:attribute, [:name, [:nominal, [String, {}]], {source: :users, alias: nil}]] + ) expect(attributes[2].to_ast) .to eql( - [:attribute, - [:email, - [:sum, - [[:constrained, - [[:nominal, [NilClass, {}]], - [:predicate, [:type?, [[:type, NilClass], [:input, ROM::Undefined]]]], - ]], - [:nominal, [String, {}]], - {}]], - source: :users, alias: nil]] - ) + [:attribute, + [:email, + [:sum, + [[:constrained, + [[:nominal, [NilClass, {}]], + [:predicate, [:type?, [[:type, NilClass], [:input, ROM::Undefined]]]]]], + [:nominal, [String, {}]], + {}]], + {source: :users, alias: nil}]] + ) end end @@ -96,7 +96,7 @@ it "removes columns from a table" do gateway.auto_migrate!(conf, inline: true) - expect(attributes.map(&:name)).to eql(%i(id name email)) + expect(attributes.map(&:name)).to eql(%i[id name email]) end end diff --git a/spec/integration/auto_migrations/postgres/column_types_spec.rb b/spec/integration/auto_migrations/postgres/column_types_spec.rb index 8eeaafe84..9dd252358 100644 --- a/spec/integration/auto_migrations/postgres/column_types_spec.rb +++ b/spec/integration/auto_migrations/postgres/column_types_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres, :helpers do include_context "database setup" diff --git a/spec/integration/combine_with_spec.rb b/spec/integration/combine_with_spec.rb index 4c93e7689..b37866fcd 100644 --- a/spec/integration/combine_with_spec.rb +++ b/spec/integration/combine_with_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Eager loading" do include_context "users and tasks" @@ -98,7 +100,7 @@ def with_drafts(_assoc, users) authors = users.combine_with(users.node(:drafts)).to_a - expect(authors.map { |a| a[:name] }).to eql(["Jane", "Joe", "John"]) + expect(authors.map { |a| a[:name] }).to eql(%w[Jane Joe John]) expect(authors.map { |a| a[:drafts].size }).to eql([0, 1, 0]) end end diff --git a/spec/integration/commands/delete_spec.rb b/spec/integration/commands/delete_spec.rb index 1a42b73dd..45315f814 100644 --- a/spec/integration/commands/delete_spec.rb +++ b/spec/integration/commands/delete_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Commands / Delete" do include_context "users and tasks" diff --git a/spec/integration/commands/update_spec.rb b/spec/integration/commands/update_spec.rb index 0239fb86f..24838021e 100644 --- a/spec/integration/commands/update_spec.rb +++ b/spec/integration/commands/update_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "dry-struct" RSpec.describe "Commands / Update", seeds: false do diff --git a/spec/integration/gateway_spec.rb b/spec/integration/gateway_spec.rb index 1de662170..6ef43a620 100644 --- a/spec/integration/gateway_spec.rb +++ b/spec/integration/gateway_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Gateway, :postgres do include_context "database setup" describe "migration" do before do - inferrable_relations.concat %i(rabbits carrots) + inferrable_relations.concat %i[rabbits carrots] end context "creating migrations inline" do @@ -38,7 +40,7 @@ context "running migrations from a file system" do before do - inferrable_relations.concat %i(schema_migrations) + inferrable_relations.concat %i[schema_migrations] end let(:migration_dir) do @@ -46,7 +48,7 @@ end let(:migrator) { ROM::SQL::Migration::Migrator.new(conn, path: migration_dir) } - let(:conf) { ROM::Setup.new(:sql, [conn, migrator: migrator]) } + let(:conf) { ROM::Setup.new(:sql, [conn, {migrator: migrator}]) } let(:container) { ROM.setup(conf) } it "returns true for pending migrations" do @@ -65,14 +67,14 @@ context "running migrations from a file system with custom path" do before do - inferrable_relations.concat %i(schema_migrations) + inferrable_relations.concat %i[schema_migrations] end let(:migration_dir) do Pathname(__FILE__).dirname.join("../fixtures/migrations").realpath end - let(:conf) { ROM::Setup.new(:sql, [conn, migrator: { path: migration_dir }]) } + let(:conf) { ROM::Setup.new(:sql, [conn, {migrator: {path: migration_dir}}]) } let(:container) { ROM.setup(conf) } it "runs migrations from a specified directory" do @@ -83,7 +85,7 @@ describe "transactions" do before do - inferrable_relations.concat %i(names) + inferrable_relations.concat %i[names] end before do @@ -112,7 +114,7 @@ gw = container.gateways[:default] names = gw.dataset(:names) - gw.transaction do |t| + gw.transaction do |_t| names.insert name: "John" concurrent_names = nil Thread.new { concurrent_names = names.to_a }.join diff --git a/spec/integration/migration_spec.rb b/spec/integration/migration_spec.rb index 9eb9147f3..66c3a1c9e 100644 --- a/spec/integration/migration_spec.rb +++ b/spec/integration/migration_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL, ".migration" do include_context "database setup" before do - inferrable_relations.concat %i(dragons schema_migrations) + inferrable_relations.concat %i[dragons schema_migrations] end with_adapters do @@ -28,8 +30,8 @@ with_adapters(:postgres) do let(:conf) do ROM::Setup.new( - default: [:sql, conn, inferrable_relations: %i(schema_migrations)], - in_memory: [:sql, DB_URIS[:sqlite], inferrable_relations: %i(schema_migrations)] + default: [:sql, conn, {inferrable_relations: %i[schema_migrations]}], + in_memory: [:sql, DB_URIS[:sqlite], {inferrable_relations: %i[schema_migrations]}] ) end diff --git a/spec/integration/plugins/associates/many_to_many_spec.rb b/spec/integration/plugins/associates/many_to_many_spec.rb index f5461516a..381afb150 100644 --- a/spec/integration/plugins/associates/many_to_many_spec.rb +++ b/spec/integration/plugins/associates/many_to_many_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Plugins / :associates / with many-to-many", :sqlite, seeds: false do include_context "users and tasks" @@ -53,17 +55,17 @@ end it "associates a child with many parents" do - add_tags = create_tag.curry([{ name: "red" }, { name: "blue" }]) + add_tags = create_tag.curry([{name: "red"}, {name: "blue"}]) add_task = create_task.curry(user_id: jane[:id], title: "Jade's task") command = add_tags >> add_task result = command.call - expect(result). - to eql([ - { id: 1, user_id: jane[:id], title: "Jade's task", tag_id: 1 }, - { id: 1, user_id: jane[:id], title: "Jade's task", tag_id: 2 } - ]) + expect(result) + .to eql([ + {id: 1, user_id: jane[:id], title: "Jade's task", tag_id: 1}, + {id: 1, user_id: jane[:id], title: "Jade's task", tag_id: 2} + ]) end end diff --git a/spec/integration/plugins/associates_spec.rb b/spec/integration/plugins/associates_spec.rb index ef283cd8d..3162c78d5 100644 --- a/spec/integration/plugins/associates_spec.rb +++ b/spec/integration/plugins/associates_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Plugins / :associates", seeds: false do include_context "relations" @@ -26,7 +28,7 @@ end let(:task) do - { title: "Task one" } + {title: "Task one"} end before do @@ -42,21 +44,21 @@ it "returns a command prepared for the given association" do command = tasks[:create].with_association(:user, key: %i[user_id id]) - expect(command.call(task, user)). - to eql(id: 1, title: "Task one", user_id: user[:id]) + expect(command.call(task, user)) + .to eql(id: 1, title: "Task one", user_id: user[:id]) end it "allows passing a parent explicitly" do command = tasks[:create].with_association(:user, key: %i[user_id id], parent: user) - expect(command.call(task)). - to eql(id: 1, title: "Task one", user_id: user[:id]) + expect(command.call(task)) + .to eql(id: 1, title: "Task one", user_id: user[:id]) end it "allows setting up multiple associations" do - command = tasks[:create]. - with_association(:user, key: %i[user_id id], parent: user). - with_association(:other, key: %i[other_id id]) + command = tasks[:create] + .with_association(:user, key: %i[user_id id], parent: user) + .with_association(:other, key: %i[other_id id]) expect(command.configured_associations).to eql(%i[user other]) end @@ -65,15 +67,15 @@ shared_context "automatic FK setting" do it "sets foreign key prior execution for many tuples" do create_user = users[:create].curry(name: "Jade") - create_task = tasks[:create_many].curry([{ title: "Task one" }, { title: "Task two" }]) + create_task = tasks[:create_many].curry([{title: "Task one"}, {title: "Task two"}]) command = create_user >> create_task result = command.call expect(result).to match_array([ - { id: 1, user_id: 1, title: "Task one" }, - { id: 2, user_id: 1, title: "Task two" } + {id: 1, user_id: 1, title: "Task one"}, + {id: 2, user_id: 1, title: "Task two"} ]) end @@ -163,7 +165,7 @@ it "sets FKs for the join table" do create_user = users[:create].curry(name: "Jade") create_task = tasks[:create].curry(title: "Jade's task") - create_tags = tags[:create].curry([{ name: "red" }, { name: "blue" }]) + create_tags = tags[:create].curry([{name: "red"}, {name: "blue"}]) command = create_user >> create_task >> create_tags @@ -171,7 +173,7 @@ tags = relations[:tasks].associations[:tags].().to_a expect(result).to eql([ - { id: 1, task_id: 1, name: "red" }, { id: 2, task_id: 1, name: "blue" } + {id: 1, task_id: 1, name: "red"}, {id: 2, task_id: 1, name: "blue"} ]) expect(tags).to eql(result) @@ -242,8 +244,8 @@ end it "automatically sets FK prior execution" do - expect(command.curry(title: "Another John task").call(john)). - to eql(id: jane_task[:id], user_id: john[:id], title: "Another John task") + expect(command.curry(title: "Another John task").call(john)) + .to eql(id: jane_task[:id], user_id: john[:id], title: "Another John task") end end end diff --git a/spec/integration/plugins/auto_restrictions_spec.rb b/spec/integration/plugins/auto_restrictions_spec.rb index 3adfcfdb6..b4e87deb3 100644 --- a/spec/integration/plugins/auto_restrictions_spec.rb +++ b/spec/integration/plugins/auto_restrictions_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Plugins / :auto_restrictions", seeds: true do include_context "users and tasks" @@ -22,13 +24,13 @@ context "with two containers" do let(:confs) do - { one: ROM::Setup.new(:sql, conn), - two: ROM::Setup.new(:sql, conn) } + {one: ROM::Setup.new(:sql, conn), + two: ROM::Setup.new(:sql, conn)} end let(:containers) do - { one: ROM.setup(confs[:one]), - two: ROM.setup(confs[:two]) } + {one: ROM.setup(confs[:one]), + two: ROM.setup(confs[:two])} end before do diff --git a/spec/integration/plugins/explain_spec.rb b/spec/integration/plugins/explain_spec.rb index 9426a62c8..c67a419e2 100644 --- a/spec/integration/plugins/explain_spec.rb +++ b/spec/integration/plugins/explain_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "yaml" RSpec.describe "Plugins / :explain", :postgres do diff --git a/spec/integration/plugins/full_text_search_spec.rb b/spec/integration/plugins/full_text_search_spec.rb index 1e9dba018..0333569fb 100644 --- a/spec/integration/plugins/full_text_search_spec.rb +++ b/spec/integration/plugins/full_text_search_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe "Plugins / :pg_full_text_search", :postgres do diff --git a/spec/integration/relation/default_views_spec.rb b/spec/integration/relation/default_views_spec.rb index 8f018635d..ff4d5031a 100644 --- a/spec/integration/relation/default_views_spec.rb +++ b/spec/integration/relation/default_views_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Relation / Default views" do include_context "database setup" diff --git a/spec/integration/relation_schema_spec.rb b/spec/integration/relation_schema_spec.rb index f0f4dfacb..e80af259c 100644 --- a/spec/integration/relation_schema_spec.rb +++ b/spec/integration/relation_schema_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Inferring schema from database" do include_context "users" include_context "posts" diff --git a/spec/integration/schema/call_spec.rb b/spec/integration/schema/call_spec.rb index 98480c363..fc6a596bd 100644 --- a/spec/integration/schema/call_spec.rb +++ b/spec/integration/schema/call_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Schema, "#call" do diff --git a/spec/integration/schema/inferrer/mysql_spec.rb b/spec/integration/schema/inferrer/mysql_spec.rb index 52d5742fb..db566aef3 100644 --- a/spec/integration/schema/inferrer/mysql_spec.rb +++ b/spec/integration/schema/inferrer/mysql_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Schema::MysqlInferrer", :mysql do include_context "database setup" before do - inferrable_relations.concat %i(test_inferrence) + inferrable_relations.concat %i[test_inferrence] end before do diff --git a/spec/integration/schema/inferrer/postgres_spec.rb b/spec/integration/schema/inferrer/postgres_spec.rb index 3a66aa2b0..52a3b3959 100644 --- a/spec/integration/schema/inferrer/postgres_spec.rb +++ b/spec/integration/schema/inferrer/postgres_spec.rb @@ -1,11 +1,13 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Schema::PostgresInferrer", :postgres, :helpers do include_context "database setup" before do - inferrable_relations.concat %i(test_inferrence) + inferrable_relations.concat %i[test_inferrence] end - colors = %w(red orange yellow green blue purple) + colors = %w[red orange yellow green blue purple] before do conn.execute("create extension if not exists hstore") @@ -76,8 +78,8 @@ it "can infer primary key on enum column" do expect(schema.to_h).to eql(attributes( - colours: ROM::SQL::Types::String.enum(*colors).meta(primary_key: true) - )) + colours: ROM::SQL::Types::String.enum(*colors).meta(primary_key: true) + )) end end @@ -178,7 +180,7 @@ let(:point_2) { ROM::SQL::Postgres::Values::Point.new(8.5, 35.5) } let(:line) { ROM::SQL::Postgres::Values::Line.new(2.3, 4.9, 3.1415) } let(:dns) { IPAddr.new("8.8.8.8") } - let(:mapping) { Hash["hot" => "cold"] } + let(:mapping) { {"hot" => "cold"} } let(:circle) { ROM::SQL::Postgres::Values::Circle.new(point, 1.0) } let(:lseg) { ROM::SQL::Postgres::Values::LineSegment.new(point, point_2) } let(:box_corrected) { ROM::SQL::Postgres::Values::Box.new(point_2, point) } @@ -193,22 +195,22 @@ let(:open_path) { ROM::SQL::Postgres::Values::Path.new([point, point_2], :open) } let(:ltree) { ROM::Types::Values::TreePath.new("Top.Countries.Europe.Russia") } - let(:int4range) { values::Range.new(0, 2, :'[)') } - let(:int8range) { values::Range.new(5, 7, :'[)') } - let(:numrange) { values::Range.new(3, 9, :'[)') } + let(:int4range) { values::Range.new(0, 2, :"[)") } + let(:int8range) { values::Range.new(5, 7, :"[)") } + let(:numrange) { values::Range.new(3, 9, :"[)") } let(:tsrange) do timestamp = Time.parse("2017-09-25 07:00:00") - values::Range.new(timestamp, timestamp + 3600 * 8, :'[)') + values::Range.new(timestamp, timestamp + (3600 * 8), :"[)") end let(:tstzrange) do timestamp = Time.parse("2017-09-25 07:00:00 +0000") - values::Range.new(timestamp, timestamp + 3600 * 8, :'[)') + values::Range.new(timestamp, timestamp + (3600 * 8), :"[)") end let(:daterange) do - values::Range.new(Date.today, Date.today.next_day, :'[)') + values::Range.new(Date.today, Date.today.next_day, :"[)") end let(:relation) { container.relations[:test_bidirectional] } diff --git a/spec/integration/schema/inferrer/sqlite_spec.rb b/spec/integration/schema/inferrer/sqlite_spec.rb index 160d61f52..5a1ae8684 100644 --- a/spec/integration/schema/inferrer/sqlite_spec.rb +++ b/spec/integration/schema/inferrer/sqlite_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + RSpec.describe "ROM::SQL::Schema::SqliteInferrer", :sqlite, helpers: true do include_context "database setup" before do - inferrable_relations.concat %i(test_inferrence) + inferrable_relations.concat %i[test_inferrence] end before do diff --git a/spec/integration/schema/inferrer_spec.rb b/spec/integration/schema/inferrer_spec.rb index 8a7235f75..bb4c5203d 100644 --- a/spec/integration/schema/inferrer_spec.rb +++ b/spec/integration/schema/inferrer_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + RSpec.describe "Schema inference for common datatypes", seeds: false do include_context "users and tasks" before do - inferrable_relations.concat %i(test_characters test_inferrence test_numeric) + inferrable_relations.concat %i[test_characters test_inferrence test_numeric] end let(:schema) { container.relations[dataset].schema } @@ -16,7 +18,7 @@ def index_by_name(indexes, name) indexes.find { |idx| idx.name == name } end - with_adapters do |adapter| + with_adapters do |_adapter| describe "inferring attributes" do before do dataset = self.dataset @@ -43,7 +45,7 @@ def index_by_name(indexes, name) let(:dataset) { :tasks } let(:source) { ROM::Relation::Name[:tasks] } - it "can infer attributes for dataset" do |ex| + it "can infer attributes for dataset" do |_ex| expect(schema[:id].source).to eql(source) expect(schema[:id].type.primitive).to be(Integer) @@ -214,7 +216,7 @@ def index_by_name(indexes, name) describe "using commands with inferred schema" do before do - inferrable_relations.concat %i(people) + inferrable_relations.concat %i[people] end let(:relation) { container.relations[:people] } @@ -385,14 +387,14 @@ def index_by_name(indexes, name) index :baz, name: :baz1_idx index :baz, name: :baz2_idx - index %i(bar baz), name: :composite_idx - index %i(foo bar), name: :unique_idx, unique: true + index %i[bar baz], name: :composite_idx + index %i[foo bar], name: :unique_idx, unique: true end conf.relation(:test_inferrence) { schema(infer: true) } - expect(schema.indexes.map(&:name)). - to match_array(%i(foo_idx bar_idx baz1_idx baz2_idx composite_idx unique_idx)) + expect(schema.indexes.map(&:name)) + .to match_array(%i[foo_idx bar_idx baz1_idx baz2_idx composite_idx unique_idx]) unique_idx = index_by_name(schema.indexes, :unique_idx) diff --git a/spec/integration/schema/prefix_spec.rb b/spec/integration/schema/prefix_spec.rb index 66be1f0a8..edfd6860c 100644 --- a/spec/integration/schema/prefix_spec.rb +++ b/spec/integration/schema/prefix_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Schema, "#prefix", :postgres, seeds: false do diff --git a/spec/integration/schema/qualified_spec.rb b/spec/integration/schema/qualified_spec.rb index f696658d0..d342d5f28 100644 --- a/spec/integration/schema/qualified_spec.rb +++ b/spec/integration/schema/qualified_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Schema, "#qualified", :postgres, seeds: false do diff --git a/spec/integration/schema/rename_spec.rb b/spec/integration/schema/rename_spec.rb index d5fd9bd8a..f9a3888c4 100644 --- a/spec/integration/schema/rename_spec.rb +++ b/spec/integration/schema/rename_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Schema, "#rename", :postgres, seeds: false do diff --git a/spec/integration/schema/view_spec.rb b/spec/integration/schema/view_spec.rb index 4fb3798fd..b4f01e537 100644 --- a/spec/integration/schema/view_spec.rb +++ b/spec/integration/schema/view_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe "Defining a view using schemas", seeds: false do @@ -22,7 +24,7 @@ it "automatically projects a relation view" do expect(relations[:users].names.to_a) - .to eql([{ name: "Jade" }, { name: "Jane" }, { name: "Joe" }]) + .to eql([{name: "Jade"}, {name: "Jane"}, {name: "Joe"}]) end end end diff --git a/spec/integration/setup_spec.rb b/spec/integration/setup_spec.rb index e46ee7150..bbf8fec7f 100644 --- a/spec/integration/setup_spec.rb +++ b/spec/integration/setup_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "ROM.setup" do include_context "database setup" diff --git a/spec/integration/support/active_support_notifications_spec.rb b/spec/integration/support/active_support_notifications_spec.rb index faa4f82bb..d6dacd63d 100644 --- a/spec/integration/support/active_support_notifications_spec.rb +++ b/spec/integration/support/active_support_notifications_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe "ActiveSupport::Notifications support", :postgres, seeds: false do diff --git a/spec/integration/support/rails_log_subscriber_spec.rb b/spec/integration/support/rails_log_subscriber_spec.rb index 71d02c623..fd0cfe96c 100644 --- a/spec/integration/support/rails_log_subscriber_spec.rb +++ b/spec/integration/support/rails_log_subscriber_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" require "active_support/log_subscriber/test_helper" diff --git a/spec/integration/wrap_spec.rb b/spec/integration/wrap_spec.rb index 70a224d2e..063aeed64 100644 --- a/spec/integration/wrap_spec.rb +++ b/spec/integration/wrap_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Wrap do with_adapters do include_context "users and tasks" @@ -15,8 +17,8 @@ it "works with by_pk" do task_with_user = tasks.wrap(name).by_pk(1).one - expect(task_with_user). - to eql(id: 1, user_id: 2, title: "Joe's task", users_name: "Joe", users_id: 2) + expect(task_with_user) + .to eql(id: 1, user_id: 2, title: "Joe's task", users_name: "Joe", users_id: 2) end end diff --git a/spec/shared/accounts.rb b/spec/shared/accounts.rb index e44564062..bef4da38e 100644 --- a/spec/shared/accounts.rb +++ b/spec/shared/accounts.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + RSpec.shared_context "accounts" do let(:accounts) { container.relations[:accounts] } let(:cards) { container.relations[:cards] } before do - inferrable_relations.concat %i(accounts cards subscriptions) + inferrable_relations.concat %i[accounts cards subscriptions] end before do |example| diff --git a/spec/shared/articles.rb b/spec/shared/articles.rb index 8c63d1f96..1dec6fc4a 100644 --- a/spec/shared/articles.rb +++ b/spec/shared/articles.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + RSpec.shared_context "articles" do before do - inferrable_relations.concat %i(articles) + inferrable_relations.concat %i[articles] end before do @@ -18,12 +20,14 @@ conf.relation(:articles) { schema(infer: true) } end - before do |example| next if example.metadata[:seeds] == false + before do |example| + next if example.metadata[:seeds] == false + conn[:users].insert(name: "John") conn[:articles].insert( article_id: 1, - author_name: "Joe", + author_name: "Joe", title: "Joe's post", body: "Joe wrote sutin", status: "draft" @@ -39,7 +43,7 @@ conn[:articles].insert( article_id: 3, - author_name: "John", + author_name: "John", title: "John's post", body: "John wrote sutin else", status: "published" diff --git a/spec/shared/database_setup.rb b/spec/shared/database_setup.rb index 4ce324d08..155812cbb 100644 --- a/spec/shared/database_setup.rb +++ b/spec/shared/database_setup.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + RSpec.shared_context "database setup" do - all_tables = %i(users tasks users_tasks tags task_tags posts puppies + all_tables = %i[users tasks users_tasks tags task_tags posts puppies accounts cards subscriptions notes destinations flights categories user_group test_inferrence test_bidirectional people dragons - rabbits carrots names schema_migrations) + rabbits carrots names schema_migrations] cleared_dbs = [] diff --git a/spec/shared/notes.rb b/spec/shared/notes.rb index 08b30368f..18e9922eb 100644 --- a/spec/shared/notes.rb +++ b/spec/shared/notes.rb @@ -1,7 +1,8 @@ -RSpec.shared_context "notes" do +# frozen_string_literal: true +RSpec.shared_context "notes" do before do - inferrable_relations.concat %i(notes) + inferrable_relations.concat %i[notes] end before do |example| diff --git a/spec/shared/posts.rb b/spec/shared/posts.rb index 8bae6fb32..489c36af9 100644 --- a/spec/shared/posts.rb +++ b/spec/shared/posts.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + RSpec.shared_context "posts" do before do - inferrable_relations.concat %i(posts) + inferrable_relations.concat %i[posts] end - before do |example| + before do |_example| conn.create_table :posts do primary_key :post_id foreign_key :author_id, :users diff --git a/spec/shared/puppies.rb b/spec/shared/puppies.rb index 25a81fbb3..e258ca460 100644 --- a/spec/shared/puppies.rb +++ b/spec/shared/puppies.rb @@ -1,6 +1,8 @@ +# frozen_string_literal: true + RSpec.shared_context "puppies" do before do - inferrable_relations.concat %i(puppies) + inferrable_relations.concat %i[puppies] end before do diff --git a/spec/shared/relations.rb b/spec/shared/relations.rb index 392e4bf3f..c5d418669 100644 --- a/spec/shared/relations.rb +++ b/spec/shared/relations.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.shared_context "relations" do include_context "users and tasks" diff --git a/spec/shared/users.rb b/spec/shared/users.rb index c4895f866..09c7bcbce 100644 --- a/spec/shared/users.rb +++ b/spec/shared/users.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.shared_context "users" do include_context "database setup" diff --git a/spec/shared/users_and_tasks.rb b/spec/shared/users_and_tasks.rb index ba0204381..d6ee6b7f2 100644 --- a/spec/shared/users_and_tasks.rb +++ b/spec/shared/users_and_tasks.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.shared_context "users and tasks" do include_context "users" @@ -8,7 +10,7 @@ let(:tag_commands) { container.commands[:tags] } before do - inferrable_relations.concat %i(tasks tags task_tags) + inferrable_relations.concat %i[tasks tags task_tags] end before do |example| @@ -29,7 +31,7 @@ end conn.create_table :task_tags do - primary_key [:tag_id, :task_id] + primary_key %i[tag_id task_id] Integer :tag_id Integer :task_id end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d992a555e..7acb86398 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require_relative "support/coverage" require "warning" diff --git a/spec/support/env_helper.rb b/spec/support/env_helper.rb index 38e5f3c56..c8233bf64 100644 --- a/spec/support/env_helper.rb +++ b/spec/support/env_helper.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module ENVHelper def db?(type, example) example.metadata[type] diff --git a/spec/support/helpers.rb b/spec/support/helpers.rb index a4b9b9282..7fff603fe 100644 --- a/spec/support/helpers.rb +++ b/spec/support/helpers.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Helpers def assoc_name(*args) ROM::Relation::Name[*args] diff --git a/spec/support/test_configuration.rb b/spec/support/test_configuration.rb index ee2742520..34888deb1 100644 --- a/spec/support/test_configuration.rb +++ b/spec/support/test_configuration.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/setup" class TestConfiguration < ROM::Setup diff --git a/spec/unit/attribute_spec.rb b/spec/unit/attribute_spec.rb index 5aa67e710..f686bb411 100644 --- a/spec/unit/attribute_spec.rb +++ b/spec/unit/attribute_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Attribute, :postgres do @@ -77,8 +79,8 @@ describe "#concat" do it "returns a concat function attribute" do - expect(users[:id].concat(users[:name]).as(:uid).sql_literal(ds)). - to eql(%(CONCAT("users"."id", ' ', "users"."name") AS "uid")) + expect(users[:id].concat(users[:name]).as(:uid).sql_literal(ds)) + .to eql(%(CONCAT("users"."id", ' ', "users"."name") AS "uid")) end end @@ -89,15 +91,15 @@ 1 => string_type.value("first"), else: string_type.value("second") } - expect(users[:id].case(mapping).as(:mapped_id).sql_literal(ds)). - to eql(%[(CASE "users"."id" WHEN 1 THEN 'first' ELSE 'second' END) AS "mapped_id"]) + expect(users[:id].case(mapping).as(:mapped_id).sql_literal(ds)) + .to eql(%[(CASE "users"."id" WHEN 1 THEN 'first' ELSE 'second' END) AS "mapped_id"]) end end describe "#aliased" do it "can alias a previously aliased attribute" do - expect(users[:id].as(:uid).as(:uuid).sql_literal(ds)). - to eql(%("users"."id" AS "uuid")) + expect(users[:id].as(:uid).as(:uuid).sql_literal(ds)) + .to eql(%("users"."id" AS "uuid")) end end @@ -107,18 +109,18 @@ ROM::SQL::TypeExtensions.register(type) do def custom(_type, _expr, value) - ROM::SQL::Attribute[ROM::SQL::Types::Bool]. - meta(sql_expr: Sequel::SQL::BooleanExpression.new(:'=', 1, value)) + ROM::SQL::Attribute[ROM::SQL::Types::Bool] + .meta(sql_expr: Sequel::SQL::BooleanExpression.new(:"=", 1, value)) end end end - let(:equality_expr) { Sequel::SQL::BooleanExpression.new(:'=', 1, 2) } + let(:equality_expr) { Sequel::SQL::BooleanExpression.new(:"=", 1, 2) } shared_context "type methods" do it "successfully invokes type-specific methods" do - expect(attribute.custom(2)). - to eql(ROM::SQL::Attribute[ROM::SQL::Types::Bool].meta(sql_expr: equality_expr)) + expect(attribute.custom(2)) + .to eql(ROM::SQL::Attribute[ROM::SQL::Types::Bool].meta(sql_expr: equality_expr)) end end diff --git a/spec/unit/commands/create_spec.rb b/spec/unit/commands/create_spec.rb index e67b37945..9a9d542f1 100644 --- a/spec/unit/commands/create_spec.rb +++ b/spec/unit/commands/create_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/commands/create" RSpec.describe ROM::SQL::Commands::Create do diff --git a/spec/unit/function_spec.rb b/spec/unit/function_spec.rb index 058410aef..6da98b44b 100644 --- a/spec/unit/function_spec.rb +++ b/spec/unit/function_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/function" RSpec.describe ROM::SQL::Function, :postgres do @@ -44,127 +46,127 @@ end it "raises error when is set already" do - expect { func.count(:id).upper.sql_literal(ds) }. - to raise_error(NoMethodError, /upper/) + expect { func.count(:id).upper.sql_literal(ds) } + .to raise_error(NoMethodError, /upper/) end end describe "#cast" do it "transforms data" do - expect(func.cast(:id, "varchar").sql_literal(ds)). - to eql(%(CAST("id" AS varchar(255)))) + expect(func.cast(:id, "varchar").sql_literal(ds)) + .to eql(%(CAST("id" AS varchar(255)))) end it "infers db_type from type if not specify" do - expect(func.cast(:id).sql_literal(ds)). - to eql(%(CAST("id" AS integer))) + expect(func.cast(:id).sql_literal(ds)) + .to eql(%(CAST("id" AS integer))) end end describe "#case" do context "when condition argument is a Hash" do it "returns an sql expression" do - expect(func.case("1" => "first", else: "last").sql_literal(ds)). - to eql(%((CASE WHEN '1' THEN 'first' ELSE 'last' END))) + expect(func.case("1" => "first", else: "last").sql_literal(ds)) + .to eql(%((CASE WHEN '1' THEN 'first' ELSE 'last' END))) end end context "when the hash consists of expressions" do it "returns an sql expression" do - expect(func.case(users[:id].is([1, 2]) => "first", else: "last").sql_literal(ds)). - to eql(%((CASE WHEN ("users"."id" IN (1, 2)) THEN 'first' ELSE 'last' END))) + expect(func.case(users[:id].is([1, 2]) => "first", else: "last").sql_literal(ds)) + .to eql(%((CASE WHEN ("users"."id" IN (1, 2)) THEN 'first' ELSE 'last' END))) end end end describe "#over" do example "with the ORDER BY clause" do - expect(func.row_number.over(order: :id).sql_literal(ds)). - to eql('ROW_NUMBER() OVER (ORDER BY "id")') + expect(func.row_number.over(order: :id).sql_literal(ds)) + .to eql('ROW_NUMBER() OVER (ORDER BY "id")') - expect(func.row_number.over(order: [:id, :name]).sql_literal(ds)). - to eql('ROW_NUMBER() OVER (ORDER BY "id", "name")') + expect(func.row_number.over(order: [:id, :name]).sql_literal(ds)) + .to eql('ROW_NUMBER() OVER (ORDER BY "id", "name")') end example "with the PARTITION BY clause" do - expect(func.row_number.over(partition: :name).sql_literal(ds)). - to eql('ROW_NUMBER() OVER (PARTITION BY "name")') + expect(func.row_number.over(partition: :name).sql_literal(ds)) + .to eql('ROW_NUMBER() OVER (PARTITION BY "name")') end example "with the PARTITION BY clause which is aliased" do - expect(func.row_number.over(partition: :name, order: :name).as(:row_numbers).sql_literal(ds)). - to eql('ROW_NUMBER() OVER (PARTITION BY "name" ORDER BY "name") AS "row_numbers"') + expect(func.row_number.over(partition: :name, order: :name).as(:row_numbers).sql_literal(ds)) + .to eql('ROW_NUMBER() OVER (PARTITION BY "name" ORDER BY "name") AS "row_numbers"') end example "with the frame clause" do - expect(func.row_number.over(frame: :all).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)") + expect(func.row_number.over(frame: :all).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)") - expect(func.row_number.over(frame: :rows).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)") + expect(func.row_number.over(frame: :rows).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)") - expect(func.row_number.over(frame: { range: :current }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW)") + expect(func.row_number.over(frame: {range: :current}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (RANGE BETWEEN CURRENT ROW AND CURRENT ROW)") - expect(func.row_number.over(frame: { range: [:current, :end] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)") + expect(func.row_number.over(frame: {range: [:current, :end]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (RANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING)") - expect(func.row_number.over(frame: { range: [:start, :current] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)") + expect(func.row_number.over(frame: {range: [:start, :current]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)") - expect(func.row_number.over(frame: { range: [-3, 3] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING)") + expect(func.row_number.over(frame: {range: [-3, 3]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (RANGE BETWEEN 3 PRECEDING AND 3 FOLLOWING)") - expect(func.row_number.over(frame: { rows: [-3, :current] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)") + expect(func.row_number.over(frame: {rows: [-3, :current]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (ROWS BETWEEN 3 PRECEDING AND CURRENT ROW)") - expect(func.row_number.over(frame: { rows: [-3, :end] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (ROWS BETWEEN 3 PRECEDING AND UNBOUNDED FOLLOWING)") + expect(func.row_number.over(frame: {rows: [-3, :end]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (ROWS BETWEEN 3 PRECEDING AND UNBOUNDED FOLLOWING)") - expect(func.row_number.over(frame: { rows: [3, 6] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (ROWS BETWEEN 3 FOLLOWING AND 6 FOLLOWING)") + expect(func.row_number.over(frame: {rows: [3, 6]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (ROWS BETWEEN 3 FOLLOWING AND 6 FOLLOWING)") - expect(func.row_number.over(frame: { rows: [-6, -3] }).sql_literal(ds)). - to eql("ROW_NUMBER() OVER (ROWS BETWEEN 6 PRECEDING AND 3 PRECEDING)") + expect(func.row_number.over(frame: {rows: [-6, -3]}).sql_literal(ds)) + .to eql("ROW_NUMBER() OVER (ROWS BETWEEN 6 PRECEDING AND 3 PRECEDING)") end it "supports aliases" do - expect(func.row_number.over(order: :id).as(:row_no).sql_literal(ds)). - to eql('ROW_NUMBER() OVER (ORDER BY "id") AS "row_no"') + expect(func.row_number.over(order: :id).as(:row_no).sql_literal(ds)) + .to eql('ROW_NUMBER() OVER (ORDER BY "id") AS "row_no"') end end describe "#filter" do it "adds basic FILTER clause" do - expect(func.sum(:id).filter(:value).sql_literal(ds)). - to eql('SUM("id") FILTER (WHERE "value")') + expect(func.sum(:id).filter(:value).sql_literal(ds)) + .to eql('SUM("id") FILTER (WHERE "value")') end it "supports restriction block" do - expect(func.sum(:id).filter { id > 1 }.sql_literal(ds)). - to eql('SUM("id") FILTER (WHERE ("users"."id" > 1))') + expect(func.sum(:id).filter { id > 1 }.sql_literal(ds)) + .to eql('SUM("id") FILTER (WHERE ("users"."id" > 1))') end it "supports combined conditions" do - expect(func.sum(:id).filter(:value) { id > 1 }.sql_literal(ds)). - to eql('SUM("id") FILTER (WHERE (("users"."id" > 1) AND "value"))') + expect(func.sum(:id).filter(:value) { id > 1 }.sql_literal(ds)) + .to eql('SUM("id") FILTER (WHERE (("users"."id" > 1) AND "value"))') end it "supports hashes" do - expect(func.count(:id).filter(id: 1).sql_literal(ds)). - to eql('COUNT("id") FILTER (WHERE ("id" = 1))') + expect(func.count(:id).filter(id: 1).sql_literal(ds)) + .to eql('COUNT("id") FILTER (WHERE ("id" = 1))') end end describe "#within_group" do it "adds WITHIN GROUP clause" do - expect(func.rank(:id).within_group(:value).sql_literal(ds)). - to eql('RANK("id") WITHIN GROUP (ORDER BY "value")') + expect(func.rank(:id).within_group(:value).sql_literal(ds)) + .to eql('RANK("id") WITHIN GROUP (ORDER BY "value")') end it "works with a block" do - expect(func.rank(:id).within_group { name }.sql_literal(ds)). - to eql('RANK("id") WITHIN GROUP (ORDER BY "users"."name")') + expect(func.rank(:id).within_group { name }.sql_literal(ds)) + .to eql('RANK("id") WITHIN GROUP (ORDER BY "users"."name")') end end diff --git a/spec/unit/gateway/new_spec.rb b/spec/unit/gateway/new_spec.rb index ff18d6e16..02234ab60 100644 --- a/spec/unit/gateway/new_spec.rb +++ b/spec/unit/gateway/new_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/gateway" RSpec.describe ROM::SQL::Gateway, "#initialize" do @@ -7,8 +9,8 @@ context "with option hash" do let(:uri) do - { adapter: "sqlite", - database: ":memory:" } + {adapter: "sqlite", + database: ":memory:"} end it "establishes connection" do diff --git a/spec/unit/gateway_spec.rb b/spec/unit/gateway_spec.rb index c03dc1bd8..fbb8a29fb 100644 --- a/spec/unit/gateway_spec.rb +++ b/spec/unit/gateway_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" require "rom/lint/spec" diff --git a/spec/unit/logger_spec.rb b/spec/unit/logger_spec.rb index 8efaea199..e228cd609 100644 --- a/spec/unit/logger_spec.rb +++ b/spec/unit/logger_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe "Logger", :postgres do diff --git a/spec/unit/migration_tasks_spec.rb b/spec/unit/migration_tasks_spec.rb index fa9dffb5d..08b796a56 100644 --- a/spec/unit/migration_tasks_spec.rb +++ b/spec/unit/migration_tasks_spec.rb @@ -1,8 +1,10 @@ +# frozen_string_literal: true + require "spec_helper" namespace :db do task :setup do - #noop + # noop end end @@ -94,7 +96,8 @@ expect { Rake::Task["db:create_migration"].execute( - Rake::TaskArguments.new([:name], [name])) + Rake::TaskArguments.new([:name], [name]) + ) }.to output("<= migration file created #{path}\n").to_stdout end @@ -103,7 +106,8 @@ expect { Rake::Task["db:create_migration"].execute( - Rake::TaskArguments.new([:name, :version], [name, version])) + Rake::TaskArguments.new([:name, :version], [name, version]) + ) }.to output("<= migration file created #{path}\n").to_stdout end end diff --git a/spec/unit/migrator_spec.rb b/spec/unit/migrator_spec.rb index e3a66d861..0f337e569 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -1,9 +1,11 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Migration::Migrator, :postgres, skip_tables: true do include_context "database setup" subject(:migrator) { ROM::SQL::Migration::Migrator.new(conn, **options) } - let(:options) { { path: TMP_PATH.join("test/migrations") } } + let(:options) { {path: TMP_PATH.join("test/migrations")} } describe "#create_file" do it "creates a migration file under configured path with specified version and name" do diff --git a/spec/unit/order_dsl_spec.rb b/spec/unit/order_dsl_spec.rb index 67f09f507..0bb03d323 100644 --- a/spec/unit/order_dsl_spec.rb +++ b/spec/unit/order_dsl_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::OrderDSL, :postgres, helpers: true do @@ -34,16 +36,16 @@ end it "delegates to sequel virtual row" do - expect(ds.literal(dsl.call { nullif(id.qualified, Sequel.lit("''")).desc }.first)). - to eql(%(NULLIF("users"."id", '') DESC)) + expect(ds.literal(dsl.call { nullif(id.qualified, Sequel.lit("''")).desc }.first)) + .to eql(%(NULLIF("users"."id", '') DESC)) end it "allows to set nulls first/last" do - expect(ds.literal(dsl.call { id.desc(nulls: :first) }.first)). - to eql(%("id" DESC NULLS FIRST)) + expect(ds.literal(dsl.call { id.desc(nulls: :first) }.first)) + .to eql(%("id" DESC NULLS FIRST)) - expect(ds.literal(dsl.call { id.desc(nulls: :last) }.first)). - to eql(%("id" DESC NULLS LAST)) + expect(ds.literal(dsl.call { id.desc(nulls: :last) }.first)) + .to eql(%("id" DESC NULLS LAST)) end end end diff --git a/spec/unit/plugin/associates_spec.rb b/spec/unit/plugin/associates_spec.rb index c9a8b07c6..58ae75ccf 100644 --- a/spec/unit/plugin/associates_spec.rb +++ b/spec/unit/plugin/associates_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "ostruct" require "rom/sql/commands" @@ -48,22 +50,22 @@ result = command.associate(post_tuples, tag_tuples, assoc: tags_assoc, keys: {}) - expect(result). - to match_array([ - { title: "post 1", tag: "red" }, { title: "post 1", tag: "green"}, - { title: "post 2", tag: "red" }, { title: "post 2", tag: "green"} - ]) + expect(result) + .to match_array([ + {title: "post 1", tag: "red"}, {title: "post 1", tag: "green"}, + {title: "post 2", tag: "red"}, {title: "post 2", tag: "green"} + ]) end end describe "#associate" do context "with plain hash tuples" do let(:post_tuples) do - [{ title: "post 1" }, { title: "post 2" }] + [{title: "post 1"}, {title: "post 2"}] end let(:tag_tuples) do - [{ name: "red" }, { name: "green" }] + [{name: "red"}, {name: "green"}] end include_context "associates result" @@ -74,7 +76,7 @@ module Test class Post < OpenStruct def to_hash - { title: title } + {title: title} end end end @@ -85,7 +87,7 @@ def to_hash end let(:tag_tuples) do - [{ name: "red" }, { name: "green" }] + [{name: "red"}, {name: "green"}] end include_context "associates result" diff --git a/spec/unit/plugin/nullify_spec.rb b/spec/unit/plugin/nullify_spec.rb index 2cfe10fb6..4b7be0330 100644 --- a/spec/unit/plugin/nullify_spec.rb +++ b/spec/unit/plugin/nullify_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/plugin/nullify" RSpec.describe ROM::Relation, "#nullify" do diff --git a/spec/unit/plugin/pagination_spec.rb b/spec/unit/plugin/pagination_spec.rb index 7cad4be2e..35e0c89ec 100644 --- a/spec/unit/plugin/pagination_spec.rb +++ b/spec/unit/plugin/pagination_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/plugin/pagination" RSpec.describe "Plugin / Pagination", seeds: false do diff --git a/spec/unit/plugin/timestamp_spec.rb b/spec/unit/plugin/timestamp_spec.rb index e56cb1f71..a53a4fdd5 100644 --- a/spec/unit/plugin/timestamp_spec.rb +++ b/spec/unit/plugin/timestamp_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe "Plugin / Timestamp" do include_context "users" include_context "notes" @@ -73,8 +75,8 @@ def assign_user(tuple, user) it "only updates specified timestamps" do initial = container.commands[:notes][:create].call(text: "testing") - sleep 1 # Unfortunate, but unless I start injecting clocks into the - # command, this is needed to make sure the time actually changes + sleep 1 # Unfortunate, but unless I start injecting clocks into the + # command, this is needed to make sure the time actually changes updated = container.commands[:notes][:update].call(text: "updated test").first expect(updated[:created_at]).to eq initial[:created_at] @@ -98,7 +100,7 @@ def assign_user(tuple, user) create_user = container.commands[:users][:create].curry(name: "John Doe") create_note = container.commands[:notes][:create_with_user].curry(text: "new note") - time = DateTime.now + time = DateTime.now command = create_user >> create_note result = command.call @@ -109,7 +111,6 @@ def assign_user(tuple, user) expect(result[:user_id]).not_to be_nil expect(created).to be_within(1).of(time) expect(updated).to eq created - end - + end end end diff --git a/spec/unit/projection_dsl_spec.rb b/spec/unit/projection_dsl_spec.rb index dbe711738..08878f13e 100644 --- a/spec/unit/projection_dsl_spec.rb +++ b/spec/unit/projection_dsl_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::ProjectionDSL, :postgres, helpers: true do @@ -23,80 +25,80 @@ describe "#call" do it "evaluates the block and returns an array with attribute types" do literals = dsl - .call { integer::count(id).as(:count) } - .map { |attr| attr.sql_literal(ds) } + .call { integer.count(id).as(:count) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(COUNT("id") AS "count")]) end it "supports chaining attribute db functions" do literals = dsl - .call { meta.pg_jsonb.get_text("name").as(:name) } - .map { |attr| attr.sql_literal(ds) } + .call { meta.pg_jsonb.get_text("name").as(:name) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%{("meta" ->> 'name') AS "name"}]) end it "supports functions with args and chaining with other functions" do literals = dsl - .call { integer::count(id.qualified).distinct } - .map { |attr| attr.sql_literal(ds) } + .call { integer.count(id.qualified).distinct } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(COUNT(DISTINCT "users"."id"))]) end it "supports functions with args and chaining with other functions and an alias" do literals = dsl - .call { integer::count(id.qualified).distinct.as(:count) } - .map { |attr| attr.sql_literal(ds) } + .call { integer.count(id.qualified).distinct.as(:count) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(COUNT(DISTINCT "users"."id") AS "count")]) end it "supports functions with arg being an attribute" do literals = dsl - .call { integer::count(id).as(:count) } - .map { |attr| attr.sql_literal(ds) } + .call { integer.count(id).as(:count) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(COUNT("id") AS "count")]) end it "supports functions with any as return type" do literals = dsl - .call { function(:count, :id).as(:count) } - .map { |attr| attr.sql_literal(ds) } + .call { function(:count, :id).as(:count) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(COUNT("id") AS "count")]) end it "supports multi-agrs functions with any as return type" do literals = dsl - .call { function(:if, id > 0, id, nil).as(:id) } - .map { |attr| attr.sql_literal(ds) } + .call { function(:if, id > 0, id, nil).as(:id) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(IF(("id" > 0), "id", NULL) AS "id")]) end it "supports functions with arg being a qualified attribute" do literals = dsl - .call { integer::count(id.qualified).as(:count) } - .map { |attr| attr.sql_literal(ds) } + .call { integer.count(id.qualified).as(:count) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(COUNT("users"."id") AS "count")]) end it "supports selecting literal strings" do literals = dsl - .call { `'event'`.as(:type) } - .map { |attr| attr.sql_literal(ds) } + .call { `'event'`.as(:type) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%('event' AS "type")]) end it "supports functions without return value" do literals = dsl - .call { void::pg_advisory_lock(1).as(:lock) } - .map { |attr| attr.sql_literal(ds) } + .call { void.pg_advisory_lock(1).as(:lock) } + .map { |attr| attr.sql_literal(ds) } expect(literals).to eql([%(PG_ADVISORY_LOCK(1) AS "lock")]) end @@ -112,7 +114,7 @@ it "supports exists operator" do rel = double(dataset: ds) - schema = dsl.call { |r| exists(rel).as(:subq) } + schema = dsl.call { |_r| exists(rel).as(:subq) } literals = schema.map { |attr| attr.sql_literal(ds) } attr = schema.to_a[0] @@ -140,7 +142,7 @@ it "returns sql functions with return type specified" do function = ROM::SQL::Function.new(ROM::SQL::Types::String).upper(schema[:name]) - expect(dsl.string::upper(schema[:name])).to eql(function) + expect(dsl.string.upper(schema[:name])).to eql(function) end it "raises NoMethodError when there is no matching attribute or type" do diff --git a/spec/unit/relation/as_hash_spec.rb b/spec/unit/relation/as_hash_spec.rb index 9e0cf0cf6..b3c3ab012 100644 --- a/spec/unit/relation/as_hash_spec.rb +++ b/spec/unit/relation/as_hash_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#as_hash" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/assoc_spec.rb b/spec/unit/relation/assoc_spec.rb index bb61f1b93..85c64f14e 100644 --- a/spec/unit/relation/assoc_spec.rb +++ b/spec/unit/relation/assoc_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::Relation do @@ -20,8 +22,8 @@ with_adapters do it "returns child tuples for a relation" do - expect(users.assoc(:tasks).where(name: "Jane").to_a). - to eql([{ id: 2, user_id: 1, title: "Jane's task" }]) + expect(users.assoc(:tasks).where(name: "Jane").to_a) + .to eql([{id: 2, user_id: 1, title: "Jane's task"}]) end end end @@ -43,9 +45,9 @@ schema(infer: true) do end - associations do - has_many :tags, through: :task_tags - end + associations do + has_many :tags, through: :task_tags + end end conn[:tags].insert id: 2, name: "whatevah" @@ -54,13 +56,13 @@ with_adapters do it "returns child tuples for a relation" do - expect(tasks.assoc(:tags).to_a). - to eql([{ id: 1, name: "important", task_id: 1 }, { id: 2, name: "whatevah", task_id: 2 }]) + expect(tasks.assoc(:tags).to_a) + .to eql([{id: 1, name: "important", task_id: 1}, {id: 2, name: "whatevah", task_id: 2}]) end it "returns child tuples for a restricted relation" do - expect(tasks.assoc(:tags).where(title: "Jane's task").to_a). - to eql([{ id: 2, name: "whatevah", task_id: 2 }]) + expect(tasks.assoc(:tags).where(title: "Jane's task").to_a) + .to eql([{id: 2, name: "whatevah", task_id: 2}]) end end end @@ -80,8 +82,8 @@ with_adapters do it "returns parent tuples for a relation" do - expect(tasks.assoc(:user).where(title: "Jane's task").to_a). - to eql([{ id: 1, task_id: 2, name: "Jane" }]) + expect(tasks.assoc(:user).where(title: "Jane's task").to_a) + .to eql([{id: 1, task_id: 2, name: "Jane"}]) end end end diff --git a/spec/unit/relation/associations_spec.rb b/spec/unit/relation/associations_spec.rb index 9951e23ef..aa6936a61 100644 --- a/spec/unit/relation/associations_spec.rb +++ b/spec/unit/relation/associations_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#associations" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/avg_spec.rb b/spec/unit/relation/avg_spec.rb index f1376767e..aba6f18fd 100644 --- a/spec/unit/relation/avg_spec.rb +++ b/spec/unit/relation/avg_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#avg" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/batch_spec.rb b/spec/unit/relation/batch_spec.rb index 22c4b2dd6..0032b8bdf 100644 --- a/spec/unit/relation/batch_spec.rb +++ b/spec/unit/relation/batch_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#each_batch", seeds: false do include_context "users and tasks" @@ -6,7 +8,7 @@ before do 7.times do |i| - conn[:users].insert name: "User #{ i + 1 }" + conn[:users].insert name: "User #{i + 1}" end end @@ -18,12 +20,12 @@ batches << rel end - expect(batches). - to eql([ - relation.limit(3), - relation.where { id > 3 }.limit(3), - relation.where { id > 6 }.limit(3) - ]) + expect(batches) + .to eql([ + relation.limit(3), + relation.where { id > 3 }.limit(3), + relation.where { id > 6 }.limit(3) + ]) end end end diff --git a/spec/unit/relation/by_pk_spec.rb b/spec/unit/relation/by_pk_spec.rb index 733331e72..c0a501e22 100644 --- a/spec/unit/relation/by_pk_spec.rb +++ b/spec/unit/relation/by_pk_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#by_pk" do include_context "users and tasks" @@ -26,7 +28,7 @@ subject(:relation) { relations[:task_tags] } it "restricts a relation by is PK" do - expect(relation.by_pk(1, 1).to_a).to eql([{ tag_id: 1, task_id: 1 }]) + expect(relation.by_pk(1, 1).to_a).to eql([{tag_id: 1, task_id: 1}]) end it "works even when PK is not projected" do @@ -55,7 +57,8 @@ expect { relation.by_pk(1) }.to \ raise_error( ROM::SQL::MissingPrimaryKeyError, - "Missing primary key for :people") + "Missing primary key for :people" + ) end end end diff --git a/spec/unit/relation/dataset_spec.rb b/spec/unit/relation/dataset_spec.rb index cd2ee82c1..a02d0f4fc 100644 --- a/spec/unit/relation/dataset_spec.rb +++ b/spec/unit/relation/dataset_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#dataset" do subject(:relation) { container.relations[:users] } @@ -17,8 +19,8 @@ end it "uses schema to infer default dataset" do - expect(relation.dataset.sql). - to eql(dataset.select(Sequel.qualify(:users, :id), Sequel.qualify(:users, :name)).order(Sequel.qualify(:users, :id)).sql) + expect(relation.dataset.sql) + .to eql(dataset.select(Sequel.qualify(:users, :id), Sequel.qualify(:users, :name)).order(Sequel.qualify(:users, :id)).sql) end end @@ -32,8 +34,8 @@ end it "uses schema to infer default dataset" do - expect(relation.dataset.sql). - to eql(dataset.select(Sequel.qualify(:users, :id)).order(Sequel.qualify(:users, :id)).sql) + expect(relation.dataset.sql) + .to eql(dataset.select(Sequel.qualify(:users, :id)).order(Sequel.qualify(:users, :id)).sql) end end diff --git a/spec/unit/relation/distinct_spec.rb b/spec/unit/relation/distinct_spec.rb index afd1c97ab..833a481f1 100644 --- a/spec/unit/relation/distinct_spec.rb +++ b/spec/unit/relation/distinct_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#distinct" do subject(:relation) { relations[:users] } @@ -9,7 +11,7 @@ with_adapters :postgres do it "delegates to dataset and returns a new relation" do - expect(relation.distinct(:name).order(:name, :id).group(:name, :id).to_a).to eql([{ id: 1, name: "Jane" }, { id: 2, name: "Joe" }]) + expect(relation.distinct(:name).order(:name, :id).group(:name, :id).to_a).to eql([{id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end end end diff --git a/spec/unit/relation/exclude_spec.rb b/spec/unit/relation/exclude_spec.rb index 9f10ec839..772a23114 100644 --- a/spec/unit/relation/exclude_spec.rb +++ b/spec/unit/relation/exclude_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#exclude" do subject(:relation) { relations[:users] } @@ -5,7 +7,7 @@ with_adapters do it "delegates to dataset and returns a new relation" do - expect(relation.exclude(name: "Jane").to_a).to eql([{ id: 2, name: "Joe" }]) + expect(relation.exclude(name: "Jane").to_a).to eql([{id: 2, name: "Joe"}]) end end end diff --git a/spec/unit/relation/exist_predicate_spec.rb b/spec/unit/relation/exist_predicate_spec.rb index 0b83a44f8..9aae0dc6f 100644 --- a/spec/unit/relation/exist_predicate_spec.rb +++ b/spec/unit/relation/exist_predicate_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#exist?" do include_context "users and tasks" diff --git a/spec/unit/relation/exists_spec.rb b/spec/unit/relation/exists_spec.rb index b2408e183..6bd1ada54 100644 --- a/spec/unit/relation/exists_spec.rb +++ b/spec/unit/relation/exists_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#exists", relations: false do include_context "users and tasks" diff --git a/spec/unit/relation/fetch_spec.rb b/spec/unit/relation/fetch_spec.rb index 8dd0c3f05..a3417694e 100644 --- a/spec/unit/relation/fetch_spec.rb +++ b/spec/unit/relation/fetch_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#fetch" do subject(:relation) { container.relations[:users] } @@ -10,7 +12,7 @@ end it "raises when tuple was not found" do - expect { relation.fetch(535315412) }.to raise_error(ROM::TupleCountMismatchError) + expect { relation.fetch(535_315_412) }.to raise_error(ROM::TupleCountMismatchError) end it "raises when more tuples were returned" do diff --git a/spec/unit/relation/group_spec.rb b/spec/unit/relation/group_spec.rb index d96fa4e83..dfc127548 100644 --- a/spec/unit/relation/group_spec.rb +++ b/spec/unit/relation/group_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#group" do subject(:relation) { relations[:users] } @@ -8,38 +10,38 @@ with_adapters do it "groups by provided attribute name" do |example| # Oracle doesn't support concise GROUP BY - group_by = oracle?(example) ? %i(id name) : %i(id) - grouped = relation. - qualified. - left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified). - group(*group_by) + group_by = oracle?(example) ? %i[id name] : %i[id] + grouped = relation + .qualified + .left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified) + .group(*group_by) - expect(grouped.to_a).to eql([{ id: 1, name: "Jane" }, { id: 2, name: "Joe"}]) + expect(grouped.to_a).to eql([{id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end it "groups by provided attribute name in a block" do - grouped = relation. - qualified. - left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified). - group { [id.qualified, name.qualified] } + grouped = relation + .qualified + .left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified) + .group { [id.qualified, name.qualified] } - expect(grouped.to_a).to eql([{ id: 1, name: "Jane" }, { id: 2, name: "Joe"}]) + expect(grouped.to_a).to eql([{id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end it "groups by aliased attributes" do - grouped = relation. - select { id.as(:user_id) }. - group(:id) + grouped = relation + .select { id.as(:user_id) } + .group(:id) - expect(grouped.to_a).to eql([{ user_id: 1 }, { user_id: 2 }]) + expect(grouped.to_a).to eql([{user_id: 1}, {user_id: 2}]) end it "groups projected relation" do - grouped = relation. - select(:id). - group(:id, :name) + grouped = relation + .select(:id) + .group(:id, :name) - expect(grouped.to_a).to eql([{ id: 1 }, { id: 2 }]) + expect(grouped.to_a).to eql([{id: 1}, {id: 2}]) end end @@ -47,23 +49,23 @@ include_context "notes" it "groups by provided attribute name in and attributes from a block" do - grouped = relation. - qualified. - left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified). - group(tasks[:title]) { id.qualified } + grouped = relation + .qualified + .left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified) + .group(tasks[:title]) { id.qualified } - expect(grouped.to_a).to eql([{ id: 1, name: "Jane" }, { id: 2, name: "Joe"}]) + expect(grouped.to_a).to eql([{id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end it "groups by a function" do notes.insert user_id: 1, text: "Foo", created_at: Time.now, updated_at: Time.now grouped = notes - .select { [integer::count(id).as(:count), time::date_trunc("day", created_at).as(:date)] } - .group { date_trunc("day", created_at) } - .unordered + .select { [integer.count(id).as(:count), time.date_trunc("day", created_at).as(:date)] } + .group { date_trunc("day", created_at) } + .unordered - expect(grouped.to_a).to eql([ count: 1, date: Date.today.to_time ]) + expect(grouped.to_a).to eql([count: 1, date: Date.today.to_time]) end end end diff --git a/spec/unit/relation/having_spec.rb b/spec/unit/relation/having_spec.rb index 80c732140..ca20c0e4b 100644 --- a/spec/unit/relation/having_spec.rb +++ b/spec/unit/relation/having_spec.rb @@ -1,10 +1,12 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#having" do subject(:relation) do relations[:users] .inner_join(:tasks, user_id: :id) .qualified .select_group(:id, :name) - .select_append { integer::count(:tasks).as(:task_count) } + .select_append { integer.count(:tasks).as(:task_count) } end include_context "users and tasks" @@ -15,8 +17,8 @@ end it "restricts a relation using HAVING clause" do - expect(relation.having { count(id.qualified) >= 2 }.to_a). - to eq([{ id: 2, name: "Joe", task_count: 2 }]) + expect(relation.having { count(id.qualified) >= 2 }.to_a) + .to eq([{id: 2, name: "Joe", task_count: 2}]) end end end diff --git a/spec/unit/relation/import_spec.rb b/spec/unit/relation/import_spec.rb index 109d2f01d..c4819ad09 100644 --- a/spec/unit/relation/import_spec.rb +++ b/spec/unit/relation/import_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/memory" RSpec.describe ROM::Relation, "#import" do @@ -28,18 +30,18 @@ it "inserts data from another relation" do relation.import(source.project { [(id + 10).as(:id), full_name.as(:name)] }) - expect(relation.to_a). - to eql([ { id: 1, name: "Jane" }, - { id: 2, name: "Joe" }, - { id: 11, name: "Jack" }, - { id: 12, name: "John" }]) + expect(relation.to_a) + .to eql([{id: 1, name: "Jane"}, + {id: 2, name: "Joe"}, + {id: 11, name: "Jack"}, + {id: 12, name: "John"}]) end end context "with a different gateway" do let(:conf) { TestConfiguration.new(default: [:sql, conn], other: [:memory]) } let(:source_dataset) do - data = [{ id: 11, name: "Jack", age: 30 }, { id: 12, name: "John", age: 40 }] + data = [{id: 11, name: "Jack", age: 30}, {id: 12, name: "John", age: 40}] ROM::Memory::Dataset.new(data) end @@ -58,11 +60,11 @@ it "inserts data" do relation.import(source.new(source_dataset).project(source[:id], source[:name])) - expect(relation.to_a). - to eql([ { id: 1, name: "Jane" }, - { id: 2, name: "Joe" }, - { id: 11, name: "Jack" }, - { id: 12, name: "John" }]) + expect(relation.to_a) + .to eql([{id: 1, name: "Jane"}, + {id: 2, name: "Joe"}, + {id: 11, name: "Jack"}, + {id: 12, name: "John"}]) end end end diff --git a/spec/unit/relation/inner_join_spec.rb b/spec/unit/relation/inner_join_spec.rb index c4e4bd9da..5ad8d7078 100644 --- a/spec/unit/relation/inner_join_spec.rb +++ b/spec/unit/relation/inner_join_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#inner_join" do subject(:relation) { relations[:users] } @@ -10,15 +12,15 @@ relation.insert id: 3, name: "Jade" result = relation - .inner_join(:tasks, user_id: :id) - .select(:name, tasks[:title]) + .inner_join(:tasks, user_id: :id) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end it "joins relations using inner join and attributes with alias set" do @@ -27,30 +29,30 @@ id = tasks[:user_id].with(alias: :user_key) result = relation - .inner_join(:tasks, user_id => id) - .select(:name, tasks[:title]) + .inner_join(:tasks, user_id => id) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end it "allows specifying table_aliases" do relation.insert id: 3, name: "Jade" result = relation - .inner_join(:tasks, { user_id: :id }, table_alias: :t1) - .select(:name, tasks[:title].qualified(:t1)) + .inner_join(:tasks, {user_id: :id}, table_alias: :t1) + .select(:name, tasks[:title].qualified(:t1)) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end context "with associations" do @@ -107,15 +109,15 @@ it "joins relation with join keys inferred" do result = relation - .inner_join(tasks) - .select(:name, tasks[:title]) + .inner_join(tasks) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end let(:task_relation_proxy) { @@ -128,15 +130,15 @@ def name it "joins relation with relation proxy objects" do result = relation - .inner_join(task_relation_proxy) - .select(:name, tasks[:title]) + .inner_join(task_relation_proxy) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end describe "joined relation with join keys inferred for m:m-through" do @@ -146,7 +148,7 @@ def name end let(:expected_result) do - [{ id: 1, user_id: 2, title: "Joe's task" }, { id: 2, user_id: 1, title: "Jane's task" }] + [{id: 1, user_id: 2, title: "Joe's task"}, {id: 2, user_id: 1, title: "Jane's task"}] end it "using target relation" do @@ -164,28 +166,28 @@ def name it "joins by association name if no condition provided" do result = relation - .inner_join(:tasks) - .select(:name, tasks[:title]) + .inner_join(:tasks) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end it "joins if association name differs from relation name" do result = relation - .inner_join(:todos) - .select(:name, tasks[:title]) + .inner_join(:todos) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"} + ]) end it "joins by relation if association name differs from relation name" do diff --git a/spec/unit/relation/inspect_spec.rb b/spec/unit/relation/inspect_spec.rb index 58b009e4b..da2d5af08 100644 --- a/spec/unit/relation/inspect_spec.rb +++ b/spec/unit/relation/inspect_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#inspect" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/instrument_spec.rb b/spec/unit/relation/instrument_spec.rb index 2f7f23c65..180108b5e 100644 --- a/spec/unit/relation/instrument_spec.rb +++ b/spec/unit/relation/instrument_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Relation, "#instrument", :sqlite do include_context "database setup" diff --git a/spec/unit/relation/invert_spec.rb b/spec/unit/relation/invert_spec.rb index e3951629d..39c30ced1 100644 --- a/spec/unit/relation/invert_spec.rb +++ b/spec/unit/relation/invert_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#invert" do subject(:relation) { relations[:users] } @@ -5,7 +7,7 @@ with_adapters do it "delegates to dataset and returns a new relation" do - expect(relation.where(name: "Jane").invert.to_a).to eql([{ id: 2, name: "Joe" }]) + expect(relation.where(name: "Jane").invert.to_a).to eql([{id: 2, name: "Joe"}]) end end end diff --git a/spec/unit/relation/join_dsl_spec.rb b/spec/unit/relation/join_dsl_spec.rb index 3d7ef3c3f..915dfe413 100644 --- a/spec/unit/relation/join_dsl_spec.rb +++ b/spec/unit/relation/join_dsl_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#join_dsl", relations: false do subject(:relation) { relations[:tasks] } @@ -23,45 +25,45 @@ shared_context "valid joined relation" do it "can join relations using arbitrary conditions" do - result = relation.join(users_arg) { |tasks:, users: | + result = relation.join(users_arg) { |tasks:, users:| tasks[:user_id].is(users[:id]) & users[:name].is("Jane") }.select(:title, users[:name]) - expect(result.to_a). - to eql([name: "Jane", title: "Jane's task" ]) + expect(result.to_a) + .to eql([name: "Jane", title: "Jane's task"]) end it "can use functions" do - result = relation.join(users_arg) { |tasks:, users: | - tasks[:user_id].is(users[:id]) & string::upper(users[:name]).is("Jane".upcase) + result = relation.join(users_arg) { |tasks:, users:| + tasks[:user_id].is(users[:id]) & string.upper(users[:name]).is("Jane".upcase) }.select(:title, users[:name]) - expect(result.to_a). - to eql([name: "Jane", title: "Jane's task" ]) + expect(result.to_a) + .to eql([name: "Jane", title: "Jane's task"]) end it "works with right join" do - result = relation.right_join(users_arg) { |tasks:, users: | + result = relation.right_join(users_arg) { |tasks:, users:| tasks[:user_id].is(users[:id]) & (users[:id] > 1) }.select(:title, users[:name]) - expect(result.to_a). - to eql([ - { name: "Joe", title: "Joe's task" }, - { name: "Jane", title: nil } - ]) + expect(result.to_a) + .to eql([ + {name: "Joe", title: "Joe's task"}, + {name: "Jane", title: nil} + ]) end it "works with left join" do - result = users.left_join(tasks_arg) { |tasks:, users: | + result = users.left_join(tasks_arg) { |tasks:, users:| tasks[:user_id].is(users[:id]) & (tasks[:id] > 1) }.select(relation[:title], :name) - expect(result.to_a). - to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: nil }, - ]) + expect(result.to_a) + .to eql([ + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: nil} + ]) end end @@ -90,7 +92,7 @@ it "can join using alias" do authors = users.as(:authors).qualified(:authors) - result = users.join(authors) { |users: | + result = users.join(authors) { |users:| users[:id].is(authors[:id]) & authors[:id].is(1) }.select(:name) diff --git a/spec/unit/relation/left_join_spec.rb b/spec/unit/relation/left_join_spec.rb index c5f574336..363692fdf 100644 --- a/spec/unit/relation/left_join_spec.rb +++ b/spec/unit/relation/left_join_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#left_join" do subject(:relation) { relations[:users] } @@ -7,16 +9,16 @@ it "joins relations using left outer join" do relation.insert id: 3, name: "Jade" - result = relation. - left_join(:tasks, user_id: :id). - select(:name, tasks[:title]) + result = relation + .left_join(:tasks, user_id: :id) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to match_array([ - { name: "Joe", title: "Joe's task" }, - { name: "Jane", title: "Jane's task" }, - { name: "Jade", title: nil } + {name: "Joe", title: "Joe's task"}, + {name: "Jane", title: "Jane's task"}, + {name: "Jade", title: nil} ]) end @@ -38,17 +40,17 @@ end it "joins relation with join keys inferred" do - result = relation. - left_join(tasks). - select(:name, tasks[:title]) + result = relation + .left_join(tasks) + .select(:name, tasks[:title]) expect(result.schema.map(&:name)).to eql(%i[name title]) expect(result.to_a).to eql([ - { name: "Jane", title: "Jane's task" }, - { name: "Joe", title: "Joe's task" }, - { name: "Jade", title: nil } - ]) + {name: "Jane", title: "Jane's task"}, + {name: "Joe", title: "Joe's task"}, + {name: "Jade", title: nil} + ]) end end end diff --git a/spec/unit/relation/lock_spec.rb b/spec/unit/relation/lock_spec.rb index 250d0983e..d128e4b16 100644 --- a/spec/unit/relation/lock_spec.rb +++ b/spec/unit/relation/lock_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "concurrent/atomic/count_down_latch" RSpec.describe ROM::Relation, "#lock" do @@ -23,7 +25,7 @@ def elapsed_time with_adapters :postgres, :mysql, :oracle do it "locks rows for update" do Thread.new do - relation.lock do |rel| + relation.lock do |_rel| latch.count_down sleep timeout @@ -87,7 +89,7 @@ def elapsed_time it "locks with UPDATE OF" do expect(lock_style(relation.lock(of: :name))).to eql("FOR UPDATE OF name") - expect(lock_style(relation.lock(of: %i(id name)))).to eql("FOR UPDATE OF id, name") + expect(lock_style(relation.lock(of: %i[id name]))).to eql("FOR UPDATE OF id, name") end end end diff --git a/spec/unit/relation/map_spec.rb b/spec/unit/relation/map_spec.rb index 1629daa1f..90f1f59c2 100644 --- a/spec/unit/relation/map_spec.rb +++ b/spec/unit/relation/map_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#map" do subject(:relation) { container.relations[:users] } @@ -6,11 +8,11 @@ with_adapters do it "yields tuples" do result = relation.map { |tuple| tuple[:name] } - expect(result).to eql(%w(Jane Joe)) + expect(result).to eql(%w[Jane Joe]) end it "plucks value" do - expect(relation.map(:name)).to eql(%w(Jane Joe)) + expect(relation.map(:name)).to eql(%w[Jane Joe]) end end end diff --git a/spec/unit/relation/max_spec.rb b/spec/unit/relation/max_spec.rb index 88f095a4d..666497203 100644 --- a/spec/unit/relation/max_spec.rb +++ b/spec/unit/relation/max_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#max" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/min_spec.rb b/spec/unit/relation/min_spec.rb index 9b5fb18e5..12f39eee3 100644 --- a/spec/unit/relation/min_spec.rb +++ b/spec/unit/relation/min_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#min" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/order_spec.rb b/spec/unit/relation/order_spec.rb index 0ae73fcb3..a298c3412 100644 --- a/spec/unit/relation/order_spec.rb +++ b/spec/unit/relation/order_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#order" do subject(:relation) { relations[:users] } @@ -11,84 +13,84 @@ it "orders by provided attribute names" do ordered = relation.order(:name, :id) - expect(ordered.to_a). - to eql([{ id: 3, name: "Jade" }, { id: 1, name: "Jane" }, { id: 2, name: "Joe" }]) + expect(ordered.to_a) + .to eql([{id: 3, name: "Jade"}, {id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end it "orders by provided attributes with alias set" do attribs = [relation.schema[:name].with(alias: :user_name), :id] ordered = relation.order(*attribs) - expect(ordered.to_a). - to eql([{ id: 3, name: "Jade" }, { id: 1, name: "Jane" }, { id: 2, name: "Joe" }]) + expect(ordered.to_a) + .to eql([{id: 3, name: "Jade"}, {id: 1, name: "Jane"}, {id: 2, name: "Joe"}]) end it "orders by provided attribute using a block" do - ordered = relation. - qualified. - select(:id, :name). - left_join(:tasks, user_id: :id). - order { name.qualified.desc } - - expect(ordered.to_a). - to eql([{ id: 2, name: "Joe" }, { id: 1, name: "Jane" }, { id: 3, name: "Jade" }]) + ordered = relation + .qualified + .select(:id, :name) + .left_join(:tasks, user_id: :id) + .order { name.qualified.desc } + + expect(ordered.to_a) + .to eql([{id: 2, name: "Joe"}, {id: 1, name: "Jane"}, {id: 3, name: "Jade"}]) end it "orders by provided attribute when aliased using a block" do - ordered = relation. - qualified. - rename(name: :user_name). - select(:id, :name). - order { name.qualified.desc } - - expect(ordered.to_a). - to eql([{ id: 2, user_name: "Joe" }, { id: 1, user_name: "Jane" }, { id: 3, user_name: "Jade" }]) + ordered = relation + .qualified + .rename(name: :user_name) + .select(:id, :name) + .order { name.qualified.desc } + + expect(ordered.to_a) + .to eql([{id: 2, user_name: "Joe"}, {id: 1, user_name: "Jane"}, {id: 3, user_name: "Jade"}]) end it "orders by provided attribute from another relation" do - ordered = relation. - select(:id). - left_join(:tasks, user_id: :id). - select_append { |r| r[:tasks][:title] }. - order { |r| r[:tasks][:title].desc }. - where { |r| r[:tasks][:title].not(nil) } - - expect(ordered.to_a). - to eql([{ id: 2, title: "Joe's task" }, { id: 1, title: "Jane's task" }]) + ordered = relation + .select(:id) + .left_join(:tasks, user_id: :id) + .select_append { |r| r[:tasks][:title] } + .order { |r| r[:tasks][:title].desc } + .where { |r| r[:tasks][:title].not(nil) } + + expect(ordered.to_a) + .to eql([{id: 2, title: "Joe's task"}, {id: 1, title: "Jane's task"}]) end it "accesses other relations through keywords" do - ordered = relation. - select(:id). - left_join(:tasks, user_id: :id). - select_append { |tasks: | tasks[:title] }. - order { |tasks: | tasks[:title].desc }. - where { |tasks: | tasks[:title].not(nil) } - - expect(ordered.to_a). - to eql([{ id: 2, title: "Joe's task" }, { id: 1, title: "Jane's task" }]) + ordered = relation + .select(:id) + .left_join(:tasks, user_id: :id) + .select_append { |tasks:| tasks[:title] } + .order { |tasks:| tasks[:title].desc } + .where { |tasks:| tasks[:title].not(nil) } + + expect(ordered.to_a) + .to eql([{id: 2, title: "Joe's task"}, {id: 1, title: "Jane's task"}]) end it "orders by provided attributes using a block" do - ordered = relation. - qualified. - select(:id, :name). - left_join(:tasks, user_id: :id). - order { [name.qualified.desc, id.qualified.desc] } - - expect(ordered.to_a). - to eql([{ id: 2, name: "Joe" }, { id: 1, name: "Jane" }, { id: 3, name: "Jade" }]) + ordered = relation + .qualified + .select(:id, :name) + .left_join(:tasks, user_id: :id) + .order { [name.qualified.desc, id.qualified.desc] } + + expect(ordered.to_a) + .to eql([{id: 2, name: "Joe"}, {id: 1, name: "Jane"}, {id: 3, name: "Jade"}]) end end with_adapters :postgres, :mysql do it "orders by virtual attributes" do - ordered = relation. - select { string::concat(id, "-", name).as(:uid) }. - order(:uid) + ordered = relation + .select { string.concat(id, "-", name).as(:uid) } + .order(:uid) - expect(ordered.to_a). - to eql([{ uid: "1-Jane" }, { uid: "2-Joe" }, { uid: "3-Jade" }]) + expect(ordered.to_a) + .to eql([{uid: "1-Jane"}, {uid: "2-Joe"}, {uid: "3-Jade"}]) end end end diff --git a/spec/unit/relation/pluck_spec.rb b/spec/unit/relation/pluck_spec.rb index a5978ac48..6dbe3872a 100644 --- a/spec/unit/relation/pluck_spec.rb +++ b/spec/unit/relation/pluck_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#pluck" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/prefix_spec.rb b/spec/unit/relation/prefix_spec.rb index e67b412d1..67d7bee76 100644 --- a/spec/unit/relation/prefix_spec.rb +++ b/spec/unit/relation/prefix_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#prefix" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/primary_key_spec.rb b/spec/unit/relation/primary_key_spec.rb index 4fac1aa7f..986d29be8 100644 --- a/spec/unit/relation/primary_key_spec.rb +++ b/spec/unit/relation/primary_key_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#primary_key" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/project_spec.rb b/spec/unit/relation/project_spec.rb index 4c54274ea..bbd609765 100644 --- a/spec/unit/relation/project_spec.rb +++ b/spec/unit/relation/project_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#project" do subject(:relation) { container.relations[:users] } @@ -23,16 +25,16 @@ def sorted describe "subqueries" do it "supports single-column relations as attributes" do - tasks_count = tasks. - project { integer::count(id).as(:id) }. - where(tasks[:user_id] => users[:id]). - where(tasks[:title].ilike("joe%")). - unordered. - query + tasks_count = tasks + .project { integer.count(id).as(:id) } + .where(tasks[:user_id] => users[:id]) + .where(tasks[:title].ilike("joe%")) + .unordered + .query results = relation.project { [id, tasks_count.as(:tasks_count)] }.to_a - expect(results).to eql([ {id: 1, tasks_count: 0}, {id: 2, tasks_count: 1} ]) + expect(results).to eql([{id: 1, tasks_count: 0}, {id: 2, tasks_count: 1}]) end end end diff --git a/spec/unit/relation/qualified_columns_spec.rb b/spec/unit/relation/qualified_columns_spec.rb index 74b6e6bab..274948f1e 100644 --- a/spec/unit/relation/qualified_columns_spec.rb +++ b/spec/unit/relation/qualified_columns_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#qualified_columns" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/qualified_spec.rb b/spec/unit/relation/qualified_spec.rb index e031183de..60b511e20 100644 --- a/spec/unit/relation/qualified_spec.rb +++ b/spec/unit/relation/qualified_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#qualified" do subject(:relation) { relations[:users] } @@ -11,15 +13,15 @@ end it "qualifies virtual attributes" do - qualified = relation. - left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified). - select(:id, tasks[:id].func { integer::count(id).as(:count) }). - qualified. - group(:id) + qualified = relation + .left_join(:tasks, tasks[:user_id].qualified => relation[:id].qualified) + .select(:id, tasks[:id].func { integer.count(id).as(:count) }) + .qualified + .group(:id) expect(qualified.schema.all?(&:qualified?)).to be(true) - expect(qualified.to_a).to eql([{ id: 1, count: 1 }, { id: 2, count: 1 }]) + expect(qualified.to_a).to eql([{id: 1, count: 1}, {id: 2, count: 1}]) end it "does not qualify attribute without dataset" do diff --git a/spec/unit/relation/read_spec.rb b/spec/unit/relation/read_spec.rb index 093ae2c1e..703f8a9ed 100644 --- a/spec/unit/relation/read_spec.rb +++ b/spec/unit/relation/read_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#read" do subject(:relation) { container.relations[:users] } @@ -9,12 +11,12 @@ end it "returns results from raw SQL" do - expect(users).to match_array([{ name: "Jane" }, { name: "Joe" }]) + expect(users).to match_array([{name: "Jane"}, {name: "Joe"}]) end it "returns a new SQL relation" do materialized = users.() - expect(materialized).to match_array([{ name: "Jane" }, { name: "Joe" }]) + expect(materialized).to match_array([{name: "Jane"}, {name: "Joe"}]) expect(materialized.source).to be(users) end diff --git a/spec/unit/relation/rename_spec.rb b/spec/unit/relation/rename_spec.rb index 08d75afed..ceaf1861e 100644 --- a/spec/unit/relation/rename_spec.rb +++ b/spec/unit/relation/rename_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#rename" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/right_join_spec.rb b/spec/unit/relation/right_join_spec.rb index c254e01a7..2cc9fb8dd 100644 --- a/spec/unit/relation/right_join_spec.rb +++ b/spec/unit/relation/right_join_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#right_join" do subject(:relation) { relations[:tasks] } @@ -8,16 +10,16 @@ users.insert id: 3, name: "Jade" relation.insert id: 3, title: "Unassigned" - result = relation. - right_join(:users, id: :user_id). - select(:title, users[:name]) + result = relation + .right_join(:users, id: :user_id) + .select(:title, users[:name]) expect(result.schema.map(&:name)).to eql(%i[title name]) expect(result.to_a).to match_array([ - { name: "Joe", title: "Joe's task" }, - { name: "Jane", title: "Jane's task" }, - { name: "Jade", title: nil } + {name: "Joe", title: "Joe's task"}, + {name: "Jane", title: "Jane's task"}, + {name: "Jade", title: nil} ]) end @@ -40,17 +42,17 @@ users.insert id: 3, name: "Jade" relation.insert id: 3, title: "Unassigned" - result = relation. - right_join(users). - select(:title, users[:name]) + result = relation + .right_join(users) + .select(:title, users[:name]) expect(result.schema.map(&:name)).to eql(%i[title name]) expect(result.to_a).to match_array([ - { name: "Joe", title: "Joe's task" }, - { name: "Jane", title: "Jane's task" }, - { name: "Jade", title: nil } - ]) + {name: "Joe", title: "Joe's task"}, + {name: "Jane", title: "Jane's task"}, + {name: "Jade", title: nil} + ]) end end end diff --git a/spec/unit/relation/select_append_spec.rb b/spec/unit/relation/select_append_spec.rb index e7e056c06..b46ce7cd9 100644 --- a/spec/unit/relation/select_append_spec.rb +++ b/spec/unit/relation/select_append_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#select_append" do subject(:relation) { relations[:tasks] } @@ -12,7 +14,7 @@ end it "supports blocks" do - selected = relation.select(:id).select_append { string::upper(title).as(:title) } + selected = relation.select(:id).select_append { string.upper(title).as(:title) } expect(selected.schema.map(&:name)).to eql(%i[id title]) expect(selected.first).to eql(id: 1, title: "JOE'S TASK") @@ -22,7 +24,7 @@ with_adapters(:postgres) do example "row_number" do selected = relation.select(:id).select_append { - [integer::row_number().over(partition: title).as(:row_numbers)] + [integer.row_number.over(partition: title).as(:row_numbers)] } expect(selected.dataset.sql).to eql(<<~SQL.strip.gsub("\n", " ")) diff --git a/spec/unit/relation/select_spec.rb b/spec/unit/relation/select_spec.rb index fd0d19f47..edc36ffd5 100644 --- a/spec/unit/relation/select_spec.rb +++ b/spec/unit/relation/select_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#select" do subject(:relation) { container.relations[:tasks] } @@ -10,12 +12,12 @@ with_adapters do it "projects a relation using a list of symbols" do expect(relation.select(:id, :title).to_a) - .to eql([{ id: 1, title: "Joe's task" }, { id: 2, title: "Jane's task"}]) + .to eql([{id: 1, title: "Joe's task"}, {id: 2, title: "Jane's task"}]) end it "projects a relation using a schema" do expect(relation.select(*relation.schema.project(:id, :title)).to_a) - .to eql([{ id: 1, title: "Joe's task" }, { id: 2, title: "Jane's task"}]) + .to eql([{id: 1, title: "Joe's task"}, {id: 2, title: "Jane's task"}]) end it "maintains schema" do diff --git a/spec/unit/relation/sum_spec.rb b/spec/unit/relation/sum_spec.rb index 2f027deae..fb1533470 100644 --- a/spec/unit/relation/sum_spec.rb +++ b/spec/unit/relation/sum_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#sum" do subject(:relation) { container.relations[:users] } diff --git a/spec/unit/relation/unfiltered_spec.rb b/spec/unit/relation/unfiltered_spec.rb index 1553a9b4d..08f3af66e 100644 --- a/spec/unit/relation/unfiltered_spec.rb +++ b/spec/unit/relation/unfiltered_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#unfiltered" do subject(:relation) { relations[:tasks].select(:id, :title) } @@ -5,9 +7,9 @@ with_adapters do it "undoes restrictions" do - expect(relation.where(id: 1).unfiltered.to_a). - to eql([{ id: 1, title: "Joe's task" }, - { id: 2, title: "Jane's task" }]) + expect(relation.where(id: 1).unfiltered.to_a) + .to eql([{id: 1, title: "Joe's task"}, + {id: 2, title: "Jane's task"}]) end end end diff --git a/spec/unit/relation/union_spec.rb b/spec/unit/relation/union_spec.rb index eeb7a3b1f..d1307c633 100644 --- a/spec/unit/relation/union_spec.rb +++ b/spec/unit/relation/union_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#union" do subject(:relation) { container.relations[:users] } @@ -22,8 +24,8 @@ end with_adapters do - let(:tasks) { container.relations[:tasks]} - let(:task_tags) { container.relations[:task_tags]} + let(:tasks) { container.relations[:tasks] } + let(:task_tags) { container.relations[:task_tags] } context "when the relations unioned have the same name" do let(:relation1) { relation.where(id: 1).select(:id, :name) } @@ -33,8 +35,8 @@ result = relation1.union(relation2) expect(result.to_a).to match_array([ - { id: 1, name: "Jane" }, - { id: 2, name: "Joe" } + {id: 1, name: "Jane"}, + {id: 2, name: "Joe"} ]) end @@ -49,8 +51,8 @@ result = unioned.select_append(unioned[:title].as(:task_title)) expect(result.to_a).to match_array([ - {:id=>1, :task_title=>"Joe's task", :title=>"Joe's task", :user_id=>2}, - {:id=>2, :task_title=>"Jane's task", :title=>"Jane's task", :user_id=>1}, + {id: 1, task_title: "Joe's task", title: "Joe's task", user_id: 2}, + {id: 2, task_title: "Jane's task", title: "Jane's task", user_id: 1} ]) end diff --git a/spec/unit/relation/unique_predicate_spec.rb b/spec/unit/relation/unique_predicate_spec.rb index 3f3ca8731..4d62800d4 100644 --- a/spec/unit/relation/unique_predicate_spec.rb +++ b/spec/unit/relation/unique_predicate_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#unique?" do subject(:relation) { container.relations[:tasks] } diff --git a/spec/unit/relation/where_spec.rb b/spec/unit/relation/where_spec.rb index 7db9e638d..2e7690064 100644 --- a/spec/unit/relation/where_spec.rb +++ b/spec/unit/relation/where_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, "#where" do subject(:relation) { relations[:tasks].select(:id, :title) } @@ -6,8 +8,8 @@ with_adapters do context "without :read types" do it "restricts relation using provided conditions" do - expect(relation.where(id: 1).to_a). - to eql([{ id: 1, title: "Joe's task" }]) + expect(relation.where(id: 1).to_a) + .to eql([{id: 1, title: "Joe's task"}]) end it "restricts relation using provided conditions and block" do @@ -25,45 +27,45 @@ it "restricts relation using aliased attributes as hash keys" do aliased_relation = relation.rename(title: :task_title) - expect(aliased_relation.where(aliased_relation[:title] => "Jane's task").to_a).to eql([{ id: 2, task_title: "Jane's task" }]) + expect(aliased_relation.where(aliased_relation[:title] => "Jane's task").to_a).to eql([{id: 2, task_title: "Jane's task"}]) end it "restricts relation using attributes from another relation" do - expect(relation.join(:users, id: :user_id).where { |r| r[:users][:name].is("Jane") }.to_a). - to eql([{ id: 2, title: "Jane's task" }]) + expect(relation.join(:users, id: :user_id).where { |r| r[:users][:name].is("Jane") }.to_a) + .to eql([{id: 2, title: "Jane's task"}]) end it "supports keywords for accessing another relation" do - expect(relation.join(:users, id: :user_id).where { |r| r[:users][:name].is("Jane") }.to_a). - to eql([{ id: 2, title: "Jane's task" }]) + expect(relation.join(:users, id: :user_id).where { |r| r[:users][:name].is("Jane") }.to_a) + .to eql([{id: 2, title: "Jane's task"}]) end it "restricts with or condition" do - expect(relation.where { id.is(1) | id.is(2) }.to_a). - to eql([{ id: 1, title: "Joe's task" }, { id: 2, title: "Jane's task" }]) + expect(relation.where { id.is(1) | id.is(2) }.to_a) + .to eql([{id: 1, title: "Joe's task"}, {id: 2, title: "Jane's task"}]) end it "restricts with a range condition" do - expect(relation.where { id.in(-1...2) }.to_a). - to eql([{ id: 1, title: "Joe's task" }]) + expect(relation.where { id.in(-1...2) }.to_a) + .to eql([{id: 1, title: "Joe's task"}]) - expect(relation.where { id.in(0...3) }.to_a). - to eql([{ id: 1, title: "Joe's task" }, { id: 2, title: "Jane's task" }]) + expect(relation.where { id.in(0...3) }.to_a) + .to eql([{id: 1, title: "Joe's task"}, {id: 2, title: "Jane's task"}]) end it "restricts with an inclusive range" do - expect(relation.where { id.in(0..2) }.to_a). - to eql([{ id: 1, title: "Joe's task" }, { id: 2, title: "Jane's task" }]) + expect(relation.where { id.in(0..2) }.to_a) + .to eql([{id: 1, title: "Joe's task"}, {id: 2, title: "Jane's task"}]) end it "restricts with an ordinary enum" do - expect(relation.where { id.in(2, 3) }.to_a). - to eql([{ id: 2, title: "Jane's task" }]) + expect(relation.where { id.in(2, 3) }.to_a) + .to eql([{id: 2, title: "Jane's task"}]) end it "restricts with enum using self syntax" do - expect(relation.where(relation[:id].in(2, 3)).to_a). - to eql([{ id: 2, title: "Jane's task" }]) + expect(relation.where(relation[:id].in(2, 3)).to_a) + .to eql([{id: 2, title: "Jane's task"}]) end context "using underscored symbols for qualifying" do @@ -71,31 +73,31 @@ after { Sequel.split_symbols = false } it "queries with a qualified name" do - expect(relation.where(tasks__id: 1).to_a). - to eql([{ id: 1, title: "Joe's task" }]) + expect(relation.where(tasks__id: 1).to_a) + .to eql([{id: 1, title: "Joe's task"}]) end end it "restricts with a function" do - expect(relation.where { string::lower(title).is("joe's task") }.to_a). - to eql([{ id: 1, title: "Joe's task" }]) + expect(relation.where { string.lower(title).is("joe's task") }.to_a) + .to eql([{id: 1, title: "Joe's task"}]) end it "restricts with a function using LIKE" do - expect(relation.where { string::lower(title).like("joe%") }.to_a). - to eql([{ id: 1, title: "Joe's task" }]) + expect(relation.where { string.lower(title).like("joe%") }.to_a) + .to eql([{id: 1, title: "Joe's task"}]) end it "works with subqueries" do conn[:users].insert name: "Jack" tasks = self.tasks users = self.users - rows = relation. - where( - tasks[:user_id].is( - users.where { users[:id].is(tasks[:user_id]) }.query - ) - ).to_a + rows = relation + .where( + tasks[:user_id].is( + users.where { users[:id].is(tasks[:user_id]) }.query + ) + ).to_a expect(rows.size).to eql(2) end @@ -104,14 +106,14 @@ tasks = self.tasks users = self.users - rows = relation. - where( - tasks[:user_id].is( - users.select(:id).order { name.desc }.limit(1).query - ) - ).to_a + rows = relation + .where( + tasks[:user_id].is( + users.select(:id).order { name.desc }.limit(1).query + ) + ).to_a - expect(rows).to match([{ id: an_instance_of(Integer), title: "Joe's task" }]) + expect(rows).to match([{id: an_instance_of(Integer), title: "Joe's task"}]) end end @@ -142,26 +144,26 @@ def to_s it "applies write_schema to hash conditions" do rel = tasks.where(id: Test::Id.new("2"), title: Test::Title.new(:"Jane's task")) - expect(rel.first). - to eql(id: 2, user_id: 1, title: "Jane's task") + expect(rel.first) + .to eql(id: 2, user_id: 1, title: "Jane's task") end it "applies write_schema to hash conditions where value is an array" do - ids = %w(1 2).map(&Test::Id.method(:new)) + ids = %w[1 2].map(&Test::Id.method(:new)) rel = tasks.where(id: ids) - expect(rel.to_a). - to eql([ - { id: 1, user_id: 2, title: "Joe's task" }, - { id: 2, user_id: 1, title: "Jane's task" } - ]) + expect(rel.to_a) + .to eql([ + {id: 1, user_id: 2, title: "Joe's task"}, + {id: 2, user_id: 1, title: "Jane's task"} + ]) end it "applies write_schema to conditions with operators other than equality" do rel = tasks.where { id >= Test::Id.new("2") } - expect(rel.first). - to eql(id: 2, user_id: 1, title: "Jane's task") + expect(rel.first) + .to eql(id: 2, user_id: 1, title: "Jane's task") end it "applies write_schema to conditions in a block" do @@ -169,8 +171,8 @@ def to_s id.is(Test::Id.new("2")) & title.is(Test::Title.new(:"Jane's task")) } - expect(rel.first). - to eql(id: 2, user_id: 1, title: "Jane's task") + expect(rel.first) + .to eql(id: 2, user_id: 1, title: "Jane's task") end end end diff --git a/spec/unit/restriction_dsl_spec.rb b/spec/unit/restriction_dsl_spec.rb index 67eec6bf8..d9347079d 100644 --- a/spec/unit/restriction_dsl_spec.rb +++ b/spec/unit/restriction_dsl_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "spec_helper" RSpec.describe ROM::SQL::RestrictionDSL, :sqlite, helpers: true do diff --git a/spec/unit/type_serializer_spec.rb b/spec/unit/type_serializer_spec.rb index 815a3ef8f..a82e743b0 100644 --- a/spec/unit/type_serializer_spec.rb +++ b/spec/unit/type_serializer_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/type_serializer" RSpec.describe ROM::SQL::TypeSerializer do diff --git a/spec/unit/types_spec.rb b/spec/unit/types_spec.rb index f1d8b18c1..f5e3082c5 100644 --- a/spec/unit/types_spec.rb +++ b/spec/unit/types_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require "rom/sql/types" RSpec.describe ROM::SQL::Types, :postgres do @@ -57,8 +59,8 @@ let(:func) { Sequel::SQL::Function.new(:count, :age) } specify do - expect { sql_literal }. - to raise_error(ROM::SQL::Attribute::QualifyError, "can't qualify :age (#{func.inspect})") + expect { sql_literal } + .to raise_error(ROM::SQL::Attribute::QualifyError, "can't qualify :age (#{func.inspect})") end end end