-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29ccae4
commit 5c3ff5e
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
...rce_mongoid/spec/lib/forest_admin_datasource_mongoid/utils/schema/field_flattener_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require 'spec_helper' | ||
|
||
module ForestAdminDatasourceMongoid | ||
module Utils | ||
module Schema | ||
describe FieldsGenerator do | ||
let(:stack) { [{ prefix: nil, as_fields: [], as_models: [] }] } | ||
|
||
describe 'Construction' do | ||
it 'does not modify the schema' do | ||
fields = described_class.build_fields_schema(Post, stack) | ||
expect(fields.keys).to eq(%w[_id created_at updated_at title body rating tag_ids]) | ||
end | ||
|
||
it 'skips flattened models' do | ||
stack = [{ prefix: nil, as_fields: ['section.body'], as_models: ['section'] }] | ||
fields = described_class.build_fields_schema(Label, stack) | ||
|
||
expect(fields.keys).to eq(['_id', 'name', 'section@@@body']) | ||
end | ||
|
||
it 'flattens all nested fields when no level is provided' do | ||
stack = [{ prefix: nil, as_fields: ['section.content', 'section.body'], as_models: [] }] | ||
fields = described_class.build_fields_schema(Label, stack) | ||
|
||
expect(fields.keys).to eq(['_id', 'name', 'section@@@content', 'section@@@body']) | ||
end | ||
|
||
it 'flattens only requested fields' do | ||
stack = [{ prefix: nil, as_fields: ['label.name'], as_models: [] }] | ||
fields = described_class.build_fields_schema(Band, stack) | ||
|
||
expect(fields.keys).to eq(['_id', 'created_at', 'updated_at', 'label', 'label@@@name']) | ||
end | ||
|
||
it 'onlies flatten selected fields when level = 1' do | ||
stack = [{ prefix: nil, as_fields: ['label.name', 'label.section'], as_models: [] }] | ||
fields = described_class.build_fields_schema(Band, stack) | ||
|
||
expect(fields.keys).to eq(['_id', 'created_at', 'updated_at', 'label@@@name', 'label@@@section']) | ||
end | ||
|
||
it 'returns a "String" as type when "_id" is generated' do | ||
fields = described_class.build_fields_schema(Post, stack) | ||
|
||
expect(fields['_id'].column_type).to eq('String') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |