Skip to content

Instantly share code, notes, and snippets.

@naoty
Last active July 17, 2022 13:55
Show Gist options
  • Save naoty/577ff73e22a9a70bd1bc849d642181e6 to your computer and use it in GitHub Desktop.
Save naoty/577ff73e22a9a70bd1bc849d642181e6 to your computer and use it in GitHub Desktop.
Boilerplate code to test MySQL behavior with ActiveRecord
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'minitest'
gem 'activerecord', require: 'active_record'
gem 'mysql2'
end
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.logger = Logger.new(STDOUT)
config = { adapter: 'mysql2', host: '127.0.0.1', username: 'root', password: 'password' }
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.recreate_database('mysql_test')
ActiveRecord::Base.establish_connection(config.merge({ database: 'mysql_test' }))
ActiveRecord::Schema.define do
create_table :accounts do |t|
t.string :name, null: false
end
create_table :withdrawals do |t|
t.string :title, null: false
t.integer :amount, null: false, default: 0
t.references :accounts, foreign_key: true
t.datetime :created_at, null: false
end
end
class Account < ActiveRecord::Base
has_many :withdrawals
end
class Withdrawal < ActiveRecord::Base
belongs_to :account
end
class MysqlTest < Minitest::Test
def test_mysql
# TODO: test
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment