tuning 1.0.0 → 4.0.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.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +23 -14
  3. data/Rakefile +1 -17
  4. data/lib/tuning.rb +1 -0
  5. data/lib/tuning/railtie.rb +1 -1
  6. data/lib/tuning/version.rb +1 -1
  7. data/test/{controllers_test.rb → controller_test.rb} +1 -1
  8. data/test/dummy/app/models/session.rb +6 -0
  9. data/test/dummy/app/models/user.rb +5 -0
  10. data/test/dummy/bin/bundle +1 -0
  11. data/test/dummy/bin/rails +1 -0
  12. data/test/dummy/bin/rake +1 -0
  13. data/test/dummy/bin/setup +1 -0
  14. data/test/dummy/config/database.yml.travis +2 -11
  15. data/test/dummy/config/environments/development.rb +1 -1
  16. data/test/dummy/config/environments/production.rb +1 -1
  17. data/test/dummy/config/environments/test.rb +2 -2
  18. data/test/dummy/config/routes.rb +52 -1
  19. data/test/dummy/config/secrets.yml +2 -2
  20. data/test/dummy/db/migrate/20161020200126_create_users.rb +9 -0
  21. data/test/dummy/db/schema.rb +2 -7
  22. data/test/dummy/log/development.log +21 -0
  23. data/test/dummy/log/test.log +383 -15
  24. data/test/dummy/public/404.html +57 -63
  25. data/test/dummy/public/422.html +57 -63
  26. data/test/dummy/public/500.html +56 -62
  27. data/test/{helpers_test.rb → helper_test.rb} +17 -5
  28. data/test/mailer_test.rb +13 -0
  29. data/test/record_test.rb +16 -0
  30. data/test/test_helper.rb +5 -2
  31. data/test/validation_test.rb +172 -0
  32. metadata +21 -19
  33. data/test/dummy/app/models/shop.rb +0 -2
  34. data/test/dummy/db/migrate/20161020200126_create_shops.rb +0 -9
  35. data/test/dummy/db/migrate/20161022144717_create_records.rb +0 -6
  36. data/test/mailers_test.rb +0 -10
  37. data/test/records_test.rb +0 -16
  38. data/test/validations_test.rb +0 -192
