Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add spec files

  • Loading branch information...
commit f1958bfecc71807c816ccf07aca9d2f5364df915 1 parent 83999f7
@tatsuo tatsuo authored
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format=d
View
15 never_wastes.gemspec
@@ -18,7 +18,16 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]
- # specify any dependencies here; for example:
- # s.add_development_dependency "rspec"
- # s.add_runtime_dependency "rest-client"
+ %w{ activesupport actionpack railties }.each do |gem|
+ s.add_dependency gem, ['>= 3.0.0']
+ end
+ s.add_development_dependency 'bundler', ['>= 1.0.0']
+ s.add_development_dependency 'sqlite3', ['>= 0']
+ %w{ activerecord activemodel }.each do |gem|
+ s.add_development_dependency gem, ['>= 3.0.0']
+ end
+ s.add_development_dependency 'rspec', ['>= 0']
+ s.add_development_dependency 'rspec-rails', ['>= 0']
+ s.add_development_dependency 'factory_girl_rails', ['>= 0']
+ s.add_development_dependency 'database_cleaner', ['>= 0']
end
View
29 spec/fake_app.rb
@@ -0,0 +1,29 @@
+require 'action_controller/railtie'
+require 'factory_girl'
+require 'database_cleaner'
+
+# database
+ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
+ActiveRecord::Base.establish_connection('test')
+
+#migrations
+class CreateAllTables < ActiveRecord::Migration
+ def self.up
+ create_table(:users) {|t| t.string :name; t.boolean :deleted; t.datetime :deleted_at }
+ end
+end
+
+# models
+class User < ActiveRecord::Base
+ never_wastes
+ validates_uniqueness_of :name, :scope => [:name, :deleted]
+end
+
+# factory
+FactoryGirl.define do
+ factory :user do
+ sequence(:name) {|n| "test %03d" %n }
+ deleted false
+ deleted_at "2012-03-12 17:37:19"
+ end
+end
View
68 spec/never_wastes_spec.rb
@@ -0,0 +1,68 @@
+require 'spec_helper'
+
+describe User do
+ describe "validation" do
+ describe "uniqueness" do
+ let!(:user) { Factory.create :user, name: "test_name", deleted: false, deleted_at: nil }
+ context "when the same name user don't already exists" do
+ subject { User.new(name: "new user name") }
+ it { should be_valid }
+ end
+
+ context "when the same name user already exists" do
+ subject { User.new(name: user.name) }
+ it { should_not be_valid }
+ end
+
+ context "when the same name user exists and is deleted" do
+ before { user.destroy }
+ subject { User.new(name: user.name) }
+ it { should be_valid }
+ end
+ end
+ end
+
+ describe 'columns' do
+ let!(:user) { Factory.create :user, name: "test_name", deleted: false, deleted_at: nil }
+ subject { user }
+
+ context 'when user is not deleted' do
+ its(:deleted) { should be_false }
+ its(:deleted_at) { should be_nil}
+ end
+
+ context 'when user is deleted' do
+ before { user.destroy }
+ its(:deleted) { should be_true }
+ its(:deleted_at) { should_not be_nil }
+ end
+ end
+
+ describe "soft delete" do
+ let!(:user) { Factory.create :user, name: "test_name", deleted: false, deleted_at: nil }
+
+ it "should be successful" do
+ expect { user.destroy }.to change(User, :count).by(-1)
+ end
+
+ it "delete user *soft*ly" do
+ expect { user.destroy }.not_to change(User.unscoped, :count)
+ end
+ end
+
+ describe "default_scope" do
+ context 'when 1 user and 1 deleted user exists' do
+ let!(:user) { Factory.create :user, name: "test_name", deleted: false, deleted_at: nil }
+ let!(:deleted_user) { Factory.create :user, deleted: true, deleted_at:Time.now }
+
+ subject { User.all }
+ it { should have(1).items }
+ it { should include(user) }
+
+ describe "able to get deleted user" do
+ subject { User.unscoped.where(deleted: true).first }
+ it { should == deleted_user }
+ end
+ end
+ end
+end
View
26 spec/spec_helper.rb
@@ -0,0 +1,26 @@
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+require 'rails'
+# Ensure we use 'syck' instead of 'psych' in 1.9.2
+# RubyGems >= 1.5.0 uses 'psych' on 1.9.2, but
+# Psych does not yet support YAML 1.1 merge keys.
+# Merge keys is often used in mongoid.yml
+# See: http://redmine.ruby-lang.org/issues/show/4300
+if RUBY_VERSION >= '1.9.2'
+ YAML::ENGINE.yamler = 'syck'
+end
+require 'active_record'
+require 'never_wastes'
+require 'fake_app'
+
+require 'rspec/rails'
+# Requires supporting files with custom matchers and macros, etc,
+# in ./support/ and its subdirectories.
+Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
+
+RSpec.configure do |config|
+ config.before :all do
+# ActiveRecord::Base.connection.execute 'CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255))' unless ActiveRecord::Base.connection.table_exists? 'users'
+ CreateAllTables.up unless ActiveRecord::Base.connection.table_exists? 'users'
+ end
+end
View
13 spec/support/database_cleaner.rb
@@ -0,0 +1,13 @@
+DatabaseCleaner.strategy = :transaction
+
+RSpec.configure do |config|
+ config.before :suite do
+ DatabaseCleaner.clean_with :truncation
+ end
+ config.before :each do
+ DatabaseCleaner.start
+ end
+ config.after :each do
+ DatabaseCleaner.clean
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.