measured-rails 2.8.1 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,100 +0,0 @@
1
- # This file is auto-generated from the current state of the database. Instead
2
- # of editing this file, please use the migrations feature of Active Record to
3
- # incrementally modify your database, and then regenerate this schema definition.
4
- #
5
- # Note that this schema.rb definition is the authoritative source for your
6
- # database schema. If you need to create the application database on another
7
- # system, you should be using db:schema:load, not running all the migrations
8
- # from scratch. The latter is a flawed and unsustainable approach (the more migrations
9
- # you'll amass, the slower it'll run and the greater likelihood for issues).
10
- #
11
- # It's strongly recommended that you check this file into your version control system.
12
-
13
- ActiveRecord::Schema.define(version: 20211105120355) do
14
-
15
- create_table "thing_with_custom_unit_accessors", force: :cascade do |t|
16
- t.decimal "length_value", precision: 10, scale: 2
17
- t.decimal "width_value", precision: 10, scale: 2
18
- t.decimal "height_value", precision: 10, scale: 2
19
- t.decimal "volume_value", precision: 10, scale: 2
20
- t.string "volume_unit", limit: 12
21
- t.string "size_unit", limit: 12
22
- t.decimal "total_weight_value", precision: 10, scale: 2, default: "10.0"
23
- t.decimal "extra_weight_value", precision: 10, scale: 2
24
- t.string "weight_unit", limit: 12
25
- t.datetime "created_at", null: false
26
- t.datetime "updated_at", null: false
27
- end
28
-
29
- create_table "things", force: :cascade do |t|
30
- t.decimal "length_value", precision: 10, scale: 2
31
- t.string "length_unit", limit: 12
32
- t.decimal "width_value", precision: 10, scale: 2
33
- t.string "width_unit", limit: 12
34
- t.decimal "height_value", precision: 10, scale: 2
35
- t.string "height_unit", limit: 12
36
- t.decimal "volume_value", precision: 10, scale: 2
37
- t.string "volume_unit", limit: 12
38
- t.decimal "total_weight_value", precision: 10, scale: 2, default: "10.0"
39
- t.string "total_weight_unit", limit: 12, default: "g"
40
- t.decimal "extra_weight_value", precision: 10, scale: 2
41
- t.string "extra_weight_unit", limit: 12
42
- t.decimal "length_with_max_on_assignment_value", precision: 10, scale: 2
43
- t.string "length_with_max_on_assignment_unit", limit: 12
44
- t.datetime "created_at", null: false
45
- t.datetime "updated_at", null: false
46
- end
47
-
48
- create_table "validated_things", force: :cascade do |t|
49
- t.decimal "length_value", precision: 10, scale: 2
50
- t.string "length_unit", limit: 12
51
- t.decimal "length_true_value", precision: 10, scale: 2
52
- t.string "length_true_unit", limit: 12
53
- t.decimal "length_message_value", precision: 10, scale: 2
54
- t.string "length_message_unit", limit: 12
55
- t.decimal "length_message_from_block_value", precision: 10, scale: 2
56
- t.string "length_message_from_block_unit", limit: 12
57
- t.decimal "length_units_value", precision: 10, scale: 2
58
- t.string "length_units_unit", limit: 12
59
- t.decimal "length_units_singular_value", precision: 10, scale: 2
60
- t.string "length_units_singular_unit", limit: 12
61
- t.decimal "length_presence_value", precision: 10, scale: 2
62
- t.string "length_presence_unit", limit: 12
63
- t.decimal "length_invalid_value", precision: 10, scale: 2
64
- t.string "length_invalid_unit", limit: 12
65
- t.datetime "created_at", null: false
66
- t.datetime "updated_at", null: false
67
- t.decimal "length_numericality_inclusive_value", precision: 10, scale: 2
68
- t.string "length_numericality_inclusive_unit", limit: 12
69
- t.decimal "length_numericality_exclusive_value", precision: 10, scale: 2
70
- t.string "length_numericality_exclusive_unit", limit: 12
71
- t.decimal "length_numericality_equality_value", precision: 10, scale: 2
72
- t.string "length_numericality_equality_unit", limit: 12
73
- t.decimal "length_invalid_comparison_value", precision: 10, scale: 2
74
- t.string "length_invalid_comparison_unit", limit: 12
75
- t.decimal "length_non_zero_scalar_value", precision: 10, scale: 2
76
- t.string "length_non_zero_scalar_unit", limit: 12
77
- t.decimal "length_zero_scalar_value", precision: 10, scale: 2
78
- t.string "length_zero_scalar_unit", limit: 12
79
- t.decimal "length_numericality_less_than_than_scalar_value", precision: 10, scale: 2
80
- t.string "length_numericality_less_than_than_scalar_unit", limit: 12
81
- end
82
-
83
- create_table "thing_with_custom_value_accessors", force: :cascade do |t|
84
- t.decimal "custom_length", precision: 10, scale: 2
85
- t.string "length_unit", limit: 12
86
- t.decimal "custom_width", precision: 10, scale: 2
87
- t.string "width_unit", limit: 12
88
- t.decimal "custom_height", precision: 10, scale: 2
89
- t.string "height_unit", limit: 12
90
- t.decimal "custom_volume", precision: 10, scale: 2
91
- t.string "volume_unit", limit: 12
92
- t.decimal "custom_weight", precision: 10, scale: 2, default: "10.0"
93
- t.string "total_weight_unit", limit: 12
94
- t.decimal "custom_extra_weight", precision: 10, scale: 2
95
- t.string "extra_weight_unit", limit: 12
96
- t.datetime "created_at", null: false
97
- t.datetime "updated_at", null: false
98
- end
99
-
100
- end
data/test/test_helper.rb DELETED
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
- require "pry" unless ENV["CI"]
3
- require "measured"
4
- require "measured-rails"
5
- require "minitest/autorun"
6
- require "minitest/reporters"
7
- require "mocha/minitest"
8
-
9
- ActiveSupport.test_order = :random
10
-
11
- # Prevent two reporters from printing
12
- # https://github.com/kern/minitest-reporters/issues/230
13
- # https://github.com/rails/rails/issues/30491
14
- Minitest.load_plugins
15
- Minitest.extensions.delete('rails')
16
- Minitest.extensions.unshift('rails')
17
-
18
- Minitest::Reporters.use! [Minitest::Reporters::ProgressReporter.new(color: true)]
19
-
20
- class ActiveSupport::TestCase
21
- def reset_db
22
- ActiveRecord::Base.subclasses.each do |model|
23
- model.delete_all
24
- end
25
- end
26
- end
27
-
28
- Dir.glob(File.expand_path("../support/models/*.rb", __FILE__)).each { |r| require r }
29
- ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
30
- require_relative "support/schema.rb"
@@ -1,238 +0,0 @@
1
- # frozen_string_literal: true
2
- require "test_helper"
3
-
4
- class Measured::Rails::ValidationTest < ActiveSupport::TestCase
5
- setup do
6
- reset_db
7
- end
8
-
9
- test "validation mock is valid" do
10
- assert thing.valid?
11
- end
12
-
13
- test "validation measurable: validation leaves a model valid and deals with blank unit" do
14
- assert ValidatedThing.new(length_presence: Measured::Length.new(4, :in)).valid?
15
- end
16
-
17
- test "validation fails when unit is nil" do
18
- thing.length_unit = ''
19
- refute thing.valid?
20
- assert_equal({ length: ["is not a valid unit"] }, thing.errors.to_hash)
21
- end
22
-
23
- test "validation fails on model with custom unit with nil value" do
24
- custom_unit_thing.size_unit = ''
25
- refute custom_unit_thing.valid?
26
- assert_equal(
27
- {
28
- length: ["is not a valid unit"],
29
- width: ["is not a valid unit"],
30
- height: ["is not a valid unit"],
31
- },
32
- custom_unit_thing.errors.to_hash
33
- )
34
- end
35
-
36
- test "validation true works by default" do
37
- thing.length_unit = "junk"
38
- refute thing.valid?
39
- assert_equal ["Length is not a valid unit"], thing.errors.full_messages
40
- end
41
-
42
- test "validation can override the message with a static string" do
43
- thing.length_message_unit = "junk"
44
- refute thing.valid?
45
- assert_equal ["Length message has a custom failure message"], thing.errors.full_messages
46
- end
47
-
48
- test "validation can override the message with a block" do
49
- thing.length_message_from_block_unit = "junk"
50
- refute thing.valid?
51
- assert_equal ["Length message from block junk is not a valid unit"], thing.errors.full_messages
52
- end
53
-
54
- test "validation may be any valid unit" do
55
- length_units.each do |unit|
56
- thing.length_unit = unit
57
- assert thing.valid?
58
- thing.length_unit = unit.to_s
59
- assert thing.valid?
60
- thing.length = Measured::Length.new(123, unit)
61
- assert thing.valid?
62
- end
63
- end
64
-
65
- test "validation accepts a list of units in any format as an option and only allows them to be valid" do
66
- thing.length_units_unit = :m
67
- assert thing.valid?
68
- thing.length_units_unit = :cm
69
- assert thing.valid?
70
- thing.length_units_unit = "cm"
71
- assert thing.valid?
72
- thing.length_units_unit = "meter"
73
- assert thing.valid?
74
- thing.length_units = Measured::Length.new(3, :cm)
75
- assert thing.valid?
76
- thing.length_units_unit = :mm
77
- refute thing.valid?
78
- thing.length_units = Measured::Length.new(3, :ft)
79
- refute thing.valid?
80
- end
81
-
82
- test "validation lets the unit be singular" do
83
- thing.length_units_singular_unit = :ft
84
- assert thing.valid?
85
- thing.length_units_singular_unit = "feet"
86
- assert thing.valid?
87
- thing.length_units_singular_unit = :mm
88
- refute thing.valid?
89
- thing.length_units_singular_unit = "meter"
90
- refute thing.valid?
91
- end
92
-
93
- test "validation for unit reasons uses the default message" do
94
- thing.length_units_unit = :mm
95
- refute thing.valid?
96
- assert_equal ["Length units is not a valid unit"], thing.errors.full_messages
97
- end
98
-
99
- test "validation for unit reasons also uses the custom message" do
100
- thing.length_units_singular_unit = :mm
101
- refute thing.valid?
102
- assert_equal ["Length units singular custom message too"], thing.errors.full_messages
103
- end
104
-
105
- test "validation for unit reasons adds one message if unit is not supported by default and is not custom supported" do
106
- thing.length_units_singular_unit = :t
107
- refute thing.valid?
108
-
109
- assert_equal ["Length units singular custom message too"], thing.errors.full_messages
110
- end
111
-
112
- test "validation presence works on measured columns" do
113
- thing.length_presence = nil
114
- refute thing.valid?
115
- assert_equal ["Length presence can't be blank"], thing.errors.full_messages
116
- thing.length_presence_unit = "m"
117
- refute thing.valid?
118
- thing.length_presence_value = "3"
119
- assert thing.valid?
120
- end
121
-
122
- test "validation fails if only only the value is set" do
123
- thing.length_unit = nil
124
- refute thing.valid?
125
- end
126
-
127
- test "validation checks that numericality comparisons are against a Measurable subclass" do
128
- thing.length_invalid_comparison = Measured::Length.new(30, :in)
129
- assert_raises ArgumentError, ":not_a_measured_subclass must be a Measurable object" do
130
- thing.valid?
131
- end
132
- end
133
-
134
- test "validation for numericality uses a default invalid message" do
135
- thing.length_numericality_inclusive = Measured::Length.new(30, :in)
136
- refute thing.valid?
137
- assert_equal ["Length numericality inclusive 30 in must be <= 20 in"], thing.errors.full_messages
138
-
139
- thing.length_numericality_inclusive = Measured::Length.new(1, :mm)
140
- refute thing.valid?
141
- assert_equal ["Length numericality inclusive 1 mm must be >= 10 in"], thing.errors.full_messages
142
- end
143
-
144
- test "validation for numericality uses the override message" do
145
- thing.length_numericality_exclusive = Measured::Length.new(2, :m)
146
- refute thing.valid?
147
- assert_equal ["Length numericality exclusive is super not ok"], thing.errors.full_messages
148
-
149
- thing.length_numericality_exclusive = Measured::Length.new(6000, :mm)
150
- refute thing.valid?
151
- assert_equal ["Length numericality exclusive is super not ok"], thing.errors.full_messages
152
- end
153
-
154
- test "validation for numericality checks :greater_than and :less_than and can use symbols as method names to look up values" do
155
- thing.length_numericality_exclusive = Measured::Length.new(4, :m)
156
- assert thing.valid?
157
-
158
- thing.length_numericality_exclusive = Measured::Length.new(1, :m)
159
- refute thing.valid?
160
- end
161
-
162
- test "validation for numericality checks :greater_than_or_equal_to and :less_than_or_equal_to" do
163
- thing.length_numericality_inclusive = Measured::Length.new(10, :in)
164
- assert thing.valid?
165
-
166
- thing.length_numericality_exclusive = Measured::Length.new(3, :m)
167
- refute thing.valid?
168
- end
169
-
170
- test "validation for numericality checks :equal_to and can use procs to look up values" do
171
- thing.length_numericality_equality = Measured::Length.new(100, :cm)
172
- assert thing.valid?
173
-
174
- thing.length_numericality_equality = Measured::Length.new(1, :m)
175
- assert thing.valid?
176
-
177
- thing.length_numericality_equality = Measured::Length.new("99.9", :cm)
178
- refute thing.valid?
179
-
180
- thing.length_numericality_equality = Measured::Length.new(101, :cm)
181
- refute thing.valid?
182
- end
183
-
184
- test "validation for numericality handles a nil unit but a valid value" do
185
- thing.length_numericality_exclusive_unit = nil
186
- thing.length_numericality_exclusive_value = 1
187
- refute thing.valid?
188
- end
189
-
190
- test "allow a nil value but a valid unit" do
191
- thing.length_numericality_exclusive_unit = :cm
192
- thing.length_numericality_exclusive_value = nil
193
- assert thing.valid?
194
- end
195
-
196
- test "validations work as expected given a measured field with custom validators" do
197
- assert custom_unit_thing.valid?
198
-
199
- assert custom_unit_thing.size_unit = 'invalid'
200
-
201
- refute custom_unit_thing.valid?
202
-
203
- assert_equal(custom_unit_thing.errors[:length], ["is not a valid unit"])
204
- assert_equal(custom_unit_thing.errors[:height], ["is not a valid unit"])
205
- assert_equal(custom_unit_thing.errors[:width], ["is not a valid unit"])
206
- end
207
-
208
- private
209
-
210
- def thing
211
- @thing ||= ValidatedThing.new(
212
- length: Measured::Length.new(1, :m),
213
- length_true: Measured::Length.new(2, :cm),
214
- length_message: Measured::Length.new(3, :mm),
215
- length_message_from_block: Measured::Length.new(7, :mm),
216
- length_units: Measured::Length.new(4, :m),
217
- length_units_singular: Measured::Length.new(5, :ft),
218
- length_presence: Measured::Length.new(6, :m),
219
- length_numericality_inclusive: Measured::Length.new(15, :in),
220
- length_numericality_exclusive: Measured::Length.new(4, :m),
221
- length_numericality_equality: Measured::Length.new(100, :cm),
222
- )
223
- end
224
-
225
- def custom_unit_thing
226
- @custom_unit_thing ||= ThingWithCustomUnitAccessor.new(
227
- length: Measured::Length.new(1, :m),
228
- width: Measured::Length.new(2, :m),
229
- height: Measured::Length.new(3, :m),
230
- total_weight: Measured::Weight.new(10, :g),
231
- extra_weight: Measured::Weight.new(12, :g),
232
- )
233
- end
234
-
235
- def length_units
236
- @length_units ||= [:m, :meter, :cm, :mm, :millimeter, :in, :ft, :feet, :yd]
237
- end
238
- end