@@ -0,0 +1,172 @@
1
+ require 'test_helper'
2
+
3
+ class ValidationTest < ActiveSupport::TestCase
4
+
5
+ MODELS = [User, Session]
6
+
7
+ test 'methods' do
8
+ MODELS.each do |model|
9
+ %i(complexity count time).each do |validator|
10
+ method = "validates_#{validator}_of"
11
+ assert model.respond_to?(method)
12
+ end
13
+ end
14
+ end
15
+
16
+ test 'complexity' do
17
+ with_validation(
18
+ :password, complexity: true
19
+ ) do |model|
20
+ instance = model.new(password: nil)
21
+ assert_not instance.valid?
22
+
23
+ instance = model.new(password: 'aaaa')
24
+ assert_not instance.valid?
25
+ assert instance.errors.added?(:password, :too_easy)
26
+
27
+ instance = model.new(password: 'aaAA33__')
28
+ assert instance.valid?
29
+ end
30
+ end
31
+
32
+ test 'count' do
33
+ with_validation(
34
+ :list, count: true
35
+ ) do |model|
36
+ instance = model.new(list: nil)
37
+ assert_not instance.valid?
38
+ assert instance.errors.added?(:list, :uncountable)
39
+ end
40
+
41
+ with_validation(
42
+ :list, count: { within: 1..2 }
43
+ ) do |model|
44
+ instance = model.new(list: [1, 2, 3])
45
+ assert_not instance.valid?
46
+ assert instance.errors.added?(:list, :too_many)
47
+
48
+ instance = model.new(list: [])
49
+ assert_not instance.valid?
50
+ assert instance.errors.added?(:list, :too_few)
51
+
52
+ instance = model.new(list: [1])
53
+ assert instance.valid?
54
+ end
55
+
56
+ with_validation(
57
+ :list, count: { in: 1..2 }
58
+ ) do |model|
59
+ instance = model.new(list: [1, 2, 3])
60
+ assert_not instance.valid?
61
+ assert instance.errors.added?(:list, :too_many)
62
+
63
+ instance = model.new(list: [])
64
+ assert_not instance.valid?
65
+ assert instance.errors.added?(:list, :too_few)
66
+
67
+ instance = model.new(list: [1])
68
+ assert instance.valid?
69
+ end
70
+
71
+ with_validation(
72
+ :list, count: { maximum: 2 }
73
+ ) do |model|
74
+ instance = model.new(list: [1, 2, 3])
75
+ assert_not instance.valid?
76
+ assert instance.errors.added?(:list, :too_many)
77
+
78
+ instance = model.new(list: [])
79
+ assert instance.valid?
80
+ end
81
+
82
+ with_validation(
83
+ :list, count: { minimum: 1 }
84
+ ) do |model|
85
+ instance = model.new(list: [])
86
+ assert_not instance.valid?
87
+ assert instance.errors.added?(:list, :too_few)
88
+
89
+ instance = model.new(list: [1])
90
+ assert instance.valid?
91
+ end
92
+ end
93
+
94
+ test 'time' do
95
+ with_validation(
96
+ :created_at, time: true
97
+ ) do |model|
98
+ instance = model.new(created_at: nil)
99
+ assert_not instance.valid?
100
+ assert instance.errors.added?(:created_at, :not_a_time)
101
+ end
102
+
103
+ with_validation(
104
+ :created_at, time: { before: Proc.new { Date.today } }
105
+ ) do |model|
106
+ instance = model.new(created_at: Date.today)
107
+ assert_not instance.valid?
108
+ assert instance.errors.added?(:created_at, :before)
109
+
110
+ instance = model.new(created_at: (Time.now + 1.day))
111
+ assert_not instance.valid?
112
+ assert instance.errors.added?(:created_at, :before)
113
+
114
+ instance = model.new(created_at: (Date.today - 1.day))
115
+ assert instance.valid?
116
+ end
117
+
118
+ with_validation(
119
+ :created_at, time: { before_or_equal_to: Date.today }
120
+ ) do |model|
121
+ instance = model.new(created_at: (Time.now + 1.day))
122
+ assert_not instance.valid?
123
+ assert instance.errors.added?(:created_at, :before_or_equal_to)
124
+
125
+ instance = model.new(created_at: Date.today)
126
+ assert instance.valid?
127
+
128
+ instance = model.new(created_at: (Date.today - 1.day))
129
+ assert instance.valid?
130
+ end
131
+
132
+ with_validation(
133
+ :created_at, time: { after: ->(instance) { instance.updated_at } }
134
+ ) do |model|
135
+ instance = model.new(created_at: Date.today, updated_at: Date.today)
136
+ assert_not instance.valid?
137
+ assert instance.errors.added?(:created_at, :after)
138
+
139
+ instance = model.new(created_at: (Date.today - 1.day), updated_at: Date.today)
140
+ assert_not instance.valid?
141
+ assert instance.errors.added?(:created_at, :after)
142
+
143
+ instance = model.new(created_at: (Time.now + 1.day), updated_at: Date.today)
144
+ assert instance.valid?
145
+ end
146
+
147
+ with_validation(
148
+ :created_at, time: { after_or_equal_to: :updated_at }
149
+ ) do |model|
150
+ instance = model.new(created_at: (Date.today - 1.day), updated_at: Date.today)
151
+ assert_not instance.valid?
152
+ assert instance.errors.added?(:created_at, :after_or_equal_to)
153
+
154
+ instance = model.new(created_at: Date.today, updated_at: Date.today)
155
+ assert instance.valid?
156
+
157
+ instance = model.new(created_at: (Date.today + 1.day), updated_at: Date.today)
158
+ assert instance.valid?
159
+ end
160
+ end
161
+
162
+ private
163
+
164
+ def with_validation(*args)
165
+ MODELS.each do |model|
166
+ model.validates *args
167
+ yield model
168
+ model.clear_validators!
169
+ end
170
+ end
171
+
172
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tuning
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 4.0.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mmontossi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-22 00:00:00.000000000 Z
11
+ date: 2016-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -60,7 +60,7 @@ dependencies:
60
60
  version: '1.1'
