sequel-sequence 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mariadb_test_helper'
4
+
5
+ class MariadbSequenceTest < Minitest::Test
6
+ include MariadbTestHelper
7
+
8
+ setup do
9
+ recreate_table
10
+ end
11
+
12
+ test 'adds sequence with default values' do
13
+ with_migration do
14
+ def up
15
+ # create_sequence :position, {start: 1, increment: 1} - default values
16
+ create_sequence :position
17
+ end
18
+ end.up
19
+
20
+ assert_equal 1, MariaDB.nextval(:position)
21
+ assert_equal 2, MariaDB.nextval(:position)
22
+ end
23
+
24
+ test 'adds sequence reader within model and its inherited class' do
25
+ with_migration do
26
+ def up
27
+ create_sequence :position
28
+ end
29
+ end.up
30
+
31
+ class Ware < Sequel::Model; end
32
+
33
+ assert_equal 1, Ware.db.nextval('position')
34
+ assert_equal 2, Ware.db.nextval('position')
35
+
36
+ class InheritedWare < Ware; end
37
+
38
+ assert_equal 3, InheritedWare.db.nextval(:position)
39
+ assert_equal 4, InheritedWare.db.nextval(:position)
40
+ end
41
+
42
+ test 'adds sequence starting at 100' do
43
+ with_migration do
44
+ def up
45
+ create_sequence :position, start: 100
46
+ end
47
+ end.up
48
+
49
+ assert_equal 100, MariaDB.nextval(:position)
50
+ assert_equal 101, MariaDB.nextval(:position)
51
+ end
52
+
53
+ test 'adds sequence incremented by 2' do
54
+ with_migration do
55
+ def up
56
+ create_sequence :position, increment: 2
57
+ end
58
+ end.up
59
+
60
+ assert_equal 1, MariaDB.nextval(:position)
61
+ assert_equal 3, MariaDB.nextval(:position)
62
+ end
63
+
64
+ test 'adds sequence incremented by 2 (using :step alias)' do
65
+ with_migration do
66
+ def up
67
+ create_sequence :position, step: 2
68
+ end
69
+ end.up
70
+
71
+ assert_equal 1, MariaDB.nextval(:position)
72
+ assert_equal 3, MariaDB.nextval(:position)
73
+ end
74
+
75
+ test "returns current/last sequence value, which doesn't increase by itself" do
76
+ with_migration do
77
+ def up
78
+ create_sequence :position
79
+ end
80
+ end.up
81
+
82
+ MariaDB.nextval(:position)
83
+
84
+ assert_equal 1, MariaDB.currval(:position)
85
+ assert_equal 1, MariaDB.lastval(:position)
86
+ assert_equal 1, MariaDB.currval(:position)
87
+ assert_equal 1, MariaDB.lastval(:position)
88
+ end
89
+
90
+ test 'sets sequence value' do
91
+ with_migration do
92
+ def up
93
+ create_sequence :position
94
+ end
95
+ end.up
96
+
97
+ MariaDB.nextval(:position)
98
+ assert_equal MariaDB.currval(:position), 1
99
+
100
+ MariaDB.setval(:position, 101)
101
+ # in MariaDB, 'lastval' only works after 'nextval' rather than 'setval'
102
+ assert_equal 1, MariaDB.lastval(:position)
103
+
104
+ MariaDB.nextval(:position)
105
+ # now the value is correct
106
+ assert_equal 102, MariaDB.lastval(:position)
107
+ end
108
+
109
+ test 'drops sequence and check_sequences' do
110
+ with_migration do
111
+ def up
112
+ create_sequence :position
113
+ end
114
+ end.up
115
+
116
+ sequence = MariaDB.check_sequences.find_all do |seq|
117
+ seq[:Tables_in_test] == 'position'
118
+ end
119
+
120
+ assert_equal 1, sequence.size
121
+
122
+ with_migration do
123
+ def down
124
+ drop_sequence :position
125
+ end
126
+ end.down
127
+
128
+ sequence = MariaDB.check_sequences.find do |seq|
129
+ seq[:Tables_in_test] == 'position'
130
+ end
131
+
132
+ assert_nil sequence
133
+ end
134
+
135
+ test 'orders sequences' do
136
+ list = MariaDB.check_sequences.map { |s| s[:Tables_in_test] }
137
+ assert !list.include?('a')
138
+ assert !list.include?('b')
139
+ assert !list.include?('c')
140
+
141
+ with_migration do
142
+ def up
143
+ drop_table :things, if_exists: true
144
+ create_sequence :c
145
+ create_sequence :a
146
+ create_sequence :b
147
+ end
148
+ end.up
149
+
150
+ list = MariaDB.check_sequences.map { |s| s[:Tables_in_test] }
151
+ assert list.include?('a')
152
+ assert list.include?('b')
153
+ assert list.include?('c')
154
+ end
155
+
156
+ test 'creates table that references sequence' do
157
+ with_migration do
158
+ def up
159
+ drop_table :builders, if_exists: true
160
+ create_sequence :position_id, if_exists: false, start: 1
161
+ create_table :builders do
162
+ primary_key :id
163
+ String :name, text: true
164
+ Bignum :position, null: false
165
+ end
166
+ set_column_default_nextval :builders, :position, :position_id
167
+ end
168
+ end.up
169
+
170
+ class Builder < Sequel::Model; end
171
+
172
+ builder1 = Builder.create(name: 'Builder 1')
173
+ pos1 = MariaDB.currval(:position_id)
174
+ assert_equal pos1, builder1.reload.position
175
+
176
+ builder2 = Builder.create(name: 'Builder 2')
177
+ pos2 = MariaDB.currval(:position_id)
178
+ assert_equal pos2, builder2.reload.position
179
+
180
+ assert_equal pos2 - pos1, 1
181
+ end
182
+ end
@@ -12,13 +12,28 @@ class MysqlSequenceTest < Minitest::Test
12
12
  test 'adds sequence with default values' do
