Skip to content

Commit d763a15

Browse files
Add comprehensive tests for raise_on_unknown_attributes with NestedDocument
- Add test classes: NestedDocWithUnknownAttributesAllowed and NestedDocWithDefaultBehavior - Add parent document test classes to test nested behavior - Test NestedDocument with raise_on_unknown_attributes = false - Test NestedDocument with default behavior (true) - Test nested documents within parent documents - Test assign_attributes on NestedDocument - All 14 tests pass (7 for base documents + 7 for nested documents)
1 parent 551d89c commit d763a15

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

spec/base_spec.rb

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,27 @@ class BaseTestWithUnknownAttributesAllowed < CouchbaseOrm::Base
2828
attribute :job, :string
2929
end
3030

31+
class NestedDocWithUnknownAttributesAllowed < CouchbaseOrm::NestedDocument
32+
self.raise_on_unknown_attributes = false
33+
attribute :title, :string
34+
attribute :value, :integer
35+
end
36+
37+
class NestedDocWithDefaultBehavior < CouchbaseOrm::NestedDocument
38+
attribute :title, :string
39+
attribute :value, :integer
40+
end
41+
42+
class ParentDocWithNestedUnknownAllowed < CouchbaseOrm::Base
43+
attribute :name, :string
44+
attribute :nested_item, :nested, type: NestedDocWithUnknownAttributesAllowed
45+
end
46+
47+
class ParentDocWithNestedDefault < CouchbaseOrm::Base
48+
attribute :name, :string
49+
attribute :nested_item, :nested, type: NestedDocWithDefaultBehavior
50+
end
51+
3152
class BaseTestWithPropertiesAlwaysExistsInDocument < CouchbaseOrm::Base
3253
self.properties_always_exists_in_document = true
3354
attribute :name, :string
@@ -412,6 +433,57 @@ class InvalidNested < CouchbaseOrm::NestedDocument
412433
}.to raise_error(ActiveModel::UnknownAttributeError)
413434
end
414435
end
436+
437+
context 'for NestedDocument classes' do
438+
it 'returns false when queried on NestedDocument with raise_on_unknown_attributes = false' do
439+
expect(NestedDocWithUnknownAttributesAllowed.raise_on_unknown_attributes).to be(false)
440+
end
441+
442+
it 'returns true by default on NestedDocument' do
443+
expect(NestedDocWithDefaultBehavior.raise_on_unknown_attributes).to be(true)
444+
end
445+
446+
it 'silently ignores unknown attributes in NestedDocument when disabled' do
447+
nested = NestedDocWithUnknownAttributesAllowed.new(title: 'Test', value: 42, unknown: 'ignored')
448+
expect(nested.title).to eq('Test')
449+
expect(nested.value).to eq(42)
450+
expect(nested.respond_to?(:unknown)).to be(false)
451+
end
452+
453+
it 'raises error for unknown attributes in NestedDocument when enabled' do
454+
expect {
455+
NestedDocWithDefaultBehavior.new(title: 'Test', value: 42, unknown: 'error')
456+
}.to raise_error(ActiveModel::UnknownAttributeError)
457+
end
458+
459+
it 'silently ignores unknown attributes in nested documents within parent' do
460+
parent = ParentDocWithNestedUnknownAllowed.new(name: 'Parent')
461+
nested = NestedDocWithUnknownAttributesAllowed.new(title: 'Nested', value: 100, extra: 'ignored')
462+
parent.nested_item = nested
463+
464+
expect(parent.nested_item.title).to eq('Nested')
465+
expect(parent.nested_item.value).to eq(100)
466+
expect(parent.nested_item.respond_to?(:extra)).to be(false)
467+
end
468+
469+
it 'raises error for unknown attributes in nested documents with default behavior' do
470+
parent = ParentDocWithNestedDefault.new(name: 'Parent')
471+
expect {
472+
NestedDocWithDefaultBehavior.new(title: 'Nested', value: 100, extra: 'error')
473+
}.to raise_error(ActiveModel::UnknownAttributeError)
474+
end
475+
476+
it 'works with assign_attributes on NestedDocument' do
477+
nested = NestedDocWithUnknownAttributesAllowed.new(title: 'Initial')
478+
expect {
479+
nested.assign_attributes(title: 'Updated', value: 50, unknown_field: 'ignored')
480+
}.not_to raise_error
481+
482+
expect(nested.title).to eq('Updated')
483+
expect(nested.value).to eq(50)
484+
expect(nested.respond_to?(:unknown_field)).to be(false)
485+
end
486+
end
415487
end
416488

417489
describe '.properties_always_exists_in_document' do

0 commit comments

Comments
 (0)