require 'helper' class AxleAttributes::SerializedChildTest < ActiveSupport::TestCase class TestModel < Superstore::Base include AxleAttributes::SerializedChild has_attributes( color: {type: :string, index: true, version: true}, price: {type: :integer, index: false, version: false} ) end def test_id record = TestModel.new refute_nil record.id assert_equal :string, TestModel.attributes['id'].type assert_equal true, TestModel.attributes['id'].indexed? end test 'equality' do same_1 = TestModel.new id: '5' same_2 = TestModel.new id: '5' different = TestModel.new id: '6' assert_equal same_1, same_2 refute_equal different, same_1 end test 'created_at' do record = TestModel.new assert_in_delta Time.now, record.created_at, 1 end test 'attributes' do refute_nil TestModel.new.attributes['id'] assert_equal({'color' => 'green'}, TestModel.new(color: 'green').attributes.except('id', 'created_at')) assert_equal({'color' => 'green'}, TestModel.new(color: 'green', price: nil).attributes.except('id', 'created_at')) assert_equal({'color' => 'green', 'price' => 25}, TestModel.new(color: 'green', price: 25).attributes.except('id', 'created_at')) end test 'attributes with unknown method' do record = Class.new(TestModel) do has_attribute :bad, type: false end.new(bad: '1') assert_equal({}, record.attributes.except('id', 'created_at')) end test 'attributes with unknown attribute' do record = Class.new(TestModel) do def bad=(v) @attributes['bad'] = v end def bad @attributes['bad'] end end.new(bad: '1') assert_equal({}, record.attributes.except('id', 'created_at')) end test 'as_json' do record = TestModel.new(color: 'green', price: 25) assert_equal record.attributes, record.as_json end test 'versioned_attributes' do record = TestModel.new(color: 'green', price: 25) search = record.versioned_attributes assert_equal 'green', search['color'] refute search.key?('created_at') refute search.key?('price') end test 'as_search' do record = TestModel.new(color: 'green', price: 25) search = record.as_search assert_equal 'green', search['color'] refute_nil search['created_at'] refute search.key?('price') end end