schema_expectations 0.0.0 → 0.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7bcaa02aa13161953b36126a982156c8eb192aac
4
- data.tar.gz: 3378e981d2f3325310f4ce37097f8cb217956562
3
+ metadata.gz: d089e8270a688859724aea6d9e3a117f0cc32154
4
+ data.tar.gz: bc468ef7199c0960ca1ad33c8f20ea18ec5431bb
5
5
  SHA512:
6
- metadata.gz: a0d519d4a0c7a1439af4e0ac32802b98f520f65abb8c61be27344e9f4502e89e0a6716613f35f2a4f9c6e37697640a3bb2f9f41cfb91ccbefb26f978d45f6d48
7
- data.tar.gz: e397d41a9aca63152a1b9052797f5cd116e8475f22f7a449beca78f5b5322fcd5f2cd0793744b2afd5ce9495f27aa65958f2b3302957ed18a08c61d5bf177f67
6
+ metadata.gz: 7be6b68419758bbb28a87033904c4721d42bb0db70d2029ac52ffcb3e284aa2794621c351cdb481f8dd40c9d64e4c2be9ad22eefe51dfbb9bccdac30485d2379
7
+ data.tar.gz: 56ec7dd5e2b85c9d05f02131c9603bfb59b5ef8f814922d4406ba2149a916a725bec39c0c1546b9916110418a989907a9c3e6c3a4764b38bc4ef1433a1595301
data/.gitignore CHANGED
@@ -1 +1,4 @@
1
1
  Gemfile.lock
2
+ /pkg
3
+ /.yardoc
4
+ /doc
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Schema Expectations Changelog
2
+
3
+ # git master
4
+
5
+ - `validate_schema_nullable` supports being called on AR instances
6
+ - documented `validate_schema_nullable`
7
+
8
+ ### 0.0.0 (February 12, 2015)
9
+
10
+ - Added `validate_schema_nullable` matcher
@@ -2,12 +2,46 @@ require 'rspec/expectations'
2
2
 
3
3
  module SchemaExpectations
4
4
  module RSpecMatchers
5
+ # The `validate_schema_nullable` matcher test that an ActiveRecord model
6
+ # has unconditional presence validation on columns with `NOT NULL` constraints,
7
+ # and vice versa.
8
+ #
9
+ # For example, we can assert that the model and database are consistent
10
+ # on whether `Record#name` should be present:
11
+ #
12
+ # create_table :records do |t|
13
+ # t.string :name, null: false
14
+ # end
15
+
16
+ # class Record < ActiveRecord::Base
17
+ # validates :name, presence: true
18
+ # end
19
+ #
20
+ # # RSpec
21
+ # describe Record do
22
+ # it { should validate_schema_nullable }
23
+ # end
24
+ #
25
+ # You can restrict the columns tested:
26
+ #
27
+ # # RSpec
28
+ # describe Record do
29
+ # it { should validate_schema_nullable.only(:name) }
30
+ # it { should validate_schema_nullable.except(:name) }
31
+ # end
32
+ #
33
+ # The `id` column is automatically skipped.
34
+ #
35
+ # @return [ValidateSchemaNullableMatcher]
5
36
  def validate_schema_nullable
6
37
  ValidateSchemaNullableMatcher.new
7
38
  end
8
39
 
9
40
  class ValidateSchemaNullableMatcher
10
41
  def matches?(model)
42
+ model = model.class if model.is_a?(ActiveRecord::Base)
43
+ fail "#{model.inspect} does not inherit from ActiveRecord::Base" unless model.ancestors.include?(ActiveRecord::Base)
44
+
11
45
  @model = model
12
46
  @not_null_columns = filter_attributes(not_null_columns(model))
13
47
  @present_attributes = filter_attributes(present_attributes(model))
@@ -35,6 +69,9 @@ module SchemaExpectations
35
69
  errors.join(', ')
36
70
  end
37
71
 
72
+ # Specifies a list of columns to restrict matcher
73
+ #
74
+ # @return [ValidateSchemaNullableMatcher] self
38
75
  def only(*args)
39
76
  fail 'cannot use only and except' if @except
40
77
  @only = Array(args)
@@ -42,6 +79,9 @@ module SchemaExpectations
42
79
  self
43
80
  end
44
81
 
82
+ # Specifies a list of columns for matcher to ignore
83
+ #
84
+ # @return [ValidateSchemaNullableMatcher] self
45
85
  def except(*args)
46
86
  fail 'cannot use only and except' if @only
47
87
  @except = Array(args)
@@ -1,3 +1,3 @@
1
1
  module SchemaExpectations
2
- VERSION = '0.0.0'
2
+ VERSION = '0.0.1'
3
3
  end
@@ -26,6 +26,7 @@ Gem::Specification.new do |gem|
26
26
 
27
27
  gem.add_development_dependency 'pry'
28
28
  gem.add_development_dependency 'rake', '~> 10.4'
