2017-10-30 01:44:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
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,
|
|
|
|
article_3
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filtering by :average' do
|
|
|
|
context 'as asc' do
|
|
|
|
it 'works' do
|
|
|
|
expect(Article.order_by_rating(:average, :asc)).to eq [
|
|
|
|
article_3,
|
|
|
|
article_2,
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'as desc' do
|
|
|
|
it 'works' do
|
|
|
|
expect(Article.order_by_rating(:average, :desc)).to eq [
|
|
|
|
article_1,
|
|
|
|
article_2,
|
|
|
|
article_3
|
|
|
|
]
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filtering by :estimate' do
|
|
|
|
context 'as asc' do
|
|
|
|
it 'works' do
|
|
|
|
expect(Article.order_by_rating(:estimate, :asc)).to eq [
|
|
|
|
article_3,
|
|
|
|
article_2,
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'as desc' do
|
|
|
|
it 'works' do
|
|
|
|
expect(Article.order_by_rating(:estimate, :desc)).to eq [
|
|
|
|
article_1,
|
|
|
|
article_2,
|
|
|
|
article_3
|
|
|
|
]
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'filtering by :sum' do
|
|
|
|
context 'as asc' do
|
|
|
|
it 'works' do
|
|
|
|
expect(Article.order_by_rating(:sum, :asc)).to eq [
|
|
|
|
article_3,
|
|
|
|
article_2,
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'as desc' do
|
|
|
|
it 'works' do
|
|
|
|
expect(Article.order_by_rating(:sum, :desc)).to eq [
|
|
|
|
article_1,
|
|
|
|
article_2,
|
|
|
|
article_3
|
|
|
|
]
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
|
|
|
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-16 00:04:05 +00:00
|
|
|
context 'filtering by :total' do
|
|
|
|
context 'as asc' do
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
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-16 00:04:05 +00:00
|
|
|
context 'as desc' do
|
|
|
|
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 [
|
|
|
|
article_1
|
|
|
|
]
|
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
|
2018-01-16 03:14:05 +00:00
|
|
|
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
|
2018-01-16 03:14:05 +00:00
|
|
|
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
|