# frozen_string_literal: true # # Copyright (c) 2019-present, Blue Marble Payroll, LLC # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # require 'spec_helper' require 'fixtures/models' describe Dbee::Model do describe '#initialize' do specify 'name is set properly' do config = { name: 'theaters' } model = described_class.make(config) expect(model.name).to eq(config[:name]) end specify 'table defaults to name' do config = { name: 'theaters' } model = described_class.make(config) expect(model.table).to eq(config[:name]) end specify 'table can be explicitly set' do config = { name: 'favorite_comedy_movies', table: 'movies' } model = described_class.make(config) expect(model.table).to eq(config[:table]) end specify 'models are set properly' do config = { name: 'theaters', models: [ { name: 'members' }, { name: 'passes' } ] } expected_association_names = config[:models].map { |a| a[:name] } model = described_class.make(config) association_names = model.models.map(&:name) expect(association_names).to eq(expected_association_names) end end describe '.make_keyed_by' do it 'returns a hash of Models where the keys equal the names of the models' do input = { model1: nil, model2: { table: :model2_table } } expected_result = { 'model1' => described_class.new(name: 'model1'), 'model2' => described_class.new(name: 'model2', table: 'model2_table') } expect(described_class.make_keyed_by(:name, input)).to eq expected_result end it 'accepts values of Dbee::Model instances' do input = { model1: described_class.new(name: :model1) } expected_result = { 'model1' => described_class.new(name: :model1) } expect(described_class.make_keyed_by(:name, input)).to eq expected_result end it 'accepts values of Dbee::Model instances when the key attribute is a string' do input = { 'model1' => described_class.new(name: 'model1') } expected_result = { 'model1' => described_class.new(name: 'model1') } expect(described_class.make_keyed_by(:name, input)).to eq expected_result end it 'raises an error when the input hash key is not equal to the name of the model' do input = { model1: described_class.new(name: :bogus) } expect do described_class.make_keyed_by(:name, input) end.to raise_error ArgumentError, "expected a name of 'model1' but got 'bogus'" end end describe '#to_s' do it 'is represented by the model name' do expect(described_class.new(name: 'foo').to_s).to eq 'foo' end end equality_cases = { tree_based: yaml_fixture('models.yaml')['Theaters, Members, and Movies Tree Based'], graph_based: yaml_fixture('models.yaml')['Theaters, Members, and Movies from DSL']['theater'] } equality_cases[:graph_based].merge!(name: 'theaters') equality_cases.each do |test_case, config| describe "equality for #{test_case}" do let(:model1) { described_class.make(config) } let(:model2) { described_class.make(config) } subject { described_class.make(config) } specify 'equality compares attributes' do expect(model1).to eq(model2) expect(model1).to eql(model2) end it 'returns false unless comparing same object types' do expect(subject).not_to eq(config) expect(subject).not_to eq(nil) end describe 'hash codes' do specify 'are equal when objects are equal' do expect(model1).to eq(model2) expect(model1.hash).to eq(model2.hash) end specify 'are not equal when objects are not equal' do different_model = described_class.new(name: :oddball) expect(model1).not_to eq(different_model) expect(model1.hash).not_to eq(different_model.hash) end end end end end