29
+ gem.add_development_dependency 'yard', '~> 0.8.7'
29
30
 
30
31
  # tests
31
32
  gem.add_development_dependency 'codeclimate-test-reporter'
@@ -1,6 +1,32 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe :validate_schema_nullable do
4
+ specify 'works on instances', :active_record do
5
+ create_table :records do |t|
6
+ t.string :name, null: false
7
+ t.string :wrong
8
+ end
9
+
10
+ stub_const('Record', Class.new(ActiveRecord::Base))
11
+
12
+ Record.instance_eval do
13
+ validates :name, :wrong, presence: true
14
+ end
15
+
16
+ expect(Record.new).to validate_schema_nullable.only(:name)
17
+ expect(Record.new).to_not validate_schema_nullable.only(:wrong)
18
+ end
19
+
20
+ specify 'doesnt raise extraneous exceptions from timestamps', :active_record do
21
+ create_table :records do |t|
22
+ t.timestamps
23
+ end
24
+
25
+ stub_const('Record', Class.new(ActiveRecord::Base))
26
+
27
+ expect(Record.new).to validate_schema_nullable
28
+ end
29
+
4
30
  specify 'asserts that presence validations match NOT NULL', :active_record do
5
31
  create_table :records do |t|
6
32
  t.string :not_null, null: false
@@ -10,7 +36,9 @@ describe :validate_schema_nullable do
10
36
  t.string :nullable_present
11
37
  end
12
38
 
13
- class Record < ActiveRecord::Base
39
+ stub_const('Record', Class.new(ActiveRecord::Base))
40
+
41
+ Record.instance_eval do
14
42
  validates :not_null_present, presence: true
15
43
  validates :nullable_present, presence: true
16
44
  end
@@ -30,7 +58,7 @@ describe :validate_schema_nullable do
30
58
  expect(Record).to validate_schema_nullable.only(:nullable_present)
31
59
  end.to raise_error 'nullable_present has unconditional presence validation but is missing NOT NULL'
32
60
 
33
- class Record < ActiveRecord::Base
61
+ Record.instance_eval do
34
62
  clear_validators!
35
63
  validates :not_null_present, presence: true, on: :create
36
64
  end
@@ -39,7 +67,7 @@ describe :validate_schema_nullable do
39
67
  expect(Record).to validate_schema_nullable.only(:not_null_present)
40
68
  end.to raise_error 'not_null_present is NOT NULL but its presence validator was conditional: {:on=>:create}'
41
69
 
42
- class Record < ActiveRecord::Base
70
+ Record.instance_eval do
43
71
  clear_validators!
44
72
  validates :not_null_present, presence: true, if: ->{ false }
45
73
  end
@@ -48,7 +76,7 @@ describe :validate_schema_nullable do
48
76
  expect(Record).to validate_schema_nullable.only(:not_null_present)
49
77
  end.to raise_error /\Anot_null_present is NOT NULL but its presence validator was conditional: {:if=>\#<Proc:.*>}\z/
50
78
 
51
- class Record < ActiveRecord::Base
79
+ Record.instance_eval do
52
80
  clear_validators!
53
81
  validates :not_null_present, presence: true, unless: ->{ true }
54
82
  end
@@ -57,7 +85,7 @@ describe :validate_schema_nullable do
57
85
  expect(Record).to validate_schema_nullable.only(:not_null_present)
58
86
  end.to raise_error /\Anot_null_present is NOT NULL but its presence validator was conditional: {:unless=>\#<Proc:.*>}\z/
59
87
 
60
- class Record < ActiveRecord::Base
88
+ Record.instance_eval do
61
89
  clear_validators!
62
90
  validates :not_null_present, presence: true, allow_nil: true
63
91
  end
@@ -66,7 +94,7 @@ describe :validate_schema_nullable do
66
94
  expect(Record).to validate_schema_nullable.only(:not_null_present)
67
95
  end.to raise_error 'not_null_present is NOT NULL but its presence validator was conditional: {:allow_nil=>true}'
68
96
 
69
- class Record < ActiveRecord::Base
97
+ Record.instance_eval do
70
98
  clear_validators!
71
99
  validates :not_null_present, presence: true, allow_blank: true
72
100
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_expectations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Emma Borhanian
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '10.4'
55
+ - !ruby/object:Gem::Dependency
56
+ name: yard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.8.7
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.8.7
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: codeclimate-test-reporter
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -119,6 +133,7 @@ files:
119
133
  - ".rspec"
120
134
  - ".ruby-version"
121
135
  - ".travis.yml"
136
+ - CHANGELOG.md
122
137
  - Gemfile
123
138
  - Guardfile
124
139
  - LICENSE
@@ -161,3 +176,4 @@ test_files:
161
176
  - spec/lib/schema_expectations/rspec_matchers/validate_schema_nullable_spec.rb
162
177
  - spec/spec_helper.rb
163
178
  - spec/support/active_record.rb
179
+ has_rdoc: