rating/spec/models/extension/order_by_rating_spec.rb

180 lines
4.1 KiB
Ruby
Raw Normal View History

2017-10-30 01:44:23 +00:00
# frozen_string_literal: true
2018-01-16 03:25:36 +00:00
require 'support/shared_context/with_database_records'
2017-10-30 01:44:23 +00:00
RSpec.describe Rating::Extension, ':order_by_rating' do
2018-01-16 03:25:36 +00:00
include_context 'with_database_records'
2017-10-30 01:44:23 +00:00
context 'with default filters' do
it 'sorts by :estimate :desc' do
expect(Article.order_by_rating).to eq [
article_1,
article_2,
2019-10-03 22:07:50 +00:00
article_3,
2017-10-30 01:44:23 +00:00
]
end
end
2018-01-26 21:58:12 +00:00
context 'when filtering by :average' do
context 'with as asc' do
2017-10-30 01:44:23 +00:00
it 'works' do
expect(Article.order_by_rating(:average, :asc)).to eq [
article_3,
article_2,
2019-10-03 22:07:50 +00:00
article_1,
2017-10-30 01:44:23 +00:00
]
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:average, :asc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2017-11-02 18:55:46 +00:00
]
end
end
2017-10-30 01:44:23 +00:00
end
2018-01-26 21:58:12 +00:00
context 'with as desc' do
2017-10-30 01:44:23 +00:00
it 'works' do
expect(Article.order_by_rating(:average, :desc)).to eq [
article_1,
article_2,
2019-10-03 22:07:50 +00:00
article_3,
2017-10-30 01:44:23 +00:00
]
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:average, :desc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2017-11-02 18:55:46 +00:00
]
end
end
2017-10-30 01:44:23 +00:00
end
end
2018-01-26 21:58:12 +00:00
context 'when filtering by :estimate' do
context 'with as asc' do
2017-10-30 01:44:23 +00:00
it 'works' do
expect(Article.order_by_rating(:estimate, :asc)).to eq [
article_3,
article_2,
2019-10-03 22:07:50 +00:00
article_1,
2017-10-30 01:44:23 +00:00
]
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:estimate, :asc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2017-11-02 18:55:46 +00:00
]
end
end
2017-10-30 01:44:23 +00:00
end
2018-01-26 21:58:12 +00:00
context 'with as desc' do
2017-10-30 01:44:23 +00:00
it 'works' do
expect(Article.order_by_rating(:estimate, :desc)).to eq [
article_1,
article_2,
2019-10-03 22:07:50 +00:00
article_3,
2017-10-30 01:44:23 +00:00
]
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:estimate, :desc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2017-11-02 18:55:46 +00:00
]
end
end
2017-10-30 01:44:23 +00:00
end
end
2018-01-26 21:58:12 +00:00
context 'when filtering by :sum' do
2017-10-30 01:44:23 +00:00
context 'as asc' do
it 'works' do
expect(Article.order_by_rating(:sum, :asc)).to eq [
article_3,
article_2,
2019-10-03 22:07:50 +00:00
article_1,
2017-10-30 01:44:23 +00:00
]
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:sum, :asc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2017-11-02 18:55:46 +00:00
]
end
end
2017-10-30 01:44:23 +00:00
end
2018-01-26 21:58:12 +00:00
context 'with as desc' do
2017-10-30 01:44:23 +00:00
it 'works' do
expect(Article.order_by_rating(:sum, :desc)).to eq [
article_1,
article_2,
2019-10-03 22:07:50 +00:00
article_3,
2017-10-30 01:44:23 +00:00
]
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:sum, :desc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2017-11-02 18:55:46 +00:00
]
end
end
2017-10-30 01:44:23 +00:00
end
2018-01-16 00:04:05 +00:00
end
2017-10-30 01:44:23 +00:00
2018-01-26 21:58:12 +00:00
context 'when filtering by :total' do
context 'with as asc' do
2018-01-16 00:04:05 +00:00
it 'works' do
result = Article.order_by_rating(:total, :asc)
2017-10-30 01:44:23 +00:00
2018-01-16 00:04:05 +00:00
expect(result[0..1]).to match_array [article_2, article_3]
expect(result.last).to eq article_1
end
2017-11-02 18:55:46 +00:00
2018-01-16 00:04:05 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:total, :asc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2018-01-16 00:04:05 +00:00
]
2017-11-02 18:55:46 +00:00
end
2017-10-30 01:44:23 +00:00
end
2018-01-16 00:04:05 +00:00
end
2017-10-30 01:44:23 +00:00
2018-01-26 21:58:12 +00:00
context 'with as desc' do
2018-01-16 00:04:05 +00:00
it 'works' do
result = Article.order_by_rating(:total, :desc)
2017-10-30 01:44:23 +00:00
2018-01-16 00:04:05 +00:00
expect(result.first).to eq article_1
expect(result[1..2]).to match_array [article_2, article_3]
end
2017-11-02 18:55:46 +00:00
2018-01-16 00:04:05 +00:00
context 'with scope' do
it 'works' do
expect(Article.order_by_rating(:total, :desc, scope: category)).to eq [
2019-10-03 22:07:50 +00:00
article_1,
2018-01-16 00:04:05 +00:00
]
2017-11-02 18:55:46 +00:00
end
2017-10-30 01:44:23 +00:00
end
end
end
context 'with other resource' do
it 'works' do
expect(Author.order_by_rating(:total, :desc)).to match_array [author_1, author_2]
2017-10-30 01:44:23 +00:00
end
2017-11-02 18:55:46 +00:00
context 'with scope' do
it 'returns empty since creation has no scope' do
expect(Author.order_by_rating(:total, :desc, scope: category)).to eq []
2017-11-02 18:55:46 +00:00
end
end
2017-10-30 01:44:23 +00:00
end
end