Permalink
Browse files

Fix bulk_upsert's fallback case, and add a test.

closes #17.
  • Loading branch information...
nelhage committed Mar 26, 2013
1 parent 5945b62 commit 06aca741df7cb67a20f147c5de36b94eba6bc62b
Showing with 41 additions and 1 deletion.
  1. +1 −1 lib/mosql/cli.rb
  2. +40 −0 test/functional/cli.rb
View
@@ -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
View
@@ -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.