fix: float does not exists on mysql

fix precision on average calculation making dividend as decimal
main
Washington Botelho 2018-02-06 17:56:23 -02:00
parent 9634ae87bc
commit 9882309485
No known key found for this signature in database
GPG Key ID: 5DE4F42A8F073617
7 changed files with 20 additions and 14 deletions

View File

@ -54,6 +54,7 @@ GEM
method_source (0.9.0) method_source (0.9.0)
mini_portile2 (2.3.0) mini_portile2 (2.3.0)
minitest (5.11.3) minitest (5.11.3)
mysql2 (0.4.10)
nokogiri (1.8.2) nokogiri (1.8.2)
mini_portile2 (~> 2.3.0) mini_portile2 (~> 2.3.0)
parallel (1.12.1) parallel (1.12.1)
@ -66,7 +67,7 @@ GEM
pry-byebug (3.5.1) pry-byebug (3.5.1)
byebug (~> 9.1) byebug (~> 9.1)
pry (~> 0.10) pry (~> 0.10)
rack (2.0.3) rack (2.0.4)
rack-test (0.8.2) rack-test (0.8.2)
rack (>= 1.0, < 3) rack (>= 1.0, < 3)
rails-dom-testing (2.0.3) rails-dom-testing (2.0.3)
@ -98,7 +99,7 @@ GEM
rspec-expectations (~> 3.7.0) rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0) rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0) rspec-support (~> 3.7.0)
rspec-support (3.7.0) rspec-support (3.7.1)
rubocop (0.52.1) rubocop (0.52.1)
parallel (~> 1.10) parallel (~> 1.10)
parser (>= 2.4.0.2, < 3.0) parser (>= 2.4.0.2, < 3.0)
@ -106,15 +107,14 @@ GEM
rainbow (>= 2.2.2, < 4.0) rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7) ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1) unicode-display_width (~> 1.0, >= 1.0.1)
rubocop-rspec (1.22.1) rubocop-rspec (1.22.2)
rubocop (>= 0.52.1) rubocop (>= 0.52.1)
ruby-progressbar (1.9.0) ruby-progressbar (1.9.0)
shoulda-matchers (3.1.2) shoulda-matchers (3.1.2)
activesupport (>= 4.0.0) activesupport (>= 4.0.0)
sqlite3 (1.3.13)
thor (0.20.0) thor (0.20.0)
thread_safe (0.3.6) thread_safe (0.3.6)
tzinfo (1.2.4) tzinfo (1.2.5)
thread_safe (~> 0.1) thread_safe (~> 0.1)
unicode-display_width (1.3.0) unicode-display_width (1.3.0)
@ -124,12 +124,12 @@ PLATFORMS
DEPENDENCIES DEPENDENCIES
database_cleaner database_cleaner
factory_bot_rails factory_bot_rails
mysql2
pry-byebug pry-byebug
rating! rating!
rspec-rails rspec-rails
rubocop-rspec rubocop-rspec
shoulda-matchers shoulda-matchers
sqlite3
BUNDLED WITH BUNDLED WITH
1.16.1 1.16.1

View File

@ -25,8 +25,8 @@ module Rating
sql = %( sql = %(
SELECT SELECT
(#{total_count} / CAST(#{distinct_count} AS float)) count_avg, (CAST(#{total_count} AS DECIMAL(17, 14)) / #{distinct_count}) count_avg,
COALESCE(AVG(value), 0) rating_avg COALESCE(AVG(value), 0) rating_avg
FROM #{rate_table_name} FROM #{rate_table_name}
WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)} WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)}
).squish ).squish

View File

@ -23,9 +23,9 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'database_cleaner' spec.add_development_dependency 'database_cleaner'
spec.add_development_dependency 'factory_bot_rails' spec.add_development_dependency 'factory_bot_rails'
spec.add_development_dependency 'mysql2'
spec.add_development_dependency 'pry-byebug' spec.add_development_dependency 'pry-byebug'
spec.add_development_dependency 'rspec-rails' spec.add_development_dependency 'rspec-rails'
spec.add_development_dependency 'rubocop-rspec' spec.add_development_dependency 'rubocop-rspec'
spec.add_development_dependency 'shoulda-matchers' spec.add_development_dependency 'shoulda-matchers'
spec.add_development_dependency 'sqlite3'
end end

View File

@ -14,7 +14,7 @@ RSpec.describe Rating::Rating, ':averager_data' do
end end
it 'returns the average of number of records for the given resource type' do it 'returns the average of number of records for the given resource type' do
expect(result.as_json['count_avg']).to eq 1.3333333333333333 expect(result.as_json['count_avg'].to_s).to eq '1.333333333333333333'
end end
end end

View File

@ -22,7 +22,7 @@ RSpec.describe Rating::Rating, ':data' do
end end
it 'returns the estimate for a resource' do it 'returns the estimate for a resource' do
expect(result[:estimate]).to eq 42.50000000000001 expect(result[:estimate].to_s).to eq '42.5000000000000000012000000505'
end end
end end

View File

@ -10,8 +10,8 @@ RSpec.describe Rating::Rating, ':update_rating' do
it 'updates the rating data of the given resource' do it 'updates the rating data of the given resource' do
record = described_class.find_by(resource: article_1) record = described_class.find_by(resource: article_1)
expect(record.average).to eq 50.50000000000001 expect(record.average).to eq 50.5
expect(record.estimate).to eq 42.50000000000001 expect(record.estimate).to eq 42.5
expect(record.sum).to eq 101 expect(record.sum).to eq 101
expect(record.total).to eq 2 expect(record.total).to eq 2
end end

View File

@ -3,9 +3,15 @@
ENV['RAILS_ENV'] ||= 'test' ENV['RAILS_ENV'] ||= 'test'
require 'active_record/railtie' require 'active_record/railtie'
require 'mysql2'
require 'pry-byebug' require 'pry-byebug'
require 'rating' require 'rating'
ActiveRecord::Base.establish_connection adapter: :sqlite3, database: ':memory:' client = Mysql2::Client.new(host: :localhost, username: :root)
client.query 'DROP DATABASE rating_test;'
client.query 'CREATE DATABASE rating_test;'
ActiveRecord::Base.establish_connection adapter: :mysql2, database: :rating_test, username: :root
Dir[File.expand_path('support/**/*.rb', __dir__)].each { |file| require file } Dir[File.expand_path('support/**/*.rb', __dir__)].each { |file| require file }