unidom-common-rspec 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d0d48e1ac08e611d4dd95378ee882855250685c7
4
- data.tar.gz: 98f45cc9b4fc220ab9acb8e7fc5a9caa0b220d11
3
+ metadata.gz: 423975fbbd2d45be90dad54b10bb33d9fca86ca3
4
+ data.tar.gz: 972f708c3a3cd7ce84ce54dca26b7db5ac5d5f82
5
5
  SHA512:
6
- metadata.gz: 856486f87c49af80c99b7e2b87fd205ec097d6be20eb9d70868518c669c4e3013e3ca646344fddc2aaef951aa4713a92e79782293e09e01969e4eed05f63d770
7
- data.tar.gz: 930fb8d810301ce089166d29018ed08c9e50d3c3da630419aa384b7ef10b3205e9e180085840a0c3ccbfa5f1dacea4cbb00e8b941e66e0762b8edbb507f31efc
6
+ metadata.gz: fe7a08d3627566465e51b205c9936bfa8bb5b9c7523c9a6b100c9243d2a4e36c7a9f2e93c9e051d2e3825060f663e18fa7dd57d8ba92a8ec10d0350e2d52701e
7
+ data.tar.gz: aae6459986887bfb5d5002d0c42363910f7ffb86de2ae1dd52ffbce38c814002ded13906ecd42af120d778371c4996bd1c144e4687ec14c47c7079e5e00492ec
data/CHANGELOG.md CHANGED
@@ -5,3 +5,6 @@
5
5
 
6
6
  ## v0.2
7
7
  1. Validates shared examples
8
+
9
+ ## v0.3
10
+ 1. Model Extension shared examples
data/README.md CHANGED
@@ -51,18 +51,21 @@ describe Person, type: :model do
51
51
  it_behaves_like 'scope', :all, [
52
52
  { attributes_collection: [ model_attributes ], count_diff: 1, args: [] },
53
53
  { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 1, args: [] },
54
- { attributes_collection: [ model_attributes.merge(closed_at: Time.now) ], count_diff: 1, args: [] } ]
54
+ { attributes_collection: [ model_attributes.merge(closed_at: Time.now) ], count_diff: 1, args: [] }
55
+ ]
55
56
 
56
57
  it_behaves_like 'scope', :none, [
57
58
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [] },
58
59
  { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 0, args: [] },
59
- { attributes_collection: [ model_attributes.merge(closed_at: Time.now) ], count_diff: 0, args: [] } ]
60
+ { attributes_collection: [ model_attributes.merge(closed_at: Time.now) ], count_diff: 0, args: [] }
61
+ ]
60
62
 
61
63
  it_behaves_like 'scope', :transited_to, [
62
64
  { attributes_collection: [ model_attributes ], count_diff: 1, args: [ 'C' ] },
63
65
  { attributes_collection: [ model_attributes ], count_diff: 0, args: [ 'A' ] },
64
66
  { attributes_collection: [ model_attributes.merge(state: 'A') ], count_diff: 0, args: [ 'C' ] },
65
- { attributes_collection: [ model_attributes.merge(state: 'A') ], count_diff: 1, args: [ 'A' ] } ]
67
+ { attributes_collection: [ model_attributes.merge(state: 'A') ], count_diff: 1, args: [ 'A' ] }
68
+ ]
66
69
 
67
70
  end
68
71
 
@@ -96,7 +99,24 @@ describe Person, type: :model do
96
99
  end
97
100
  ```
98
101
 
102
+ ### Model Extension shared examples Model Extension 共享用例
99
103
 
104
+ Assume the model class is ``Person``, and the model alread extend the ``Unidom::Common::Concerns::ModelExtension``, the ``person_spec.rb`` looks like the following:
105
+ ```ruby
106
+ require 'rails_helper'
107
+
108
+ describe Person, type: :model do
109
+
110
+ context do
111
+
112
+ tim_attributes = { name: 'Tim' }
113
+
114
+ it_behaves_like 'Unidom::Common::Concerns::ModelExtension', model_attributes
115
+
116
+ end
117
+
118
+ end
119
+ ```
100
120
 
101
121
  ## Development
102
122
 
data/ROADMAP.md CHANGED
@@ -5,3 +5,6 @@
5
5
 
6
6
  ## v0.2
7
7
  1. Validates shared examples
8
+
9
+ ## v0.3
10
+ 1. Model Extension shared examples
@@ -2,6 +2,7 @@ require "unidom/common/rspec/version"
2
2
 
3
3
  require 'unidom/common/rspec/scope_shared_examples'
4
4
  require 'unidom/common/rspec/validates_shared_examples'
5
+ require 'unidom/common/rspec/model_extension_shared_examples'
5
6
 
6
7
  module Unidom
7
8
  module Common
@@ -0,0 +1,314 @@
1
+ shared_examples 'Unidom::Common::Concerns::ModelExtension' do |model_attributes|
2
+
3
+ # scopes for the #id column
4
+
5
+ describe '.included_by' do
6
+
7
+ id = SecureRandom.uuid
8
+
9
+ it_behaves_like 'scope', :included_by, [
10
+ { attributes_collection: [ model_attributes.merge(id: id) ], count_diff: 1, args: [ id ] },
11
+ { attributes_collection: [ model_attributes.merge(id: id) ], count_diff: 0, args: [ Unidom::Common::NULL_UUID ] }
12
+ ]
13
+
14
+ end
15
+
16
+ describe '.excluded_by' do
17
+
18
+ id = SecureRandom.uuid
19
+
20
+ it_behaves_like 'scope', :excluded_by, [
21
+ { attributes_collection: [ model_attributes.merge(id: id) ], count_diff: 0, args: [ id ] },
22
+ { attributes_collection: [ model_attributes.merge(id: id) ], count_diff: 1, args: [ Unidom::Common::NULL_UUID ] }
23
+ ]
24
+
25
+ end
26
+
27
+ # validations & scopes for the #state column
28
+
29
+ describe '#state' do
30
+
31
+ it 'column exists' do expect(described_class.columns_hash['state']).to be_present end
32
+ it 'column length must be 1' do expect(described_class.columns_hash['state'].limit).to eq(1) end
33
+
34
+ it_behaves_like 'validates', model_attributes, :state,
35
+ { } => 0,
36
+ { state: nil } => 2,
37
+ { state: '' } => 2,
38
+ { state: 'A' } => 0,
39
+ { state: 'AA' } => 1
40
+
41
+ end
42
+
43
+ describe '.transited_to' do
44
+
45
+ it_behaves_like 'scope', :transited_to, [
46
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ 'C' ] },
47
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ 'A' ] },
48
+ { attributes_collection: [ model_attributes.merge(state: 'A') ], count_diff: 0, args: [ 'C' ] },
49
+ { attributes_collection: [ model_attributes.merge(state: 'A') ], count_diff: 1, args: [ 'A' ] }
50
+ ]
51
+
52
+ end
53
+
54
+ # scopes for the #opened_at column & the #closed_at column
55
+
56
+ describe '#opened_at' do
57
+ it 'column exists' do expect(described_class.columns_hash['opened_at']).to be_present end
58
+ end
59
+
60
+ describe '#closed_at' do
61
+ it 'column exists' do expect(described_class.columns_hash['closed_at']).to be_present end
62
+ end
63
+
64
+ describe '.valid_at' do
65
+
66
+ opened_at = Time.utc described_class.columns_hash['opened_at'].default
67
+ closed_at = Time.utc described_class.columns_hash['closed_at'].default
68
+
69
+ it_behaves_like 'scope', :valid_at, [
70
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [] },
71
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ { now: opened_at-1.second } ] },
72
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ { now: opened_at } ] },
73
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ { now: opened_at+1.second } ] },
74
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ { now: closed_at-1.second } ] },
75
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ { now: closed_at } ] },
76
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ { now: closed_at+1.second } ] }
77
+ ]
78
+
79
+ it_behaves_like 'scope', :valid_duration, [
80
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ opened_at-2.second..opened_at-1.second ] },
81
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at-1.second..closed_at-1.second ] },
82
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at-1.second..closed_at ] },
83
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at-1.second..closed_at+1.second ] },
84
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at..closed_at-1.second ] },
85
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at..closed_at ] },
86
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at..closed_at+1.second ] },
87
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ opened_at+1.second..closed_at-1.second ] },
88
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at+1.second..closed_at ] },
89
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ opened_at+1.second..closed_at+1.second ] },
90
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ closed_at+1.second..closed_at+2.second ] }
91
+ ]
92
+
93
+ end
94
+
95
+ # scopes for the #defunct column
96
+
97
+ describe '#defunct' do
98
+ it 'column exists' do expect(described_class.columns_hash['defunct']).to be_present end
99
+ end
100
+
101
+ describe '.alive' do
102
+
103
+ it_behaves_like 'scope', :alive, [
104
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [] },
105
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ { living: true } ] },
106
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ { living: false } ] },
107
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 0, args: [] },
108
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 0, args: [ { living: true } ] },
109
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 1, args: [ { living: false } ] }
110
+ ]
111
+
112
+ end
113
+
114
+ describe '.dead' do
115
+
116
+ it_behaves_like 'scope', :dead, [
117
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [] },
118
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ { defunct: true } ] },
119
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ { defunct: false } ] },
120
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 1, args: [] },
121
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 1, args: [ { defunct: true } ] },
122
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 0, args: [ { defunct: false } ] }
123
+ ]
124
+
125
+ end
126
+
127
+ # validations & scopes for the #ordinal column
128
+
129
+ ordinal_column = described_class.columns_hash['ordinal']
130
+
131
+ if ordinal_column.present?&&:integer==ordinal_column.type
132
+
133
+ it_behaves_like 'validates', model_attributes, :ordinal,
134
+ { } => 3,
135
+ { ordinal: nil } => 3,
136
+ { ordinal: '' } => 3,
137
+ { ordinal: 'A' } => 2,
138
+ { ordinal: 'AA' } => 2,
139
+ { ordinal: '0' } => 1,
140
+ { ordinal: 0 } => 1,
141
+ { ordinal: -1 } => 1,
142
+ { ordinal: -11 } => 1,
143
+ { ordinal: 1.1 } => 1,
144
+ { ordinal: '1' } => 0,
145
+ { ordinal: '11' } => 0,
146
+ { ordinal: 1 } => 0,
147
+ { ordinal: 11 } => 0
148
+
149
+ it_behaves_like 'scope', :ordinal_is, [
150
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:ordinal]-1 ] },
151
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:ordinal] ] },
152
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:ordinal]+1 ] }
153
+ ]
154
+
155
+ end
156
+
157
+ # validations & scopes for the #uuid column
158
+
159
+ uuid_column = described_class.columns_hash['uuid']
160
+
161
+ if uuid_column.present?
162
+
163
+ it_behaves_like 'validates', model_attributes, :uuid,
164
+ { } => 2,
165
+ { uuid: nil } => 2,
166
+ { uuid: '' } => 2,
167
+ { uuid: 'A' } => 1,
168
+ { uuid: 'AA' } => 1,
169
+ { uuid: Unidom::Common::NULL_UUID[0..34] } => 1,
170
+ { uuid: Unidom::Common::NULL_UUID } => 1,
171
+ { uuid: "#{Unidom::Common::NULL_UUID}1" } => 1
172
+
173
+ it_behaves_like 'scope', :uuid_is, [
174
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:uuid] ] },
175
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:uuid] ] },
176
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:uuid] ] }
177
+ ]
178
+
179
+ end
180
+
181
+ # scopes for the #elemental column
182
+
183
+ elemental_column = described_class.columns_hash['elemental']
184
+
185
+ if elemental_column.present?
186
+
187
+ it_behaves_like 'scope', :primary, [
188
+ { attributes_collection: [ model_attributes.merge(elemental: true ) ], count_diff: 1, args: [ true ] },
189
+ { attributes_collection: [ model_attributes.merge(elemental: true ) ], count_diff: 0, args: [ false ] },
190
+ { attributes_collection: [ model_attributes.merge(elemental: false) ], count_diff: 0, args: [ true ] },
191
+ { attributes_collection: [ model_attributes.merge(elemental: false) ], count_diff: 1, args: [ false ] }
192
+ ]
193
+
194
+ end
195
+
196
+ # validations & scopes for the #grade column
197
+
198
+ grade_column = described_class.columns_hash['grade']
199
+
200
+ if grade_column.present?&&:integer==grade_column.type
201
+
202
+ it_behaves_like 'validates', model_attributes, :grade,
203
+ { } => 3,
204
+ { grade: nil } => 3,
205
+ { grade: '' } => 3,
206
+ { grade: 'A' } => 2,
207
+ { grade: 'AA' } => 2,
208
+ { grade: -1 } => 1,
209
+ { grade: -11 } => 1,
210
+ { grade: 1.1 } => 1,
211
+ { grade: '0' } => 0,
212
+ { grade: '1' } => 0,
213
+ { grade: '11' } => 0,
214
+ { grade: 0 } => 0,
215
+ { grade: 1 } => 0,
216
+ { grade: 11 } => 0
217
+
218
+ it_behaves_like 'scope', :grade_is, [
219
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:grade]-1 ] },
220
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:grade] ] },
221
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:grade]+1 ] }
222
+ ]
223
+
224
+ it_behaves_like 'scope', :grade_higher_than, [
225
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:grade]-1 ] },
226
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:grade] ] },
227
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:grade]+1 ] }
228
+ ]
229
+
230
+ it_behaves_like 'scope', :grade_lower_than, [
231
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:grade]-1 ] },
232
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:grade] ] },
233
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:grade]+1 ] }
234
+ ]
235
+
236
+ end
237
+
238
+ # validations & scopes for the #priority column
239
+
240
+ priority_column = described_class.columns_hash['priority']
241
+
242
+ if priority_column.present?&&:integer==priority_column.type
243
+
244
+ it_behaves_like 'validates', model_attributes, :priority,
245
+ { } => 3,
246
+ { priority: nil } => 3,
247
+ { priority: '' } => 3,
248
+ { priority: 'A' } => 2,
249
+ { priority: 'AA' } => 2,
250
+ { priority: -1 } => 1,
251
+ { priority: -11 } => 1,
252
+ { priority: 1.1 } => 1,
253
+ { priority: '0' } => 0,
254
+ { priority: '1' } => 0,
255
+ { priority: '11' } => 0,
256
+ { priority: 0 } => 0,
257
+ { priority: 1 } => 0,
258
+ { priority: 11 } => 0
259
+
260
+ it_behaves_like 'scope', :grade_is, [
261
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]-1 ] },
262
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:priority] ] },
263
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]+1 ] }
264
+ ]
265
+
266
+ it_behaves_like 'scope', :grade_higher_than, [
267
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:priority]-1 ] },
268
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority] ] },
269
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]+1 ] }
270
+ ]
271
+
272
+ it_behaves_like 'scope', :grade_lower_than, [
273
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority]-1 ] },
274
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [ model_attributes[:priority] ] },
275
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [ model_attributes[:priority]+1 ] }
276
+ ]
277
+
278
+ end
279
+
280
+ # scopes for the #slug column
281
+
282
+ slug_column = described_class.columns_hash['slug']
283
+
284
+ if slug_column.present?
285
+
286
+ slug = SecureRandom.uuid+SecureRandom.uuid
287
+
288
+ it_behaves_like 'scope', :slug_is, [
289
+ #{ attributes_collection: [ model_attributes.merge(slug: slug) ], count_diff: 1, args: [ slug ] },
290
+ { attributes_collection: [ model_attributes.merge(slug: slug) ], count_diff: 0, args: [ "#{slug}1" ] },
291
+ { attributes_collection: [ model_attributes.merge(slug: slug) ], count_diff: 0, args: [ slug[0..34] ] }
292
+ ]
293
+
294
+ end
295
+
296
+ # Active Record default scopes
297
+
298
+ context '.scope' do
299
+
300
+ it_behaves_like 'scope', :all, [
301
+ { attributes_collection: [ model_attributes ], count_diff: 1, args: [] },
302
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 1, args: [] },
303
+ { attributes_collection: [ model_attributes.merge(closed_at: Time.now) ], count_diff: 1, args: [] }
304
+ ]
305
+
306
+ it_behaves_like 'scope', :none, [
307
+ { attributes_collection: [ model_attributes ], count_diff: 0, args: [] },
308
+ { attributes_collection: [ model_attributes.merge(defunct: true) ], count_diff: 0, args: [] },
309
+ { attributes_collection: [ model_attributes.merge(closed_at: Time.now) ], count_diff: 0, args: [] }
310
+ ]
311
+
312
+ end
313
+
314
+ end
@@ -32,4 +32,5 @@ shared_examples 'validates' do |model_attributes, attribute_name, error_attribut
32
32
  end
33
33
 
34
34
  end
35
+
35
36
  end
@@ -1,7 +1,7 @@
1
1
  module Unidom
2
2
  module Common
3
3
  module RSpec
4
- VERSION = '0.2'
4
+ VERSION = '0.3'
5
5
  end
6
6
  end
7
7
  end
@@ -31,6 +31,6 @@ Gem::Specification.new do |spec|
31
31
 
32
32
  spec.add_development_dependency 'bundler', '~> 1.12'
33
33
  spec.add_development_dependency 'rake', '~> 10.0'
34
- spec.add_development_dependency 'rspec', "~> 3.0"
34
+ spec.add_development_dependency 'rspec', '~> 3.0'
35
35
 
36
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unidom-common-rspec
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Topbit Du
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-12-26 00:00:00.000000000 Z
11
+ date: 2016-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-rails
@@ -87,6 +87,7 @@ files:
87
87
  - bin/console
88
88
  - bin/setup
89
89
  - lib/unidom/common/rspec.rb
90
+ - lib/unidom/common/rspec/model_extension_shared_examples.rb
90
91
  - lib/unidom/common/rspec/scope_shared_examples.rb
91
92
  - lib/unidom/common/rspec/validates_shared_examples.rb
92
93
  - lib/unidom/common/rspec/version.rb