13
13
  with_migration do
14
14
  def up
15
- # create_sequence :position, {start: 1, increment: 1} - default values
15
+ # create_sequence :position, {start: 1, increment: 1, numeric_label: 0} - default values
16
16
  create_sequence :position
17
17
  end
18
18
  end.up
19
19
 
20
- assert_equal 1, MysqlDB.nextval(:position)
21
20
  assert_equal 2, MysqlDB.nextval(:position)
21
+ assert_equal 3, MysqlDB.nextval(:position)
22
+ end
23
+
24
+ test 'adds sequence with numeric_label' do
25
+ with_migration do
26
+ def up
27
+ # create_sequence :position, {start: 1, increment: 1, numeric_label: 0} - default values
28
+ create_sequence :position, numeric_label: 10
29
+ end
30
+ end.up
31
+
32
+ assert_equal 1, MysqlDB.currval(:position)
33
+
34
+ fiction_list = MysqlDB.fetch('SELECT fiction FROM position;').all
35
+ assert_equal 1, fiction_list.size
36
+ assert_equal 10, fiction_list.first[:fiction]
22
37
  end
23
38
 
24
39
  test 'adds sequence reader within model and its inherited class' do
@@ -28,15 +43,15 @@ class MysqlSequenceTest < Minitest::Test
28
43
  end
29
44
  end.up
30
45
 
31
- class Ware < Sequel::Model; end
46
+ class Stuff < Sequel::Model; end
32
47
 
33
- assert_equal 1, Ware.db.nextval('position')
34
- assert_equal 2, Ware.db.nextval('position')
48
+ assert_equal 2, Stuff.db.nextval('position')
49
+ assert_equal 3, Stuff.db.nextval('position')
35
50
 
36
- class InheritedWare < Ware; end
51
+ class InheritedStuff < Stuff; end
37
52
 
38
- assert_equal 3, InheritedWare.db.nextval(:position)
39
- assert_equal 4, InheritedWare.db.nextval(:position)
53
+ assert_equal 4, InheritedStuff.db.nextval(:position)
54
+ assert_equal 5, InheritedStuff.db.nextval(:position)
40
55
  end
41
56
 
42
57
  test 'adds sequence starting at 100' do
@@ -46,33 +61,37 @@ class MysqlSequenceTest < Minitest::Test
46
61
  end
47
62
  end.up
48
63
 
49
- assert_equal 100, MysqlDB.nextval(:position)
50
64
  assert_equal 101, MysqlDB.nextval(:position)
65
+ assert_equal 102, MysqlDB.nextval(:position)
51
66
  end
52
67
 
53
- test 'adds sequence incremented by 2' do
68
+ test 'adds a sequence that we are trying to increase by a value greater than 1' do
69
+ @step = 4
54
70
  with_migration do
55
71
  def up
56
- create_sequence :position, increment: 2
72
+ create_sequence :position, increment: 4
57
73
  end
58
74
  end.up
59
75
 
60
- assert_equal 1, MysqlDB.nextval(:position)
76
+ assert_equal 2, MysqlDB.nextval(:position)
61
77
  assert_equal 3, MysqlDB.nextval(:position)
78
+ assert_operator (2 + @step), :>, MysqlDB.nextval(:position)
62
79
  end
63
80
 
64
- test 'adds sequence incremented by 2 (using :step alias)' do
81
+ test 'adds a sequence that we are trying to increase by a value greater than 1 (using :step alias)' do
82
+ @step = 4
65
83
  with_migration do
66
84
  def up
67
- create_sequence :position, step: 2
85
+ create_sequence :position, step: 4
68
86
  end
69
87
  end.up
70
88
 
71
- assert_equal 1, MysqlDB.nextval(:position)
89
+ assert_equal 2, MysqlDB.nextval(:position)
72
90
  assert_equal 3, MysqlDB.nextval(:position)
91
+ assert_operator (2 + @step), :>, MysqlDB.nextval(:position)
73
92
  end
74
93
 
75
- test 'returns current/last sequence value without incrementing it' do
94
+ test "returns current/last sequence value, which doesn't increase by itself" do
76
95
  with_migration do
77
96
  def up
78
97
  create_sequence :position
@@ -80,33 +99,70 @@ class MysqlSequenceTest < Minitest::Test
80
99
  end.up
81
100
 
82
101
  MysqlDB.nextval(:position)
102
+ # changed value (=2) after default one (=1)
83
103
 
84
- assert_equal 1, MysqlDB.currval(:position)
85
- assert_equal 1, MysqlDB.lastval(:position)
86
- assert_equal 1, MysqlDB.currval(:position)
87
- assert_equal 1, MysqlDB.lastval(:position)
104
+ assert_equal 2, MysqlDB.currval(:position)
105
+ assert_equal 2, MysqlDB.lastval(:position)
106
+ assert_equal 2, MysqlDB.currval(:position)
107
+ assert_equal 2, MysqlDB.lastval(:position)
88
108
  end
89
109
 
90
- test 'sets sequence value' do
110
+ test 'sets a new sequence value greater than the current one' do
91
111
  with_migration do
92
112
  def up
93
113
  create_sequence :position
94
114
  end
95
115
  end.up
96
116
 
97
- MysqlDB.nextval(:position)
98
117
  assert_equal MysqlDB.currval(:position), 1
99
118
 
119
+ MysqlDB.nextval(:position)
120
+ assert_equal MysqlDB.currval(:position), 2
121
+
100
122
  MysqlDB.setval(:position, 101)
101
- # in mariaDB, 'lastval' only works after 'nextval' rather than 'setval'
102
- assert_equal 1, MysqlDB.lastval(:position)
123
+ assert_equal 101, MysqlDB.lastval(:position)
124
+
125
+ assert_equal 102, MysqlDB.nextval(:position)
126
+ end
127
+
128
+ test 'sets a new sequence value less than the current one' do
129
+ with_migration do
130
+ def up
131
+ create_sequence :position, start: 100
132
+ end
133
+ end.up
134
+
135
+ assert_equal MysqlDB.currval(:position), 100
136
+
137
+ MysqlDB.nextval(:position)
138
+ assert_equal MysqlDB.currval(:position), 101
139
+
140
+ MysqlDB.setval(:position, 1)
141
+ assert_equal 101, MysqlDB.lastval(:position)
103
142
 
104
143
  MysqlDB.nextval(:position)
105
- # now the value is correct
106
144
  assert_equal 102, MysqlDB.lastval(:position)
107
145
  end
108
146
 
