activevalidation 0.1.0 → 1.0.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.
- checksums.yaml +4 -4
- data/.gitignore +32 -9
- data/.overcommit.yml +93 -0
- data/.rspec +1 -1
- data/.rubocop.yml +80 -0
- data/.rubocop_todo.yml +7 -0
- data/.travis.yml +42 -2
- data/.yardops +9 -0
- data/Appraisals +27 -0
- data/Gemfile +24 -1
- data/MIT-LICENSE +20 -0
- data/README.md +142 -20
- data/Rakefile +21 -3
- data/activevalidation.gemspec +35 -25
- data/bin/console +4 -7
- data/gemfiles/am_5.0.gemfile +17 -0
- data/gemfiles/am_5.0.gemfile.lock +159 -0
- data/gemfiles/am_5.1.gemfile +17 -0
- data/gemfiles/am_5.1.gemfile.lock +159 -0
- data/gemfiles/am_5.2.gemfile +17 -0
- data/gemfiles/am_5.2.gemfile.lock +159 -0
- data/gemfiles/am_6.0.gemfile +18 -0
- data/gemfiles/am_6.0.gemfile.lock +158 -0
- data/lib/active_validation.rb +27 -0
- data/lib/active_validation/base_adapter.rb +103 -0
- data/lib/active_validation/configuration.rb +52 -0
- data/lib/active_validation/decorator.rb +27 -0
- data/lib/active_validation/decorators/consistent_registry.rb +36 -0
- data/lib/active_validation/decorators/disallows_duplicates_registry.rb +17 -0
- data/lib/active_validation/errors.rb +28 -0
- data/lib/active_validation/ext/add_active_validation_context_check.rb +21 -0
- data/lib/active_validation/formatters/manifest_name_formatter.rb +13 -0
- data/lib/active_validation/formatters/validation_context_formatter.rb +28 -0
- data/lib/active_validation/frameworks/rspec.rb +10 -0
- data/lib/active_validation/frameworks/rspec/helpers.rb +15 -0
- data/lib/active_validation/internal/models/check.rb +51 -0
- data/lib/active_validation/internal/models/concerns/to_internal.rb +27 -0
- data/lib/active_validation/internal/models/manifest.rb +122 -0
- data/lib/active_validation/internal/models/manifest/installer.rb +86 -0
- data/lib/active_validation/internal/observers/manifest.rb +114 -0
- data/lib/active_validation/model_extension_base.rb +33 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/adapter.rb +59 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/internals/active_validation/internal_model_extensions/check.rb +11 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/model_extension/active_validation/active_record_model_extension.rb +25 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/check.rb +31 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/concerns/method_must_be_allowed.rb +38 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/validate_method.rb +9 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/validates_method.rb +9 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/validates_with_method.rb +19 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/concerns/protect_from_mutable_instance_methods.rb +31 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/models/active_validation/manifest.rb +21 -0
- data/lib/active_validation/orm_plugins/active_record_plugin/types/active_validation/type/version.rb +17 -0
- data/lib/active_validation/registry.rb +55 -0
- data/lib/active_validation/values/base.rb +39 -0
- data/lib/active_validation/values/method_name.rb +22 -0
- data/lib/active_validation/values/version.rb +17 -0
- data/lib/active_validation/verifier.rb +150 -0
- data/lib/active_validation/version.rb +5 -0
- data/spec/active_validation/base_adapter_spec.rb +23 -0
- data/spec/active_validation/configuration_spec.rb +52 -0
- data/spec/active_validation/decorators/consistent_registry_spec.rb +117 -0
- data/spec/active_validation/decorators/disallows_duplicates_registry_spec.rb +21 -0
- data/spec/active_validation/formatters/manifest_name_formatter_spec.rb +7 -0
- data/spec/active_validation/formatters/validation_context_formatter_spec.rb +39 -0
- data/spec/active_validation/internal/models/check_spec.rb +67 -0
- data/spec/active_validation/internal/models/manifest/installer_spec.rb +177 -0
- data/spec/active_validation/internal/models/manifest_spec.rb +136 -0
- data/spec/active_validation/internal/observers/manifest_spec.rb +201 -0
- data/spec/active_validation/model_extension_base_spec.rb +71 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/adapter_spec.rb +31 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/adapter_spec_orm_specific_spec.rb +84 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/validate_method_spec.rb +26 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/validates_method_spec.rb +26 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/models/active_validation/check/validates_with_method_spec.rb +34 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/models/active_validation/check_spec.rb +48 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/models/active_validation/manifest_spec.rb +61 -0
- data/spec/active_validation/orm_plugins/active_record_plugin/readme_spec.rb +89 -0
- data/spec/active_validation/registry_spec.rb +76 -0
- data/spec/active_validation/values/base_spec.rb +61 -0
- data/spec/active_validation/values/method_name_spec.rb +16 -0
- data/spec/active_validation/values/version_spec.rb +36 -0
- data/spec/active_validation/verifier_spec.rb +214 -0
- data/spec/active_validation_spec.rb +19 -0
- data/spec/factories/internal/internal_check.rb +43 -0
- data/spec/features/active_record/child_record.feature +32 -0
- data/spec/features/active_record/new_record.feature +22 -0
- data/spec/features/no_orm/install.feature +19 -0
- data/spec/features/no_orm/validate.feature +27 -0
- data/spec/features/no_orm/validate_with_multiple_manifests.feature +29 -0
- data/spec/features/no_orm/validate_with_multiple_versions.feature +42 -0
- data/spec/features/placeholders/be_matcher.rb +7 -0
- data/spec/features/placeholders/klass.rb +5 -0
- data/spec/features/placeholders/version.rb +11 -0
- data/spec/features/placeholders/whether_to.rb +11 -0
- data/spec/features/step_definitions/active_record_steps.rb +7 -0
- data/spec/features/step_definitions/steps.rb +85 -0
- data/spec/orm/active_record/db_adapters/database.mysql.yml +12 -0
- data/spec/orm/active_record/db_adapters/database.postgres.yml +11 -0
- data/spec/orm/active_record/db_adapters/database.sqlite.yml +8 -0
- data/spec/orm/active_record/factories/check/check_validate.rb +8 -0
- data/spec/orm/active_record/factories/check/check_validates.rb +8 -0
- data/spec/orm/active_record/factories/check/check_validates_with.rb +19 -0
- data/spec/orm/active_record/factories/manifest.rb +29 -0
- data/spec/orm/active_record/prepare_db.rb +89 -0
- data/spec/orm/active_record/setup.rb +11 -0
- data/spec/orm/mongoid/setup.rb +15 -0
- data/spec/spec_helper.rb +38 -0
- data/spec/support/database_cleaner.rb +16 -0
- data/spec/support/deferred_garbage_collection.rb +31 -0
- data/spec/support/define_constant_macros.rb +17 -0
- data/spec/support/factory_bot.rb +12 -0
- data/spec/support/helpers.rb +3 -0
- data/spec/support/helpers/only_with_active_record.rb +15 -0
- data/spec/support/matchers/delegate.rb +50 -0
- data/spec/support/matchers/have_attr.rb +58 -0
- data/spec/support/mongoid.yml +6 -0
- data/spec/support/shared_examples/check_attributes.rb +9 -0
- data/spec/support/shared_examples/verifiers_registry.rb +10 -0
- data/spec/support/simplecov.rb +11 -0
- data/spec/turnip_helper.rb +4 -0
- metadata +304 -20
- data/lib/activevalidation.rb +0 -6
- data/lib/activevalidation/version.rb +0 -3
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::Internal::Observers::Manifest do
|
|
4
|
+
subject { described_class.new verifier }
|
|
5
|
+
|
|
6
|
+
let(:manifest_id) { 42 }
|
|
7
|
+
let(:manifest) { instance_double ActiveValidation::Internal::Models::Manifest }
|
|
8
|
+
let(:verifier) { instance_double ActiveValidation::Verifier, enabled?: true }
|
|
9
|
+
|
|
10
|
+
%i[manifest failed_attempt_retry_time enabled?].each do |m|
|
|
11
|
+
it { is_expected.to delegate(m).to(:verifier) }
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
context "#current_manifest" do
|
|
15
|
+
let(:verifier) { instance_double ActiveValidation::Verifier, current_manifest: manifest }
|
|
16
|
+
|
|
17
|
+
context "no raise" do
|
|
18
|
+
before do
|
|
19
|
+
allow(manifest).to receive(:to_internal_manifest).and_return(manifest)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it("return current manifest") { expect(subject.current_manifest).to eq manifest }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
context "raise" do
|
|
26
|
+
let(:error) { StandardError }
|
|
27
|
+
let(:time) { Time.now }
|
|
28
|
+
|
|
29
|
+
before do
|
|
30
|
+
allow(manifest).to receive(:to_internal_manifest).and_raise(error)
|
|
31
|
+
allow(Time).to receive(:now).and_return(time)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it("error is not raised and nil returned") { expect { subject.current_manifest }.not_to raise_error }
|
|
35
|
+
it("last_failed_attempt_time set") do
|
|
36
|
+
subject.current_manifest
|
|
37
|
+
expect(subject.last_failed_attempt_time).to eq time
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it("returns nil") { expect(subject.current_manifest).to be_nil }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
context "#reset_last_failed_attempt_time" do
|
|
45
|
+
it("resets the variable") do
|
|
46
|
+
subject.instance_variable_set("@last_failed_attempt_time", "some_value")
|
|
47
|
+
expect(subject.reset_last_failed_attempt_time).to be_nil
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# rubocop:disable RSpec/SubjectStub
|
|
52
|
+
context "#install" do
|
|
53
|
+
context "not enabled" do
|
|
54
|
+
let(:verifier) { instance_double ActiveValidation::Verifier, enabled?: false }
|
|
55
|
+
|
|
56
|
+
it("returns status") { expect(subject.install).to eq described_class::DISABLED_VERIFIER }
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context "attempt_to_was_failed_recently?" do
|
|
60
|
+
before do
|
|
61
|
+
allow(subject).to receive(:attempt_to_was_failed_recently?).and_return(true)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it("returns status") { expect(subject.install).to eq described_class::RECENT_FAILURE }
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context "with not found manifest" do
|
|
68
|
+
before do
|
|
69
|
+
allow(subject).to receive(:current_manifest)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it("returns status") { expect(subject.install).to eq described_class::NOT_FOUND }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
context "with found manifest" do
|
|
76
|
+
context "already_installed" do
|
|
77
|
+
context "found" do
|
|
78
|
+
let(:manifest) do
|
|
79
|
+
instance_double ActiveValidation::Internal::Models::Manifest, installed?: true, id: manifest_id
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
before do
|
|
83
|
+
subject.installed_manifests << manifest
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it("returns status") do
|
|
87
|
+
expect(subject.install(manifest_id: manifest_id)).to eq described_class::ALREADY_INSTALLED
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
context "current" do
|
|
92
|
+
let(:another_manifest) { instance_double ActiveValidation::Internal::Models::Manifest, installed?: true }
|
|
93
|
+
|
|
94
|
+
before do
|
|
95
|
+
allow(subject).to receive(:current_manifest).and_return(another_manifest)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it("returns status") { expect(subject.install).to eq described_class::ALREADY_INSTALLED }
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
context "required to be installed" do
|
|
103
|
+
before do
|
|
104
|
+
allow(subject).to receive(:install!)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
context "found" do
|
|
108
|
+
let(:manifest) do
|
|
109
|
+
instance_double ActiveValidation::Internal::Models::Manifest, installed?: false, id: manifest_id
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
before do
|
|
113
|
+
subject.installed_manifests << manifest
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
it("returns status") do
|
|
117
|
+
subject.install(manifest_id: manifest_id)
|
|
118
|
+
expect(subject).to have_received :install!
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
context "current" do
|
|
123
|
+
let(:another_manifest) { instance_double ActiveValidation::Internal::Models::Manifest, installed?: false }
|
|
124
|
+
|
|
125
|
+
before do
|
|
126
|
+
allow(subject).to receive(:current_manifest).and_return(another_manifest)
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it("returns status") do
|
|
130
|
+
subject.install
|
|
131
|
+
expect(subject).to have_received :install!
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
context "private #install!" do
|
|
139
|
+
context "already installed" do
|
|
140
|
+
let(:manifest) do
|
|
141
|
+
instance_double ActiveValidation::Internal::Models::Manifest, installed?: true, id: manifest_id
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it("returns status") do
|
|
145
|
+
subject.installed_manifests << manifest
|
|
146
|
+
expect(subject.install!(internal_manifest: manifest)).to eq described_class::ALREADY_INSTALLED
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
context "new installation" do
|
|
151
|
+
let(:manifest) do
|
|
152
|
+
instance_double ActiveValidation::Internal::Models::Manifest, installed?: false, id: manifest_id, install: nil
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
it "installed manifest include the manifest" do
|
|
156
|
+
subject.install!(internal_manifest: manifest)
|
|
157
|
+
expect(subject.installed_manifests).to include manifest
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
it "run install method in the manifest" do
|
|
161
|
+
subject.install!(internal_manifest: manifest)
|
|
162
|
+
expect(manifest).to have_received(:install)
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
it("returns status") do
|
|
166
|
+
expect(subject.install!(internal_manifest: manifest)).to eq described_class::INSTALLED
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
# rubocop:enable RSpec/SubjectStub
|
|
171
|
+
|
|
172
|
+
context "uninstall" do
|
|
173
|
+
context "with not found manifest" do
|
|
174
|
+
it("returns status") { expect(subject.uninstall(manifest_id: manifest_id)).to eq described_class::NOT_FOUND }
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
context "found" do
|
|
178
|
+
let(:manifest) do
|
|
179
|
+
instance_double ActiveValidation::Internal::Models::Manifest, installed?: true, id: manifest_id, uninstall: nil
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
before do
|
|
183
|
+
subject.installed_manifests << manifest
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it("returns status") do
|
|
187
|
+
expect(subject.uninstall(manifest_id: manifest_id)).to eq described_class::UNINSTALLED
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
it "run uninstall method in the manifest" do
|
|
191
|
+
subject.uninstall(manifest_id: manifest_id)
|
|
192
|
+
expect(manifest).to have_received(:uninstall)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it "installed manifest include the manifest" do
|
|
196
|
+
subject.uninstall(manifest_id: manifest_id)
|
|
197
|
+
expect(subject.installed_manifests).to be_empty
|
|
198
|
+
end
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::ModelExtensionBase do
|
|
4
|
+
let(:klass) { Class.new { include ActiveValidation::ModelExtensionBase } }
|
|
5
|
+
|
|
6
|
+
context "::active_validation" do
|
|
7
|
+
it { expect(klass).to respond_to :active_validation }
|
|
8
|
+
|
|
9
|
+
it "pass forward the block" do
|
|
10
|
+
expect { |b| klass.active_validation(&b) }.to yield_control
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "proxies self as an argument" do
|
|
14
|
+
allow(::ActiveValidation::Verifier).to receive(:find_or_build).with(klass)
|
|
15
|
+
klass.active_validation
|
|
16
|
+
expect(::ActiveValidation::Verifier).to have_received(:find_or_build)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
context "register_active_validation_relations" do
|
|
20
|
+
before do
|
|
21
|
+
allow(::ActiveValidation::Verifier).to receive(:find_or_build)
|
|
22
|
+
allow(klass).to receive(:register_active_validation_relations)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "registers relations" do
|
|
26
|
+
allow(::ActiveValidation::Verifier.registry).to receive(:registered?).and_return(false)
|
|
27
|
+
klass.active_validation
|
|
28
|
+
expect(klass).to have_received(:register_active_validation_relations)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "does not register relations" do
|
|
32
|
+
allow(::ActiveValidation::Verifier.registry).to receive(:registered?).and_return(true)
|
|
33
|
+
klass.active_validation
|
|
34
|
+
expect(klass).not_to have_received(:register_active_validation_relations)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
context "#process_active_validation" do
|
|
40
|
+
let(:verifier) { instance_double ::ActiveValidation::Verifier }
|
|
41
|
+
|
|
42
|
+
before do
|
|
43
|
+
allow(::ActiveValidation::Verifier).to receive(:find_or_build).with(klass).and_return(verifier)
|
|
44
|
+
allow(verifier).to receive(:install!)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
context "with manifest_id" do
|
|
48
|
+
before do
|
|
49
|
+
klass.send(:define_method, :manifest_id) { 42 }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "installs the manifest" do
|
|
53
|
+
klass.new.send(:process_active_validation)
|
|
54
|
+
expect(::ActiveValidation::Verifier).to have_received(:find_or_build)
|
|
55
|
+
expect(verifier).to have_received(:install!).with(manifest_id: 42)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context "with out manifest_id" do
|
|
60
|
+
before do
|
|
61
|
+
klass.send(:define_method, :manifest_id) { nil }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "installs the manifest" do
|
|
65
|
+
klass.new.send(:process_active_validation)
|
|
66
|
+
expect(::ActiveValidation::Verifier).to have_received(:find_or_build)
|
|
67
|
+
expect(verifier).to have_received(:install!).with(manifest_id: nil)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::OrmPlugins::ActiveRecordPlugin::Adapter do
|
|
4
|
+
context "global configuration" do
|
|
5
|
+
subject { ActiveValidation.config }
|
|
6
|
+
|
|
7
|
+
it "is in the registry" do
|
|
8
|
+
expect(subject.orm_adapters_registry).to be_registered(:active_record_plugin)
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
context "default" do
|
|
13
|
+
it "is not abstract" do
|
|
14
|
+
expect(described_class.abstract).to be_falsey
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "does not contain '(abstract)' suffix" do
|
|
18
|
+
expect(described_class.to_s).not_to match(/\(abstract\)\z/)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "is initialised after setup" do
|
|
22
|
+
described_class.initialised = false
|
|
23
|
+
described_class.new
|
|
24
|
+
expect(described_class.initialised).to be_truthy
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "includes current ORM adapter by default" do
|
|
28
|
+
expect(described_class.adapter_name).to eq ENV["ORM"]
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/spec/active_validation/orm_plugins/active_record_plugin/adapter_spec_orm_specific_spec.rb
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::OrmPlugins::ActiveRecordPlugin::Adapter, type: :active_record do
|
|
4
|
+
let(:manifest) { ActiveValidation::Internal::Models::Manifest.new version: 1, base_klass: "Foo", name: "subject" }
|
|
5
|
+
let(:check_validates) do
|
|
6
|
+
ActiveValidation::Internal::Models::Check.new method_name: "validates",
|
|
7
|
+
argument: "name",
|
|
8
|
+
options: { presence: true }
|
|
9
|
+
end
|
|
10
|
+
let(:check_validate) do
|
|
11
|
+
ActiveValidation::Internal::Models::Check.new method_name: "validate", argument: "my_method"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
let(:check_validates_with) do
|
|
15
|
+
define_const "MyValidator", superclass: ActiveModel::Validator
|
|
16
|
+
ActiveValidation::Internal::Models::Check.new method_name: "validates_with", argument: "MyValidator"
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it "has right plugin name" do
|
|
20
|
+
expect(described_class.plugin_name).to eq "active_record_plugin"
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "has right adapter name" do
|
|
24
|
+
expect(described_class.adapter_name).to eq "active_record"
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
context "#add_manifest" do
|
|
28
|
+
it "Add manifest with out checks" do
|
|
29
|
+
expect(subject.add_manifest(manifest)).to eq manifest
|
|
30
|
+
expect(ActiveValidation::Manifest.count).to eq 1
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
it "Add manifest with checks" do
|
|
34
|
+
manifest.checks << check_validate
|
|
35
|
+
manifest.checks << check_validates
|
|
36
|
+
manifest.checks << check_validates_with
|
|
37
|
+
expect(subject.add_manifest(manifest)).to eq manifest
|
|
38
|
+
expect(ActiveValidation::Manifest.count).to eq 1
|
|
39
|
+
expect(ActiveValidation::Check.count).to eq 3
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
context "search" do
|
|
44
|
+
let!(:ar_manifest1) { create :manifest }
|
|
45
|
+
let!(:ar_manifest2) { create :manifest, name: "subject" }
|
|
46
|
+
let!(:ar_manifest3) { create :manifest }
|
|
47
|
+
let!(:ar_manifest4) { create :manifest, version: 2 }
|
|
48
|
+
|
|
49
|
+
context "#find_manifest" do
|
|
50
|
+
it "find correct manifest" do
|
|
51
|
+
expect(subject.find_manifest(name: "subject")).to eq ar_manifest2
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "find correct manifest with checks" do
|
|
55
|
+
create :check_validates, manifest: ar_manifest2
|
|
56
|
+
create :check_validate, manifest: ar_manifest2, argument: "my_method"
|
|
57
|
+
manifest.checks << check_validates
|
|
58
|
+
manifest.checks << check_validate
|
|
59
|
+
expect(subject.find_manifest(name: "subject")).to eq manifest
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "raise NotFound error id nothing found" do
|
|
63
|
+
expect { subject.find_manifest(name: "not_exist") }.to raise_error ActiveRecord::RecordNotFound
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
context "#find_manifests" do
|
|
68
|
+
let!(:ar_manifest5) { create :manifest, base_klass: "Bar" }
|
|
69
|
+
let(:json_options) { { include: { checks: { methods: %i[method_name] } }, root: false } }
|
|
70
|
+
|
|
71
|
+
it "find correct manifests" do
|
|
72
|
+
result = [ar_manifest1, ar_manifest2, ar_manifest3].map do |record|
|
|
73
|
+
ActiveValidation::Internal::Models::Manifest.new record.as_json(json_options).to_options!
|
|
74
|
+
end
|
|
75
|
+
expect(subject.find_manifests(version: 1, base_klass: "Foo")).to match_array result
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "return array with 1 element if only 1 record found" do
|
|
79
|
+
result = ActiveValidation::Internal::Models::Manifest.new ar_manifest5.as_json(json_options).to_options!
|
|
80
|
+
expect(subject.find_manifests(base_klass: "Bar")).to match_array result
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::Check::ValidateMethod, type: :active_record do
|
|
4
|
+
include_examples "check attributes check"
|
|
5
|
+
|
|
6
|
+
it("contains field type") { expect(subject.attributes).to have_key("type") }
|
|
7
|
+
|
|
8
|
+
context "validate the method invocation" do
|
|
9
|
+
before do
|
|
10
|
+
define_const("Foo", superclass: ActiveRecord::Base) do
|
|
11
|
+
def foo_allowed; end
|
|
12
|
+
|
|
13
|
+
def foo_not_allowed; end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "invokes allowed method" do
|
|
18
|
+
expect(build(:check_validate, argument: "foo_allowed")).to be_valid
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "does not invoke globally restricted method" do
|
|
22
|
+
manifest = build :manifest, base_klass: "Foo"
|
|
23
|
+
expect(build(:check_validate, argument: "delete", manifest: manifest)).not_to be_valid
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::Check::ValidatesMethod, type: :active_record do
|
|
4
|
+
include_examples "check attributes check"
|
|
5
|
+
|
|
6
|
+
it("contains field type") { expect(subject.attributes).to have_key("type") }
|
|
7
|
+
|
|
8
|
+
context "validate the method invocation" do
|
|
9
|
+
before do
|
|
10
|
+
define_const("Foo", superclass: ActiveRecord::Base) do
|
|
11
|
+
def foo_allowed; end
|
|
12
|
+
|
|
13
|
+
def foo_not_allowed; end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "invokes allowed method" do
|
|
18
|
+
expect(build(:check_validates, argument: "foo_allowed")).to be_valid
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "does not invoke globally restricted method" do
|
|
22
|
+
manifest = build :manifest, base_klass: "Foo"
|
|
23
|
+
expect(build(:check_validates, argument: "delete", manifest: manifest)).not_to be_valid
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
describe ActiveValidation::Check::ValidatesWithMethod, type: :active_record do
|
|
4
|
+
include_examples "check attributes check"
|
|
5
|
+
|
|
6
|
+
it("contains field type") { expect(subject.attributes).to have_key("type") }
|
|
7
|
+
|
|
8
|
+
context "simple validator" do
|
|
9
|
+
before do
|
|
10
|
+
define_const("FooValidator", superclass: ActiveModel::Validator) { def validate(*); true; end }
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "is valid record with correct validator" do
|
|
14
|
+
expect(build(:check_validates_with, argument: "FooValidator")).to be_valid
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "is not valid record with incorrect validator" do
|
|
18
|
+
record = build(:check_validates_with, argument: "NotExist", with_validator_klass: false)
|
|
19
|
+
expect(record).not_to be_valid
|
|
20
|
+
expect(record.errors[:argument].size).to eq 1
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "is not valid record without validator" do
|
|
24
|
+
record = build(:check_validates_with, argument: "", with_validator_klass: false)
|
|
25
|
+
expect(record).not_to be_valid
|
|
26
|
+
expect(record.errors[:argument].size).to eq 1
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "is valid with nested record with correct validator" do
|
|
30
|
+
define_const("NestedValidator", superclass: FooValidator)
|
|
31
|
+
expect(build(:check_validates_with, argument: "NestedValidator")).to be_valid
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|