# encoding: utf-8 # require 'spec_helper' describe Mode::Sdk::Column do describe '#validate!' do it 'returns true if no exceptions are raised' do column = Mode::Sdk::Column.new({}) expect(column).to receive(:validate_keys!) expect(column).to receive(:validate_name!) expect(column).to receive(:validate_type!) expect(column.validate!).to eq(true) end end describe '#validate_keys!' do it 'raises error if column is missing name key' do column = Mode::Sdk::Column.new(type: 'text') expect { column.send(:validate_keys!) }.to raise_error(Mode::Sdk::Column::InvalidError) do |exception| expect(exception.message).to match(/missing required key 'name'/i) end end it 'raises error if column is missing type key' do column = Mode::Sdk::Column.new(name: 'name') expect { column.send(:validate_keys!) }.to raise_error(Mode::Sdk::Column::InvalidError) do |exception| expect(exception.message).to match(/missing required key 'type'/i) end end it 'raises error if column has extra keys' do column = Mode::Sdk::Column.new(name: 'name', type: 'text', foo: 'bar') expect { column.send(:validate_keys!) }.to raise_error(Mode::Sdk::Column::InvalidError) do |exception| expect(exception.message).to match(/has unexpected keys/i) end end it 'returns true for valid keys' do column = Mode::Sdk::Column.new(name: nil, type: nil) expect(column.send(:validate_keys!)).to eq(true) end end describe '#validate_name!' do it 'raises error if column name is too short' do column = Mode::Sdk::Column.new(name: 'n') expect { column.send(:validate_name!) }.to raise_error(Mode::Sdk::Column::InvalidError) do |exception| expect(exception.message).to match(/name is invalid/i) end end it 'raises error if column name is too long' do column = Mode::Sdk::Column.new(name: 'no' * 50) expect { column.send(:validate_name!) }.to raise_error(Mode::Sdk::Column::InvalidError) do |exception| expect(exception.message).to match(/name is invalid/i) end end it 'returns true for valid name' do column = Mode::Sdk::Column.new(name: 'name') expect(column.send(:validate_name!)).to eq(true) end end describe '#validate_type!' do it 'raises error if column type is not recognized' do column = Mode::Sdk::Column.new(type: 'nope') expect { column.send(:validate_type!) }.to raise_error(Mode::Sdk::Column::InvalidError) do |exception| expect(exception.message).to match(/type is invalid/i) end end it 'returns true for valid type' do column = Mode::Sdk::Column.new(type: 'integer') expect(column.send(:validate_type!)).to eq(true) end end end