nullalign 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +3 -0
- data/README.md +11 -0
- data/lib/nullalign/introspectors/validates_presence_of.rb +1 -1
- data/lib/nullalign/version.rb +1 -1
- data/nullalign.gemspec +1 -1
- data/spec/introspectors/table_data_spec.rb +19 -0
- data/spec/introspectors/validates_presence_of_spec.rb +36 -0
- data/spec/models_spec.rb +7 -8
- data/spec/nonnull_constraint_spec.rb +53 -0
- data/spec/reporter_spec.rb +27 -0
- data/spec/support/models/with_if_account.rb +5 -0
- data/spec/support/models/with_on_account.rb +5 -0
- data/spec/support/models/with_unless_account.rb +5 -0
- data/spec/support/schema.rb +15 -0
- metadata +18 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1af15eca93e06667b2a429e1c853c03c101a9425
|
4
|
+
data.tar.gz: 3ba414cae84fceaf9ff1e66f65322eb3e6b153bc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cb64c015c7b25c0c070d97157df4b5d027b77170884bafe5d15bfb2cbdae3f5a144be744ed4a7a750e0106da661e303a1c32f4a0c8f0f224cb08fe2cb9f91034
|
7
|
+
data.tar.gz: 46e03a6c9c628434afbbdf27f2be293eaa4ed7b68ceb146f14fa97f7b94b9e98ccbde3fff140ba73c35f7bc319064e6e6641937746d61e11d74aa2faaebb9ce6
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -47,6 +47,17 @@ or pull request if so!
|
|
47
47
|
To disable nullalign, I could add a thing that checks column comments for a string
|
48
48
|
like 'nonullalign' if people think that would be useful. Just let me know.
|
49
49
|
|
50
|
+
## Contributors
|
51
|
+
|
52
|
+
* [Tom Copeland](https://thomasleecopeland.com) - author
|
53
|
+
* [Woongcheol Yang](https://github.com/woongy) - support for conditional validations
|
54
|
+
|
55
|
+
## Tests
|
56
|
+
|
57
|
+
You can run the tests with:
|
58
|
+
|
59
|
+
bundle exec rspec
|
60
|
+
|
50
61
|
## License
|
51
62
|
|
52
63
|
Released under the MIT License. See the LICENSE file for further details.
|
@@ -5,7 +5,7 @@ module Nullalign
|
|
5
5
|
class ValidatesPresenceOf
|
6
6
|
def instances(model)
|
7
7
|
model.validators.select do |v|
|
8
|
-
v.class == ActiveRecord::Validations::PresenceValidator
|
8
|
+
v.class == ActiveRecord::Validations::PresenceValidator && (v.options.keys & %i(on if unless)).empty?
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
data/lib/nullalign/version.rb
CHANGED
data/nullalign.gemspec
CHANGED
@@ -16,7 +16,7 @@ non-null constraint to go with it.
|
|
16
16
|
EOF
|
17
17
|
s.license = "MIT"
|
18
18
|
|
19
|
-
s.add_development_dependency "activerecord", "~>
|
19
|
+
s.add_development_dependency "activerecord", "~>4.0"
|
20
20
|
s.add_development_dependency "sqlite3", "~>1.3"
|
21
21
|
s.add_development_dependency "rspec", "~>3.2"
|
22
22
|
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Nullalign::Introspectors::TableData do
|
4
|
+
|
5
|
+
describe "finding nonnull constraints" do
|
6
|
+
it "finds none when the table does not exist" do
|
7
|
+
expect(subject.nonnull_constraints(Nonexistent)).to be_empty
|
8
|
+
end
|
9
|
+
|
10
|
+
it "gets one" do
|
11
|
+
expect(
|
12
|
+
Nullalign::Introspectors::TableData.new.nonnull_constraints_by_table(
|
13
|
+
CorrectAccount,
|
14
|
+
CorrectAccount.table_name
|
15
|
+
)[1].column
|
16
|
+
).to eq "email"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Nullalign::Introspectors::ValidatesPresenceOf do
|
4
|
+
|
5
|
+
describe "finding missing nonnull constraints" do
|
6
|
+
it "finds one" do
|
7
|
+
indexes = subject.missing_nonnull_constraints(WrongAccount)
|
8
|
+
|
9
|
+
expect(indexes).to eq([
|
10
|
+
Nullalign::NonnullConstraint.new(
|
11
|
+
WrongAccount,
|
12
|
+
WrongAccount.table_name,
|
13
|
+
"email"
|
14
|
+
)
|
15
|
+
])
|
16
|
+
end
|
17
|
+
|
18
|
+
it "finds none when they're already in place" do
|
19
|
+
expect(subject.missing_nonnull_constraints(CorrectAccount)).to be_empty
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'finds none if there is an if condition' do
|
23
|
+
expect(subject.missing_nonnull_constraints(WithIfAccount)).to be_empty
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'finds none if there is an on condition' do
|
27
|
+
expect(subject.missing_nonnull_constraints(WithOnAccount)).to be_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'finds none if there is an unless condition' do
|
31
|
+
expect(subject.missing_nonnull_constraints(WithUnlessAccount)).to be_empty
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
data/spec/models_spec.rb
CHANGED
@@ -37,14 +37,13 @@ describe Nullalign::Models do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
it "gets all models" do
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
# expect(models([]).all).to eq([model_a, model_c, model_b])
|
40
|
+
model_a = double(:name => "animal")
|
41
|
+
model_b = double(:name => "cat")
|
42
|
+
model_c = double(:name => "beach_ball")
|
43
|
+
|
44
|
+
allow(ActiveRecord::Base).to receive(:descendants).and_return([model_a, model_b, model_c])
|
45
|
+
|
46
|
+
expect(models([]).all).to eq([model_a, model_c, model_b])
|
48
47
|
end
|
49
48
|
|
50
49
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
describe Nullalign::NonnullConstraint do
|
2
|
+
|
3
|
+
let(:nonnull_constraint) do
|
4
|
+
Nullalign::NonnullConstraint.new(
|
5
|
+
CorrectAccount,
|
6
|
+
CorrectAccount.table_name,
|
7
|
+
"city"
|
8
|
+
)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "value objectiness" do
|
12
|
+
it "holds onto model, table name, and columns" do
|
13
|
+
expect(nonnull_constraint.model).to eq(CorrectAccount)
|
14
|
+
expect(nonnull_constraint.table_name).to eq("correct_accounts")
|
15
|
+
expect(nonnull_constraint.column).to eq(
|
16
|
+
"city"
|
17
|
+
)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "equality test" do
|
22
|
+
it "passes when everything matches" do
|
23
|
+
expect(nonnull_constraint).to eq(
|
24
|
+
Nullalign::NonnullConstraint.new(
|
25
|
+
"CorrectAccount".constantize,
|
26
|
+
"correct_accounts",
|
27
|
+
"city"
|
28
|
+
)
|
29
|
+
)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "fails when tables are different" do
|
33
|
+
expect(nonnull_constraint).not_to eq(
|
34
|
+
Nullalign::NonnullConstraint.new(
|
35
|
+
NewCorrectAccount,
|
36
|
+
NewCorrectAccount.table_name,
|
37
|
+
"city"
|
38
|
+
)
|
39
|
+
)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "fails when columns are different" do
|
43
|
+
expect(nonnull_constraint).not_to eq(
|
44
|
+
Nullalign::NonnullConstraint.new(
|
45
|
+
CorrectAccount,
|
46
|
+
NewCorrectAccount.table_name,
|
47
|
+
"city"
|
48
|
+
)
|
49
|
+
)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
describe Nullalign::Reporter do
|
2
|
+
|
3
|
+
context "validates_presence_of" do
|
4
|
+
it "says everything's good" do
|
5
|
+
expect {
|
6
|
+
subject.report_validates_presence_problems([])
|
7
|
+
}.to output(/Hooray!/).to_stdout
|
8
|
+
end
|
9
|
+
|
10
|
+
it "shows a missing constraint on a single model" do
|
11
|
+
missing_constraints = [
|
12
|
+
Nullalign::NonnullConstraint.new(
|
13
|
+
WrongAccount,
|
14
|
+
WrongAccount.table_name,
|
15
|
+
"email"
|
16
|
+
)
|
17
|
+
]
|
18
|
+
|
19
|
+
expect {
|
20
|
+
subject.report_validates_presence_problems(
|
21
|
+
WrongAccount => missing_constraints
|
22
|
+
)
|
23
|
+
}.to output(/wrong_accounts:\s+email/).to_stdout
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
data/spec/support/schema.rb
CHANGED
@@ -11,6 +11,21 @@ ActiveRecord::Schema.define(version: 0) do
|
|
11
11
|
t.timestamps
|
12
12
|
end
|
13
13
|
|
14
|
+
create_table :with_if_accounts do |t|
|
15
|
+
t.string :email
|
16
|
+
t.timestamps
|
17
|
+
end
|
18
|
+
|
19
|
+
create_table :with_on_accounts do |t|
|
20
|
+
t.string :email
|
21
|
+
t.timestamps
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table :with_unless_accounts do |t|
|
25
|
+
t.string :email
|
26
|
+
t.timestamps
|
27
|
+
end
|
28
|
+
|
14
29
|
execute 'CREATE VIEW new_correct_people AS '\
|
15
30
|
'SELECT * FROM correct_people '\
|
16
31
|
'WHERE created_at = updated_at'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nullalign
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Copeland
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
19
|
+
version: '4.0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
26
|
+
version: '4.0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: sqlite3
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -80,12 +80,19 @@ files:
|
|
80
80
|
- lib/nullalign/reporters/validates_presence_of.rb
|
81
81
|
- lib/nullalign/version.rb
|
82
82
|
- nullalign.gemspec
|
83
|
+
- spec/introspectors/table_data_spec.rb
|
84
|
+
- spec/introspectors/validates_presence_of_spec.rb
|
83
85
|
- spec/models_spec.rb
|
86
|
+
- spec/nonnull_constraint_spec.rb
|
87
|
+
- spec/reporter_spec.rb
|
84
88
|
- spec/spec_helper.rb
|
85
89
|
- spec/support/active_record.rb
|
86
90
|
- spec/support/models/correct_account.rb
|
87
91
|
- spec/support/models/new_correct_account.rb
|
88
92
|
- spec/support/models/nonexistent.rb
|
93
|
+
- spec/support/models/with_if_account.rb
|
94
|
+
- spec/support/models/with_on_account.rb
|
95
|
+
- spec/support/models/with_unless_account.rb
|
89
96
|
- spec/support/models/wrong_account.rb
|
90
97
|
- spec/support/schema.rb
|
91
98
|
homepage: http://github.com/tcopeland/nullalign
|
@@ -113,11 +120,18 @@ signing_key:
|
|
113
120
|
specification_version: 4
|
114
121
|
summary: A tool to detect missing non-null constraints
|
115
122
|
test_files:
|
123
|
+
- spec/introspectors/table_data_spec.rb
|
124
|
+
- spec/introspectors/validates_presence_of_spec.rb
|
116
125
|
- spec/models_spec.rb
|
126
|
+
- spec/nonnull_constraint_spec.rb
|
127
|
+
- spec/reporter_spec.rb
|
117
128
|
- spec/spec_helper.rb
|
118
129
|
- spec/support/active_record.rb
|
119
130
|
- spec/support/models/correct_account.rb
|
120
131
|
- spec/support/models/new_correct_account.rb
|
121
132
|
- spec/support/models/nonexistent.rb
|
133
|
+
- spec/support/models/with_if_account.rb
|
134
|
+
- spec/support/models/with_on_account.rb
|
135
|
+
- spec/support/models/with_unless_account.rb
|
122
136
|
- spec/support/models/wrong_account.rb
|
123
137
|
- spec/support/schema.rb
|