Skip to content
This repository has been archived by the owner on Dec 22, 2020. It is now read-only.

Commit

Permalink
Fix bulk_upsert's fallback case, and add a test.
Browse files Browse the repository at this point in the history
closes #17.
  • Loading branch information
nelhage committed Mar 26, 2013
1 parent 5945b62 commit 06aca74
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/mosql/cli.rb
Expand Up @@ -164,7 +164,7 @@ def bulk_upsert(table, ns, items)
items.each do |it|
h = {}
cols.zip(it).each { |k,v| h[k] = v }
@sql.upsert(table, primary_sql_key_for_ns(ns), h)
@sql.upsert(table, @schemamap.primary_sql_key_for_ns(ns), h)
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions test/functional/cli.rb
Expand Up @@ -110,4 +110,44 @@ def fake_cli
})
assert_equal(0, sequel[:sqltable2].where(:id => o['_id'].to_s).count)
end

describe '.bulk_upsert' do
it 'inserts multiple rows' do
objs = [
{ '_id' => BSON::ObjectId.new, 'var' => 0 },
{ '_id' => BSON::ObjectId.new, 'var' => 1 },
{ '_id' => BSON::ObjectId.new, 'var' => 3 },
].map { |o| @map.transform('mosql_test.collection', o) }

@cli.bulk_upsert(sequel[:sqltable], 'mosql_test.collection',
objs)

assert(sequel[:sqltable].where(:_id => objs[0].first, :var => 0).count)
assert(sequel[:sqltable].where(:_id => objs[1].first, :var => 1).count)
assert(sequel[:sqltable].where(:_id => objs[2].first, :var => 3).count)
end

it 'upserts' do
_id = BSON::ObjectId.new
objs = [
{ '_id' => _id, 'var' => 0 },
{ '_id' => BSON::ObjectId.new, 'var' => 1 },
{ '_id' => BSON::ObjectId.new, 'var' => 3 },
].map { |o| @map.transform('mosql_test.collection', o) }

@cli.bulk_upsert(sequel[:sqltable], 'mosql_test.collection',
objs)

newobjs = [
{ '_id' => _id, 'var' => 117 },
{ '_id' => BSON::ObjectId.new, 'var' => 32 },
].map { |o| @map.transform('mosql_test.collection', o) }
@cli.bulk_upsert(sequel[:sqltable], 'mosql_test.collection',
newobjs)


assert(sequel[:sqltable].where(:_id => newobjs[0].first, :var => 117).count)
assert(sequel[:sqltable].where(:_id => newobjs[1].first, :var => 32).count)
end
end
end

0 comments on commit 06aca74

Please sign in to comment.