@@ -22,6 +22,12 @@ class BaseTestWithIgnoredProperties < CouchbaseOrm::Base
2222 attribute :job , :string
2323end
2424
25+ class BaseTestWithUnknownAttributesAllowed < CouchbaseOrm ::Base
26+ self . raise_on_unknown_attributes = false
27+ attribute :name , :string
28+ attribute :job , :string
29+ end
30+
2531class BaseTestWithPropertiesAlwaysExistsInDocument < CouchbaseOrm ::Base
2632 self . properties_always_exists_in_document = true
2733 attribute :name , :string
@@ -349,6 +355,65 @@ class InvalidNested < CouchbaseOrm::NestedDocument
349355 end
350356 end
351357
358+ describe 'handling unknown attributes' do
359+ context 'when raise_on_unknown_attributes is set to false' do
360+ it 'returns false when queried' do
361+ expect ( BaseTestWithUnknownAttributesAllowed . raise_on_unknown_attributes ) . to be ( false )
362+ end
363+
364+ it 'silently ignores unknown attributes in new' do
365+ model = BaseTestWithUnknownAttributesAllowed . new ( name : 'test' , job : 'dev' , unknown_attr : 'value' )
366+ expect ( model . name ) . to eq ( 'test' )
367+ expect ( model . job ) . to eq ( 'dev' )
368+ expect ( model . respond_to? ( :unknown_attr ) ) . to be ( false )
369+ end
370+
371+ it 'silently ignores unknown attributes in assign_attributes' do
372+ model = BaseTestWithUnknownAttributesAllowed . new ( name : 'test' )
373+ expect {
374+ model . assign_attributes ( name : 'updated' , job : 'engineer' , foo : 'bar' , baz : 'qux' )
375+ } . not_to raise_error
376+ expect ( model . name ) . to eq ( 'updated' )
377+ expect ( model . job ) . to eq ( 'engineer' )
378+ expect ( model . respond_to? ( :foo ) ) . to be ( false )
379+ expect ( model . respond_to? ( :baz ) ) . to be ( false )
380+ end
381+
382+ it 'only stores known attributes' do
383+ model = BaseTestWithUnknownAttributesAllowed . new (
384+ name : 'Alice' ,
385+ job : 'Developer' ,
386+ unknown_field_1 : 'value1' ,
387+ unknown_field_2 : 'value2'
388+ )
389+ # Only known attributes should be stored
390+ expect ( model . name ) . to eq ( 'Alice' )
391+ expect ( model . job ) . to eq ( 'Developer' )
392+ expect ( model . respond_to? ( :unknown_field_1 ) ) . to be ( false )
393+ expect ( model . respond_to? ( :unknown_field_2 ) ) . to be ( false )
394+ end
395+ end
396+
397+ context 'default behavior (raise_on_unknown_attributes = true)' do
398+ it 'returns true by default' do
399+ expect ( BaseTest . raise_on_unknown_attributes ) . to be ( true )
400+ end
401+
402+ it 'raises ActiveModel::UnknownAttributeError on unknown attributes in new' do
403+ expect {
404+ BaseTest . new ( name : 'bob' , job : 'dev' , foo : 'bar' )
405+ } . to raise_error ( ActiveModel ::UnknownAttributeError )
406+ end
407+
408+ it 'raises ActiveModel::UnknownAttributeError on unknown attributes in assign_attributes' do
409+ model = BaseTest . new ( name : 'bob' )
410+ expect {
411+ model . assign_attributes ( job : 'dev' , foo : 'bar' )
412+ } . to raise_error ( ActiveModel ::UnknownAttributeError )
413+ end
414+ end
415+ end
416+
352417 describe '.properties_always_exists_in_document' do
353418 it 'Uses NOT VALUED when properties_always_exists_in_document = false' do
354419 where_clause = BaseTest . where ( name : nil )
0 commit comments