61
61
  description: Common tools used in rails extracted into a gem.
62
62
  email:
63
- - mmontossi@buyin.io
63
+ - mmontossi@gmail.com
64
64
  executables: []
65
65
  extensions: []
66
66
  extra_rdoc_files: []
@@ -80,14 +80,15 @@ files:
80
80
  - lib/tuning/validations/count.rb
81
81
  - lib/tuning/validations/time.rb
82
82
  - lib/tuning/version.rb
83
- - test/controllers_test.rb
83
+ - test/controller_test.rb
84
84
  - test/dummy/Rakefile
85
85
  - test/dummy/app/assets/javascripts/application.js
86
86
  - test/dummy/app/assets/stylesheets/application.css
87
87
  - test/dummy/app/controllers/application_controller.rb
88
88
  - test/dummy/app/helpers/application_helper.rb
89
89
  - test/dummy/app/mailers/user_mailer.rb
90
- - test/dummy/app/models/shop.rb
90
+ - test/dummy/app/models/session.rb
91
+ - test/dummy/app/models/user.rb
91
92
  - test/dummy/app/views/layouts/application.html.erb
92
93
  - test/dummy/app/views/layouts/market.html.erb
93
94
  - test/dummy/app/views/user_mailer/invite.text.erb
@@ -115,19 +116,19 @@ files:
115
116
  - test/dummy/config/locales/en.yml
116
117
  - test/dummy/config/routes.rb
117
118
  - test/dummy/config/secrets.yml
118
- - test/dummy/db/migrate/20161020200126_create_shops.rb
119
- - test/dummy/db/migrate/20161022144717_create_records.rb
119
+ - test/dummy/db/migrate/20161020200126_create_users.rb
120
120
  - test/dummy/db/schema.rb
121
+ - test/dummy/log/development.log
121
122
  - test/dummy/log/test.log
122
123
  - test/dummy/public/404.html
123
124
  - test/dummy/public/422.html
124
125
  - test/dummy/public/500.html
125
126
  - test/dummy/public/favicon.ico
126
- - test/helpers_test.rb
127
- - test/mailers_test.rb
128
- - test/records_test.rb
127
+ - test/helper_test.rb
128
+ - test/mailer_test.rb
129
+ - test/record_test.rb
129
130
  - test/test_helper.rb
130
- - test/validations_test.rb
131
+ - test/validation_test.rb
131
132
  homepage: https://github.com/mmontossi/tuning
132
133
  licenses:
133
134
  - MIT
@@ -153,13 +154,14 @@ signing_key:
153
154
  specification_version: 4
154
155
  summary: Tuning for rails.
155
156
  test_files:
156
- - test/controllers_test.rb
157
+ - test/controller_test.rb
157
158
  - test/dummy/app/assets/javascripts/application.js
158
159
  - test/dummy/app/assets/stylesheets/application.css
159
160
  - test/dummy/app/controllers/application_controller.rb
160
161
  - test/dummy/app/helpers/application_helper.rb
161
162
  - test/dummy/app/mailers/user_mailer.rb
162
- - test/dummy/app/models/shop.rb
163
+ - test/dummy/app/models/session.rb
164
+ - test/dummy/app/models/user.rb
163
165
  - test/dummy/app/views/layouts/application.html.erb
164
166
  - test/dummy/app/views/layouts/market.html.erb
165
167
  - test/dummy/app/views/user_mailer/invite.text.erb
@@ -187,17 +189,17 @@ test_files:
187
189
  - test/dummy/config/routes.rb
188
190
  - test/dummy/config/secrets.yml
189
191
  - test/dummy/config.ru
190
- - test/dummy/db/migrate/20161020200126_create_shops.rb
191
- - test/dummy/db/migrate/20161022144717_create_records.rb
192
+ - test/dummy/db/migrate/20161020200126_create_users.rb
192
193
  - test/dummy/db/schema.rb
194
+ - test/dummy/log/development.log
193
195
  - test/dummy/log/test.log
194
196
  - test/dummy/public/404.html
195
197
  - test/dummy/public/422.html
196
198
  - test/dummy/public/500.html
197
199
  - test/dummy/public/favicon.ico
