diff --git a/.rubocop.yml b/.rubocop.yml index 3397e4c6c..9f867c32a 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -31,7 +31,7 @@ Layout/SpaceInLambdaLiteral: Layout/SpaceInsideHashLiteralBraces: Enabled: true - EnforcedStyle: no_space + EnforcedStyle: space EnforcedStyleForEmptyBraces: no_space Lint/BooleanSymbol: @@ -111,6 +111,10 @@ Style/ClassAndModuleChildren: Exclude: - "spec/**/*_spec.rb" +Style/ColonMethodCall: + Exclude: + - "spec/**/*_spec.rb" + Style/ConditionalAssignment: Enabled: false diff --git a/Gemfile b/Gemfile index b898449d0..11565c986 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,5 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gemspec @@ -9,10 +11,10 @@ if ENV['DRY_TYPES_FROM_MASTER'].eql?('true') end git 'https://github.com/rom-rb/rom.git', branch: 'release-5.4' do - gem 'rom-core' + gem 'rom' gem 'rom-changeset' + gem 'rom-core' gem 'rom-repository' - gem 'rom' end if ENV['SEQUEL_FROM_MASTER'].eql?('true') @@ -30,18 +32,18 @@ group :test do end if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.4') - gem 'ostruct' gem 'mutex_m' + gem 'ostruct' end gem 'activesupport', '~> 5.0' - gem 'pg', '~> 1.2', platforms: :ruby - gem 'mysql2', '~> 0.5', platforms: :ruby - gem 'jdbc-postgres', '>= 9.4.1212', platforms: :jruby + gem 'dotenv', require: false gem 'jdbc-mysql', platforms: :jruby - gem 'sqlite3', '~> 1.4', platforms: :ruby + gem 'jdbc-postgres', '>= 9.4.1212', platforms: :jruby gem 'jdbc-sqlite3', platforms: :jruby + gem 'mysql2', '~> 0.5', platforms: :ruby + gem 'pg', '~> 1.2', platforms: :ruby gem 'ruby-oci8', platforms: :ruby if ENV['ROM_USE_ORACLE'] - gem 'dotenv', require: false gem 'sequel_pg', require: false, platforms: :ruby + gem 'sqlite3', '~> 1.4', platforms: :ruby end diff --git a/Gemfile.devtools b/Gemfile.devtools index 2d50b472d..a62418c49 100644 --- a/Gemfile.devtools +++ b/Gemfile.devtools @@ -5,14 +5,14 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" } group :test do - gem "simplecov", require: false, platforms: :ruby - gem "simplecov-cobertura", require: false, platforms: :ruby - gem "rexml", require: false + gem 'simplecov', require: false, platforms: :ruby + gem 'simplecov-cobertura', require: false, platforms: :ruby + gem 'rexml', require: false - gem "warning" + gem 'warning' end group :tools do # this is the same version that we use on codacy - gem "rubocop", "1.69.2" + gem 'rubocop', '1.69.2' end diff --git a/spec/unit/attribute_spec.rb b/spec/unit/attribute_spec.rb index 8f3adf7ba..beedd4983 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 @@ -12,7 +14,7 @@ end it 'returns a boolean equality expression for attribute' do - expect((users[:id].is(1)).sql_literal(ds)).to eql('("users"."id" = 1)') + expect(users[:id].is(1).sql_literal(ds)).to eql('("users"."id" = 1)') end end @@ -22,7 +24,7 @@ end it 'returns an IS NULL expression for attribute' do - expect((users[:id].is(nil)).sql_literal(ds)).to eql('("users"."id" IS NULL)') + expect(users[:id].is(nil).sql_literal(ds)).to eql('("users"."id" IS NULL)') end end @@ -32,13 +34,13 @@ end it 'returns an IS TRUE expression for attribute' do - expect((users[:id].is(true)).sql_literal(ds)).to eql('("users"."id" IS TRUE)') + expect(users[:id].is(true).sql_literal(ds)).to eql('("users"."id" IS TRUE)') end end context 'with a boolean false' do it 'returns an IS FALSE expression' do - expect((users[:id].is(false)).sql_literal(ds)).to eql('("users"."id" IS FALSE)') + expect(users[:id].is(false).sql_literal(ds)).to eql('("users"."id" IS FALSE)') end end end @@ -46,7 +48,7 @@ describe '#not' do context 'with a standard value' do it 'returns a negated boolean equality expression' do - expect((users[:id].not(1)).sql_literal(ds)).to eql('("users"."id" != 1)') + expect(users[:id].not(1).sql_literal(ds)).to eql('("users"."id" != 1)') end end @@ -58,7 +60,7 @@ context 'with a boolean true' do it 'returns an IS NOT TRUE expression' do - expect((users[:id].not(true)).sql_literal(ds)).to eql('("users"."id" IS NOT TRUE)') + expect(users[:id].not(true).sql_literal(ds)).to eql('("users"."id" IS NOT TRUE)') end end @@ -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,19 @@ 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,8 +113,8 @@ 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 @@ -117,8 +123,9 @@ def custom(_type, _expr, value) 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 0c0e0ede2..6d34879b2 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 8cea65df4..a3eec0948 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,168 @@ 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: :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, :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: [-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, :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: [-6, -3] }).sql_literal(ds)). - to eql('ROW_NUMBER() OVER (ROWS BETWEEN 6 PRECEDING AND 3 PRECEDING)') + 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: { 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: [: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: { 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, 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)' + ) 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 8ab0f6054..fef7dd269 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 diff --git a/spec/unit/gateway_spec.rb b/spec/unit/gateway_spec.rb index b828d6b76..cb1d39a78 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 cf42b04bf..526a261de 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 f2d342f45..dd1675f2d 100644 --- a/spec/unit/migration_tasks_spec.rb +++ b/spec/unit/migration_tasks_spec.rb @@ -1,13 +1,15 @@ -require "spec_helper" +# frozen_string_literal: true + +require 'spec_helper' namespace :db do task :setup do - #noop + # noop end end -RSpec.describe "MigrationTasks", :postgres, skip_tables: true do - include_context "database setup" +RSpec.describe 'MigrationTasks', :postgres, skip_tables: true do + include_context 'database setup' let(:migrator) { container.gateways[:default].migrator } let(:task) { nil } @@ -23,10 +25,10 @@ task.reenable end - context "db:reset" do - let(:task) { Rake::Task["db:reset"] } + context 'db:reset' do + let(:task) { Rake::Task['db:reset'] } - it "calls proper commands" do + it 'calls proper commands' do expect(migrator).to receive(:run).with({ target: 0 }) expect(migrator).to receive(:run) @@ -35,12 +37,12 @@ }.to output("<= db:reset executed\n").to_stdout end - context "with custom migration_options" do + context 'with custom migration_options' do before do ROM::SQL::RakeSupport.migration_options = { table: :custom_table, column: :custom_column } end - it "calls proper commands" do + it 'calls proper commands' do expect(migrator).to receive(:run).with({ target: 0, table: :custom_table, column: :custom_column }) expect(migrator).to receive(:run).with({ table: :custom_table, column: :custom_column }) @@ -51,11 +53,11 @@ end end - context "db:migrate" do - let(:task) { Rake::Task["db:migrate"] } + context 'db:migrate' do + let(:task) { Rake::Task['db:migrate'] } - context "with VERSION" do - it "calls proper commands" do + context 'with VERSION' do + it 'calls proper commands' do expect(migrator).to receive(:run).with({ target: 1 }) expect { @@ -64,8 +66,8 @@ end end - context "without VERSION" do - it "calls proper commands" do + context 'without VERSION' do + it 'calls proper commands' do expect(migrator).to receive(:run) expect { @@ -74,7 +76,7 @@ end end - it "raises an error on missing both env and Gateway.instance" do + it 'raises an error on missing both env and Gateway.instance' do ROM::SQL::RakeSupport.env = nil ROM::SQL::Gateway.instance = nil @@ -83,12 +85,12 @@ }.to raise_error(ROM::SQL::RakeSupport::MissingEnv) end - context "with custom migration_options" do + context 'with custom migration_options' do before do ROM::SQL::RakeSupport.migration_options = { table: :custom_table, column: :custom_column } end - it "calls proper commands" do + it 'calls proper commands' do expect(migrator).to receive(:run).with({ table: :custom_table, column: :custom_column }) expect { @@ -98,10 +100,10 @@ end end - context "db:clean" do - let(:task) { Rake::Task["db:clean"] } + context 'db:clean' do + let(:task) { Rake::Task['db:clean'] } - it "calls proper commands" do + it 'calls proper commands' do expect(migrator).to receive(:run).with({ target: 0 }) expect { @@ -109,12 +111,12 @@ }.to output("<= db:clean executed\n").to_stdout end - context "with custom migration_options" do + context 'with custom migration_options' do before do ROM::SQL::RakeSupport.migration_options = { table: :custom_table, column: :custom_column } end - it "calls proper commands" do + it 'calls proper commands' do expect(migrator).to receive(:run).with({ target: 0, table: :custom_table, column: :custom_column }) expect { @@ -124,11 +126,11 @@ end end - context "db:create_migration" do - let(:task) { Rake::Task["db:create_migration"] } + context 'db:create_migration' do + let(:task) { Rake::Task['db:create_migration'] } - context "without NAME" do - it "exit without creating any file" do + context 'without NAME' do + it 'exit without creating any file' do expect(File).to_not receive(:write) expect { @@ -139,28 +141,30 @@ end end - context "with NAME" do - let(:dirname) { "tmp/db/migrate" } - let(:name) { "foo_bar" } - let(:version) { "001" } + context 'with NAME' do + let(:dirname) { 'tmp/db/migrate' } + let(:name) { 'foo_bar' } + let(:version) { '001' } let(:filename) { "#{version}_#{name}.rb" } let(:path) { File.join(dirname, filename) } - it "calls proper commands with default VERSION" do + it 'calls proper commands with default VERSION' do expect(migrator).to receive(:create_file).with(name).and_return(path) expect { task.execute( - Rake::TaskArguments.new([:name], [name])) + Rake::TaskArguments.new([:name], [name]) + ) }.to output("<= migration file created #{path}\n").to_stdout end - it "calls proper commands with manualy set VERSION" do + it 'calls proper commands with manualy set VERSION' do expect(migrator).to receive(:create_file).with(name, version).and_return(path) expect { task.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 0926c2b74..6132a1857 100644 --- a/spec/unit/migrator_spec.rb +++ b/spec/unit/migrator_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Migration::Migrator, :postgres, skip_tables: true do include_context 'database setup' diff --git a/spec/unit/order_dsl_spec.rb b/spec/unit/order_dsl_spec.rb index f344cf007..c529a6a31 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 @@ -23,7 +25,7 @@ describe '#`' do it 'produces a string literal' do - expect(ds.literal(dsl.call { `foo` }.first)).to eql("foo") + expect(ds.literal(dsl.call { `foo` }.first)).to eql('foo') end end @@ -34,16 +36,19 @@ 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 fd9066f69..1e477d5f7 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,11 +50,10 @@ 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 diff --git a/spec/unit/plugin/nullify_spec.rb b/spec/unit/plugin/nullify_spec.rb index e76424031..7acc82bd2 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 04c734458..e7f92156b 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 56a45d9ef..c9ba9ed1b 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' @@ -42,9 +44,9 @@ def assign_user(tuple, user) end end - it "applies timestamps by default" do + it 'applies timestamps by default' do time = DateTime.now - result = container.commands[:notes].create.call(text: "This is a test") + result = container.commands[:notes].create.call(text: 'This is a test') created = DateTime.parse(result[:created_at].to_s) updated = DateTime.parse(result[:updated_at].to_s) @@ -53,14 +55,14 @@ def assign_user(tuple, user) expect(updated).to eq created end - it "applies datestamps by default" do - result = container.commands[:notes].create.call(text: "This is a test") + it 'applies datestamps by default' do + result = container.commands[:notes].create.call(text: 'This is a test') expect(Date.parse(result[:written].to_s)).to eq Date.today end - it "sets timestamps on multi-tuple inputs" do + it 'sets timestamps on multi-tuple inputs' do time = DateTime.now - input = [{text: "note one"}, {text: "note two"}] + input = [{ text: 'note one' }, { text: 'note two' }] results = container.commands[:notes].create_many.call(input) @@ -71,21 +73,21 @@ def assign_user(tuple, user) end end - 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 - updated = container.commands[:notes].update.call(text: "updated test").first + 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 + updated = container.commands[:notes].update.call(text: 'updated test').first expect(updated[:created_at]).to eq initial[:created_at] expect(updated[:updated_at]).not_to eq initial[:updated_at] end - it "allows overriding timestamps" do |ex| + it 'allows overriding timestamps' do |ex| tomorrow = (Time.now + (60 * 60 * 24)) - container.commands[:notes].create.call(text: "testing") - updated = container.commands[:notes].update.call(text: "updated test", updated_at: tomorrow).first + container.commands[:notes].create.call(text: 'testing') + updated = container.commands[:notes].update.call(text: 'updated test', updated_at: tomorrow).first if jruby? && sqlite?(ex) expect(updated[:updated_at]).to eql(tomorrow.strftime('%Y-%m-%d %H:%M:%S.%6N')) @@ -94,11 +96,11 @@ def assign_user(tuple, user) end end - it "works with chained commands" do - create_user = container.commands[:users].create.curry(name: "John Doe") - create_note = container.commands[:notes].create_with_user.curry(text: "new note") + it 'works with chained commands' do + 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 c8fe35681..ecbd0a002 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,56 +25,56 @@ 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 @@ -87,24 +89,24 @@ 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 @@ -120,7 +122,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] diff --git a/spec/unit/relation/as_hash_spec.rb b/spec/unit/relation/as_hash_spec.rb index f736e744a..6ed164b03 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 } @@ -11,11 +13,17 @@ with_adapters do it 'returns a hash with all tuples been the key the primary key' do - expect(relation.as_hash).to eql({1 => {id: 1, name: 'Jane'}, 2 => {id: 2, name: 'Joe'}}) + expect(relation.as_hash).to eql( + 1 => { id: 1, name: 'Jane' }, + 2 => { id: 2, name: 'Joe' } + ) end it 'returns a hash with all tuples been the key the one specify in the args' do - expect(relation.as_hash(:name)).to eql({'Jane' => {id: 1, name: 'Jane'}, 'Joe' => {id: 2, name: 'Joe'}}) + expect(relation.as_hash(:name)).to eql( + 'Jane' => { id: 1, name: 'Jane' }, + 'Joe' => { id: 2, name: 'Joe' } + ) end end end diff --git a/spec/unit/relation/assoc_spec.rb b/spec/unit/relation/assoc_spec.rb index 80c79d0c3..508ae4307 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,9 @@ 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 @@ -53,13 +56,16 @@ 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 @@ -79,8 +85,9 @@ 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 a42993839..03f7c6144 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 dbb30271a..cb16a43fd 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 96c391741..b2a82d675 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,11 @@ 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 9112198a8..74e173c75 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' @@ -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 7490be6da..ff380545f 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,9 @@ 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 +35,9 @@ 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 950120523..6699d129f 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] } diff --git a/spec/unit/relation/exclude_spec.rb b/spec/unit/relation/exclude_spec.rb index aa1aabd92..dbbfc9e52 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] } diff --git a/spec/unit/relation/exist_predicate_spec.rb b/spec/unit/relation/exist_predicate_spec.rb index 11f2f6f92..88cd6b939 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 77d7c4df9..bad782c4b 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 d5e90c64f..88c896d19 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,11 +12,15 @@ 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 - expect { relation.fetch([1, 2]) }.to raise_error(ROM::TupleCountMismatchError) + expect { + relation.fetch([1, 2]) + }.to raise_error(ROM::TupleCountMismatchError) end end end diff --git a/spec/unit/relation/group_spec.rb b/spec/unit/relation/group_spec.rb index 24a9feb12..bf30d7db1 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,36 +10,36 @@ 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 }]) 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 }]) 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 f05626378..a5b749607 100644 --- a/spec/unit/relation/having_spec.rb +++ b/spec/unit/relation/having_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, '#having' do subject(:relation) do relations[:users] @@ -15,8 +17,9 @@ 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 eql([ + { 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 0a4997479..798089127 100644 --- a/spec/unit/relation/import_spec.rb +++ b/spec/unit/relation/import_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::Relation, '#import' do subject(:relation) { relations[:users] } @@ -26,11 +28,12 @@ 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 @@ -56,11 +59,12 @@ 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 62c99e0bc..5895ca16e 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,35 +29,35 @@ 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 before do - inferrable_relations.concat %i[puzzles] + inferrable_relations.push(:puzzles) end before 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 @@ -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 7ba892c9b..7f3c8b763 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 344e603ab..11a55cb8c 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' @@ -42,27 +44,31 @@ def instrument(*args, &block) it 'instruments relation materialization' do relation.to_a - expect(notifications.logs). - to include([:sql, name: :sqlite, query: relation.dataset.sql]) + expect(notifications.logs).to include( + [:sql, name: :sqlite, query: relation.dataset.sql] + ) end it 'instruments methods that return a single tuple' do relation.first - expect(notifications.logs). - to include([:sql, name: :sqlite, query: relation.limit(1).dataset.sql]) + expect(notifications.logs).to include( + [:sql, name: :sqlite, query: relation.limit(1).dataset.sql] + ) relation.last - expect(notifications.logs). - to include([:sql, name: :sqlite, query: relation.reverse.limit(1).dataset.sql]) + expect(notifications.logs).to include( + [:sql, name: :sqlite, query: relation.reverse.limit(1).dataset.sql] + ) end it 'instruments aggregation methods' do relation.count - expect(notifications.logs). - to include([:sql, name: :sqlite, query: "SELECT count(*) AS 'count' FROM `users` LIMIT 1"]) + expect(notifications.logs).to include( + [:sql, name: :sqlite, query: "SELECT count(*) AS 'count' FROM `users` LIMIT 1"] + ) end context 'two containers with shared gateway' do diff --git a/spec/unit/relation/invert_spec.rb b/spec/unit/relation/invert_spec.rb index ac9304433..3fa975785 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] } diff --git a/spec/unit/relation/join_dsl_spec.rb b/spec/unit/relation/join_dsl_spec.rb index 12abc84ce..79d3e6ea8 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,43 @@ 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 +90,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 16e500d7b..943af27f1 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,9 +9,9 @@ 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]) @@ -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 b2847ffd6..621578a48 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 @@ -12,7 +14,7 @@ def lock_style(relation) context 'with hitting the database' do let(:latch) { Concurrent::CountDownLatch.new } - let(:timeout) { (defined? JRUBY_VERSION) ? 2 : 0.2 } + let(:timeout) { defined?(JRUBY_VERSION) ? 2 : 0.2 } let!(:start) { Time.now } @@ -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 378e89ccc..1bbbf2388 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 6f4e1c34c..37af4aa4d 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 648ea7f57..84a06a5dc 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 fc13c92dc..1437b9a2a 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,106 @@ 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) - - expect(ordered.to_a). - to eql([{ uid: '1-Jane' }, { uid: '2-Joe' }, { uid: '3-Jade' }]) + 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' } + ]) end end end diff --git a/spec/unit/relation/pluck_spec.rb b/spec/unit/relation/pluck_spec.rb index a25e3658f..82716028f 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 } @@ -9,7 +11,7 @@ end it 'returns a list of hashes containing values from multiple specified columns' do - expect(relation.pluck(:id, :name)).to eql([[1, "Jane"], [2, "Joe"]]) + expect(relation.pluck(:id, :name)).to eql([[1, 'Jane'], [2, 'Joe']]) end end end diff --git a/spec/unit/relation/prefix_spec.rb b/spec/unit/relation/prefix_spec.rb index 27c899f7c..cfaf7b660 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 16c695cfe..a6db79a20 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 b5470feb4..a6ff7d192 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 e995530d8..899b729d5 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 51800a0e6..03fe415a9 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,11 +13,11 @@ 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) diff --git a/spec/unit/relation/read_spec.rb b/spec/unit/relation/read_spec.rb index 5ac1a366b..6b92fa47c 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 } diff --git a/spec/unit/relation/rename_spec.rb b/spec/unit/relation/rename_spec.rb index 4d4e42958..3e4015c09 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 a2250fea4..f408d4424 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,9 +10,9 @@ 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]) @@ -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 212a2b6f3..6dbe8d2ab 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] } @@ -22,10 +24,10 @@ 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", " ")) + expect(selected.dataset.sql).to eql(<<~SQL.strip.gsub("\n", ' ')) SELECT "tasks"."id", ROW_NUMBER() OVER (PARTITION BY "tasks"."title") AS "row_numbers" FROM "tasks" ORDER BY "tasks"."id" diff --git a/spec/unit/relation/select_spec.rb b/spec/unit/relation/select_spec.rb index 8258b2c5e..94d2d4a7c 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 } @@ -9,13 +11,15 @@ 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"}]) + expect(relation.select(:id, :title).to_a).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"}]) + expect(relation.select(*relation.schema.project(:id, :title)).to_a).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 45f36b9ae..9fb8a51a2 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 202785299..7b5b80b97 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,10 @@ 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 793cbab2b..94ef3c5c8 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) } @@ -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 d7aa30589..e0bcfdea3 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 483cac7c9..a1c1b9ab6 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,16 +8,16 @@ 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 - expect(relation.where(id: 1) { title.like("%Jane%") }.to_a).to be_empty + expect(relation.where(id: 1) { title.like('%Jane%') }.to_a).to be_empty end it 'restricts relation using provided conditions in a block' do - expect(relation.where { (id > 2) & title.like("%Jane%") }.to_a).to be_empty + expect(relation.where { (id > 2) & title.like('%Jane%') }.to_a).to be_empty end it 'restricts relation using canonical attributes' do @@ -25,45 +27,50 @@ 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 +78,30 @@ 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,12 +110,11 @@ 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" }]) end @@ -140,28 +145,30 @@ def to_s end it 'applies write_schema to hash conditions' do - rel = tasks.where(id: Test::Id.new('2'), title: Test::Title.new(:"Jane's task")) + 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 +176,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 50681e6be..95941f39a 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 @@ -20,9 +22,9 @@ expect(conn[:users].literal(dsl.call { count(id) >= 3 })).to eql('(count(`id`) >= 3)') end - it "supports using blocks in filter clause" do + it 'supports using blocks in filter clause' do expect(conn[:users].literal(dsl.call { integer.count(id).filter { id > 0 } >= 3 })).to eql( - "(COUNT(`id`) FILTER (WHERE (`id` > 0)) >= 3)" + '(COUNT(`id`) FILTER (WHERE (`id` > 0)) >= 3)' ) end end diff --git a/spec/unit/schema_spec.rb b/spec/unit/schema_spec.rb index 614088ec2..16e0a06bd 100644 --- a/spec/unit/schema_spec.rb +++ b/spec/unit/schema_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + RSpec.describe ROM::SQL::Schema, :postgres do describe '#primary_key' do it 'returns primary key attributes' do diff --git a/spec/unit/type_serializer_spec.rb b/spec/unit/type_serializer_spec.rb index 191a35020..cb4c52ead 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 a5ebc9026..d7283cd5f 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