diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e065ec6..6fe1616 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,12 +4,19 @@ on: [push, pull_request] jobs: test: + env: + DB: ${{ matrix.db }} + runs-on: ubuntu-latest strategy: fail-fast: false matrix: + db: + - mysql + - postgres + ruby: - '2.7' - '3.0' @@ -27,6 +34,23 @@ jobs: ports: - 3306:3306 + postgres: + env: + POSTGRES_DB: rating_test + POSTGRES_USER: postgres + POSTGRES_HOST_AUTH_METHOD: trust + + image: postgres:alpine + + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + + ports: + - 5432:5432 + steps: - name: Checkout uses: actions/checkout@v3 diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index ac2d604..ada1cbb 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -51,7 +51,7 @@ Metrics/ParameterLists: # AllowedIdentifiers: capture3, iso8601, rfc1123_date, rfc822, rfc2822, rfc3339 Naming/VariableNumber: Exclude: - - 'spec/support/mysql.rb' + - 'spec/support/database.rb' # Offense count: 9 # This cop supports safe auto-correction (--auto-correct). diff --git a/Gemfile.lock b/Gemfile.lock index a506804..1c744e3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,6 +71,7 @@ GEM parallel (1.22.1) parser (3.1.2.0) ast (~> 2.4.1) + pg (1.3.5) racc (1.6.0) rack (2.2.3) rack-test (1.1.0) @@ -128,7 +129,7 @@ GEM activesupport (>= 4.2.0) rack (>= 1.1) rubocop (>= 1.7.0, < 2.0) - rubocop-rspec (2.10.0) + rubocop-rspec (2.11.0) rubocop (~> 1.19) ruby-progressbar (1.11.0) shoulda-matchers (5.1.0) @@ -154,6 +155,7 @@ DEPENDENCIES debug factory_bot_rails mysql2 + pg rating! rspec-rails rubocop-performance diff --git a/rating.gemspec b/rating.gemspec index 4e3872a..dc000a9 100644 --- a/rating.gemspec +++ b/rating.gemspec @@ -22,6 +22,7 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'debug' spec.add_development_dependency 'factory_bot_rails' spec.add_development_dependency 'mysql2' + spec.add_development_dependency 'pg' spec.add_development_dependency 'rspec-rails' spec.add_development_dependency 'rubocop-performance' spec.add_development_dependency 'rubocop-rails' diff --git a/spec/common_helper.rb b/spec/common_helper.rb index 347e720..8f88c0e 100644 --- a/spec/common_helper.rb +++ b/spec/common_helper.rb @@ -9,7 +9,7 @@ require 'debug' require 'rating' require 'support/common' -require 'support/mysql' +require 'support/database' require 'support/database_cleaner' require 'support/factory_bot' require 'support/migrate' diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index a6a95e5..b3e7686 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -13,7 +13,14 @@ 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_s).to eq '1.333333333333333333' + 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 end end diff --git a/spec/models/rating/data_spec.rb b/spec/models/rating/data_spec.rb index f3726c8..1453054 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -21,7 +21,14 @@ RSpec.describe Rating::Rating, ':data' do end it 'returns the estimate for a resource' do - expect(result[:estimate].to_s).to eq '42.5000000000000000012000000505' + 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 end end diff --git a/spec/models/rating/update_rating_spec.rb b/spec/models/rating/update_rating_spec.rb index 8c3bb25..863cf44 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -9,10 +9,19 @@ 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.5 - expect(record.estimate).to eq 42.5 - expect(record.sum).to eq 101 - expect(record.total).to eq 2 + 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.sum).to be(101) + expect(record.total).to be(2) end end @@ -22,26 +31,26 @@ RSpec.describe Rating::Rating, ':update_rating' do it 'updates the rating data of the given resource respecting the scope' do record = described_class.find_by(resource: article_1, scopeable: category) - expect(record.average).to eq 1.5 - expect(record.estimate).to eq 1.5 - expect(record.sum).to eq 3 - expect(record.total).to eq 2 + expect(record.average).to eq(BigDecimal('1.5')) + expect(record.estimate).to eq(BigDecimal('1.5')) + expect(record.sum).to be(3) + expect(record.total).to be(2) end end context 'when rate table has no record' do - let!(:resource) { create :article } - let!(:scope) { nil } + let!(:resource) { create(:article) } + let!(:scope) { nil } it 'calculates with counts values as zero' do - described_class.update_rating resource, scope + described_class.update_rating(resource, scope) record = described_class.last - expect(record.average).to eq 0 - expect(record.estimate).to eq 0 - expect(record.sum).to eq 0 - expect(record.total).to eq 0 + expect(record.average).to eq(BigDecimal('0.0')) + expect(record.estimate).to eq(BigDecimal('0.0')) + expect(record.sum).to be(0) + expect(record.total).to be(0) end end end diff --git a/spec/support/database.rb b/spec/support/database.rb new file mode 100644 index 0000000..bc2fa66 --- /dev/null +++ b/spec/support/database.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +ENV['DB'] ||= 'postgres' + +conn_params = { database: :rating_test, host: '127.0.0.1' } + +case ENV.fetch('DB') +when 'mysql' + require 'mysql2' + + client = Mysql2::Client.new(host: conn_params[:host], username: :root) + + conn_params[:adapter] = :mysql2 + conn_params[:username] = :root +when 'postgres' + require 'pg' + + client = PG::Connection.new(host: conn_params[:host], password: '', user: :postgres) + + conn_params[:adapter] = :postgresql + conn_params[:username] = :postgres +end + +client.query('DROP DATABASE IF EXISTS rating_test;') +client.query('CREATE DATABASE rating_test;') + +ActiveRecord::Base.establish_connection(conn_params) diff --git a/spec/support/db/migrate/create_rating_table.rb b/spec/support/db/migrate/create_rating_table.rb index 2294724..293ab06 100644 --- a/spec/support/db/migrate/create_rating_table.rb +++ b/spec/support/db/migrate/create_rating_table.rb @@ -5,8 +5,8 @@ class CreateRatingTable < ActiveRecord::Migration[7.0] 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.bigint :sum, default: 0, mull: false - t.bigint :total, default: 0, mull: false + t.bigint :sum, default: 0, mull: false + t.bigint :total, default: 0, mull: false t.references :resource, index: true, null: false, polymorphic: true t.references :scopeable, index: true, null: true, polymorphic: true diff --git a/spec/support/db/migrate/create_review_ratings_table.rb b/spec/support/db/migrate/create_review_ratings_table.rb index eb24b43..86bd599 100644 --- a/spec/support/db/migrate/create_review_ratings_table.rb +++ b/spec/support/db/migrate/create_review_ratings_table.rb @@ -5,8 +5,8 @@ class CreateReviewRatingsTable < ActiveRecord::Migration[7.0] create_table :review_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.bigint :sum, default: 0, mull: false - t.bigint :total, default: 0, mull: false + t.bigint :sum, default: 0, mull: false + t.bigint :total, default: 0, mull: false t.references :resource, index: true, null: false, polymorphic: true t.references :scopeable, index: true, null: true, polymorphic: true diff --git a/spec/support/mysql.rb b/spec/support/mysql.rb deleted file mode 100644 index 7efd9e1..0000000 --- a/spec/support/mysql.rb +++ /dev/null @@ -1,10 +0,0 @@ -# frozen_string_literal: true - -require 'mysql2' - -client = Mysql2::Client.new(host: '127.0.0.1', username: :root) - -client.query('DROP DATABASE IF EXISTS rating_test;') -client.query('CREATE DATABASE IF NOT EXISTS rating_test;') - -ActiveRecord::Base.establish_connection(adapter: :mysql2, database: :rating_test, username: :root, host: '127.0.0.1')