198
200
  - test/dummy/Rakefile
199
- - test/helpers_test.rb
200
- - test/mailers_test.rb
201
- - test/records_test.rb
201
+ - test/helper_test.rb
202
+ - test/mailer_test.rb
203
+ - test/record_test.rb
202
204
  - test/test_helper.rb
203
- - test/validations_test.rb
205
+ - test/validation_test.rb
@@ -1,2 +0,0 @@
1
- class Shop < ActiveRecord::Base
2
- end
@@ -1,9 +0,0 @@
1
- class CreateShops < ActiveRecord::Migration
2
- def change
3
- create_table :shops do |t|
4
- t.boolean :enabled
5
- t.boolean :visible
6
- t.string :name
7
- end
8
- end
9
- end
@@ -1,6 +0,0 @@
1
- class CreateRecords < ActiveRecord::Migration
2
- def change
3
- create_table :records do |t|
4
- end
5
- end
6
- end
data/test/mailers_test.rb DELETED
@@ -1,10 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MailersTest < ActionMailer::TestCase
4
-
5
- test 'normalization' do
6
- email = UserMailer.invite('test@mail.com')
7
- assert_equal "Hi there,\n\nPlease come check our website!\n\n-- Signature\n", email.body.to_s
8
- end
9
-
10
- end
data/test/records_test.rb DELETED
@@ -1,16 +0,0 @@
1
- require 'test_helper'
2
-
3
- class RecordsTest < ActiveSupport::TestCase
4
-
5
- test 'nilify blanks' do
6
- shop = Shop.new(name: '')
7
- shop.save
8
- assert_nil shop.name
9
- assert_nil shop.reload.name
10
- end
11
-
12
- test 'validate' do
13
- assert Shop.new.respond_to?(:validate)
14
- end
15
-
16
- end
@@ -1,192 +0,0 @@
1
- require 'test_helper'
2
-
3
- class ValidationsTest < ActiveSupport::TestCase
4
-
5
- test 'methods' do
6
- with_models.each do |model|
7
- %i(complexity count time).each do |validator|
8
- method = "validates_#{validator}_of"
9
- assert model.respond_to?(method)
10
- end
11
- end
12
- end
13
-
14
- test 'complexity' do
15
- with_models{
16
- attr_accessor :password
17
- validates :password, complexity: true
18
- }.each do |model|
19
- instance = model.new(password: nil)
20
- assert_not instance.valid?
21
-
22
- instance = model.new(password: 'aaaa')
23
- assert_not instance.valid?
24
- assert instance.errors.added?(:password, :too_easy)
25
-
26
- instance = model.new(password: 'aaAA33__')
27
- assert instance.valid?
28
- end
29
- end
30
-
31
- test 'count' do
32
- with_models{
33
- attr_accessor :list
34
- validates :list, count: true
35
- }.each do |model|
36
- instance = model.new(list: nil)
37
- assert_not instance.valid?
38
- assert instance.errors.added?(:list, :uncountable)
39
- end
40
-
41
- with_models{
42
- attr_accessor :list
43
- validates :list, count: { within: 1..2 }
44
- }.each do |model|
45
- instance = model.new(list: [1, 2, 3])
46
- assert_not instance.valid?
47
- assert instance.errors.added?(:list, :too_many)
48
-
49
- instance = model.new(list: [])
50
- assert_not instance.valid?
51
- assert instance.errors.added?(:list, :too_few)
52
-
53
- instance = model.new(list: [1])
54
- assert instance.valid?
55
- end
56
-
57
- with_models{
58
- attr_accessor :list
59
- validates :list, count: { in: 1..2 }
60
- }.each do |model|
61
- instance = model.new(list: [1, 2, 3])
62
- assert_not instance.valid?
63
- assert instance.errors.added?(:list, :too_many)
64
-
65
- instance = model.new(list: [])
66
- assert_not instance.valid?
67
- assert instance.errors.added?(:list, :too_few)
68
-
69
- instance = model.new(list: [1])
70
- assert instance.valid?
71
- end
72
-
73
- with_models{
74
- attr_accessor :list
75
- validates :list, count: { maximum: 2 }
76
- }.each do |model|
77
- instance = model.new(list: [1, 2, 3])
78
- assert_not instance.valid?
79
- assert instance.errors.added?(:list, :too_many)
80
-
81
- instance = model.new(list: [])
82
- assert instance.valid?
83
- end
84
-
85
- with_models{
86
- attr_accessor :list
87
- validates :list, count: { minimum: 1 }
88
- }.each do |model|
89
- instance = model.new(list: [])
90
- assert_not instance.valid?
91
- assert instance.errors.added?(:list, :too_few)
92
-
93
- instance = model.new(list: [1])
94
- assert instance.valid?
95
- end
96
- end
97
-
98
- test 'time' do
99
- with_models{
100
- attr_accessor :delivered_at
101
- validates :delivered_at, time: true
102
- }.each do |model|
103
- instance = model.new(delivered_at: nil)
104
- assert_not instance.valid?
105
- assert instance.errors.added?(:delivered_at, :not_a_time)
106
- end
107
-
108
- with_models{
109
- attr_accessor :delivered_at
110
- validates :delivered_at, time: { before: Proc.new { Date.today } }
111
- }.each do |model|
112
- instance = model.new(delivered_at: Date.today)
113
- assert_not instance.valid?
114
- assert instance.errors.added?(:delivered_at, :before)
115
-
116
- instance = model.new(delivered_at: (Time.now + 1.day))
117
- assert_not instance.valid?
118
- assert instance.errors.added?(:delivered_at, :before)
119
-
120
- instance = model.new(delivered_at: (Date.today - 1.day))
121
- assert instance.valid?
122
- end
123
-
124
- with_models{
125
- attr_accessor :delivered_at
126
- validates :delivered_at, time: { before_or_equal_to: Date.today }
127
- }.each do |model|
128
- instance = model.new(delivered_at: (Time.now + 1.day))
129
- assert_not instance.valid?
130
- assert instance.errors.added?(:delivered_at, :before_or_equal_to)
131
-
132
- instance = model.new(delivered_at: Date.today)
133
- assert instance.valid?
134
-
135
- instance = model.new(delivered_at: (Date.today - 1.day))
136
- assert instance.valid?
137
- end
138
-
139
- with_models{
140
- attr_accessor :processed_at, :delivered_at
141
- validates :delivered_at, time: { after: ->(instance) { instance.processed_at } }
142
- }.each do |model|
143
- instance = model.new(delivered_at: Date.today, processed_at: Date.today)
144
- assert_not instance.valid?
145
- assert instance.errors.added?(:delivered_at, :after)
146
-
147
- instance = model.new(delivered_at: (Date.today - 1.day), processed_at: Date.today)
148
- assert_not instance.valid?
149
- assert instance.errors.added?(:delivered_at, :after)
150
-
151
- instance = model.new(delivered_at: (Time.now + 1.day), processed_at: Date.today)
152
- assert instance.valid?
153
- end
154
-
155
- with_models{
156
- attr_accessor :processed_at, :delivered_at
157
- validates :delivered_at, time: { after_or_equal_to: :processed_at }
158
- }.each do |model|
159
- instance = model.new(delivered_at: (Date.today - 1.day), processed_at: Date.today)
160
- assert_not instance.valid?
161
- assert instance.errors.added?(:delivered_at, :after_or_equal_to)
162
-
163
- instance = model.new(delivered_at: Date.today, processed_at: Date.today)
164
- assert instance.valid?
165
-
166
- instance = model.new(delivered_at: (Date.today + 1.day), processed_at: Date.today)
167
- assert instance.valid?
168
- end
169
- end
170
-
171
- private
172
-
173
- def with_models(&block)
174
- record = Class.new(ActiveRecord::Base)
175
- model = Class.new do
176
- include ActiveModel::Model
177
- end
178
- if block_given?
179
- record.class_eval(&block)
180
- model.class_eval(&block)
181
- end
182
- [set_constant('Record', record), set_constant('Model', model)]
183
- end
184
-
185
- def set_constant(name, value)
186
- if self.class.const_defined?(name)
187
- self.class.send :remove_const, name
188
- end
189
- self.class.const_set(name, value)
190
- end
191
-
192
- end