Skip to content

Commit

Permalink
up: average and estimate now rounds 2
Browse files Browse the repository at this point in the history
  • Loading branch information
wbotelhos committed Mar 4, 2023
1 parent 9866f33 commit 853887c
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class CreateRateTable < ActiveRecord::Migration[5.0]
def change
create_table :rating_rates do |t|
t.decimal :value, default: 0, precision: 25, scale: 16
t.decimal :value, default: 0, precision: 11, scale: 2

t.references :author, index: true, null: false, polymorphic: true
t.references :resource, index: true, null: false, polymorphic: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
class CreateRatingTable < ActiveRecord::Migration[5.0]
def change
create_table :rating_ratings do |t|
t.decimal :average, default: 0, mull: false, precision: 25, scale: 16
t.decimal :estimate, default: 0, mull: false, precision: 25, scale: 16
t.decimal :average, default: 0, mull: false, precision: 11, scale: 2
t.decimal :estimate, default: 0, mull: false, precision: 11, scale: 2
t.integer :sum, default: 0, mull: false
t.integer :total, default: 0, mull: false

Expand Down
4 changes: 2 additions & 2 deletions lib/rating/models/rating/rating.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def data(resource, scopeable)
values = values_data(resource, scopeable)

{
average: values.rating_avg,
estimate: estimate(averager, values),
average: values.rating_avg.round(2),
estimate: estimate(averager, values).round(2),
sum: values.rating_sum,
total: values.rating_count,
}
Expand Down
8 changes: 4 additions & 4 deletions spec/models/extension/unscoped_rating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

rating = ratings[0]

expect(rating.average.to_s).to eq '2.6666666666666667'
expect(rating.estimate.to_s).to eq '2.6666666666666667'
expect(rating.average.to_s).to eq '2.67'
expect(rating.estimate.to_s).to eq '2.67'
expect(rating.resource).to eq resource
expect(rating.scopeable).to be(nil)
expect(rating.sum).to eq 8
Expand Down Expand Up @@ -86,8 +86,8 @@

rating = ratings[1]

expect(rating.average.to_s).to eq '2.6666666666666667'
expect(rating.estimate.to_s).to eq '2.6666666666666667'
expect(rating.average.to_s).to eq('2.67')
expect(rating.estimate.to_s).to eq('2.67')
expect(rating.resource).to eq resource
expect(rating.scopeable).to be(nil)
expect(rating.sum).to eq 8
Expand Down
15 changes: 4 additions & 11 deletions spec/models/rating/averager_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,23 @@
subject(:result) { described_class.averager_data article_1, nil }

it 'returns the values average of given resource type' do
expect(result.as_json['rating_avg'].to_f.to_s).to eq '30.5'
expect(result.rating_avg.to_s).to eq('30.5')
end

it 'returns the average of number of records for the given resource type' do
case ENV.fetch('DB')
when 'mysql'
expect(result.count_avg).to eq(BigDecimal('1.333333333333333333'))
when 'postgres'
expect(result.count_avg).to eq(BigDecimal('1.3333333333333333'))
else
raise('DB env missing!')
end
expect(result.count_avg.round(2).to_s).to eq('1.33')
end
end

context 'with scopeable' do
subject(:result) { described_class.averager_data article_1, category }

it 'returns the values average of given resource type' do
expect(result.as_json['rating_avg'].to_f.to_s).to eq '1.5'
expect(result.rating_avg.to_s).to eq('1.5')
end

it 'returns the average of number of records for the given resource type' do
expect(result.as_json['count_avg'].to_f.to_s).to eq '2.0'
expect(result.count_avg.to_s).to eq('2.0')
end
end
end
9 changes: 1 addition & 8 deletions spec/models/rating/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
end

it 'returns the estimate for a resource' do
case ENV.fetch('DB')
when 'mysql'
expect(result[:estimate]).to eq(BigDecimal('42.5000000000000000012000000505'))
when 'postgres'
expect(result[:estimate]).to eq(BigDecimal('42.5000000000000001200000000000000012505'))
else
raise('DB env missing!')
end
expect(result[:estimate]).to eq(BigDecimal('42.50'))
end
end

Expand Down
12 changes: 2 additions & 10 deletions spec/models/rating/update_rating_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@
it 'updates the rating data of the given resource' do
record = described_class.find_by(resource: article_1)

case ENV.fetch('DB')
when 'mysql'
expect(record.average).to eq(BigDecimal('50.5'))
expect(record.estimate).to eq(BigDecimal('42.5'))
when 'postgres'
expect(record.average).to eq(BigDecimal('50.5'))
expect(record.estimate).to eq(BigDecimal('42.5000000000000001'))
else
raise('DB env missing!')
end
expect(record.average).to eq(BigDecimal('50.50'))
expect(record.estimate).to eq(BigDecimal('42.50'))

expect(record.sum).to be(101)
expect(record.total).to be(2)
Expand Down

0 comments on commit 853887c

Please sign in to comment.