Skip to content

Commit

Permalink
Add database and schema for specs
Browse files Browse the repository at this point in the history
  • Loading branch information
shioyama committed Nov 24, 2016
1 parent 9498acb commit e015b56
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -7,3 +7,7 @@
/pkg/
/spec/reports/
/tmp/

.ruby-version
.byebug_history
Guardfile
11 changes: 11 additions & 0 deletions Gemfile
Expand Up @@ -2,3 +2,14 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in mobility.gemspec
gemspec

group :development, :test do
platforms :ruby do
gem 'guard-rspec'
gem 'pry-byebug'
gem 'guard-rspec'
gem 'sqlite3'
gem 'mysql2', '~> 0.3.10'
gem 'pg'
end
end
28 changes: 28 additions & 0 deletions spec/database.rb
@@ -0,0 +1,28 @@
module Mobility
module Test
class Database
class << self
def connect
::ActiveRecord::Base.establish_connection config[driver]

if in_memory?
::ActiveRecord::Migration.verbose = false
Schema.migrate :up
end
end

def config
@config ||= YAML::load(File.open(File.expand_path("../databases.yml", __FILE__)))
end

def driver
(ENV["DB"] or "sqlite3").downcase
end

def in_memory?
config[driver]["database"] == ":memory:"
end
end
end
end
end
19 changes: 19 additions & 0 deletions spec/databases.yml
@@ -0,0 +1,19 @@
mysql:
adapter: mysql2
database: mobility_test
username: root
hostname: localhost
encoding: utf8

postgres:
adapter: postgresql
host: localhost
port: 5432
username: postgres
database: mobility_test
encoding: utf8

sqlite3:
adapter: sqlite3
database: ":memory:"
encoding: utf8
21 changes: 21 additions & 0 deletions spec/schema.rb
@@ -0,0 +1,21 @@
module Mobility
module Test
class Schema < ::ActiveRecord::Migration[[::ActiveRecord::VERSION::MAJOR, ::ActiveRecord::VERSION::MINOR].join(".")]
class << self
def up
create_table "articles" do |t|
t.string :slug
end

create_table "mobility_translations" do |t|
t.string :locale
t.string :key
t.text :value
t.integer :translatable_id
t.string :translatable_type
end
end
end
end
end
end
47 changes: 47 additions & 0 deletions spec/spec_helper.rb
@@ -1,2 +1,49 @@
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

require 'active_record'
require 'pry-byebug'
require 'i18n'
require 'rspec'
require 'rspec/its'
require 'shoulda-matchers'

require 'mobility'

I18n.enforce_available_locales = true
I18n.available_locales = [:en, :ja, :fr, :de, :cz, :pl]

Dir[File.expand_path("./spec/support/**/*.rb")].each { |f| require f }

require "database"
require "schema"

Mobility::Test::Database.connect
at_exit {ActiveRecord::Base.connection.disconnect!}

require 'database_cleaner'
DatabaseCleaner.strategy = :transaction

RSpec.configure do |config|
config.filter_run focus: true
config.run_all_when_everything_filtered = true
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end

config.before :each do
DatabaseCleaner.start
Mobility.locale = :en
end
config.after :each do
DatabaseCleaner.clean
end

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :active_record
end
end

config.include(Shoulda::Matchers::ActiveModel, type: :model)
end

0 comments on commit e015b56

Please sign in to comment.