commit
4af463642c
|
@ -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
|
||||
|
|
|
@ -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).
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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')
|
Loading…
Reference in New Issue