require File.join(File.dirname(__FILE__), '../../../_lib.rb') class MoSQL::Test::SchemaTest < MoSQL::Test TEST_MAP = < "row 1", 'var' => 6, 'str' => 'a string', 'arry' => [1,2,3]}) assert_equal(["row 1", 6, 'a string', [1,2,3]], out) end it 'Includes extra props' do out = @map.transform('db.with_extra_props', {'_id' => 7, 'var' => 6, 'other var' => {'key' => 'value'}}) assert_equal(2, out.length) assert_equal(7, out[0]) assert_equal({'var' => 6, 'other var' => {'key' => 'value'}}, JSON.parse(out[1])) end it 'gets all_columns right' do assert_equal(['id', 'var', 'str', 'arry'], @map.all_columns(@map.find_ns('db.collection'))) assert_equal(['id', '_extra_props'], @map.all_columns(@map.find_ns('db.with_extra_props'))) end it 'stringifies symbols' do out = @map.transform('db.collection', {'_id' => "row 1", 'str' => :stringy, 'arry' => [1,2,3]}) assert_equal(["row 1", nil, 'stringy', [1,2,3]], out) end it 'extracts object ids from a DBRef' do oid = BSON::ObjectId.new out = @map.transform('db.collection', {'_id' => "row 1", 'str' => BSON::DBRef.new('db.otherns', oid)}) assert_equal(["row 1", nil, oid.to_s, nil], out) end it 'changes NaN to null in extra_props' do out = @map.transform('db.with_extra_props', {'_id' => 7, 'nancy' => 0.0/0.0}) extra = JSON.parse(out[1]) assert(extra.key?('nancy')) assert_equal(nil, extra['nancy']) end it 'base64-encodes BSON::Binary blobs in extra_props' do out = @map.transform('db.with_extra_props', {'_id' => 7, 'blob' => BSON::Binary.new("\x00\x00\x00"), 'embedded' => {'thing' => BSON::Binary.new("\x00\x00\x00")}}) extra = JSON.parse(out[1]) assert(extra.key?('blob')) assert_equal('AAAA', extra['blob'].strip) refute_nil(extra['embedded']) refute_nil(extra['embedded']['thing']) assert_equal('AAAA', extra['embedded']['thing'].strip) end it 'will treat arrays as strings when schame says to' do out = @map.transform('db.treat_array_as_string', {'_id' => 1, 'arry' => [1, 2, 3]}) assert_equal(out[0], 1) assert_equal(out[1], '[1,2,3]') end end describe 'when copying data' do it 'quotes special characters' do assert_equal(%q{\\\\}, @map.quote_copy(%q{\\})) assert_equal(%Q{\\\t}, @map.quote_copy( %Q{\t})) assert_equal(%Q{\\\n}, @map.quote_copy( %Q{\n})) assert_equal(%Q{some text}, @map.quote_copy(%Q{some text})) end end describe 'fetch_and_delete_dotted' do def check(orig, path, expect, result) assert_equal(expect, @map.fetch_and_delete_dotted(orig, path)) assert_equal(result, orig) end it 'works on things without dots' do check({'a' => 1, 'b' => 2}, 'a', 1, {'b' => 2}) end it 'works if the key does not exist' do check({'a' => 1, 'b' => 2}, 'c', nil, {'a' => 1, 'b' => 2}) end it 'fetches nested hashes' do check({'a' => 1, 'b' => { 'c' => 1, 'd' => 2 }}, 'b.d', 2, {'a' => 1, 'b' => { 'c' => 1 }}) end it 'fetches deeply nested hashes' do check({'a' => 1, 'b' => { 'c' => { 'e' => 8, 'f' => 9 }, 'd' => 2 }}, 'b.c.e', 8, {'a' => 1, 'b' => { 'c' => { 'f' => 9 }, 'd' => 2 }}) end it 'cleans up empty hashes' do check({'a' => { 'b' => 4}}, 'a.b', 4, {}) check({'a' => { 'b' => { 'c' => 5 }, 'd' => 9}}, 'a.b.c', 5, {'a' => { 'd' => 9 }}) end it 'recursively cleans' do check({'a' => { 'b' => { 'c' => { 'd' => 99 }}}}, 'a.b.c.d', 99, {}) end it 'handles missing path components' do check({'a' => { 'c' => 4 }}, 'a.b.c.d', nil, {'a' => { 'c' => 4 }}) end end describe 'when handling a map with aliases' do ALIAS_MAP = < 'a' }) assert_equal(['a', Sequel.function(:now)], r) end it 'rejects unknown specials' do assert_raises(MoSQL::SchemaError) do r = @othermap.transform('db.invalid', { '_id' => 'a' }) end end end describe 'dotted names' do MAP = <