109
- test 'drops sequence and check_sequences' do
147
+ test 'sets a new sequence value with a label' do
148
+ with_migration do
149
+ def up
150
+ create_sequence :position
151
+ end
152
+ end.up
153
+
154
+ MysqlDB.nextval(:position)
155
+ MysqlDB.nextval_with_label(:position, 1)
156
+ MysqlDB.nextval_with_label(:position, 1)
157
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position where fiction = 1;').all.size
158
+ assert_equal 2, fiction_set_size
159
+
160
+ # create_sequence + nextval
161
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position where fiction = 0;').all.size
162
+ assert_equal 2, fiction_set_size
163
+ end
164
+
165
+ test 'drops the sequence and the check_sequences' do
110
166
  with_migration do
111
167
  def up
112
168
  create_sequence :position
@@ -114,7 +170,7 @@ class MysqlSequenceTest < Minitest::Test
114
170
  end.up
115
171
 
116
172
  sequence = MysqlDB.check_sequences.find_all do |seq|
117
- seq[:Tables_in_test] == 'position'
173
+ seq[:name] == 'position'
118
174
  end
119
175
 
120
176
  assert_equal 1, sequence.size
@@ -126,14 +182,40 @@ class MysqlSequenceTest < Minitest::Test
126
182
  end.down
127
183
 
128
184
  sequence = MysqlDB.check_sequences.find do |seq|
129
- seq[:sequence_name] == 'position'
185
+ seq[:name] == 'position'
186
+ end
187
+
188
+ assert_nil sequence
189
+ end
190
+
191
+ test 'drops the sequence with the parameter if_exists' do
192
+ with_migration do
193
+ def up
194
+ create_sequence :position
195
+ end
196
+ end.up
197
+
198
+ sequence = MysqlDB.check_sequences.find_all do |seq|
199
+ seq[:name] == 'position'
200
+ end
201
+
202
+ assert_equal 1, sequence.size
203
+
204
+ with_migration do
205
+ def down
206
+ drop_sequence :position, if_exists: true
207
+ end
208
+ end.down
209
+
210
+ sequence = MysqlDB.check_sequences.find do |seq|
211
+ seq[:name] == 'position'
130
212
  end
131
213
 
132
214
  assert_nil sequence
133
215
  end
134
216
 
135
217
  test 'orders sequences' do
136
- list = MysqlDB.check_sequences.map { |s| s[:Tables_in_test] }
218
+ list = MysqlDB.check_sequences.map { |s| s[:name] }
137
219
  assert !list.include?('a')
138
220
  assert !list.include?('b')
139
221
  assert !list.include?('c')
@@ -141,42 +223,133 @@ class MysqlSequenceTest < Minitest::Test
141
223
  with_migration do
142
224
  def up
143
225
  drop_table :things, if_exists: true
144
- create_sequence :c
145
- create_sequence :a
226
+ create_sequence :c, { start: 1 }
227
+ create_sequence :a, { start: 3 }
146
228
  create_sequence :b
147
229
  end
148
230
  end.up
149
231
 
150
- list = MysqlDB.check_sequences.map { |s| s[:Tables_in_test] }
232
+ list = MysqlDB.check_sequences.map { |s| s[:name] }
151
233
  assert list.include?('a')
152
234
  assert list.include?('b')
153
235
  assert list.include?('c')
154
236
  end
155
237
 
