-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from cookpad/integration_test
Introduce integration tests using real MySQL and PostgreSQL
- Loading branch information
Showing
11 changed files
with
263 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
name: Integration Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
branches: | ||
- master | ||
|
||
env: | ||
RUBY_VERSION: 3.3 | ||
|
||
jobs: | ||
mysql: | ||
continue-on-error: true | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./integration_test | ||
env: | ||
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile | ||
MYSQL_HOST: 127.0.0.1 | ||
strategy: | ||
matrix: | ||
gemfile: | ||
- ar_6.1 | ||
- ar_7.0 | ||
- ar_7.1 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Start DB | ||
run: docker compose up -d mysql | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ env.RUBY_VERSION }} | ||
- name: Run bundle install | ||
run: bundle install | ||
- name: Run integration test | ||
run: bundle exec rspec spec/mysql2_spec.rb | ||
|
||
postgresql: | ||
continue-on-error: true | ||
runs-on: ubuntu-latest | ||
defaults: | ||
run: | ||
working-directory: ./integration_test | ||
env: | ||
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile | ||
POSTGRES_HOST: 127.0.0.1 | ||
strategy: | ||
matrix: | ||
gemfile: | ||
- ar_6.1 | ||
- ar_7.0 | ||
- ar_7.1 | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Start DB | ||
run: docker compose up -d postgres | ||
- name: Set up Ruby | ||
uses: ruby/setup-ruby@v1 | ||
with: | ||
ruby-version: ${{ env.RUBY_VERSION }} | ||
- name: Run bundle install | ||
run: bundle install | ||
- name: Run integration test | ||
run: bundle exec rspec spec/postgresql_spec.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
*.swp | ||
*.gem | ||
Gemfile.lock | ||
gemfiles/*.lock | ||
*.gemfile.lock | ||
.bundle/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../Appraisals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
source 'https://rubygems.org' | ||
|
||
gem 'arproxy', path: '..' | ||
gem 'rspec' | ||
gem 'appraisal' | ||
gem 'mysql2' | ||
gem 'pg' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
version: '3' | ||
|
||
services: | ||
mysql: | ||
image: mysql:9.0 | ||
restart: always | ||
environment: | ||
MYSQL_ROOT_PASSWORD: rootpassword | ||
MYSQL_DATABASE: arproxy_test | ||
MYSQL_USER: arproxy | ||
MYSQL_PASSWORD: password | ||
ports: | ||
- "23306:3306" | ||
|
||
postgres: | ||
image: postgres:16 | ||
restart: always | ||
environment: | ||
POSTGRES_DB: arproxy_test | ||
POSTGRES_USER: arproxy | ||
POSTGRES_PASSWORD: password | ||
ports: | ||
- "25432:5432" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "arproxy", path: "../.." | ||
gem "rspec" | ||
gem "appraisal" | ||
gem "mysql2" | ||
gem "pg" | ||
gem "activerecord", "~> 6.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "arproxy", path: "../.." | ||
gem "rspec" | ||
gem "appraisal" | ||
gem "mysql2" | ||
gem "pg" | ||
gem "activerecord", "~> 7.0.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# This file was generated by Appraisal | ||
|
||
source "https://rubygems.org" | ||
|
||
gem "arproxy", path: "../.." | ||
gem "rspec" | ||
gem "appraisal" | ||
gem "mysql2" | ||
gem "pg" | ||
gem "activerecord", "~> 7.1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require_relative 'spec_helper' | ||
require 'mysql2' | ||
|
||
context 'MySQL' do | ||
before(:all) do | ||
ActiveRecord::Base.establish_connection( | ||
adapter: 'mysql2', | ||
host: ENV.fetch('MYSQL_HOST', '127.0.0.1'), | ||
port: ENV.fetch('MYSQL_PORT', '23306').to_i, | ||
database: 'arproxy_test', | ||
username: 'arproxy', | ||
password: 'password' | ||
) | ||
|
||
Arproxy.configure do |config| | ||
config.adapter = 'mysql2' | ||
config.use HelloProxy | ||
config.use QueryLogger | ||
end | ||
Arproxy.enable! | ||
|
||
ActiveRecord::Base.connection.create_table :products, force: true do |t| | ||
t.string :name | ||
t.integer :price | ||
end | ||
|
||
Product.create(name: 'apple', price: 100) | ||
Product.create(name: 'banana', price: 200) | ||
Product.create(name: 'orange', price: 300) | ||
end | ||
|
||
after(:all) do | ||
ActiveRecord::Base.connection.drop_table :products | ||
ActiveRecord::Base.connection.close | ||
Arproxy.disable! | ||
end | ||
|
||
before(:each) do | ||
QueryLogger.reset! | ||
end | ||
|
||
it do | ||
expect(QueryLogger.log.size).to eq(0) | ||
|
||
expect(Product.count).to eq(3) | ||
expect(Product.first.name).to eq('apple') | ||
|
||
expect(QueryLogger.log.size).to eq(2) | ||
expect(QueryLogger.log[0]).to eq('SELECT COUNT(*) FROM `products` -- Hello Arproxy!') | ||
expect(QueryLogger.log[1]).to eq('SELECT `products`.* FROM `products` ORDER BY `products`.`id` ASC LIMIT 1 -- Hello Arproxy!') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require_relative 'spec_helper' | ||
require 'pg' | ||
|
||
context 'PostgreSQL' do | ||
before(:all) do | ||
ActiveRecord::Base.establish_connection( | ||
adapter: 'postgresql', | ||
host: ENV.fetch('POSTGRES_HOST', '127.0.0.1'), | ||
port: ENV.fetch('POSTGRES_PORT', '25432').to_i, | ||
database: 'arproxy_test', | ||
username: 'arproxy', | ||
password: 'password' | ||
) | ||
|
||
Arproxy.configure do |config| | ||
config.adapter = 'postgresql' | ||
config.use HelloProxy | ||
config.use QueryLogger | ||
end | ||
Arproxy.enable! | ||
|
||
ActiveRecord::Base.connection.create_table :products, force: true do |t| | ||
t.string :name | ||
t.integer :price | ||
end | ||
|
||
Product.create(name: 'apple', price: 100) | ||
Product.create(name: 'banana', price: 200) | ||
Product.create(name: 'orange', price: 300) | ||
end | ||
|
||
after(:all) do | ||
ActiveRecord::Base.connection.drop_table :products | ||
ActiveRecord::Base.connection.close | ||
Arproxy.disable! | ||
end | ||
|
||
before(:each) do | ||
QueryLogger.reset! | ||
end | ||
|
||
it do | ||
expect(QueryLogger.log.size).to eq(0) | ||
|
||
expect(Product.count).to eq(3) | ||
expect(Product.first.name).to eq('apple') | ||
|
||
expect(QueryLogger.log.size).to eq(2) | ||
expect(QueryLogger.log[0]).to eq('SELECT COUNT(*) FROM `products` -- Hello Arproxy!') | ||
expect(QueryLogger.log[1]).to eq('SELECT `products`.* FROM `products` ORDER BY `products`.`id` ASC LIMIT 1 -- Hello Arproxy!') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
require 'arproxy' | ||
require 'active_record' | ||
|
||
class Product < ActiveRecord::Base | ||
end | ||
|
||
class QueryLogger < Arproxy::Base | ||
def execute(sql, name = nil) | ||
@@log ||= [] | ||
@@log << sql | ||
puts "QueryLogger: #{sql}" | ||
super | ||
end | ||
|
||
def self.log | ||
@@log | ||
end | ||
|
||
def self.reset! | ||
@@log = [] | ||
end | ||
end | ||
|
||
class HelloProxy < Arproxy::Base | ||
def execute(sql, name = nil) | ||
super("#{sql} -- Hello Arproxy!", name) | ||
end | ||
end |