require 'flydata-core/table_def/base' module FlydataCore module TableDef describe Base do let(:subject_object) { described_class.new(table_def, table_name, columns, column_def, unique_keys, default_charset, default_source_charset, comment) } let(:table_def) { double('table_def') } let(:table_name) { double('table_name') } let(:columns) { double('columns') } let(:column_def) { double('column_def') } let(:unique_keys) { double('unique_keys') } let(:default_charset) { double('default_charset') } let(:default_source_charset) { double('default_source_charset') } let(:comment) { double('comment') } let(:options) { double('options') } describe '#pk_columns' do subject { subject_object.pk_columns } let(:columns) do [ {column: "name"}, {column: "address_id", primary_key: pk1_value}, {column: "zip"}, {column: "user_id", primary_key: pk2_value}, ] end context 'with unordered primary keys' do let(:pk1_value) { 't' } let(:pk2_value) { 't' } it "returns primary keys in the column order" do is_expected.to eq %w(address_id user_id) end end context 'with ordered primary keys' do let(:pk1_value) { 2 } let(:pk2_value) { 1 } it "returns primary keys in the pk order" do is_expected.to eq %w(user_id address_id) end end end describe '.override_pk' do subject { described_class.send(:override_pk, table_name, columns, options) } let(:columns) { [ col1def, col2def, ] } let(:col1def) { {column: col1name, primary_key: true} } let(:col2def) { {column: col2name} } let(:col1name) { double("col1name") } let(:col2name) { double("col2name") } shared_examples "nothings changed" do it { subject expect(col1def[:primary_key]).to be_truthy expect(col2def.has_key?(:primary_key)).to be_falsey } end context 'when no :pk_override attribute exists' do let(:options) { {} } it_behaves_like "nothings changed" end context 'when the table has no pk_override' do let(:options) { {pk_override:{}} } it_behaves_like "nothings changed" end context 'when the table has pk_override' do let(:options) { {pk_override:{table_name => [col2name]}} } it 'overrides primary key settings' do subject expect(col1def.has_key?(:primary_key)).to be_falsey expect(col2def[:primary_key]).to be_truthy end end context 'when the column specified in pk_override does not exist' do let(:options) { {pk_override:{table_name => ["asdf"]}} } it do expect{subject}.to raise_error /the column does not exist in the table/ end end end describe '.check_pk' do subject { described_class.send(:check_pk, table_name, columns, options) } let(:columns) { [ col1def, col2def, ] } let(:col1def) { {} } let(:col2def) { {} } let(:options) { {} } context 'when a pk column exists' do let(:col1def) { {primary_key: true} } it { expect{subject}.to_not raise_error } end context 'when no pk column exists' do let(:col1def) { {} } let(:col2def) { {} } it { expect{subject}.to raise_error(TableDefError) } context 'if skip_primary_key_check option is set' do let(:options) { {skip_primary_key_check: true} } it { expect{subject}.to_not raise_error } end end end end end end