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

- old
+ new

@@ -37,10 +37,27 @@ :columns: - _id: TEXT - arry: TEXT :meta: :table: sqltable5 + with_composite_key: + :meta: + :table: sqltable6 + :composite_key: + - store + - time + :columns: + - store: + :source: _id.s + :type: TEXT + - time: + :source: id.t + :type: TEXT + - var: + :source: var + :type: TEXT + EOF before do Sequel.extension(:pg_array) @map = MoSQL::Schema.new(YAML.load(TEST_MAP)) @@ -85,21 +102,22 @@ assert_equal '_id', id_mapping[:name] assert_equal 'TEXT', id_mapping[:type] end it 'Can find the primary key of the SQL table' do - assert_equal('id', @map.primary_sql_key_for_ns('db.collection')) - assert_equal('_id', @map.primary_sql_key_for_ns('db.old_conf_syntax')) + assert_equal(['id'], @map.primary_sql_key_for_ns('db.collection')) + assert_equal(['_id'], @map.primary_sql_key_for_ns('db.old_conf_syntax')) end 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') + db.expects(:create_table?).with('sqltable6') @map.create_schema(db) end it 'creates a SQL schema with the right fields' do @@ -125,10 +143,15 @@ 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]) + stub_6 = stub('table 6') + stub_6.expects(:column).with('store', 'TEXT', {}) + stub_6.expects(:column).with('time', 'TEXT', {}) + stub_6.expects(:column).with('var', 'TEXT', {}) + stub_6.expects(:primary_key).with([:store, :time]) (class << db; self; end).send(:define_method, :create_table?) do |tbl, &blk| case tbl when "sqltable" o = stub_1 when "sqltable2" @@ -137,10 +160,12 @@ o = stub_3 when "sqltable4" o = stub_4 when "sqltable5" o = stub_5 + when "sqltable6" + o = stub_6 else assert(false, "Tried to create an unexpected table: #{tbl}") end o.instance_eval(&blk) end @@ -173,9 +198,16 @@ 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 'converts DBRef to object id in arrays' do + oid = [ BSON::ObjectId.new, BSON::ObjectId.new] + o = {'_id' => "row 1", "str" => [ BSON::DBRef.new('db.otherns', oid[0]), BSON::DBRef.new('db.otherns', oid[1]) ] } + out = @map.transform('db.collection', o) + assert_equal(["row 1", nil, JSON.dump(oid.map! {|o| o.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])