mv-test 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/migration_validators/spec/macros/db_macros.rb +133 -0
- data/lib/migration_validators/spec/matchers/db_matchers.rb +201 -0
- data/lib/migration_validators/spec/support/column_wrapper.rb +68 -0
- data/lib/migration_validators/spec/support/db.rb +58 -0
- data/lib/migration_validators/spec/support/table_wrapper.rb +25 -0
- data/lib/migration_validators/spec/support/test_adapter.rb +47 -0
- data/lib/mv-test.rb +15 -0
- data/spec/migration_validators/spec/macros/db_macros_spec.rb +25 -0
- data/spec/migration_validators/spec/matchers/db_matchers_spec.rb +37 -0
- data/spec/migration_validators/spec/support/column_wrapper_spec.rb +148 -0
- data/spec/migration_validators/spec/support/db_spec.rb +56 -0
- data/spec/migration_validators/spec/support/table_wrapper_spec.rb +20 -0
- data/spec/migration_validators/spec/support/test_adapter_spec.rb +4 -0
- data/spec/mv-test_spec.rb +4 -0
- data/spec/spec_helper.rb +12 -0
- metadata +134 -0
@@ -0,0 +1,133 @@
|
|
1
|
+
module MigrationValidators
|
2
|
+
module Spec
|
3
|
+
module Macros
|
4
|
+
module DBMacros
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def for_table table_name, &block
|
11
|
+
describe "for table #{table_name}" do
|
12
|
+
before :all do
|
13
|
+
@table_wrapper = table(table_name)
|
14
|
+
end
|
15
|
+
|
16
|
+
before :each do
|
17
|
+
@table_wrapper.drop
|
18
|
+
end
|
19
|
+
|
20
|
+
instance_eval(&block)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def for_test_table &block
|
25
|
+
for_table :test_table, &block
|
26
|
+
end
|
27
|
+
|
28
|
+
def for_column column_name, column_type, column_options = {}, &block
|
29
|
+
describe "for column #{column_name} (#{column_type})" do
|
30
|
+
before :all do
|
31
|
+
raise "'for_column' must be called in table context only" unless @table_wrapper.kind_of?(MigrationValidators::Spec::Support::TableWrapper)
|
32
|
+
|
33
|
+
@column_wrapper = MigrationValidators::Spec::Support::ColumnWrapper.new(column_name, @table_wrapper, db)
|
34
|
+
@column_type = column_type
|
35
|
+
@column_options ||= column_options
|
36
|
+
end
|
37
|
+
|
38
|
+
subject do
|
39
|
+
@column_wrapper
|
40
|
+
end
|
41
|
+
|
42
|
+
before :each do
|
43
|
+
new_table @table_wrapper.table_name do |t|
|
44
|
+
t.column @column_wrapper.column_name, @column_type, @column_options.clone
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
instance_eval(&block)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def for_test_column column_type, column_options = {}, &block
|
54
|
+
for_column :test_column, column_type, column_options, &block
|
55
|
+
end
|
56
|
+
|
57
|
+
%w(integer decimal float string date datetime time).each do |type|
|
58
|
+
define_method :"for_#{type}_column" do |*args, &block|
|
59
|
+
options = args.first || {}
|
60
|
+
|
61
|
+
for_test_column type, options, &block
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
|
66
|
+
def with_validator validator_name, opts = true, &block
|
67
|
+
describe "validated for #{validator_name}" do
|
68
|
+
before :all do
|
69
|
+
raise "'for_validator' must be called in column context only" unless @column_wrapper.kind_of?(MigrationValidators::Spec::Support::ColumnWrapper)
|
70
|
+
|
71
|
+
@validator_name = validator_name
|
72
|
+
@column_options.merge!(:validates => {validator_name => opts})
|
73
|
+
end
|
74
|
+
|
75
|
+
subject do
|
76
|
+
@column_wrapper
|
77
|
+
end
|
78
|
+
|
79
|
+
instance_eval(&block)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def with_options opts = {}, &block
|
84
|
+
describe "with options #{opts}" do
|
85
|
+
before :all do
|
86
|
+
raise "'with_options' must be called in validator context only" if @validator_name.blank?
|
87
|
+
|
88
|
+
@column_options = @column_options.clone
|
89
|
+
@column_options[:validates] = @column_options[:validates].clone
|
90
|
+
validator_options = @column_options[:validates][@validator_name]
|
91
|
+
|
92
|
+
|
93
|
+
@column_options[:validates][@validator_name] = validator_options.kind_of?(Hash) ? validator_options.merge(opts) : opts
|
94
|
+
end
|
95
|
+
|
96
|
+
|
97
|
+
subject do
|
98
|
+
@column_wrapper
|
99
|
+
end
|
100
|
+
|
101
|
+
instance_eval(&block)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
alias_method :with_option, :with_options
|
105
|
+
|
106
|
+
def with_change opts = {}, &block
|
107
|
+
describe "with change in column #{opts}" do
|
108
|
+
before :all do
|
109
|
+
raise "'with_change' must be called in column context only" unless @column_wrapper.kind_of?(MigrationValidators::Spec::Support::ColumnWrapper)
|
110
|
+
end
|
111
|
+
|
112
|
+
before :each do
|
113
|
+
validation_options = @column_options[:validates]
|
114
|
+
validation_options = {} unless validation_options.kind_of?(Hash)
|
115
|
+
validation_options.merge!(opts)
|
116
|
+
|
117
|
+
chg_table @table_wrapper.table_name do |t|
|
118
|
+
t.change_validates @column_wrapper.column_name, validation_options
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
subject do
|
123
|
+
@column_wrapper
|
124
|
+
end
|
125
|
+
|
126
|
+
instance_eval(&block)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
module MigrationValidators
|
2
|
+
module Spec
|
3
|
+
module Matchers
|
4
|
+
module DBMatchers
|
5
|
+
class BaseDbMatcher
|
6
|
+
def initialize *values
|
7
|
+
update_and_insert *values
|
8
|
+
@all = true
|
9
|
+
end
|
10
|
+
|
11
|
+
def initial *values
|
12
|
+
@initial = values
|
13
|
+
self
|
14
|
+
end
|
15
|
+
alias_method :from_initial, :initial
|
16
|
+
alias_method :with_initial, :initial
|
17
|
+
|
18
|
+
def update *values
|
19
|
+
@values = values.flatten
|
20
|
+
@update = true
|
21
|
+
@insert = false
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def insert *values
|
26
|
+
@values = values.flatten
|
27
|
+
@update = false
|
28
|
+
@insert = true
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def from *values
|
33
|
+
@values = values.flatten
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def update?
|
38
|
+
@update
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
def insert?
|
43
|
+
@insert
|
44
|
+
end
|
45
|
+
|
46
|
+
def all *values
|
47
|
+
@values = values.flatten
|
48
|
+
@all = true
|
49
|
+
self
|
50
|
+
end
|
51
|
+
alias_method :for_all_from, :all
|
52
|
+
alias_method :for_all, :all
|
53
|
+
|
54
|
+
def at_least_one *values
|
55
|
+
@values = values.flatten
|
56
|
+
@all = false
|
57
|
+
self
|
58
|
+
end
|
59
|
+
alias_method :for_at_least_one_from, :at_least_one
|
60
|
+
alias_method :for_at_least_one, :at_least_one
|
61
|
+
|
62
|
+
|
63
|
+
def all?
|
64
|
+
@all
|
65
|
+
end
|
66
|
+
|
67
|
+
def at_least_one?
|
68
|
+
!all?
|
69
|
+
end
|
70
|
+
|
71
|
+
def values
|
72
|
+
@values
|
73
|
+
end
|
74
|
+
|
75
|
+
def update_and_insert *values
|
76
|
+
@values = values.flatten
|
77
|
+
@update = true
|
78
|
+
@insert = true
|
79
|
+
self
|
80
|
+
end
|
81
|
+
alias_method :insert_and_update, :update_and_insert
|
82
|
+
|
83
|
+
|
84
|
+
def operations_array
|
85
|
+
res = []
|
86
|
+
|
87
|
+
res << :update if update?
|
88
|
+
res << :insert if insert?
|
89
|
+
|
90
|
+
res
|
91
|
+
end
|
92
|
+
|
93
|
+
def with_message message
|
94
|
+
@message = message
|
95
|
+
self
|
96
|
+
end
|
97
|
+
alias_method :and_message, :with_message
|
98
|
+
|
99
|
+
def message
|
100
|
+
@message || ""
|
101
|
+
end
|
102
|
+
|
103
|
+
def matches? column_wrapper
|
104
|
+
column_wrapper.insert(@initial) if @initial
|
105
|
+
|
106
|
+
column_wrapper.to_array(values).each do |value|
|
107
|
+
passed = true
|
108
|
+
|
109
|
+
if insert?
|
110
|
+
column_wrapper.insert value
|
111
|
+
passed = check_result(value, column_wrapper.last_exception, :insert)
|
112
|
+
end
|
113
|
+
|
114
|
+
if update? && passed
|
115
|
+
column_wrapper.update value
|
116
|
+
passed = check_result(value, column_wrapper.last_exception, :update)
|
117
|
+
end
|
118
|
+
|
119
|
+
return false if all? && !passed
|
120
|
+
return true if passed && at_least_one?
|
121
|
+
end
|
122
|
+
|
123
|
+
all?
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
protected
|
128
|
+
|
129
|
+
attr_reader :last_operation
|
130
|
+
attr_reader :last_value
|
131
|
+
attr_reader :last_exception
|
132
|
+
|
133
|
+
def check_result value, exception, operation
|
134
|
+
@last_operation = operation
|
135
|
+
@last_value = value
|
136
|
+
@last_exception = exception
|
137
|
+
|
138
|
+
true
|
139
|
+
end
|
140
|
+
|
141
|
+
def compose_message &block
|
142
|
+
|
143
|
+
operations_name = operations_array.join(' and operation ')
|
144
|
+
elements_name = all? ? 'all_elements' : 'at least one element'
|
145
|
+
last_message = last_exception ? "'#{last_exception.message}'" : ""
|
146
|
+
expected_message = message.blank? ? "" : "with message '#{message.kind_of?(Regexp) ? message.source : message.inspect }'"
|
147
|
+
|
148
|
+
yield operations_name, elements_name, last_message, expected_message
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
class Deny < BaseDbMatcher
|
153
|
+
def check_result value, exception, operation
|
154
|
+
super && exception && exception.message =~ /#{message}/
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
def failure_message
|
159
|
+
compose_message do |operations_name, elements_name, last_message, expected_message|
|
160
|
+
"expected that operation #{operations_name} would fail for #{elements_name} from #{values} #{expected_message}. But #{last_operation} #{last_message.blank? ? 'successed with ' : 'raised ' + last_message + ' for '}'#{last_value}'"
|
161
|
+
end
|
162
|
+
end
|
163
|
+
|
164
|
+
def negative_failure_message
|
165
|
+
compose_message do |operations_name, elements_name, last_message, expected_message|
|
166
|
+
"not expected that operation #{operations_name} would fail for #{elements_name} from #{values} #{expected_message}. But it happened with #{last_operation} on '#{last_value}'"
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
|
172
|
+
class Allow < BaseDbMatcher
|
173
|
+
def check_result value, exception, operation
|
174
|
+
super && exception.blank?
|
175
|
+
end
|
176
|
+
|
177
|
+
|
178
|
+
def failure_message
|
179
|
+
compose_message do |operations_name, elements_name, last_message, expected_message|
|
180
|
+
"expected that operation #{operations_name} would success for #{elements_name} from #{values}. But #{last_operation} raised #{last_message} for '#{last_value}'"
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
def negative_failure_message
|
185
|
+
compose_message do |operations_name, elements_name, last_message, expected_message|
|
186
|
+
"not expected that operation #{operations_name} would success for #{elements_name} from #{values}. But it happened with #{last_operation} on '#{last_value}'"
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
def allow *values
|
192
|
+
Allow.new *values
|
193
|
+
end
|
194
|
+
|
195
|
+
def deny *values
|
196
|
+
Deny.new *values
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/db.rb')
|
2
|
+
|
3
|
+
module MigrationValidators
|
4
|
+
module Spec
|
5
|
+
module Support
|
6
|
+
class ColumnWrapper
|
7
|
+
attr_accessor :column_name
|
8
|
+
attr_accessor :last_exception
|
9
|
+
|
10
|
+
def initialize column_name, table_wrapper, db
|
11
|
+
@column_name = column_name
|
12
|
+
@table_wrapper = table_wrapper
|
13
|
+
@db = db
|
14
|
+
end
|
15
|
+
|
16
|
+
def drop
|
17
|
+
@db.remove_column @table_wrapper.table_name, column_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def update *values
|
21
|
+
@last_exception = nil
|
22
|
+
to_array(values).each{|value| execute(value, "UPDATE #{@table_wrapper.table_name} SET #{column_name} = #{value}")}
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
def insert *values
|
27
|
+
@last_exception = nil
|
28
|
+
to_array(values).each {|value| execute(value, "INSERT INTO #{@table_wrapper.table_name}(#{column_name}) VALUES(#{value})")}
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_array values
|
33
|
+
values.collect do |value|
|
34
|
+
case value.class.name
|
35
|
+
when "Range" then to_array(value.to_a)
|
36
|
+
when "Array" then to_array(value)
|
37
|
+
when "String" then ['NULL', "''"].include?(value.upcase) ? value : quote(value)
|
38
|
+
when "Date" then quote(value.strftime('%Y-%m-%d'))
|
39
|
+
when "Time" then quote(value.strftime('%Y-%m-%d %H:%M:%S'))
|
40
|
+
when "DateTime" then quote(value.strftime('%Y-%m-%d %H:%M:%S'))
|
41
|
+
when "NilClass" then 'NULL'
|
42
|
+
else value
|
43
|
+
end
|
44
|
+
end.flatten
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def quote value
|
50
|
+
value = "'#{value}" unless value.starts_with?("'")
|
51
|
+
value = "#{value}'" unless value.ends_with?("'")
|
52
|
+
value
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
def execute value, statement
|
57
|
+
@last_exception = nil
|
58
|
+
|
59
|
+
begin
|
60
|
+
@db.execute(statement)
|
61
|
+
rescue Exception => e
|
62
|
+
@last_exception = e
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module MigrationValidators
|
2
|
+
module Spec
|
3
|
+
module Support
|
4
|
+
module DB
|
5
|
+
def use_db config
|
6
|
+
::ActiveRecord::Base.remove_connection if ::ActiveRecord::Base.connected?
|
7
|
+
::ActiveRecord::Base.establish_connection config
|
8
|
+
end
|
9
|
+
|
10
|
+
def use_memory_db
|
11
|
+
use_db :adapter => "sqlite3", :database => ":memory:"
|
12
|
+
end
|
13
|
+
|
14
|
+
def db
|
15
|
+
::ActiveRecord::Base.connection
|
16
|
+
end
|
17
|
+
|
18
|
+
def migrate &block
|
19
|
+
migration_class = Class.new(::ActiveRecord::Migration) do
|
20
|
+
def self.up
|
21
|
+
up_migrate
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
migration_class.class.instance_eval do
|
26
|
+
define_method :up_migrate, &block
|
27
|
+
end
|
28
|
+
|
29
|
+
migration_class.migrate(:up)
|
30
|
+
end
|
31
|
+
|
32
|
+
def new_table table_name = :test_table, &block
|
33
|
+
migrate do
|
34
|
+
create_table(table_name) do |t|
|
35
|
+
yield t
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
table(table_name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def chg_table table_name = :test_table, &block
|
43
|
+
migrate do
|
44
|
+
change_table(table_name) do |t|
|
45
|
+
yield t
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
table(table_name)
|
50
|
+
end
|
51
|
+
|
52
|
+
def table table_name
|
53
|
+
MigrationValidators::Spec::Support::TableWrapper.new(table_name, db)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module MigrationValidators
|
2
|
+
module Spec
|
3
|
+
module Support
|
4
|
+
class TableWrapper
|
5
|
+
attr_accessor :table_name
|
6
|
+
|
7
|
+
def initialize table_name, db
|
8
|
+
@table_name = table_name
|
9
|
+
@db = db
|
10
|
+
end
|
11
|
+
|
12
|
+
def drop
|
13
|
+
@db.drop_table(table_name) if @db.table_exists?(table_name)
|
14
|
+
end
|
15
|
+
|
16
|
+
alias_method :old_method_missing, :method_missing
|
17
|
+
def method_missing method_name, *args
|
18
|
+
return MigrationValidators::Spec::Support::ColumnWrapper.new(method_name, self, @db) if @db.column_exists?(table_name, method_name)
|
19
|
+
|
20
|
+
old_method_missing method_name, *args
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module MigrationValidators
|
2
|
+
module Spec
|
3
|
+
module Support
|
4
|
+
class TestAdapter
|
5
|
+
def name
|
6
|
+
"TestAdapter"
|
7
|
+
end
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def stub_method method_name_prefix, validator_name, db_form
|
11
|
+
method_name_suffix = db_form ? "_#{db_form}" : ""
|
12
|
+
method_name = :"#{method_name_prefix}_#{validator_name}#{method_name_suffix}"
|
13
|
+
|
14
|
+
define_method method_name do |validators|
|
15
|
+
TestAdapter.call(method_name, validators)
|
16
|
+
yield(validators) if block_given?
|
17
|
+
end
|
18
|
+
end
|
19
|
+
private :stub_method
|
20
|
+
|
21
|
+
def stub_validate_method validator_name, db_form = nil, &block
|
22
|
+
stub_method :validate, validator_name, db_form, &block
|
23
|
+
end
|
24
|
+
|
25
|
+
def stub_remove_validate_method validator_name, db_form = nil, &block
|
26
|
+
stub_method :remove_validate, validator_name, db_form, &block
|
27
|
+
end
|
28
|
+
|
29
|
+
def log
|
30
|
+
@log ||= {}
|
31
|
+
end
|
32
|
+
|
33
|
+
def clear
|
34
|
+
@log = nil
|
35
|
+
|
36
|
+
public_instance_methods.grep(/^validate_/) { |method_name| undef_method method_name }
|
37
|
+
public_instance_methods.grep(/^remove_validate_/) { |method_name| undef_method method_name }
|
38
|
+
end
|
39
|
+
|
40
|
+
def call method_name, validators
|
41
|
+
(log[method_name] ||= []) << validators
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
data/lib/mv-test.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_record'
|
3
|
+
|
4
|
+
require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/spec/support/test_adapter'
|
5
|
+
require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/spec/support/column_wrapper'
|
6
|
+
require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/spec/support/table_wrapper'
|
7
|
+
require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/spec/support/db'
|
8
|
+
require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/spec/matchers/db_matchers'
|
9
|
+
require File.expand_path(File.dirname(__FILE__)) + '/migration_validators/spec/macros/db_macros'
|
10
|
+
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.include(MigrationValidators::Spec::Support::DB, :type => :mv_test)
|
13
|
+
config.include(MigrationValidators::Spec::Matchers::DBMatchers, :type => :mv_test)
|
14
|
+
config.include(MigrationValidators::Spec::Macros::DBMacros, :type => :mv_test)
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Spec::Macros::DBMacros, "supports", :type => :mv_test do
|
4
|
+
before :all do
|
5
|
+
use_memory_db
|
6
|
+
end
|
7
|
+
|
8
|
+
describe :for_column, "creates column for each internal test" do
|
9
|
+
for_table :test_table do
|
10
|
+
for_column :column, :integer do
|
11
|
+
it { db.column_exists?(:test_table, :column).should be_true }
|
12
|
+
it { db.columns(:test_table).find{|col| col.name.to_s == "column"}.type.to_s.should == "integer" }
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe :with_validator do
|
18
|
+
end
|
19
|
+
|
20
|
+
describe :with_option do
|
21
|
+
end
|
22
|
+
|
23
|
+
describe :with_change do
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Spec::Matchers::DBMatchers, "supports", :type => :mv_test do
|
4
|
+
before :all do
|
5
|
+
use_memory_db
|
6
|
+
end
|
7
|
+
|
8
|
+
before :each do
|
9
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
10
|
+
|
11
|
+
migrate do
|
12
|
+
create_table :test_table do |t|
|
13
|
+
t.string :column
|
14
|
+
end
|
15
|
+
|
16
|
+
add_index :test_table, :column, :unique => true
|
17
|
+
end
|
18
|
+
|
19
|
+
@column = table(:test_table).column
|
20
|
+
end
|
21
|
+
|
22
|
+
describe :allow do
|
23
|
+
it { @column.should allow.insert(1, 2) }
|
24
|
+
it { @column.should allow.update(2).with_initial(1) }
|
25
|
+
it { @column.should allow(1) }
|
26
|
+
it { @column.should allow.at_least_one(1, 1) }
|
27
|
+
it { @column.should allow.insert.at_least_one(1, 1) }
|
28
|
+
it { @column.should allow.update.at_least_one(1, 1) }
|
29
|
+
end
|
30
|
+
|
31
|
+
describe :deny do
|
32
|
+
it { @column.should deny.at_least_one(1, 1).with_initial(1, 2) }
|
33
|
+
it { @column.should deny.at_least_one.insert(1, 1) }
|
34
|
+
it { @column.should deny.at_least_one.insert(1, 1).with_message(/not unique/) }
|
35
|
+
it { @column.should deny.insert(1, 1).with_initial(1).with_message(/not unique/) }
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,148 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Spec::Support::ColumnWrapper, "supports", :type => :mv_test do
|
4
|
+
before :each do
|
5
|
+
use_memory_db
|
6
|
+
|
7
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
8
|
+
@wrapper = new_table {|t| t.string :str_column }.str_column
|
9
|
+
end
|
10
|
+
|
11
|
+
it "drop" do
|
12
|
+
chg_table(:test_table) {|t| t.string :str_column_1}.str_column_1.drop
|
13
|
+
|
14
|
+
db.column_exists?(:test_table, :str_column_1).should be_false
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "sql wrapping for" do
|
18
|
+
before :each do
|
19
|
+
db.drop_table(:items) if db.table_exists?(:items)
|
20
|
+
|
21
|
+
Item = Class.new(ActiveRecord::Base) do
|
22
|
+
def self.all
|
23
|
+
Item.find(:all, :order => "column ASC").collect{|item| item.column}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe :insert do
|
29
|
+
describe "range" do
|
30
|
+
it "open" do
|
31
|
+
new_table(:items){|t| t.integer :column}.column.insert(1..3)
|
32
|
+
Item.all.should == [1, 2, 3]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "closed" do
|
36
|
+
new_table(:items){|t| t.integer :column}.column.insert(1...3)
|
37
|
+
Item.all.should == [1, 2]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "array" do
|
42
|
+
before :each do
|
43
|
+
@column = new_table(:items){|t| t.integer :column}.column
|
44
|
+
end
|
45
|
+
|
46
|
+
it "of simple elements" do
|
47
|
+
@column.insert([1, 2, 3])
|
48
|
+
Item.all.should == [1, 2, 3]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "as params list" do
|
52
|
+
@column.insert(1, 2, 3)
|
53
|
+
Item.all.should == [1, 2, 3]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "as composite elements" do
|
57
|
+
@column.insert(1, 2, 3, [4, 5], 6..8)
|
58
|
+
Item.all.should == [1, 2, 3, 4, 5, 6, 7, 8]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "string" do
|
63
|
+
before :each do
|
64
|
+
@column = new_table(:items){|t| t.string :column}.column
|
65
|
+
end
|
66
|
+
|
67
|
+
it "parameters" do
|
68
|
+
@column.insert("str1", "str2")
|
69
|
+
Item.all.should == ["str1", "str2"]
|
70
|
+
end
|
71
|
+
|
72
|
+
it "interprets NULL correctly" do
|
73
|
+
@column.insert("NULL")
|
74
|
+
|
75
|
+
Item.all.should == [nil]
|
76
|
+
end
|
77
|
+
|
78
|
+
it "supports internal arrays" do
|
79
|
+
@column.insert(["str1", ["str2"]])
|
80
|
+
Item.all.should == ["str1", "str2"]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
it "date" do
|
85
|
+
column = new_table(:items){|t| t.date :column}.column
|
86
|
+
date = Date.today
|
87
|
+
|
88
|
+
column.insert(date)
|
89
|
+
Item.all.should == [date]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "time" do
|
93
|
+
column = new_table(:items){|t| t.time :column}.column
|
94
|
+
time = Time.now
|
95
|
+
|
96
|
+
column.insert(time)
|
97
|
+
Item.all.first.strftime("%H:%M:%S").should == time.strftime("%H:%M:%S")
|
98
|
+
end
|
99
|
+
it "datetime" do
|
100
|
+
column = new_table(:items){|t| t.datetime :column}.column
|
101
|
+
datetime = DateTime.now
|
102
|
+
|
103
|
+
column.insert(datetime)
|
104
|
+
Item.all.first.strftime("%y-%m-%d %H:%M:%S").should == datetime.strftime("%y-%m-%d %H:%M:%S")
|
105
|
+
end
|
106
|
+
|
107
|
+
it "nil" do
|
108
|
+
column = new_table(:items){|t| t.string :column}.column
|
109
|
+
column.insert(nil)
|
110
|
+
|
111
|
+
Item.all.should == [nil]
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
it :update do
|
116
|
+
new_table(:items){|t| t.integer :column}.column.insert(1..3).update(3)
|
117
|
+
Item.all.should == [3, 3, 3]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "last exception" do
|
122
|
+
it "equals nil if operation successed" do
|
123
|
+
column = new_table(:items){|t| t.string :column}.column
|
124
|
+
column.insert("value")
|
125
|
+
|
126
|
+
column.last_exception.should be_blank
|
127
|
+
end
|
128
|
+
|
129
|
+
it "equals last exception if something wrong" do
|
130
|
+
column = new_table(:items){|t| t.string :column}.column
|
131
|
+
db.drop_table(:items)
|
132
|
+
column.insert("value")
|
133
|
+
|
134
|
+
column.last_exception.should_not be_blank
|
135
|
+
end
|
136
|
+
|
137
|
+
it "clears before each new request" do
|
138
|
+
column = new_table(:items){|t| t.string :column}.column
|
139
|
+
db.drop_table(:items)
|
140
|
+
column.insert("value")
|
141
|
+
|
142
|
+
new_table(:items){|t| t.string :column}
|
143
|
+
column.insert("value")
|
144
|
+
|
145
|
+
column.last_exception.should be_blank
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Spec::Support::DB, "supports", :type => :mv_test do
|
4
|
+
it "connect to the memory db (sqlite in memory)" do
|
5
|
+
use_memory_db
|
6
|
+
|
7
|
+
ActiveRecord::Base.connection.should_not be_blank
|
8
|
+
end
|
9
|
+
|
10
|
+
it "shortcut to active record connection" do
|
11
|
+
use_memory_db
|
12
|
+
|
13
|
+
db.should == ActiveRecord::Base.connection
|
14
|
+
end
|
15
|
+
|
16
|
+
it "migration shortcut" do
|
17
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
18
|
+
|
19
|
+
migrate do
|
20
|
+
create_table :test_table do |t|
|
21
|
+
t.string :str_column
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
db.table_exists?(:test_table).should be_true
|
26
|
+
end
|
27
|
+
|
28
|
+
it "create table shortcut" do
|
29
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
30
|
+
|
31
|
+
new_table :test_table do |t|
|
32
|
+
t.string :str_column
|
33
|
+
end
|
34
|
+
|
35
|
+
db.table_exists?(:test_table).should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
it "change table shortcut" do
|
39
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
40
|
+
new_table {|t| t.string :str_column }
|
41
|
+
|
42
|
+
chg_table :test_table do |t|
|
43
|
+
t.string :str_column_1
|
44
|
+
end
|
45
|
+
|
46
|
+
db.column_exists?(:test_table, :str_column_1).should be_true
|
47
|
+
end
|
48
|
+
|
49
|
+
it "shortcut for table info" do
|
50
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
51
|
+
new_table {|t| t.string :str_column }
|
52
|
+
|
53
|
+
table(:test_table).should be_kind_of(MigrationValidators::Spec::Support::TableWrapper)
|
54
|
+
table(:test_table).table_name.should == :test_table
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Spec::Support::TableWrapper, "supports", :type => :mv_test do
|
4
|
+
before :each do
|
5
|
+
use_memory_db
|
6
|
+
|
7
|
+
db.drop_table(:test_table) if db.table_exists?(:test_table)
|
8
|
+
@wrapper = new_table {|t| t.string :str_column }
|
9
|
+
end
|
10
|
+
|
11
|
+
it "column information requests" do
|
12
|
+
@wrapper.str_column.column_name.should == :str_column
|
13
|
+
end
|
14
|
+
|
15
|
+
it "supports dropping" do
|
16
|
+
@wrapper.drop
|
17
|
+
|
18
|
+
db.table_exists?(:test_table).should be_false
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'mv-test'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mv-test
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Valeriy Prokopchuk
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-03-22 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activerecord
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.3.5
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.3.0
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: bundler
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.0.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: jeweler
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.5.2
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: rcov
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
description: Contains macros / matchers for database behavious testing
|
72
|
+
email: vprokopchuk@gmail.com
|
73
|
+
executables: []
|
74
|
+
|
75
|
+
extensions: []
|
76
|
+
|
77
|
+
extra_rdoc_files: []
|
78
|
+
|
79
|
+
files:
|
80
|
+
- lib/migration_validators/spec/macros/db_macros.rb
|
81
|
+
- lib/migration_validators/spec/matchers/db_matchers.rb
|
82
|
+
- lib/migration_validators/spec/support/column_wrapper.rb
|
83
|
+
- lib/migration_validators/spec/support/db.rb
|
84
|
+
- lib/migration_validators/spec/support/table_wrapper.rb
|
85
|
+
- lib/migration_validators/spec/support/test_adapter.rb
|
86
|
+
- lib/mv-test.rb
|
87
|
+
- spec/migration_validators/spec/macros/db_macros_spec.rb
|
88
|
+
- spec/migration_validators/spec/matchers/db_matchers_spec.rb
|
89
|
+
- spec/migration_validators/spec/support/column_wrapper_spec.rb
|
90
|
+
- spec/migration_validators/spec/support/db_spec.rb
|
91
|
+
- spec/migration_validators/spec/support/table_wrapper_spec.rb
|
92
|
+
- spec/migration_validators/spec/support/test_adapter_spec.rb
|
93
|
+
- spec/mv-test_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
has_rdoc: true
|
96
|
+
homepage: http://github.com/vprokochuk256/mv-test
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ">="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 113089354217665216
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
version: "0"
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: "0"
|
119
|
+
requirements: []
|
120
|
+
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 1.6.2
|
123
|
+
signing_key:
|
124
|
+
specification_version: 3
|
125
|
+
summary: Migration Validators project test suite
|
126
|
+
test_files:
|
127
|
+
- spec/migration_validators/spec/macros/db_macros_spec.rb
|
128
|
+
- spec/migration_validators/spec/matchers/db_matchers_spec.rb
|
129
|
+
- spec/migration_validators/spec/support/column_wrapper_spec.rb
|
130
|
+
- spec/migration_validators/spec/support/db_spec.rb
|
131
|
+
- spec/migration_validators/spec/support/table_wrapper_spec.rb
|
132
|
+
- spec/migration_validators/spec/support/test_adapter_spec.rb
|
133
|
+
- spec/mv-test_spec.rb
|
134
|
+
- spec/spec_helper.rb
|