238
+ test 'checks custom sequence generated from code' do
239
+ assert_equal MysqlDB.custom_sequence?(:c), false
240
+
241
+ with_migration do
242
+ def up
243
+ create_sequence :c
244
+ end
245
+ end.up
246
+
247
+ assert_equal MysqlDB.custom_sequence?(:c), true
248
+ end
249
+
250
+ test 'recreates the same sequence with the same start value' do
251
+ with_migration do
252
+ def up
253
+ create_sequence :position_id, if_exists: false, start: 1
254
+ end
255
+ end.up
256
+
257
+ assert_equal 1, MysqlDB.currval(:position_id)
258
+
259
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
260
+ assert_equal 1, fiction_set_size
261
+
262
+ with_migration do
263
+ def up
264
+ create_sequence :position_id, if_exists: false, start: 1
265
+ end
266
+ end.up
267
+
268
+ assert_equal 1, MysqlDB.currval(:position_id)
269
+
270
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
271
+ assert_equal 1, fiction_set_size
272
+ end
273
+
274
+ test 'recreates the same sequence with a smaller start value' do
275
+ with_migration do
276
+ def up
277
+ create_sequence :position_id, if_exists: false, start: 100
278
+ end
279
+ end.up
280
+
281
+ assert_equal 100, MysqlDB.currval(:position_id)
282
+
283
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
284
+ assert_equal 1, fiction_set_size
285
+
286
+ with_migration do
287
+ def up
288
+ create_sequence :position_id, if_exists: false, start: 1
289
+ end
290
+ end.up
291
+
292
+ assert_equal 100, MysqlDB.currval(:position_id)
293
+
294
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
295
+ assert_equal 1, fiction_set_size
296
+ end
297
+
298
+ test 'recreates the same sequence with a greater start value' do
299
+ with_migration do
300
+ def up
301
+ create_sequence :position_id, if_exists: false, start: 1
302
+ end
303
+ end.up
304
+
305
+ assert_equal 1, MysqlDB.currval(:position_id)
306
+
307
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
308
+ assert_equal 1, fiction_set_size
309
+
310
+ with_migration do
311
+ def up
312
+ create_sequence :position_id, if_exists: false, start: 100
313
+ end
314
+ end.up
315
+
316
+ assert_equal 100, MysqlDB.currval(:position_id)
317
+
318
+ fiction_set_size = MysqlDB.fetch('SELECT * FROM position_id where fiction = 0;').all.size
319
+ assert_equal 2, fiction_set_size
320
+ end
321
+
156
322
  test 'creates table that references sequence' do
157
323
  with_migration do
158
324
  def up
159
- drop_table :builders, if_exists: true
160
- create_sequence :position_id, if_exists: false
161
- create_table :builders do
325
+ create_sequence :position_id, if_exists: false, start: 1
326
+ create_table :creators do
162
327
  primary_key :id
163
328
  String :name, text: true
164
- Bignum :position, null: false
329
+ Bignum :position
165
330
  end
166
- set_column_default_nextval :builders, :position, :position_id
331
+ set_column_default_nextval :creators, :position, :position_id
167
332
  end
168
333
  end.up
169
334
 
170
- class Builder < Sequel::Model; end
335
+ class Creator < Sequel::Model; end
171
336
 
172
- builder1 = Builder.create(name: 'Builder 1')
173
- pos1 = MysqlDB.currval(:position_id)
174
- assert_equal pos1, builder1.reload.position
337
+ creator1 = Creator.create(name: 'Creator 1')
338
+ pos1 = MysqlDB.lastval(:position_id)
339
+ assert_equal pos1, creator1.reload.position
175
340
 
176
- builder2 = Builder.create(name: 'Builder 2')
341
+ creator2 = Creator.create(name: 'Creator 2')
177
342
  pos2 = MysqlDB.currval(:position_id)
178
- assert_equal pos2, builder2.reload.position
343
+ assert_equal pos2, creator2.reload.position
179
344
 
180
345
  assert_equal pos2 - pos1, 1
346
+
347
+ MysqlDB.nextval(:position_id)
348
+
349
+ creator4 = Creator.create(name: 'Creator 4')
350
+ pos4 = MysqlDB.currval(:position_id)
351
+ assert_equal pos4, creator4.reload.position
352
+
353
+ assert_equal pos4 - pos2, 2
181
354
  end
182
355
  end
@@ -72,7 +72,7 @@ class PostgresqlSequenceTest < Minitest::Test
72
72
  assert_equal 3, PostgresqlDB.nextval(:position)
73
73
  end
74
74
 
75
- test 'returns current (or last as alias) sequence value without incrementing it' do
75
+ test "returns current/last sequence value, which doesn't increase by itself" do
76
76
  with_migration do
77
77
  def up
78
78
  create_sequence :position, start: 2, increment: 2
@@ -164,7 +164,7 @@ class PostgresqlSequenceTest < Minitest::Test
164
164
  with_migration do
165
165
  def up
166
166
  drop_table :masters, if_exists: true
167
- create_sequence :position_id, if_exists: false
167
+ create_sequence :position_id, if_exists: false, start: 1
168
168
  create_table :masters, if_not_exists: true do
169
169
  primary_key :id
170
170
  String :name, text: true