-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return primary keys when using blocks or values #43
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -132,6 +132,7 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase | |||||||||||||||||||||
@insert.save! | ||||||||||||||||||||||
|
||||||||||||||||||||||
assert_equal 0, @insert.result_sets.count | ||||||||||||||||||||||
assert_nil @insert.inserted_ids | ||||||||||||||||||||||
end | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
@@ -159,11 +160,13 @@ class BulkInsertWorkerTest < ActiveSupport::TestCase | |||||||||||||||||||||
worker.add greeting: "second" | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On this test I would follow something similar to: bulk_insert/test/bulk_insert_test.rb Lines 34 to 43 in 4a69aff
you can eventually also capture the error message as per the example linked and perform a partial matching on the error message |
||||||||||||||||||||||
worker.save! | ||||||||||||||||||||||
assert_equal 1, worker.result_sets.count | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here the result_sets for non postgresql adapters would look like
|
||||||||||||||||||||||
assert_equal [], worker.inserted_ids | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the expected result here should be
|
||||||||||||||||||||||
|
||||||||||||||||||||||
worker.add greeting: "third" | ||||||||||||||||||||||
worker.add greeting: "fourth" | ||||||||||||||||||||||
worker.save! | ||||||||||||||||||||||
assert_equal 2, worker.result_sets.count | ||||||||||||||||||||||
assert_equal [], worker.inserted_ids | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the expected result here should be
|
||||||||||||||||||||||
end | ||||||||||||||||||||||
|
||||||||||||||||||||||
test "initialized with empty result sets array" do | ||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,13 +44,26 @@ class BulkInsertTest < ActiveSupport::TestCase | |
|
||
test "bulk_insert with array should save the array immediately" do | ||
assert_difference "Testing.count", 2 do | ||
Testing.bulk_insert values: [ | ||
result = Testing.bulk_insert values: [ | ||
[ "Hello", 15, true ], | ||
{ greeting: "Hey", age: 20, happy: false } | ||
] | ||
assert_nil result | ||
end | ||
end | ||
|
||
test "bulk_insert with an empty value should not return worker" do | ||
assert_nil Testing.bulk_insert values: [] | ||
end | ||
|
||
test "bulk_insert with option to return primary keys and values should return the new ids" do | ||
output = Testing.bulk_insert( | ||
values: [["Hello", 15, true]], | ||
return_primary_keys: true | ||
) | ||
Comment on lines
+60
to
+63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as per the previous comment on skip based on the adapter. |
||
assert_equal output, [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the expected result here should be
|
||
end | ||
|
||
test "default_bulk_columns should return all columns without id" do | ||
default_columns = %w(greeting age happy created_at updated_at color) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This option on non postgresql adapters may lead to some errors like:
Old rails versions
Or it may return empty arrays in SQLite and Mysql (only if rails >=6).
In order to avoid confusion and future bugs, I would suggest to fail as soon as possible in case of misconfiguration.
E.g.
bulk_insert/lib/bulk_insert/worker.rb
Lines 22 to 23 in 4a69aff