From cb3538cde63e3da14c5e40eb8fb292d43f29b28a Mon Sep 17 00:00:00 2001 From: Peter Goldstein Date: Thu, 21 Apr 2022 16:44:05 -0700 Subject: [PATCH 01/15] Get Postgres running --- .github/workflows/tests.yml | 21 ++++++++++++++++++++- .rubocop_todo.yml | 2 +- Gemfile.lock | 2 ++ rating.gemspec | 1 + spec/common_helper.rb | 2 +- spec/support/database.rb | 33 +++++++++++++++++++++++++++++++++ spec/support/mysql.rb | 10 ---------- 7 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 spec/support/database.rb delete mode 100644 spec/support/mysql.rb diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index e065ec6..da3bc68 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -14,7 +14,9 @@ jobs: - '2.7' - '3.0' - '3.1' - + db: + - mysql + - postgres services: mysql: env: @@ -26,7 +28,24 @@ jobs: ports: - 3306:3306 + postgres: + image: postgres + env: + POSTGRES_DB: rating_user + POSTGRES_USER: rating_user + POSTGRES_PASSWORD: rating_password + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 + ports: + - 5432:5432 + env: + DB: ${{ matrix.db }} + POSTGRES_USER: rating_user + POSTGRES_PASSWORD: rating_password 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..dea80bf 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) @@ -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/support/database.rb b/spec/support/database.rb new file mode 100644 index 0000000..d58e10a --- /dev/null +++ b/spec/support/database.rb @@ -0,0 +1,33 @@ +# frozen_string_literal: true + +require 'mysql2' +require 'pg' + +ENV['DB'] ||= 'mysql' + +conn_params = { + database: :rating_test, + host: '127.0.0.1', +} + +case ENV.fetch('DB', nil) +when 'mysql' + client = Mysql2::Client.new(host: '127.0.0.1', username: :root) + conn_params[:adapter] = :mysql2 + conn_params[:username] = :root +when 'postgres' + ENV['POSTGRES_USER'] ||= 'rating_user' + client = PG::Connection.new(host: '127.0.0.1', user: ENV.fetch('POSTGRES_USER', nil), + password: ENV.fetch('POSTGRES_PASSWORD', nil) + ) + conn_params.merge!( + adapter: :postgresql, + username: ENV.fetch('POSTGRES_USER', nil), + password: ENV.fetch('POSTGRES_PASSWORD', nil) + ) +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/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') From 90dc76e4d9f1827b205097f582f90a8de0eeb972 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 09:54:53 -0300 Subject: [PATCH 02/15] spec: using the correct types --- spec/models/rating/averager_data_spec.rb | 2 +- spec/models/rating/data_spec.rb | 2 +- spec/models/rating/update_rating_spec.rb | 30 ++++++++++++------------ 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index a6a95e5..a962769 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -13,7 +13,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_s).to eq '1.333333333333333333' + expect(result.count_avg).to eq(BigDecimal('1.333333333333333333')) end end diff --git a/spec/models/rating/data_spec.rb b/spec/models/rating/data_spec.rb index f3726c8..d870424 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -21,7 +21,7 @@ RSpec.describe Rating::Rating, ':data' do end it 'returns the estimate for a resource' do - expect(result[:estimate].to_s).to eq '42.5000000000000000012000000505' + expect(result[:estimate]).to eq(BigDecimal('42.5000000000000000012000000505')) end end diff --git a/spec/models/rating/update_rating_spec.rb b/spec/models/rating/update_rating_spec.rb index 8c3bb25..319f3f1 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -9,10 +9,10 @@ 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 + expect(record.average).to eq(BigDecimal('50.5')) + expect(record.estimate).to eq(BigDecimal('42.5')) + expect(record.sum).to be(101) + expect(record.total).to be(2) end end @@ -22,26 +22,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 From 2985636b667debedbaed864d430164cf6eb0130a Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:10:14 -0300 Subject: [PATCH 03/15] gem: update --- Gemfile.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Gemfile.lock b/Gemfile.lock index dea80bf..1c744e3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -129,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) From 277c0c08f33fc09addb5e23440cf633e697dfc49 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:11:38 -0300 Subject: [PATCH 04/15] up: avoids user and password for local tests The `require` now is local avoiding require unnecessary driver. --- spec/support/database.rb | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/spec/support/database.rb b/spec/support/database.rb index d58e10a..cd19fbe 100644 --- a/spec/support/database.rb +++ b/spec/support/database.rb @@ -1,30 +1,24 @@ # frozen_string_literal: true -require 'mysql2' -require 'pg' - ENV['DB'] ||= 'mysql' -conn_params = { - database: :rating_test, - host: '127.0.0.1', -} +conn_params = { database: :rating_test, host: '127.0.0.1' } case ENV.fetch('DB', nil) when 'mysql' - client = Mysql2::Client.new(host: '127.0.0.1', username: :root) + require 'mysql2' + + client = Mysql2::Client.new(host: conn_params[:host], username: :root) + conn_params[:adapter] = :mysql2 conn_params[:username] = :root when 'postgres' - ENV['POSTGRES_USER'] ||= 'rating_user' - client = PG::Connection.new(host: '127.0.0.1', user: ENV.fetch('POSTGRES_USER', nil), - password: ENV.fetch('POSTGRES_PASSWORD', nil) - ) - conn_params.merge!( - adapter: :postgresql, - username: ENV.fetch('POSTGRES_USER', nil), - password: ENV.fetch('POSTGRES_PASSWORD', nil) - ) + require 'pg' + + client = PG::Connection.new(host: conn_params[:host], user: :postgres) + + conn_params[:adapter] = :postgresql + conn_params[:username] = :postgres end client.query('DROP DATABASE IF EXISTS rating_test;') From 0944c5135b805501dfd4225b3c9411e70cf1fbe0 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:11:50 -0300 Subject: [PATCH 05/15] lint --- spec/support/db/migrate/create_rating_table.rb | 4 ++-- spec/support/db/migrate/create_review_ratings_table.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) 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 From 60610b818d7979323fe9290ea6e6976867ba9d27 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:40:21 -0300 Subject: [PATCH 06/15] fix: for no password we should use empty string --- spec/support/database.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/database.rb b/spec/support/database.rb index cd19fbe..ea50d67 100644 --- a/spec/support/database.rb +++ b/spec/support/database.rb @@ -15,7 +15,7 @@ when 'mysql' when 'postgres' require 'pg' - client = PG::Connection.new(host: conn_params[:host], user: :postgres) + client = PG::Connection.new(host: conn_params[:host], password: '', user: :postgres) conn_params[:adapter] = :postgresql conn_params[:username] = :postgres From 5ff11b887b50d6541043601415a9b7e407339e9e Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:42:53 -0300 Subject: [PATCH 07/15] ci: removing password from pg service --- .github/workflows/tests.yml | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index da3bc68..62e5fa7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -10,14 +10,19 @@ jobs: fail-fast: false matrix: + db: + - mysql + - postgres + ruby: - '2.7' - '3.0' - '3.1' - db: - - mysql - - postgres + services: + env: + DB: ${{ matrix.db }} + mysql: env: MYSQL_DATABASE: rating_test @@ -28,24 +33,24 @@ jobs: ports: - 3306:3306 + postgres: - image: postgres env: - POSTGRES_DB: rating_user - POSTGRES_USER: rating_user - POSTGRES_PASSWORD: rating_password + POSTGRES_DB: rating_test + POSTGRES_USER: postgres + POSTGRES_HOST_AUTH_METHOD: trust + + image: postgres + options: >- --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5 + ports: - 5432:5432 - env: - DB: ${{ matrix.db }} - POSTGRES_USER: rating_user - POSTGRES_PASSWORD: rating_password steps: - name: Checkout uses: actions/checkout@v3 From 50345671894310ed6c731602bb2e5f3958f528ef Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:45:14 -0300 Subject: [PATCH 08/15] ci: put it back to the previous level --- .github/workflows/tests.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 62e5fa7..0133e47 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -4,6 +4,9 @@ on: [push, pull_request] jobs: test: + env: + DB: ${{ matrix.db }} + runs-on: ubuntu-latest strategy: @@ -20,9 +23,6 @@ jobs: - '3.1' services: - env: - DB: ${{ matrix.db }} - mysql: env: MYSQL_DATABASE: rating_test From ed227c71daee31872ce5a8d617dd3312862eb16e Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:48:42 -0300 Subject: [PATCH 09/15] ci: uses alpine images --- .github/workflows/tests.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0133e47..cc4400d 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,7 +28,7 @@ jobs: MYSQL_DATABASE: rating_test MYSQL_ALLOW_EMPTY_PASSWORD: yes - image: mysql:8 + image: mysql:alpine options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 ports: @@ -40,7 +40,7 @@ jobs: POSTGRES_USER: postgres POSTGRES_HOST_AUTH_METHOD: trust - image: postgres + image: postgres:alpine options: >- --health-cmd pg_isready From 4b76e100256c6e53426e98cb6cc6137c1e284947 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:52:55 -0300 Subject: [PATCH 10/15] ci: fix mysql version Looks like mysql has no alpine. --- .github/workflows/tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index cc4400d..6fe1616 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,7 +28,7 @@ jobs: MYSQL_DATABASE: rating_test MYSQL_ALLOW_EMPTY_PASSWORD: yes - image: mysql:alpine + image: mysql:8 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 ports: From d9b45b849743f9a2a0813cbe32f8a239d0595f65 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 10:54:56 -0300 Subject: [PATCH 11/15] ci: conditional loading db service --- .github/workflows/tests.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 6fe1616..b1123da 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,6 +28,7 @@ jobs: MYSQL_DATABASE: rating_test MYSQL_ALLOW_EMPTY_PASSWORD: yes + if: ${{ matrix.db == 'mysql' }} image: mysql:8 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 @@ -40,6 +41,7 @@ jobs: POSTGRES_USER: postgres POSTGRES_HOST_AUTH_METHOD: trust + if: ${{ matrix.db == 'postgres' }} image: postgres:alpine options: >- From 06b2caaa4bfd6c6a479463253040a7f3ea7aaf67 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 11:01:21 -0300 Subject: [PATCH 12/15] ci: condition looks like not work on services --- .github/workflows/tests.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b1123da..6fe1616 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -28,7 +28,6 @@ jobs: MYSQL_DATABASE: rating_test MYSQL_ALLOW_EMPTY_PASSWORD: yes - if: ${{ matrix.db == 'mysql' }} image: mysql:8 options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3 @@ -41,7 +40,6 @@ jobs: POSTGRES_USER: postgres POSTGRES_HOST_AUTH_METHOD: trust - if: ${{ matrix.db == 'postgres' }} image: postgres:alpine options: >- From 9691ba81a0a6d099b6e3ad5a8cd13425c1e94eb6 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 11:15:20 -0300 Subject: [PATCH 13/15] spc: uses conditional assert For some reason that I don't know, the MySQL and Postgres has different precision to keep the decimal, so instead to match the exactly number, we're justing checking the Rating logic without worry with that precision. --- spec/models/rating/averager_data_spec.rb | 8 +++++++- spec/models/rating/data_spec.rb | 8 +++++++- spec/models/rating/update_rating_spec.rb | 16 ++++++++++++---- spec/support/database.rb | 2 +- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index a962769..1e7ca27 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -13,7 +13,13 @@ RSpec.describe Rating::Rating, ':averager_data' do end it 'returns the average of number of records for the given resource type' do - expect(result.count_avg).to eq(BigDecimal('1.333333333333333333')) + if ENV.fetch('DB') == 'mysql' + expect(result.count_avg).to eq(BigDecimal('1.333333333333333333')) + elsif ENV.fetch('DB') == '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 d870424..adfa826 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -21,7 +21,13 @@ RSpec.describe Rating::Rating, ':data' do end it 'returns the estimate for a resource' do - expect(result[:estimate]).to eq(BigDecimal('42.5000000000000000012000000505')) + if ENV.fetch('DB') == 'mysql' + expect(result[:estimate]).to eq(BigDecimal('42.5000000000000000012000000505')) + elsif ENV.fetch('DB') == '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 319f3f1..0afddca 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -9,10 +9,18 @@ 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(BigDecimal('50.5')) - expect(record.estimate).to eq(BigDecimal('42.5')) - expect(record.sum).to be(101) - expect(record.total).to be(2) + if ENV.fetch('DB') == 'mysql' + expect(record.average).to eq(BigDecimal('50.5')) + expect(record.estimate).to eq(BigDecimal('42.5')) + elsif ENV.fetch('DB') == '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 diff --git a/spec/support/database.rb b/spec/support/database.rb index ea50d67..9a38ec8 100644 --- a/spec/support/database.rb +++ b/spec/support/database.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -ENV['DB'] ||= 'mysql' +ENV['DB'] ||= 'postgres' conn_params = { database: :rating_test, host: '127.0.0.1' } From 3e251e00bb2f9b8c670907363bae589993da08e1 Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 11:25:02 -0300 Subject: [PATCH 14/15] spec: on the top we already make sure it exists --- spec/support/database.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/support/database.rb b/spec/support/database.rb index 9a38ec8..bc2fa66 100644 --- a/spec/support/database.rb +++ b/spec/support/database.rb @@ -4,7 +4,7 @@ ENV['DB'] ||= 'postgres' conn_params = { database: :rating_test, host: '127.0.0.1' } -case ENV.fetch('DB', nil) +case ENV.fetch('DB') when 'mysql' require 'mysql2' From 88d15ff40898d06fb3cab7dc51f4ee0d03d7872f Mon Sep 17 00:00:00 2001 From: Washington Botelho Date: Wed, 18 May 2022 11:26:52 -0300 Subject: [PATCH 15/15] spec: uses case conditional --- spec/models/rating/averager_data_spec.rb | 5 +++-- spec/models/rating/data_spec.rb | 5 +++-- spec/models/rating/update_rating_spec.rb | 5 +++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/spec/models/rating/averager_data_spec.rb b/spec/models/rating/averager_data_spec.rb index 1e7ca27..b3e7686 100644 --- a/spec/models/rating/averager_data_spec.rb +++ b/spec/models/rating/averager_data_spec.rb @@ -13,9 +13,10 @@ RSpec.describe Rating::Rating, ':averager_data' do end it 'returns the average of number of records for the given resource type' do - if ENV.fetch('DB') == 'mysql' + case ENV.fetch('DB') + when 'mysql' expect(result.count_avg).to eq(BigDecimal('1.333333333333333333')) - elsif ENV.fetch('DB') == 'postgres' + when 'postgres' expect(result.count_avg).to eq(BigDecimal('1.3333333333333333')) else raise('DB env missing!') diff --git a/spec/models/rating/data_spec.rb b/spec/models/rating/data_spec.rb index adfa826..1453054 100644 --- a/spec/models/rating/data_spec.rb +++ b/spec/models/rating/data_spec.rb @@ -21,9 +21,10 @@ RSpec.describe Rating::Rating, ':data' do end it 'returns the estimate for a resource' do - if ENV.fetch('DB') == 'mysql' + case ENV.fetch('DB') + when 'mysql' expect(result[:estimate]).to eq(BigDecimal('42.5000000000000000012000000505')) - elsif ENV.fetch('DB') == 'postgres' + when 'postgres' expect(result[:estimate]).to eq(BigDecimal('42.5000000000000001200000000000000012505')) else raise('DB env missing!') diff --git a/spec/models/rating/update_rating_spec.rb b/spec/models/rating/update_rating_spec.rb index 0afddca..863cf44 100644 --- a/spec/models/rating/update_rating_spec.rb +++ b/spec/models/rating/update_rating_spec.rb @@ -9,10 +9,11 @@ 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) - if ENV.fetch('DB') == 'mysql' + case ENV.fetch('DB') + when 'mysql' expect(record.average).to eq(BigDecimal('50.5')) expect(record.estimate).to eq(BigDecimal('42.5')) - elsif ENV.fetch('DB') == 'postgres' + when 'postgres' expect(record.average).to eq(BigDecimal('50.5')) expect(record.estimate).to eq(BigDecimal('42.5000000000000001')) else