Skip to content

Commit

Permalink
fix: Require AR 5.0+
Browse files Browse the repository at this point in the history
  • Loading branch information
Amnesthesia committed May 23, 2023
1 parent 2408edf commit ffeb31a
Showing 1 changed file with 146 additions and 144 deletions.
290 changes: 146 additions & 144 deletions test/support/postgresql/import_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,193 +252,195 @@ def should_support_postgresql_import_functionality
end
end

describe 'with :omit_columns option' do
let(:values) do
[
{
title: "Invictus",
author_name: "William Ernest Henley",
url: 'https://www.poetryfoundation.org/poems/51642/invictus',
guid: '' # guid is blank, but will be ignored
},
{
title: 'If',
author_name: 'Rudyard Kipling',
guid: SecureRandom.uuid, # this will still be ignored
url: 'https://www.poetryfoundation.org/poems/46473/if---'
}
]
end

context 'with array of hashes' do
context ':omit_columns is a Proc' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
if ENV['AR_VERSION'].to_f >= 5.0
describe 'with :omit_columns option' do
let(:values) do
[
{
title: "Invictus",
author_name: "William Ernest Henley",
url: 'https://www.poetryfoundation.org/poems/51642/invictus',
guid: '' # guid is blank, but will be ignored
},
{
title: 'If',
author_name: 'Rudyard Kipling',
guid: SecureRandom.uuid, # this will still be ignored
url: 'https://www.poetryfoundation.org/poems/46473/if---'
}
]
end

context 'with array of hashes' do
context ':omit_columns is a Proc' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
end
end
end

it "should generate guid on match" do
Redirection.import! values, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end
it "should generate guid on match" do
Redirection.import! values, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! values, omit_columns: ->(model, _column_names) { [:guid] unless model == Redirection }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
it "should not generate guid when not ignored" do
Redirection.import! values, omit_columns: ->(model, _column_names) { [:guid] unless model == Redirection }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end
end

context ':omit_columns is an Array' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns: [:guid]
context ':omit_columns is an Array' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns: [:guid]
end
end
end

it "should generate guid" do
Redirection.import! values, omit_columns: [:guid]
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end
it "should generate guid" do
Redirection.import! values, omit_columns: [:guid]
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! values
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
it "should not generate guid when not ignored" do
Redirection.import! values
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end
end

context ':omit_columns is a Hash' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns: { Redirection => [:guid] }
context ':omit_columns is a Hash' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns: { Redirection => [:guid] }
end
end
end

it "should generate guid" do
Redirection.import! values, omit_columns: { Redirection => [:guid] }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end
it "should generate guid" do
Redirection.import! values, omit_columns: { Redirection => [:guid] }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! values, omit_columns: { Book => [:guid] }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
it "should not generate guid when not ignored" do
Redirection.import! values, omit_columns: { Book => [:guid] }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end
end
end

context 'with array of models' do
let(:models) { values.map { |hash| Redirection.new(hash) } }
context ':omit_columns is a Proc' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
context 'with array of models' do
let(:models) { values.map { |hash| Redirection.new(hash) } }
context ':omit_columns is a Proc' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
end
end
end

it "should generate guid on match" do
Redirection.import! models, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, models.last[:guid]
end
it "should generate guid on match" do
Redirection.import! models, omit_columns: ->(model, _column_names) { [:guid] if model == Redirection }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, models.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! models, omit_columns: ->(model, _column_names) { [:guid] unless model == Redirection }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
it "should not generate guid when not ignored" do
Redirection.import! models, omit_columns: ->(model, _column_names) { [:guid] unless model == Redirection }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end
end

context ':omit_columns is an Array' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns: [:guid]
context ':omit_columns is an Array' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns: [:guid]
end
end
end

it "should generate guid" do
Redirection.import! models, omit_columns: [:guid]
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
it "should generate guid" do
Redirection.import! models, omit_columns: [:guid]
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, values.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! models
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end

it "should not generate guid when not ignored" do
Redirection.import! models
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
context ':omit_columns is a Hash' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns: { Redirection => [:guid] }
end
end

it "should generate guid" do
Redirection.import! models, omit_columns: { Redirection => [:guid] }
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, models.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! models, omit_columns: { Book => [:guid] }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end
end
end

describe 'with :omit_columns_with_default_functions option' do
let(:values) do
[
{
title: "Invictus",
author_name: "William Ernest Henley",
url: 'https://www.poetryfoundation.org/poems/51642/invictus',
guid: '' # guid is blank, but will be ignored
},
{
title: 'If',
author_name: 'Rudyard Kipling',
guid: SecureRandom.uuid, # this will still be ignored
url: 'https://www.poetryfoundation.org/poems/46473/if---'
}
]
end

context ':omit_columns is a Hash' do
context 'with array of hashes' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns: { Redirection => [:guid] }
Redirection.import values, omit_columns_with_default_functions: true
end
end

it "should generate guid" do
Redirection.import! models, omit_columns: { Redirection => [:guid] }
Redirection.import values, omit_columns_with_default_functions: true
assert_not_nil Redirection.last.guid
assert_not_equal Redirection.last.guid, models.last[:guid]
end

it "should not generate guid when not ignored" do
Redirection.import! models, omit_columns: { Book => [:guid] }
assert_nil Redirection.first.guid
assert_equal values.last[:guid], Redirection.last.guid
end
end
end
end

describe 'with :omit_columns_with_default_functions option' do
let(:values) do
[
{
title: "Invictus",
author_name: "William Ernest Henley",
url: 'https://www.poetryfoundation.org/poems/51642/invictus',
guid: '' # guid is blank, but will be ignored
},
{
title: 'If',
author_name: 'Rudyard Kipling',
guid: SecureRandom.uuid, # this will still be ignored
url: 'https://www.poetryfoundation.org/poems/46473/if---'
}
]
end

context 'with array of hashes' do
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import values, omit_columns_with_default_functions: true
context 'with array of models' do
let(:models) { values.map { |hash| Redirection.new(hash) } }
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
Redirection.import models, omit_columns_with_default_functions: true
end
end
end

it "should generate guid" do
Redirection.import values, omit_columns_with_default_functions: true
assert_not_nil Redirection.last.guid
end
end

context 'with array of models' do
let(:models) { values.map { |hash| Redirection.new(hash) } }
it "should succeed with guid being generated" do
assert_difference "Redirection.count", +2 do
it "should generate guid" do
Redirection.import models, omit_columns_with_default_functions: true
assert_not_nil Redirection.last.guid
end
end

it "should generate guid" do
Redirection.import models, omit_columns_with_default_functions: true
assert_not_nil Redirection.last.guid
end
end
end
end
Expand Down

0 comments on commit ffeb31a

Please sign in to comment.