mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
first commit
This commit is contained in:
75
spec/attribute_methods_spec.rb
Normal file
75
spec/attribute_methods_spec.rb
Normal file
@@ -0,0 +1,75 @@
|
||||
require File.dirname(__FILE__) + '/spec_helper'
|
||||
|
||||
describe ValidatesTimeliness::AttributeMethods do
|
||||
|
||||
describe "for Time columns" do
|
||||
before do
|
||||
Person.define_read_method_for_time_zone_conversion(:birth_date_and_time)
|
||||
Person.define_write_method_for_time_zone_conversion(:birth_date_and_time)
|
||||
@person = Person.new
|
||||
end
|
||||
|
||||
it "should define attribute read method for column" do
|
||||
@person.respond_to?(:birth_date_and_time).should be_true
|
||||
end
|
||||
|
||||
it "should define attribute write method for column" do
|
||||
@person.respond_to?(:birth_date_and_time=).should be_true
|
||||
end
|
||||
|
||||
it "should return string value for attribute_before_type_cast when written as string" do
|
||||
@person.birth_date_and_time = "1980-12-25 01:02:03"
|
||||
@person.birth_date_and_time_before_type_cast.should == "1980-12-25 01:02:03"
|
||||
end
|
||||
|
||||
it "should return Time object for attribute_before_type_cast when written as Time" do
|
||||
@person.birth_date_and_time = time = Time.mktime(1980, 12, 25, 1, 2, 3)
|
||||
@person.birth_date_and_time_before_type_cast.should == time
|
||||
end
|
||||
|
||||
it "should return Time object using attribute read method when written with string" do
|
||||
@person.birth_date_and_time = "1980-12-25 01:02:03"
|
||||
@person.birth_date_and_time.should == Time.mktime(1980, 12, 25, 1, 2, 3)
|
||||
end
|
||||
|
||||
it "should read stored time with correct timezone"
|
||||
|
||||
it "should return nil when date is invalid"
|
||||
end
|
||||
|
||||
describe "for Date columns" do
|
||||
before do
|
||||
Person.define_read_method_for_time_zone_conversion(:birth_date)
|
||||
Person.define_write_method_for_time_zone_conversion(:birth_date)
|
||||
@person = Person.new
|
||||
end
|
||||
|
||||
it "should define attribute read method for column" do
|
||||
@person.respond_to?(:birth_date).should be_true
|
||||
end
|
||||
|
||||
it "should define attribute write method for column" do
|
||||
@person.respond_to?(:birth_date=).should be_true
|
||||
end
|
||||
|
||||
it "should return string value for attribute_before_type_cast when written as string" do
|
||||
@person.birth_date = "1980-12-25"
|
||||
@person.birth_date_before_type_cast.should == "1980-12-25"
|
||||
end
|
||||
|
||||
it "should return Date object for attribute_before_type_cast when written as Date" do
|
||||
@person.birth_date = date = Date.new(1980, 12, 25)
|
||||
@person.birth_date_before_type_cast.should == date
|
||||
end
|
||||
|
||||
it "should return Date object using attribute read method when written with string" do
|
||||
@person.birth_date = "1980-12-25"
|
||||
@person.birth_date.should == Date.new(1980, 12, 25)
|
||||
end
|
||||
|
||||
it "should read stored time with correct timezone"
|
||||
|
||||
it "should return nil when date is invalid"
|
||||
end
|
||||
|
||||
end
|
||||
37
spec/base_spec.rb
Normal file
37
spec/base_spec.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
require File.dirname(__FILE__) + '/spec_helper'
|
||||
|
||||
describe ValidatesTimeliness::Base do
|
||||
|
||||
class AttributeAssignmentError; def initialize(*args); end; end
|
||||
class MultiparameterAssignmentErrors; def initialize(*args); end; end
|
||||
|
||||
before do
|
||||
self.class.stub!(:reflect_on_aggregation).and_return(nil)
|
||||
|
||||
end
|
||||
|
||||
it "should convert time array into string" do
|
||||
time_string = time_array_to_string([2000,1,1,12,12,0])
|
||||
time_string.should == "2000-01-01 12:12:00"
|
||||
end
|
||||
|
||||
describe "execute_callstack_for_multiparameter_attributes" do
|
||||
before do
|
||||
@date_array = [1980,1,1,0,0,0]
|
||||
end
|
||||
|
||||
it "should store time string for a Time class column" do
|
||||
self.stub!(:column_for_attribute).and_return( mock('Column', :klass => Time) )
|
||||
self.should_receive(:birth_date_and_time=).once.with("1980-01-01 00:00:00")
|
||||
callstack = {'birth_date_and_time' => @date_array}
|
||||
execute_callstack_for_multiparameter_attributes(callstack)
|
||||
end
|
||||
|
||||
it "should store time string for a Date class column" do
|
||||
self.stub!(:column_for_attribute).and_return( mock('Column', :klass => Date) )
|
||||
self.should_receive(:birth_date=).once.with("1980-01-01 00:00:00")
|
||||
callstack = {'birth_date' => @date_array}
|
||||
execute_callstack_for_multiparameter_attributes(callstack)
|
||||
end
|
||||
end
|
||||
end
|
||||
3
spec/resources/person.rb
Normal file
3
spec/resources/person.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
class Person < ActiveRecord::Base
|
||||
|
||||
end
|
||||
9
spec/resources/schema.rb
Normal file
9
spec/resources/schema.rb
Normal file
@@ -0,0 +1,9 @@
|
||||
ActiveRecord::Schema.define(:version => 1) do
|
||||
|
||||
create_table "people", :force => true do |t|
|
||||
t.string "name"
|
||||
t.datetime "birth_date_and_time"
|
||||
t.date "birth_date"
|
||||
end
|
||||
|
||||
end
|
||||
18
spec/spec_helper.rb
Normal file
18
spec/spec_helper.rb
Normal file
@@ -0,0 +1,18 @@
|
||||
$: << File.dirname(__FILE__) + '/../lib' << File.dirname(__FILE__)
|
||||
|
||||
require 'rubygems'
|
||||
require 'spec'
|
||||
require 'active_support'
|
||||
require 'active_record'
|
||||
|
||||
require 'validates_timeliness'
|
||||
|
||||
conn = {
|
||||
:adapter => 'sqlite3',
|
||||
:database => ':memory:'
|
||||
}
|
||||
|
||||
ActiveRecord::Base.establish_connection(conn)
|
||||
|
||||
require 'resources/schema'
|
||||
require 'resources/person'
|
||||
Reference in New Issue
Block a user