Skip to content

Commit

Permalink
test against YAML desirialization permission options
Browse files Browse the repository at this point in the history
  • Loading branch information
fursich committed Oct 1, 2024
1 parent cf70460 commit e2d385d
Show file tree
Hide file tree
Showing 5 changed files with 734 additions and 490 deletions.
15 changes: 15 additions & 0 deletions spec/lib/dumped_railers/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
config.ignorable_columns = [:uuid]
config.preprocessors = [:foo, :bar]
config.authorized_models = [:model1, :model2]
config.yaml_column_permitted_classes = [Date]
config.a_random_option = 'something'
end
end
Expand All @@ -110,6 +111,7 @@
ignorable_columns: [:uuid],
preprocessors: [:foo, :bar],
authorized_models: [:model1, :model2],
yaml_column_permitted_classes: [Date],
a_random_option: 'something',
)
}
Expand All @@ -118,6 +120,7 @@
subject {
klass.options[:ignorable_columns] << :published_at
klass.options[:preprocessors] << :baz
klass.options[:yaml_column_permitted_classes] << Time
}

it 'does updates original configurations' do
Expand All @@ -126,6 +129,7 @@
ignorable_columns: [:uuid, :published_at],
preprocessors: [:foo, :bar, :baz],
authorized_models: [:model1, :model2],
yaml_column_permitted_classes: [Date, Time],
a_random_option: 'something',
}
)
Expand All @@ -141,10 +145,17 @@
config.ignorable_columns = [:uuid]
config.preprocessors = [:foo, :bar]
config.authorized_models = [:model1, :model2]
config.yaml_column_permitted_classes = [Date, Time]
config.a_random_option = :something
end
end

it 'has preset options' do
expect { subject }.to change { klass.options.keys }.to contain_exactly(
*%i[ignorable_columns preprocessors authorized_models yaml_column_permitted_classes]
)
end

it 'resets ignorable_columns' do
expect { subject }.to change { klass.ignorable_columns }.to %w[id created_at updated_at]
end
Expand All @@ -157,6 +168,10 @@
expect { subject }.to change { klass.authorized_models }.to :any
end

it 'resets yaml_column_permitted_classes' do
expect { subject }.to change { klass.yaml_column_permitted_classes }.to match_array(ActiveRecord.yaml_column_permitted_classes + [Date Time DateTime])
end

it 'resets other options' do
expect { subject }.to change { klass.instance_variable_get(:@_config).a_random_option }.to nil
end
Expand Down
166 changes: 110 additions & 56 deletions spec/lib/dumped_railers/dump_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

let!(:author1) { Author.create!(name: 'William Shakespeare') }
let!(:author2) { Author.create!(name: 'Shikibu Murasaki') }
let!(:article1) { Article.create!(title: 'Romeo and Juliet', writer: author1) }
let!(:article2) { Article.create!(title: 'King Lear', writer: author1) }
let!(:article3) { Article.create!(title: 'Genji Monogatari', writer: author2) }
let!(:article1) { Article.create!(title: 'Romeo and Juliet', writer: author1, published_date: '2020-10-10', published_time: '12:00:00', first_drafted_at: '2020-10-01 12:00:00') }
let!(:article2) { Article.create!(title: 'King Lear', writer: author1, published_date: '2021-10-10', published_time: '12:00:00', first_drafted_at: '2021-10-01 12:00:00') }
let!(:article3) { Article.create!(title: 'Genji Monogatari', writer: author2, published_date: '2022-10-10', published_time: '12:00:00', first_drafted_at: '2022-10-01 12:00:00') }
let(:models) { [Author, Article] }

