test/unit/lib/mosql/schema.rb in mosql-0.2.0 vs test/unit/lib/mosql/schema.rb in mosql-0.3.0

- old
+ new

@@ -10,10 +10,11 @@ :columns: - id: :source: _id :type: TEXT - var: INTEGER + - str: TEXT with_extra_props: :meta: :table: sqltable2 :extra_props: true :columns: @@ -86,20 +87,21 @@ end it 'creates a SQL schema with the right fields' do db = {} stub_1 = stub() - stub_1.expects(:column).with('id', 'TEXT') - stub_1.expects(:column).with('var', 'INTEGER') + 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('_extra_props').never stub_1.expects(:primary_key).with([:id]) stub_2 = stub() - stub_2.expects(:column).with('id', 'TEXT') + 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.expects(:column).with('_id', 'TEXT') + stub_3.expects(:column).with('_id', 'TEXT', {}) stub_3.expects(:column).with('_extra_props').never stub_3.expects(:primary_key).with([:_id]) (class << db; self; end).send(:define_method, :create_table?) do |tbl, &blk| case tbl when "sqltable" @@ -116,25 +118,37 @@ @map.create_schema(db) end describe 'when transforming' do it 'transforms rows' do - out = @map.transform('db.collection', {'_id' => "row 1", 'var' => 6}) - assert_equal(["row 1", 6], out) + out = @map.transform('db.collection', {'_id' => "row 1", 'var' => 6, 'str' => 'a string'}) + assert_equal(["row 1", 6, 'a string'], 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'], @map.all_columns(@map.find_ns('db.collection'))) + assert_equal(['id', 'var', 'str'], @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) + 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 end describe 'when copying data' do it 'quotes special characters' do assert_equal(%q{\\\\}, @map.quote_copy(%q{\\})) @@ -191,8 +205,80 @@ 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 = <<EOF +--- +db: + :meta: + :alias: db_[0-9]+ + collection: + :meta: + :table: sqltable + :columns: + - _id: TEXT + - var: INTEGER +EOF + before do + @map = MoSQL::Schema.new(YAML.load(ALIAS_MAP)) + end + + it 'can look up collections by aliases' do + ns = @map.find_ns("db.collection") + assert_equal(ns, @map.find_ns("db_00.collection")) + assert_equal(ns, @map.find_ns("db_01.collection")) + end + + it 'caches negative lookups' do + assert_equal(nil, @map.find_ns("nosuchdb.foo")) + assert(@map.instance_variable_get(:@map).key?("nosuchdb")) + end + + it 'can do lookups after a negative cache' do + @map.find_ns("nosuchdb.foo") + assert_nil(@map.find_ns("otherdb.collection")) + end + end + + describe 'parsing magic source values' do + OTHER_MAP = <<EOF +--- +db: + collection: + :meta: + :table: a_table + :columns: + - _id: TEXT + - mosql_created: + :source: $timestamp + :type: timestamp + invalid: + :meta: + :table: invalid + :columns: + - _id: TEXT + - magic: + :source: $magic + :type: timestamp +EOF + + before do + @othermap = MoSQL::Schema.new(YAML.load(OTHER_MAP)) + end + + it 'translates $timestamp' do + r = @othermap.transform('db.collection', { '_id' => '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 end