test/unit/lib/mosql/schema.rb in mosql-0.3.1 vs test/unit/lib/mosql/schema.rb in mosql-0.3.2

- old
+ new

@@ -11,10 +11,11 @@ - id: :source: _id :type: TEXT - var: INTEGER - str: TEXT + - arry: INTEGER ARRAY with_extra_props: :meta: :table: sqltable2 :extra_props: true :columns: @@ -24,13 +25,26 @@ old_conf_syntax: :columns: - _id: TEXT :meta: :table: sqltable3 + with_extra_props_type: + :meta: + :table: sqltable4 + :extra_props: JSON + :columns: + - _id: TEXT + treat_array_as_string: + :columns: + - _id: TEXT + - arry: TEXT + :meta: + :table: sqltable5 EOF before do + Sequel.extension(:pg_array) @map = MoSQL::Schema.new(YAML.load(TEST_MAP)) end it 'Loads the schema' do assert(@map) @@ -80,75 +94,116 @@ it 'can create a SQL schema' do db = stub() db.expects(:create_table?).with('sqltable') db.expects(:create_table?).with('sqltable2') db.expects(:create_table?).with('sqltable3') + db.expects(:create_table?).with('sqltable4') + db.expects(:create_table?).with('sqltable5') @map.create_schema(db) end it 'creates a SQL schema with the right fields' do db = {} - stub_1 = stub() + stub_1 = stub('table 1') stub_1.expects(:column).with('id', 'TEXT', {}) stub_1.expects(:column).with('var', 'INTEGER', {}) stub_1.expects(:column).with('str', 'TEXT', {}) + stub_1.expects(:column).with('arry', 'INTEGER ARRAY', {}) stub_1.expects(:column).with('_extra_props').never stub_1.expects(:primary_key).with([:id]) - stub_2 = stub() + stub_2 = stub('table 2') stub_2.expects(:column).with('id', 'TEXT', {}) stub_2.expects(:column).with('_extra_props', 'TEXT') stub_2.expects(:primary_key).with([:id]) - stub_3 = stub() + stub_3 = stub('table 3') stub_3.expects(:column).with('_id', 'TEXT', {}) stub_3.expects(:column).with('_extra_props').never stub_3.expects(:primary_key).with([:_id]) + stub_4 = stub('table 4') + stub_4.expects(:column).with('_id', 'TEXT', {}) + stub_4.expects(:column).with('_extra_props', 'JSON') + stub_4.expects(:primary_key).with([:_id]) + stub_5 = stub('table 5') + stub_5.expects(:column).with('_id', 'TEXT', {}) + stub_5.expects(:column).with('arry', 'TEXT', {}) + stub_5.expects(:primary_key).with([:_id]) (class << db; self; end).send(:define_method, :create_table?) do |tbl, &blk| case tbl when "sqltable" o = stub_1 when "sqltable2" o = stub_2 when "sqltable3" o = stub_3 + when "sqltable4" + o = stub_4 + when "sqltable5" + o = stub_5 else assert(false, "Tried to create an unexpected table: #{tbl}") end o.instance_eval(&blk) end @map.create_schema(db) end describe 'when transforming' do it 'transforms rows' do - out = @map.transform('db.collection', {'_id' => "row 1", 'var' => 6, 'str' => 'a string'}) - assert_equal(["row 1", 6, 'a string'], out) + out = @map.transform('db.collection', {'_id' => "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'], @map.all_columns(@map.find_ns('db.collection'))) + 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}) - assert_equal(["row 1", nil, 'stringy'], out) + 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{\\})) @@ -277,8 +332,27 @@ 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 = <<EOF +db: + my.collection: + :meta: + :table: table + :columns: + - _id: TEXT +EOF + + it 'handles dotted names' do + @map = MoSQL::Schema.new(YAML.load(MAP)) + collections = @map.collections_for_mongo_db('db') + assert(collections.include?('my.collection'), + "#{collections} doesn't include `my.collection`") + assert(@map.find_ns('db.my.collection')) end end end