2017-10-30 01:44:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Rating::Rate, ':rate_for' do
|
2018-01-16 03:14:05 +00:00
|
|
|
let!(:author) { create :author }
|
2017-10-30 01:44:23 +00:00
|
|
|
let!(:article) { create :article }
|
|
|
|
|
2018-03-01 16:34:44 +00:00
|
|
|
before(:all) { AddExtraScopesOnRatingRatesTable.new.change }
|
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'with no scopeable' do
|
|
|
|
context 'when rate does not exist' do
|
2018-03-01 16:34:44 +00:00
|
|
|
it { expect(described_class.rate_for(author: author, resource: article)).to eq nil }
|
2017-11-02 18:55:46 +00:00
|
|
|
end
|
|
|
|
|
2018-03-01 16:34:44 +00:00
|
|
|
context 'when rate exists' do
|
|
|
|
let!(:record) do
|
|
|
|
described_class.create author: author, extra_scopes: {}, metadata: {}, resource: article, value: 3
|
|
|
|
end
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
it 'returns the record' do
|
2018-03-01 16:34:44 +00:00
|
|
|
expect(described_class.rate_for(author: author, resource: article)).to eq record
|
2017-11-02 18:55:46 +00:00
|
|
|
end
|
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
|
2017-11-02 18:55:46 +00:00
|
|
|
context 'with scopeable' do
|
|
|
|
let!(:category) { create :category }
|
|
|
|
|
|
|
|
context 'when rate does not exist' do
|
2018-03-01 16:34:44 +00:00
|
|
|
it do
|
|
|
|
expect(described_class.rate_for(author: author, resource: article, scopeable: category)).to eq nil
|
|
|
|
end
|
2017-11-02 18:55:46 +00:00
|
|
|
end
|
|
|
|
|
2018-03-01 16:34:44 +00:00
|
|
|
context 'when rate exists' do
|
|
|
|
let!(:record) do
|
|
|
|
described_class.create(
|
|
|
|
author: author,
|
|
|
|
extra_scopes: {},
|
|
|
|
metadata: {},
|
|
|
|
resource: article,
|
|
|
|
scopeable: category,
|
|
|
|
value: 3
|
|
|
|
)
|
|
|
|
end
|
2017-11-02 18:55:46 +00:00
|
|
|
|
|
|
|
it 'returns the record' do
|
2018-01-16 03:14:05 +00:00
|
|
|
query = described_class.rate_for(author: author, resource: article, scopeable: category)
|
2017-10-30 01:44:23 +00:00
|
|
|
|
2018-03-01 16:34:44 +00:00
|
|
|
expect(query).to eq record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with extra scopes' do
|
|
|
|
let!(:category) { create :category }
|
|
|
|
|
|
|
|
context 'when matches all attributes including the extra scopes' do
|
|
|
|
let!(:record) do
|
|
|
|
described_class.create(
|
|
|
|
author: author,
|
|
|
|
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
|
|
|
metadata: {},
|
|
|
|
resource: article,
|
|
|
|
scopeable: category,
|
|
|
|
value: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the record' do
|
|
|
|
result = described_class.rate_for(
|
|
|
|
author: author,
|
|
|
|
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
|
|
|
resource: article,
|
|
|
|
scopeable: category
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result).to eq record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when matches all attributes but at least one extra scopes' do
|
|
|
|
let!(:record) do
|
|
|
|
described_class.create(
|
|
|
|
author: author,
|
|
|
|
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_2' },
|
|
|
|
metadata: {},
|
|
|
|
resource: article,
|
|
|
|
scopeable: category,
|
|
|
|
value: 1
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not return the record' do
|
|
|
|
result = described_class.rate_for(
|
|
|
|
author: author,
|
|
|
|
extra_scopes: { scope_1: 'scope_1', scope_2: 'scope_missing' },
|
|
|
|
resource: article,
|
|
|
|
scopeable: category
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(result).to eq nil
|
2017-11-02 18:55:46 +00:00
|
|
|
end
|
2017-10-30 01:44:23 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|