From 9882309485524f9e50b9ebc574de88075a4ef232 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Tue, 6 Feb 2018 17:56:23 -0200 Subject: [PATCH] fix: float does not exists on mysql fix precision on average calculation making dividend as decimal --- Gemfile.lock | 12 ++++++------ lib/rating/models/rating/rating.rb | 4 ++-- rating.gemspec | 2 +- spec/models/rating/averager_data_spec.rb | 2 +- spec/models/rating/data_spec.rb | 2 +- spec/models/rating/update_rating_spec.rb | 4 ++-- spec/rails_helper.rb | 8 +++++++- 7 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 9c9dbd2..27c1a99 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -54,6 +54,7 @@ GEM method_source (0.9.0) mini_portile2 (2.3.0) minitest (5.11.3) + mysql2 (0.4.10) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) parallel (1.12.1) @@ -66,7 +67,7 @@ GEM pry-byebug (3.5.1) byebug (~> 9.1) pry (~> 0.10) - rack (2.0.3) + rack (2.0.4) rack-test (0.8.2) rack (>= 1.0, < 3) rails-dom-testing (2.0.3) @@ -98,7 +99,7 @@ GEM rspec-expectations (~> 3.7.0) rspec-mocks (~> 3.7.0) rspec-support (~> 3.7.0) - rspec-support (3.7.0) + rspec-support (3.7.1) rubocop (0.52.1) parallel (~> 1.10) parser (>= 2.4.0.2, < 3.0) @@ -106,15 +107,14 @@ GEM rainbow (>= 2.2.2, < 4.0) ruby-progressbar (~> 1.7) unicode-display_width (~> 1.0, >= 1.0.1) - rubocop-rspec (1.22.1) + rubocop-rspec (1.22.2) rubocop (>= 0.52.1) ruby-progressbar (1.9.0) shoulda-matchers (3.1.2) activesupport (>= 4.0.0) - sqlite3 (1.3.13) thor (0.20.0) thread_safe (0.3.6) - tzinfo (1.2.4) + tzinfo (1.2.5) thread_safe (~> 0.1) unicode-display_width (1.3.0) @@ -124,12 +124,12 @@ PLATFORMS DEPENDENCIES database_cleaner factory_bot_rails + mysql2 pry-byebug rating! rspec-rails rubocop-rspec shoulda-matchers - sqlite3 BUNDLED WITH 1.16.1 diff --git a/lib/rating/models/rating/rating.rb b/lib/rating/models/rating/rating.rb index cd4d19e..d43c169 100644 --- a/lib/rating/models/rating/rating.rb +++ b/lib/rating/models/rating/rating.rb @@ -25,8 +25,8 @@ module Rating sql = %( SELECT - (#{total_count} / CAST(#{distinct_count} AS float)) count_avg, - COALESCE(AVG(value), 0) rating_avg + (CAST(#{total_count} AS DECIMAL(17, 14)) / #{distinct_count}) count_avg, + COALESCE(AVG(value), 0) rating_avg FROM #{rate_table_name} WHERE resource_type = :resource_type AND #{scope_type_query(scopeable)} ).squish diff --git a/rating.gemspec b/rating.gemspec index a6c7b46..2286b83 100644 --- a/rating.gemspec +++ b/rating.gemspec @@ -23,9 +23,9 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'database_cleaner' spec.add_development_dependency 'factory_bot_rails' + spec.add_development_dependency 'mysql2' spec.add_development_dependency 'pry-byebug' spec.add_development_dependency 'rspec-rails' spec.add_development_dependency 'rubocop-rspec' spec.add_development_dependency 'shoulda-matchers' - spec.add_development_dependency 'sqlite3' end diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index a6faf6d..b17f192 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -14,7 +14,7 @@ RSpec.describe Rating::Rating, ':averager_data' do end 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 diff --git a/spec/models/rating/data_spec.rb b/spec/models/rating/data_spec.rb index 484fe1f..b2674ff 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -22,7 +22,7 @@ RSpec.describe Rating::Rating, ':data' do end 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 diff --git a/spec/models/rating/update_rating_spec.rb b/spec/models/rating/update_rating_spec.rb index fca017a..d339580 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -10,8 +10,8 @@ RSpec.describe Rating::Rating, ':update_rating' do it 'updates the rating data of the given resource' do record = described_class.find_by(resource: article_1) - expect(record.average).to eq 50.50000000000001 - expect(record.estimate).to eq 42.50000000000001 + expect(record.average).to eq 50.5 + expect(record.estimate).to eq 42.5 expect(record.sum).to eq 101 expect(record.total).to eq 2 end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index c3e5af4..e3546f6 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -3,9 +3,15 @@ ENV['RAILS_ENV'] ||= 'test' require 'active_record/railtie' +require 'mysql2' require 'pry-byebug' 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 }