require File.join(File.dirname(__FILE__), '_lib.rb') class MoSQL::Test::Functional::SchemaTest < MoSQL::Test::Functional TEST_MAP = < "a", 'var' => 0}, {'_id' => "b", 'var' => 1}, {'_id' => "c"}, {'_id' => "d", 'other_var' => "hello"} ] @map.copy_data(@sequel, 'db.collection', objects.map { |o| @map.transform('db.collection', o) } ) assert_equal(4, table.count) rows = table.select.sort_by { |r| r[:_id] } assert_equal(%w[a b c d], rows.map { |r| r[:_id] }) assert_equal(nil, rows[2][:var]) assert_equal(nil, rows[3][:var]) end it 'Can COPY dotted data' do objects = [ {'_id' => "a", 'vars' => {'a' => 1, 'b' => 2}}, {'_id' => "b", 'vars' => {}}, {'_id' => "c", 'vars' => {'a' => 2, 'c' => 6}}, {'_id' => "d", 'vars' => {'a' => 1, 'c' => 7}, 'extra' => 'moo'} ] @map.copy_data(@sequel, 'db.with_dotted', objects.map { |o| @map.transform('db.with_dotted', o) } ) assert_equal(4, table3.count) o = table3.first(:_id => 'a') assert_equal("1", o[:var_a]) assert_equal("2", o[:var_b]) o = table3.first(:_id => 'b') assert_equal({}, JSON.parse(o[:_extra_props])) o = table3.first(:_id => 'c') assert_equal({'vars' => { 'c' => 6} }, JSON.parse(o[:_extra_props])) o = table3.first(:_id => 'd') assert_equal({'vars' => { 'c' => 7}, 'extra' => 'moo' }, JSON.parse(o[:_extra_props])) assert_equal(nil, o[:var_b]) end it 'Can COPY BSON::ObjectIDs' do o = {'_id' => BSON::ObjectId.new, 'var' => 0} @map.copy_data(@sequel, 'db.collection', [ @map.transform('db.collection', o)] ) assert_equal(o['_id'].to_s, table.select.first[:_id]) end it 'Can transform BSON::ObjectIDs' do o = {'_id' => BSON::ObjectId.new, 'var' => 0} row = @map.transform('db.collection', o) table.insert(row) assert_equal(o['_id'].to_s, table.select.first[:_id]) end describe 'special fields' do SPECIAL_MAP = < "a"}, {'_id' => "b"} ] before = @sequel.select(Sequel.function(:NOW)).first[:now] @specialmap.copy_data(@sequel, 'db.collection', objects.map { |o| @specialmap.transform('db.collection', o) } ) after = @sequel.select(Sequel.function(:NOW)).first[:now] rows = @sequel[:special].select.sort_by { |r| r[:_id] } assert_instance_of(Time, rows[0][:mosql_updated]) assert(rows[0][:mosql_updated] > before && rows[0][:mosql_updated] < after) end end end