context 'without any preprocessors' do
Expand All @@ -19,42 +19,69 @@
is_expected.to match(
'authors' =>
{
'_fixture' =>
{
'model_class' => 'Author',
'fixture_generated_by' => 'DumpedRailers',
},
"__author_#{author1.id}" => {
'id' => author1.id,
'_fixture' =>
{
'model_class' => 'Author',
'fixture_generated_by' => 'DumpedRailers',
},
"__author_#{author1.id}" => {
'id' => author1.id,
'name' => author1.name
},
"__author_#{author2.id}" => {
'id' => author2.id,
},
"__author_#{author2.id}" => {
'id' => author2.id,
'name' => author2.name
},
},
},
},
'articles' =>
{
'_fixture' =>
{
'model_class' => 'Article',
'fixture_generated_by' => 'DumpedRailers',
},
"__article_#{article1.id}" => {
'id' => article1.id,
'title' => article1.title,
'_fixture' =>
{
'model_class' => 'Article',
'fixture_generated_by' => 'DumpedRailers',
},
"__article_#{article1.id}" => {
'id' => article1.id,
'title' => article1.title,
'writer' => "__author_#{author1.id}",
},
"__article_#{article2.id}" => {
'id' => article2.id,
'title' => article2.title,
'published_date' => have_attributes(
to_formatted_s: '2020-10-10'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => have_attributes(
to_formatted_s: a_string_including('2020-10-01 12:00:00')
),
},
"__article_#{article2.id}" => {
'id' => article2.id,
'title' => article2.title,
'writer' => "__author_#{author1.id}",
},
"__article_#{article3.id}" => {
'id' => article3.id,
'title' => article3.title,
'published_date' => have_attributes(
to_formatted_s: '2021-10-10'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => have_attributes(
to_formatted_s: a_string_including('2021-10-01 12:00:00')
),
},
"__article_#{article3.id}" => {
'id' => article3.id,
'title' => article3.title,
'writer' => "__author_#{author2.id}",
},
'published_date' => have_attributes(
to_formatted_s: '2022-10-10'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => have_attributes(
to_formatted_s: a_string_including('2022-10-01 12:00:00')
),
},
}
)
}
Expand All @@ -78,37 +105,64 @@
is_expected.to match(
'authors' =>
{
'_fixture' =>
{
'model_class' => 'Author',
'fixture_generated_by' => 'DumpedRailers',
},
"__author_#{author1.id}" => {
'_fixture' =>
{
'model_class' => 'Author',
'fixture_generated_by' => 'DumpedRailers',
},
"__author_#{author1.id}" => {
'name' => author1.name.upcase
},
"__author_#{author2.id}" => {
},
"__author_#{author2.id}" => {
'name' => author2.name.upcase
},
},
},
},
'articles' =>
{
'_fixture' =>
{
'model_class' => 'Article',
'fixture_generated_by' => 'DumpedRailers',
},
"__article_#{article1.id}" => {
'title' => article1.title,
'_fixture' =>
{
'model_class' => 'Article',
'fixture_generated_by' => 'DumpedRailers',
},
"__article_#{article1.id}" => {
'title' => article1.title,
'writer' => "__author_#{author1.id}",
},
"__article_#{article2.id}" => {
'title' => article2.title,
'published_date' => have_attributes(
to_formatted_s: '2020-10-10'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => have_attributes(
to_formatted_s: a_string_including('2020-10-01 12:00:00')
),
},
"__article_#{article2.id}" => {
'title' => article2.title,
'writer' => "__author_#{author1.id}",
},
"__article_#{article3.id}" => {
'title' => article3.title,
'published_date' => have_attributes(
to_formatted_s: '2021-10-10'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => have_attributes(
to_formatted_s: a_string_including('2021-10-01 12:00:00')
),
},
"__article_#{article3.id}" => {
'title' => article3.title,
'writer' => "__author_#{author2.id}",
},
'published_date' => have_attributes(
to_formatted_s: '2022-10-10'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => have_attributes(
to_formatted_s: a_string_including('2022-10-01 12:00:00')
),
},
}
)
}
Expand Down
15 changes: 11 additions & 4 deletions spec/lib/dumped_railers/fixture_builder/record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,21 @@

context 'when the record has a direct reference (using primary_key)' do
let(:author) { Author.create!(name: 'Edgar Allan Poe') }
let(:record) { Article.create!(title: 'The Murders in the Rue Morgue', writer: author) }
let(:record) { Article.create!(title: 'The Murders in the Rue Morgue', writer: author, published_date: '2020-01-01', published_time: '12:00:00') }
let(:model) { Article }

it {
is_expected.to match(
'id' => record.id,
'title' => 'The Murders in the Rue Morgue',
'writer' => "__author_#{author.id}"
'id' => record.id,
'title' => 'The Murders in the Rue Morgue',
'writer' => "__author_#{author.id}",
'published_date' => have_attributes(
to_formatted_s: '2020-01-01'
),
'published_time' => have_attributes(
to_formatted_s: a_string_including('12:00:00')
),
'first_drafted_at' => nil,
)
}
end
Expand Down
Loading

0 comments on commit e2d385d

Please sign in to comment.