mv-core 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/LICENSE.txt +20 -0
- data/README.rdoc +88 -0
- data/lib/migration_validators/active_record/base.rb +24 -0
- data/lib/migration_validators/active_record/connection_adapters/abstract_adapter.rb +36 -0
- data/lib/migration_validators/active_record/connection_adapters/native_adapter.rb +118 -0
- data/lib/migration_validators/active_record/connection_adapters/table.rb +15 -0
- data/lib/migration_validators/active_record/connection_adapters/table_definition.rb +24 -0
- data/lib/migration_validators/active_record/migration.rb +29 -0
- data/lib/migration_validators/active_record/schema.rb +31 -0
- data/lib/migration_validators/active_record/schema_dumper.rb +24 -0
- data/lib/migration_validators/adapters/base.rb +15 -0
- data/lib/migration_validators/adapters/containers.rb +102 -0
- data/lib/migration_validators/adapters/routing.rb +104 -0
- data/lib/migration_validators/adapters/syntax.rb +53 -0
- data/lib/migration_validators/adapters/validator_definitions.rb +131 -0
- data/lib/migration_validators/core/adapter_wrapper.rb +88 -0
- data/lib/migration_validators/core/db_validator.rb +178 -0
- data/lib/migration_validators/core/statement_builder.rb +60 -0
- data/lib/migration_validators/core/validator_container.rb +110 -0
- data/lib/migration_validators/core/validator_definition.rb +91 -0
- data/lib/migration_validators/core/validator_router.rb +45 -0
- data/lib/mv-core.rb +100 -0
- data/lib/options.rb +7 -0
- data/spec/migration_validators/active_record/connection_adapters/abstract_adapter_spec.rb +440 -0
- data/spec/migration_validators/active_record/connection_adapters/table_definition_spec.rb +4 -0
- data/spec/migration_validators/active_record/migration.rb +82 -0
- data/spec/migration_validators/active_record/schema_dumper_spec.rb +44 -0
- data/spec/migration_validators/adapters/base_spec.rb +89 -0
- data/spec/migration_validators/core/adapter_wrapper_spec.rb +168 -0
- data/spec/migration_validators/core/db_validator_spec.rb +347 -0
- data/spec/migration_validators/core/statement_builder_spec.rb +36 -0
- data/spec/migration_validators/core/validator_container_spec.rb +121 -0
- data/spec/migration_validators/core/validator_definition_spec.rb +131 -0
- data/spec/migration_validators/core/validator_router_spec.rb +60 -0
- data/spec/mv-core_spec.rb +4 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/support/factories/db_validator.rb +43 -0
- metadata +152 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
Migration = ActiveRecord::Migration
|
4
|
+
|
5
|
+
class TestMigration < Migration, :type => :mv_test
|
6
|
+
def self.up
|
7
|
+
create_table :migration_test_table do |t|
|
8
|
+
t.string :column_str, :validates => {:uniqueness => true}
|
9
|
+
t.integer :column_int, :validates => {:uniqueness => true}
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :migration_test_table_1 do |t1|
|
13
|
+
t1.string :col
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
validate_column :migration_test_table, :column_str, :uniqueness => false
|
19
|
+
validate_column :migration_test_table, :column_int, :uniqueness => true
|
20
|
+
|
21
|
+
drop_table :migration_test_table_1
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe Migration, "migration validators extension" do
|
26
|
+
before :all do
|
27
|
+
ActiveRecord::Migration.verbose = false
|
28
|
+
use_memory_db
|
29
|
+
end
|
30
|
+
|
31
|
+
before :each do
|
32
|
+
TestAdapter.clear
|
33
|
+
TestAdapter.stub_validate_method :uniqueness
|
34
|
+
TestAdapter.stub_remove_validate_method :uniqueness
|
35
|
+
|
36
|
+
MigrationValidators.adapter = TestAdapter.new
|
37
|
+
DbValidator.rollback
|
38
|
+
db.drop_table(:migration_test_table) if DB.table_exists?(:migration_test_table)
|
39
|
+
db.drop_table(:migration_test_table_1) if DB.table_exists?(:migration_test_table_1)
|
40
|
+
TestMigration.migrate :up
|
41
|
+
end
|
42
|
+
|
43
|
+
describe :migrate do
|
44
|
+
describe :up do
|
45
|
+
it "does ordinary :up migration" do
|
46
|
+
db.table_exists?(:migration_test_table).should be_true
|
47
|
+
end
|
48
|
+
|
49
|
+
it "creates all defined validators" do
|
50
|
+
DbValidator.column_validators(:migration_test_table, :column_str).should_not be_blank
|
51
|
+
end
|
52
|
+
|
53
|
+
it "call adapter validator creation methods" do
|
54
|
+
TestAdapter.log[:validate_uniqueness].should_not be_blank
|
55
|
+
TestAdapter.log[:remove_validate_uniqueness].should_not be_blank
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe :down do
|
60
|
+
before :each do
|
61
|
+
TestMigration.migrate :down
|
62
|
+
end
|
63
|
+
|
64
|
+
it "does ordinary :down migration" do
|
65
|
+
db.table_exists?(:migration_test_table_1).should be_false
|
66
|
+
end
|
67
|
+
|
68
|
+
it "creates all defined validators" do
|
69
|
+
DbValidator.column_validators(:migration_test_table, :column_int).should_not be_blank
|
70
|
+
end
|
71
|
+
|
72
|
+
it "removes all removed validators" do
|
73
|
+
DbValidator.column_validators(:migration_test_table, :column_str).should be_blank
|
74
|
+
end
|
75
|
+
|
76
|
+
it "call adapter validator creation methods" do
|
77
|
+
TestAdapter.log[:validate_uniqueness].should_not be_blank
|
78
|
+
TestAdapter.log[:remove_validate_uniqueness].should_not be_blank
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::ActiveRecord::SchemaDumper, "migration validators extension", :type => :mv_test do
|
4
|
+
before :all do
|
5
|
+
ActiveRecord::Migration.verbose = false
|
6
|
+
use_memory_db
|
7
|
+
db.initialize_schema_migrations_table
|
8
|
+
end
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
MigrationValidators::Core::DbValidator.clear_all
|
12
|
+
db.drop_table :test_table if db.table_exists?(:test_table)
|
13
|
+
|
14
|
+
db.create_table :test_table do |t|
|
15
|
+
t.string :str_column, :validates => {:length => {:in => [1..5], :message => :SomeErrorMessage} }
|
16
|
+
end
|
17
|
+
|
18
|
+
MigrationValidators::Core::DbValidator.commit
|
19
|
+
|
20
|
+
|
21
|
+
schema = StringIO.new
|
22
|
+
::ActiveRecord::SchemaDumper.dump(db, schema)
|
23
|
+
|
24
|
+
MigrationValidators::Core::DbValidator.clear_all
|
25
|
+
|
26
|
+
db.drop_table :test_table
|
27
|
+
|
28
|
+
db.instance_eval(schema.string)
|
29
|
+
|
30
|
+
MigrationValidators::Core::DbValidator.commit
|
31
|
+
|
32
|
+
@validators = MigrationValidators::Core::DbValidator.table_validators "test_table"
|
33
|
+
end
|
34
|
+
|
35
|
+
describe :dump do
|
36
|
+
it "validator info" do
|
37
|
+
@validators.length.should == 1
|
38
|
+
@validators.first.validator_name.should == "length"
|
39
|
+
@validators.first.column_name.should == "str_column"
|
40
|
+
@validators.first.options.should == {:in => [1..5], :message => :SomeErrorMessage}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Adapters::Base, :type => :mv_test do
|
4
|
+
before :all do
|
5
|
+
Driver = Class.new(MigrationValidators::Adapters::Base)
|
6
|
+
use_memory_db
|
7
|
+
db.initialize_schema_migrations_table
|
8
|
+
end
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
@validator = Factory.build :db_validator,
|
12
|
+
:validator_name => :validator_name,
|
13
|
+
:table_name => :table_name,
|
14
|
+
:column_name => :column_name,
|
15
|
+
:options => {:property_name => :property_value}
|
16
|
+
|
17
|
+
Driver.syntax do
|
18
|
+
operation :some_interesting_operation do |stmt, value|
|
19
|
+
"#{stmt} SOME_INTERESTING_OPERATION #{value}"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Driver.validator :validator_name do
|
24
|
+
property :property_name do |property_value|
|
25
|
+
column.some_interesting_operation(property_value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
Driver.container :container_name do
|
30
|
+
operation :some_interesting_operation do |stmt, value|
|
31
|
+
"#{stmt} SOME_INTERESTING_CONTAINER_OPERATION #{value}"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe :syntax do
|
37
|
+
it "allows to define basic operations syntax" do
|
38
|
+
Driver.syntax.some_interesting_operation(:value).to_s.should == " SOME_INTERESTING_OPERATION value"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe :validator do
|
43
|
+
it "allows to define validator definition" do
|
44
|
+
Driver.validators[:validator_name].should_not be_blank
|
45
|
+
Driver.validators[:validator_name].process(@validator).should == ["column_name SOME_INTERESTING_OPERATION property_value"]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe :container do
|
50
|
+
it "allows to define validator definition" do
|
51
|
+
Driver.containers[:container_name].should_not be_blank
|
52
|
+
Driver.containers[:container_name].add_validators([@validator]).should == ["column_name SOME_INTERESTING_CONTAINER_OPERATION property_value"]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe :route do
|
57
|
+
before :each do
|
58
|
+
Driver.clear_routing
|
59
|
+
end
|
60
|
+
|
61
|
+
it "allows to define routing method" do
|
62
|
+
Driver.route :validator_name, :container do
|
63
|
+
to :container_name
|
64
|
+
end
|
65
|
+
|
66
|
+
Driver.public_instance_methods.include?(:validate_validator_name_container).should be_true
|
67
|
+
Driver.public_instance_methods.include?(:remove_validate_validator_name_container).should be_true
|
68
|
+
Driver.public_instance_methods.include?(:validate_validator_name).should be_false
|
69
|
+
Driver.public_instance_methods.include?(:remove_validate_validator_name).should be_false
|
70
|
+
end
|
71
|
+
|
72
|
+
it "allows to deny remove method" do
|
73
|
+
Driver.route :validator_name, :container, :remove => false do
|
74
|
+
to :container_name
|
75
|
+
end
|
76
|
+
|
77
|
+
Driver.public_instance_methods.include?(:remove_validate_validator_name_container).should be_false
|
78
|
+
end
|
79
|
+
|
80
|
+
it "allows to define default methods" do
|
81
|
+
Driver.route :validator_name, :container, :default => true do
|
82
|
+
to :container_name
|
83
|
+
end
|
84
|
+
|
85
|
+
Driver.public_instance_methods.include?(:validate_validator_name).should be_true
|
86
|
+
Driver.public_instance_methods.include?(:remove_validate_validator_name).should be_true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
describe MigrationValidators::Core::AdapterWrapper, :type => :mv_test do
|
4
|
+
before :each do
|
5
|
+
use_memory_db
|
6
|
+
db.initialize_schema_migrations_table
|
7
|
+
MigrationValidators::Spec::Support::TestAdapter.clear
|
8
|
+
@wrapper = MigrationValidators::Core::AdapterWrapper.new MigrationValidators::Spec::Support::TestAdapter.new
|
9
|
+
end
|
10
|
+
|
11
|
+
describe :create_validators do
|
12
|
+
it "calls driver method if such validator is supported" do
|
13
|
+
validator = Factory.build :uniqueness_check
|
14
|
+
|
15
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :uniqueness, :check
|
16
|
+
@wrapper.create_validators [validator]
|
17
|
+
|
18
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:validate_uniqueness_check].should_not be_blank
|
19
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:validate_uniqueness_check].first.should == [validator]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "groups validators by table_name, validator_name and db form" do
|
23
|
+
validator1 = Factory.build :presense_check, :column_name => :column_name_1
|
24
|
+
validator2 = Factory.build :presense_check, :column_name => :column_name_2
|
25
|
+
|
26
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :presense, :check
|
27
|
+
@wrapper.create_validators [validator1, validator2]
|
28
|
+
|
29
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:validate_presense_check].first.should == [validator1, validator2]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "calls driver method until all validatos are created if not all validators were handled" do
|
33
|
+
validator1 = Factory.build :uniqueness_check, :column_name => :column_name_1
|
34
|
+
validator2 = Factory.build :uniqueness_check, :column_name => :column_name_2
|
35
|
+
|
36
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :uniqueness, :check do |validators|
|
37
|
+
[validators.first]
|
38
|
+
end
|
39
|
+
|
40
|
+
@wrapper.create_validators [validator1, validator2]
|
41
|
+
|
42
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:validate_uniqueness_check].first.should == [validator1, validator2]
|
43
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:validate_uniqueness_check].last.should == [validator2]
|
44
|
+
end
|
45
|
+
|
46
|
+
it "raises an exception is driver does not support specified validator" do
|
47
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :presense, :check
|
48
|
+
|
49
|
+
validator = Factory.build :uniqueness_check
|
50
|
+
|
51
|
+
lambda {
|
52
|
+
@wrapper.create_validators [validator]
|
53
|
+
}.should raise_error MigrationValidators::MigrationValidatorsException, /Action 'validate' for 'uniqueness' is not supported. Available validators: \['presense'\]/
|
54
|
+
end
|
55
|
+
|
56
|
+
it "raises an exception if driver does not support default db form for specified validator" do
|
57
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :presense, :check
|
58
|
+
|
59
|
+
validator = Factory.build :presense
|
60
|
+
|
61
|
+
lambda {
|
62
|
+
@wrapper.create_validators [validator]
|
63
|
+
}.should raise_error MigrationValidators::MigrationValidatorsException, /Action 'validate' for 'presense' with default db form is not supported/
|
64
|
+
end
|
65
|
+
|
66
|
+
it "raises an exception is driver does not support specified validator in specified db form" do
|
67
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :uniqueness, :trigger
|
68
|
+
|
69
|
+
validator = Factory.build :uniqueness_check
|
70
|
+
|
71
|
+
lambda {
|
72
|
+
@wrapper.create_validators [validator]
|
73
|
+
}.should raise_error MigrationValidators::MigrationValidatorsException, /Action 'validate' for db form 'check' for validator 'uniqueness' is not supported. Available db forms: \['trigger'\]/
|
74
|
+
end
|
75
|
+
|
76
|
+
it "handles omitted db form" do
|
77
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_validate_method :uniqueness
|
78
|
+
|
79
|
+
validator = Factory.build :uniqueness
|
80
|
+
|
81
|
+
lambda {
|
82
|
+
@wrapper.create_validators [validator]
|
83
|
+
}.should_not raise_error
|
84
|
+
|
85
|
+
|
86
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:validate_uniqueness].first.should == [validator]
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe :remove_validators do
|
91
|
+
it "calls driver method if such validator remove is supported" do
|
92
|
+
validator = Factory.build :uniqueness_check
|
93
|
+
|
94
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :uniqueness, :check
|
95
|
+
@wrapper.remove_validators [validator]
|
96
|
+
|
97
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:remove_validate_uniqueness_check].should_not be_blank
|
98
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:remove_validate_uniqueness_check].first.should == [validator]
|
99
|
+
end
|
100
|
+
|
101
|
+
it "groups validators by table_name, validator_name and db form" do
|
102
|
+
validator1 = Factory.build :presense_check, :column_name => :column_name_1
|
103
|
+
validator2 = Factory.build :presense_check, :column_name => :column_name_2
|
104
|
+
|
105
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :presense, :check
|
106
|
+
@wrapper.remove_validators [validator1, validator2]
|
107
|
+
|
108
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:remove_validate_presense_check].first.should == [validator1, validator2]
|
109
|
+
end
|
110
|
+
|
111
|
+
it "calls driver method until all validatos are removed if not all validators were handled" do
|
112
|
+
validator1 = Factory.build :uniqueness_check, :column_name => :column_name_1
|
113
|
+
validator2 = Factory.build :uniqueness_check, :column_name => :column_name_2
|
114
|
+
|
115
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :uniqueness, :check do |validators|
|
116
|
+
[validators.first]
|
117
|
+
end
|
118
|
+
|
119
|
+
@wrapper.remove_validators [validator1, validator2]
|
120
|
+
|
121
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:remove_validate_uniqueness_check].first.should == [validator1, validator2]
|
122
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:remove_validate_uniqueness_check].last.should == [validator2]
|
123
|
+
end
|
124
|
+
|
125
|
+
it "raises an exception is driver does not support removing of the specified validator" do
|
126
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :presense, :check
|
127
|
+
|
128
|
+
validator = Factory.build :uniqueness_check
|
129
|
+
|
130
|
+
lambda {
|
131
|
+
@wrapper.remove_validators [validator]
|
132
|
+
}.should raise_error MigrationValidators::MigrationValidatorsException, /Action 'remove_validate' for 'uniqueness' is not supported. Available validators: \['presense'\]/
|
133
|
+
end
|
134
|
+
|
135
|
+
it "raises an exception if driver does not support removing validator with in default db form" do
|
136
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :presense, :check
|
137
|
+
|
138
|
+
validator = Factory.build :presense
|
139
|
+
|
140
|
+
lambda {
|
141
|
+
@wrapper.remove_validators [validator]
|
142
|
+
}.should raise_error MigrationValidators::MigrationValidatorsException, /Action 'remove_validate' for 'presense' with default db form is not supported/
|
143
|
+
end
|
144
|
+
|
145
|
+
it "raises an exception is driver does not support specified removing validator in specified db form" do
|
146
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :uniqueness, :trigger
|
147
|
+
|
148
|
+
validator = Factory.build :uniqueness_check
|
149
|
+
|
150
|
+
lambda {
|
151
|
+
@wrapper.remove_validators [validator]
|
152
|
+
}.should raise_error MigrationValidators::MigrationValidatorsException, /Action 'remove_validate' for db form 'check' for validator 'uniqueness' is not supported. Available db forms: \['trigger'\]/
|
153
|
+
end
|
154
|
+
|
155
|
+
it "handles omitted db form" do
|
156
|
+
MigrationValidators::Spec::Support::TestAdapter.stub_remove_validate_method :uniqueness
|
157
|
+
|
158
|
+
validator = Factory.build :uniqueness
|
159
|
+
|
160
|
+
lambda {
|
161
|
+
@wrapper.remove_validators [validator]
|
162
|
+
}.should_not raise_error
|
163
|
+
|
164
|
+
|
165
|
+
MigrationValidators::Spec::Support::TestAdapter.log[:remove_validate_uniqueness].first.should == [validator]
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
@@ -0,0 +1,347 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
|
2
|
+
|
3
|
+
|
4
|
+
describe MigrationValidators::Core::DbValidator, :type => :mv_test do
|
5
|
+
before :all do
|
6
|
+
use_memory_db
|
7
|
+
db.initialize_schema_migrations_table
|
8
|
+
end
|
9
|
+
|
10
|
+
before :each do
|
11
|
+
MigrationValidators::Core::DbValidator.delete_all
|
12
|
+
MigrationValidators::Core::DbValidator.rollback
|
13
|
+
|
14
|
+
db.create_table(:table_name) do |t|
|
15
|
+
t.string :column_name
|
16
|
+
t.string :column_name_1
|
17
|
+
t.string :column_name_2
|
18
|
+
t.string :column_name_3
|
19
|
+
t.string :column_name_4
|
20
|
+
t.string :column_name_5
|
21
|
+
t.string :column_name_6
|
22
|
+
t.string :column_name_7
|
23
|
+
t.string :column_name_8
|
24
|
+
t.string :column_name_9
|
25
|
+
t.string :column_name_10
|
26
|
+
t.string :column_name_11
|
27
|
+
end unless db.table_exists?(:table_name)
|
28
|
+
end
|
29
|
+
|
30
|
+
subject do
|
31
|
+
Factory.create :db_validator, :table_name => :table_name
|
32
|
+
end
|
33
|
+
|
34
|
+
it { should have_db_column(:table_name).of_type(:string).with_options(:length => 255, :null => false) }
|
35
|
+
it { should validate_presence_of(:table_name) }
|
36
|
+
it { should have_db_index(:table_name)}
|
37
|
+
it { should ensure_length_of(:table_name).is_at_most(255) }
|
38
|
+
|
39
|
+
it { should have_db_column(:column_name).of_type(:string).with_options(:length => 255, :null => true) }
|
40
|
+
it { should ensure_length_of(:column_name).is_at_most(255) }
|
41
|
+
|
42
|
+
|
43
|
+
it { should have_db_column(:options).of_type(:text)}
|
44
|
+
it { should have_db_column(:constraints).of_type(:text)}
|
45
|
+
|
46
|
+
|
47
|
+
it { should have_db_column(:validator_name).of_type(:string).with_options(:length => 255, :null => false) }
|
48
|
+
it { should validate_presence_of(:validator_name) }
|
49
|
+
it { should ensure_length_of(:validator_name).is_at_most(255) }
|
50
|
+
|
51
|
+
it "should support read only name property composed of attributes" do
|
52
|
+
validator = Factory.create :db_validator, :table_name => :table_name
|
53
|
+
|
54
|
+
validator.name.should == "#{validator.table_name}_#{validator.column_name}_#{validator.validator_name}"
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "Error message" do
|
58
|
+
it "is made of validator name, table_name and colum name if special message not defined" do
|
59
|
+
validator = Factory.build :db_validator, :table_name => :table_name
|
60
|
+
|
61
|
+
validator.error_message.should == "#{validator.validator_name} violated for #{validator.table_name} field #{validator.column_name}"
|
62
|
+
end
|
63
|
+
|
64
|
+
it "might be re - defined" do
|
65
|
+
validator = Factory.build :db_validator, :table_name => :table_name, :options => {:message => "Custom message"}
|
66
|
+
|
67
|
+
validator.error_message.should == 'Custom message'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should support options serialization" do
|
72
|
+
db_validator = Factory.build :db_validator, :table_name => :table_name
|
73
|
+
|
74
|
+
db_validator.options = {:message => "some message"}
|
75
|
+
|
76
|
+
db_validator.save!
|
77
|
+
|
78
|
+
db_validator = MigrationValidators::Core::DbValidator.find(db_validator.id)
|
79
|
+
|
80
|
+
db_validator.options[:message].should == "some message"
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should support containers serialization" do
|
84
|
+
db_validator = Factory.build :db_validator, :table_name => :table_name
|
85
|
+
|
86
|
+
db_validator.constraints = [:some_constraint]
|
87
|
+
|
88
|
+
db_validator.save!
|
89
|
+
|
90
|
+
db_validator = MigrationValidators::Core::DbValidator.find(db_validator.id)
|
91
|
+
|
92
|
+
db_validator.constraints.should == [:some_constraint]
|
93
|
+
end
|
94
|
+
|
95
|
+
|
96
|
+
describe "helper methods" do
|
97
|
+
before :each do
|
98
|
+
|
99
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name", :option_name => :option_value
|
100
|
+
MigrationValidators::Core::DbValidator.commit
|
101
|
+
|
102
|
+
@db_validator = MigrationValidators::Core::DbValidator.first
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "validators update operations" do
|
106
|
+
describe :add_column_validator do
|
107
|
+
describe "creates new record in the validators table" do
|
108
|
+
it "with correct name" do
|
109
|
+
@db_validator.name.should == "table_name_column_name_validator_name"
|
110
|
+
end
|
111
|
+
|
112
|
+
it "with correct table_name" do
|
113
|
+
@db_validator.table_name.should == "table_name"
|
114
|
+
end
|
115
|
+
|
116
|
+
it "with correct column_name" do
|
117
|
+
@db_validator.column_name.should == "column_name"
|
118
|
+
end
|
119
|
+
|
120
|
+
it "with correct validator_name" do
|
121
|
+
@db_validator.validator_name.should == "validator_name"
|
122
|
+
end
|
123
|
+
|
124
|
+
it "with correct options" do
|
125
|
+
@db_validator.options.length.should == 1
|
126
|
+
@db_validator.options[:option_name].should == :option_value
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "checks table and column existence" do
|
131
|
+
it "raises an error if table does not exists" do
|
132
|
+
lambda {
|
133
|
+
MigrationValidators::Core::DbValidator.add_column_validator "wrong_table", "column_name", "validator_name", :option_name => :option_value
|
134
|
+
MigrationValidators::Core::DbValidator.commit
|
135
|
+
}.should raise_error /table 'wrong_table' does not exist/
|
136
|
+
end
|
137
|
+
|
138
|
+
it "raises an error if column does not exists" do
|
139
|
+
lambda {
|
140
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "wrong_column_name", "validator_name", :option_name => :option_value
|
141
|
+
MigrationValidators::Core::DbValidator.commit
|
142
|
+
}.should raise_error /column 'wrong_column_name' does not exist in the table 'table_name'/
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
it "updates existing validator" do
|
147
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name", :option_name1 => :option_value1
|
148
|
+
MigrationValidators::Core::DbValidator.commit
|
149
|
+
|
150
|
+
|
151
|
+
MigrationValidators::Core::DbValidator.first.options[:option_name1].should == :option_value1
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe :remove_column_validator do
|
156
|
+
it "removes validatro with specified parameters" do
|
157
|
+
MigrationValidators::Core::DbValidator.remove_column_validator "table_name", "column_name", "validator_name"
|
158
|
+
MigrationValidators::Core::DbValidator.commit
|
159
|
+
|
160
|
+
MigrationValidators::Core::DbValidator.count.should be_zero
|
161
|
+
end
|
162
|
+
|
163
|
+
it "works well with symbols" do
|
164
|
+
MigrationValidators::Core::DbValidator.remove_column_validator "table_name", :column_name, :validator_name
|
165
|
+
MigrationValidators::Core::DbValidator.commit
|
166
|
+
|
167
|
+
MigrationValidators::Core::DbValidator.count.should be_zero
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe :remove_column_validators do
|
172
|
+
it "removes all validators that are defined for the specified column" do
|
173
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name1", :option_name => :option_value
|
174
|
+
MigrationValidators::Core::DbValidator.commit
|
175
|
+
|
176
|
+
MigrationValidators::Core::DbValidator.remove_column_validators "table_name", "column_name"
|
177
|
+
MigrationValidators::Core::DbValidator.commit
|
178
|
+
|
179
|
+
MigrationValidators::Core::DbValidator.count.should be_zero
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
describe :rename_column do
|
184
|
+
it "updates db validators" do
|
185
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name1", :option_name => :option_value
|
186
|
+
MigrationValidators::Core::DbValidator.commit
|
187
|
+
|
188
|
+
MigrationValidators::Core::DbValidator.rename_column "table_name", "column_name", "column_name_1"
|
189
|
+
MigrationValidators::Core::DbValidator.commit
|
190
|
+
|
191
|
+
MigrationValidators::Core::DbValidator.column_validators("table_name", "column_name_1").should_not be_blank
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
describe :rename_table do
|
196
|
+
it "updates db validators" do
|
197
|
+
MigrationValidators::Core::DbValidator.rename_table "table_name", "new_table_name"
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
describe :remove_table_validators do
|
202
|
+
it "removes all validators that are defined for the specified table" do
|
203
|
+
MigrationValidators::Core::DbValidator.remove_table_validators "table_name"
|
204
|
+
MigrationValidators::Core::DbValidator.commit
|
205
|
+
|
206
|
+
MigrationValidators::Core::DbValidator.count.should == 0
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe :satisfies do
|
212
|
+
before :each do
|
213
|
+
@validator = Factory.build(:db_validator, :options => {:property_name => :property_value})
|
214
|
+
end
|
215
|
+
|
216
|
+
it "returns false if the validator has different property values than specified" do
|
217
|
+
@validator.satisfies(:property_name => :property_value).should be_true
|
218
|
+
end
|
219
|
+
|
220
|
+
it "returns true if values of the specified property values are equals to validator's ones" do
|
221
|
+
@validator = Factory.build(:db_validator, :options => {:property_name => :property_value})
|
222
|
+
end
|
223
|
+
|
224
|
+
it "returns false if at least one specified property is not defined in validator's options" do
|
225
|
+
@validator = Factory.build(:db_validator, :options => {:property_name => :property_value})
|
226
|
+
end
|
227
|
+
|
228
|
+
it "returns true if empty was specified" do
|
229
|
+
@validator = Factory.build(:db_validator, :options => {:property_name => :property_value})
|
230
|
+
end
|
231
|
+
|
232
|
+
it "allows arrays of possible values to be spesified" do
|
233
|
+
@validator.satisfies(:property_name => [:property_value, :property_value1]).should be_true
|
234
|
+
@validator.satisfies(:property_name1 => [:property_value, :property_value1]).should be_false
|
235
|
+
@validator.satisfies(:property_name1 => [nil, :property_value1]).should be_true
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
describe :table_validators do
|
240
|
+
before :each do
|
241
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name_1", "validator_name", :option_name => :option_value_1
|
242
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name_2", "validator_name", :option_name => :option_value_2
|
243
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name_3", "validator_name", {}
|
244
|
+
MigrationValidators::Core::DbValidator.commit
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should return all validators that are defined for the specified table" do
|
248
|
+
MigrationValidators::Core::DbValidator.table_validators("table_name").length.should == 4
|
249
|
+
end
|
250
|
+
|
251
|
+
it "allows to define options filter for the selected validators" do
|
252
|
+
MigrationValidators::Core::DbValidator.table_validators("table_name", :option_name => :option_value_1).should == MigrationValidators::Core::DbValidator.column_validators(:table_name, :column_name_1)
|
253
|
+
end
|
254
|
+
|
255
|
+
it "allows to define nil in properties filter" do
|
256
|
+
MigrationValidators::Core::DbValidator.table_validators("table_name", :option_name => [nil, :option_value_1]).should == MigrationValidators::Core::DbValidator.column_validators(:table_name, :column_name_1) + MigrationValidators::Core::DbValidator.column_validators(:table_name, :column_name_3)
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
describe :column_validators do
|
261
|
+
before :each do
|
262
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name_1", :option_name => :option_value_1
|
263
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name_2", :option_name => :option_value_2
|
264
|
+
MigrationValidators::Core::DbValidator.add_column_validator "table_name", "column_name", "validator_name_3", {}
|
265
|
+
MigrationValidators::Core::DbValidator.commit
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should return all validators that are defined for the specified column" do
|
269
|
+
MigrationValidators::Core::DbValidator.column_validators("table_name", "column_name").length.should == 4
|
270
|
+
end
|
271
|
+
|
272
|
+
it "allows to define options filter for the selected validators" do
|
273
|
+
column_validators = MigrationValidators::Core::DbValidator.column_validators("table_name", "column_name", :option_name => :option_value_1)
|
274
|
+
|
275
|
+
column_validators.length.should == 1
|
276
|
+
column_validators.first.validator_name.should == "validator_name_1"
|
277
|
+
end
|
278
|
+
|
279
|
+
it "allows to define nil in properties filter" do
|
280
|
+
column_validators = MigrationValidators::Core::DbValidator.column_validators("table_name", "column_name", :option_name => [nil, :option_value_1])
|
281
|
+
|
282
|
+
column_validators.length.should == 2
|
283
|
+
column_validators.first.validator_name.should == "validator_name_1"
|
284
|
+
column_validators.last.validator_name.should == "validator_name_3"
|
285
|
+
end
|
286
|
+
end
|
287
|
+
|
288
|
+
describe :save_to_constraint do
|
289
|
+
it "updates validator constraints list with string representation of the specified constraint name" do
|
290
|
+
validator = Factory.create :db_validator, :table_name => :table_name
|
291
|
+
|
292
|
+
validator.save_to_constraint :constraint
|
293
|
+
|
294
|
+
validator.constraints.should == ["constraint"]
|
295
|
+
end
|
296
|
+
|
297
|
+
it "does nothing if such constraint already exists in the list" do
|
298
|
+
validator = Factory.create :db_validator, :table_name => :table_name
|
299
|
+
|
300
|
+
validator.save_to_constraint :constraint
|
301
|
+
validator.save_to_constraint :constraint
|
302
|
+
|
303
|
+
validator.constraints.should == ["constraint"]
|
304
|
+
end
|
305
|
+
end
|
306
|
+
|
307
|
+
describe :remove_from_constraint do
|
308
|
+
it "removes specified constraint name from the internal constraints list" do
|
309
|
+
validator = Factory.create :db_validator, :table_name => :table_name
|
310
|
+
validator.constraints = ["constraint"]
|
311
|
+
|
312
|
+
validator.remove_from_constraint :constraint
|
313
|
+
|
314
|
+
validator.constraints.should be_blank
|
315
|
+
end
|
316
|
+
|
317
|
+
it "does nothing if validator was not added to the constraint with the specified name" do
|
318
|
+
validator = Factory.create :db_validator, :table_name => :table_name
|
319
|
+
validator.constraints = nil
|
320
|
+
|
321
|
+
validator.remove_from_constraint :constraint
|
322
|
+
|
323
|
+
validator.constraints.should be_blank
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
describe :in_constraint? do
|
328
|
+
it "returns true if validator belongs to the constriant with specified name" do
|
329
|
+
validator = Factory.create :db_validator, :table_name => :table_name
|
330
|
+
|
331
|
+
validator.save_to_constraint :constraint
|
332
|
+
|
333
|
+
validator.in_constraint?(:constraint).should be_true
|
334
|
+
validator.in_constraint?("constraint").should be_true
|
335
|
+
end
|
336
|
+
end
|
337
|
+
|
338
|
+
describe :constraint_validators do
|
339
|
+
it "searches validators that were included to constraint with specified name" do
|
340
|
+
validator = Factory.create :db_validator, :table_name => :table_name, :column_name => :column_name, :constraints => ["constraint"]
|
341
|
+
validator1 = Factory.create :db_validator, :table_name => :table_name, :column_name => :column_name_1, :constraints => ["constraint1"]
|
342
|
+
|
343
|
+
MigrationValidators::Core::DbValidator.constraint_validators("constraint").to_a.should == [validator]
|
344
|
+
end
|
345
|
+
end
|
346
|
+
end
|
347
|
+
end
|