sequel-sequence 0.1.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,257 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sqlite_test_helper'
4
+
5
+ class SqliteSequenceTest < Minitest::Test
6
+ include SqliteTestHelper
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 2, SQLiteDB.nextval(:position)
21
+ assert_equal 3, SQLiteDB.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 Object < Sequel::Model; end
32
+
33
+ assert_equal 2, Object.db.nextval('position')
34
+ assert_equal 3, Object.db.nextval('position')
35
+
36
+ class InheritedObject < Object; end
37
+
38
+ assert_equal 4, InheritedObject.db.nextval(:position)
39
+ assert_equal 5, InheritedObject.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 101, SQLiteDB.nextval(:position)
50
+ assert_equal 102, SQLiteDB.nextval(:position)
51
+ end
52
+
53
+ test 'adds a sequence that we are trying to increase by a value greater than 1' do
54
+ @step = 4
55
+ with_migration do
56
+ def up
57
+ create_sequence :position, increment: 4
58
+ end
59
+ end.up
60
+
61
+ assert_equal 2, SQLiteDB.nextval(:position)
62
+ assert_equal 3, SQLiteDB.nextval(:position)
63
+ assert_operator (2 + @step), :>, SQLiteDB.nextval(:position)
64
+ end
65
+
66
+ test 'adds a sequence that we are trying to increase by a value greater than 1 (using :step alias)' do
67
+ @step = 4
68
+ with_migration do
69
+ def up
70
+ create_sequence :position, step: 4
71
+ end
72
+ end.up
73
+
74
+ assert_equal 2, SQLiteDB.nextval(:position)
75
+ assert_equal 3, SQLiteDB.nextval(:position)
76
+ assert_operator (2 + @step), :>, SQLiteDB.nextval(:position)
77
+ end
78
+
79
+ test "returns current/last sequence value, which doesn't increase by itself" do
80
+ with_migration do
81
+ def up
82
+ create_sequence :position
83
+ end
84
+ end.up
85
+
86
+ SQLiteDB.nextval(:position)
87
+ # changed value (=2) after default one (=1)
88
+
89
+ assert_equal 2, SQLiteDB.currval(:position)
90
+ assert_equal 2, SQLiteDB.lastval(:position)
91
+ assert_equal 2, SQLiteDB.currval(:position)
92
+ assert_equal 2, SQLiteDB.lastval(:position)
93
+ end
94
+
95
+ test 'sets a new sequence value greater than the current one' do
96
+ with_migration do
97
+ def up
98
+ create_sequence :position
99
+ end
100
+ end.up
101
+
102
+ assert_equal SQLiteDB.currval(:position), 1
103
+
104
+ SQLiteDB.nextval(:position)
105
+ assert_equal SQLiteDB.currval(:position), 2
106
+
107
+ SQLiteDB.setval(:position, 101)
108
+ assert_equal 101, SQLiteDB.lastval(:position)
109
+
110
+ SQLiteDB.nextval(:position)
111
+ assert_equal 102, SQLiteDB.lastval(:position)
112
+ end
113
+
114
+ test 'sets a new sequence value less than the current one' do
115
+ with_migration do
116
+ def up
117
+ create_sequence :position, start: 100
118
+ end
119
+ end.up
120
+
121
+ assert_equal SQLiteDB.currval(:position), 100
122
+
123
+ SQLiteDB.nextval(:position)
124
+ assert_equal SQLiteDB.currval(:position), 101
125
+
126
+ SQLiteDB.setval(:position, 1)
127
+ assert_equal 101, SQLiteDB.lastval(:position)
128
+
129
+ SQLiteDB.nextval(:position)
130
+ assert_equal 102, SQLiteDB.lastval(:position)
131
+ end
132
+
133
+ test 'sets a new sequence value with a label' do
134
+ with_migration do
135
+ def up
136
+ create_sequence :position
137
+ end
138
+ end.up
139
+
140
+ SQLiteDB.nextval(:position)
141
+ SQLiteDB.nextval_with_label(:position, 1)
142
+ SQLiteDB.nextval_with_label(:position, 1)
143
+ fiction_set_size = SQLiteDB.fetch('SELECT * FROM position where fiction = 1;').all.size
144
+ assert_equal 2, fiction_set_size
145
+
146
+ fiction_set_size = SQLiteDB.fetch('SELECT * FROM position where fiction = 0;').all.size
147
+ assert_equal 1, fiction_set_size
148
+ end
149
+
150
+ test 'drops the sequence and the check_sequences' do
151
+ with_migration do
152
+ def up
153
+ create_sequence :position
154
+ end
155
+ end.up
156
+
157
+ sequence = SQLiteDB.check_sequences.find_all do |seq|
158
+ seq[:name] == 'position'
159
+ end
160
+
161
+ assert_equal 1, sequence.size
162
+
163
+ with_migration do
164
+ def down
165
+ drop_sequence :position
166
+ end
167
+ end.down
168
+
169
+ sequence = SQLiteDB.check_sequences.find do |seq|
170
+ seq[:name] == 'position'
171
+ end
172
+
173
+ assert_nil sequence
174
+ end
175
+
176
+ test 'dropsthe sequence with the parameter if_exists' do
177
+ with_migration do
178
+ def up
179
+ create_sequence :position
180
+ end
181
+ end.up
182
+
183
+ sequence = SQLiteDB.check_sequences.find_all do |seq|
184
+ seq[:name] == 'position'
185
+ end
186
+
187
+ assert_equal 1, sequence.size
188
+
189
+ with_migration do
190
+ def down
191
+ drop_sequence :position, if_exists: true
192
+ end
193
+ end.down
194
+
195
+ sequence = SQLiteDB.check_sequences.find do |seq|
196
+ seq[:name] == 'position'
197
+ end
198
+
199
+ assert_nil sequence
200
+ end
201
+
202
+ test 'orders sequences' do
203
+ list = SQLiteDB.check_sequences.map { |s| s[:name] }
204
+ assert !list.include?('a')
205
+ assert !list.include?('b')
206
+ assert !list.include?('c')
207
+
208
+ with_migration do
209
+ def up
210
+ drop_table :things, if_exists: true
211
+ create_sequence :c, { start: 1 }
212
+ create_sequence :a, { start: 3 }
213
+ create_sequence :b
214
+ end
215
+ end.up
216
+
217
+ list = SQLiteDB.check_sequences.map { |s| s[:name] }
218
+ assert list.include?('a')
219
+ assert list.include?('b')
220
+ assert list.include?('c')
221
+ end
222
+
223
+ test 'creates table that references sequence' do
224
+ with_migration do
225
+ def up
226
+ drop_table :apprentices, if_exists: true
227
+ create_sequence :position_id, if_exists: false, start: 1
228
+ create_table :apprentices do
229
+ primary_key :id
230
+ String :name, text: true
231
+ Bignum :position
232
+ end
233
+ set_column_default_nextval :apprentices, :position, :position_id
234
+ end
235
+ end.up
236
+
237
+ class Apprentice < Sequel::Model; end
238
+
239
+ apprentice1 = Apprentice.create(name: 'Apprentice 1')
240
+ pos1 = SQLiteDB.lastval(:position_id) - 1
241
+ assert_equal pos1, apprentice1.reload.position
242
+
243
+ apprentice2 = Apprentice.create(name: 'Apprentice 2')
244
+ pos2 = SQLiteDB.currval(:position_id) - 1
245
+ assert_equal pos2, apprentice2.reload.position
246
+
247
+ assert_equal pos2 - pos1, 1
248
+
249
+ SQLiteDB.nextval(:position_id)
250
+
251
+ apprentice4 = Apprentice.create(name: 'Apprentice 4')
252
+ pos4 = SQLiteDB.currval(:position_id) - 1
253
+ assert_equal pos4, apprentice4.reload.position
254
+
255
+ assert_equal pos4 - pos2, 2
256
+ end
257
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_helper'
4
+ require 'logger'
5
+
6
+ SQLiteDB = Sequel.connect(
7
+ "sqlite://#{ENV.fetch('TEST_SQLITE_DATABASE', nil) || 'db/test.sqlite3'}",
8
+ loggers: [Logger.new($stdout)]
9
+ )
10
+
11
+ module SqliteTestHelper
12
+ def recreate_table
13
+ SQLiteDB.drop_sequence 'position'
14
+ SQLiteDB.run 'DROP TABLE IF EXISTS objects'
15
+ SQLiteDB.drop_sequence 'a'
16
+ SQLiteDB.drop_sequence 'b'
17
+ SQLiteDB.drop_sequence 'c'
18
+ sql = 'CREATE TABLE objects (id INTEGER PRIMARY KEY AUTOINCREMENT, quantity INTEGER DEFAULT(0), slug VARCHAR(255));'
19
+ SQLiteDB.run sql
20
+ end
21
+
22
+ def with_migration(&block)
23
+ migration_class = Sequel::Migration
24
+
25
+ Sequel::Model.db = SQLiteDB
26
+
27
+ Class.new(migration_class, &block).new(SQLiteDB)
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-sequence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nikolai Bocharov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-11 00:00:00.000000000 Z
11
+ date: 2023-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -16,126 +16,146 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '5.28'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5.73'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '0'
29
+ version: '5.28'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5.73'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - ">="
32
38
  - !ruby/object:Gem::Version
33
- version: '0'
39
+ version: 2.2.4
34
40
  type: :development
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
44
  - - ">="
39
45
  - !ruby/object:Gem::Version
40
- version: '0'
46
+ version: 2.2.4
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: minitest-utils
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
45
- - - ">="
51
+ - - "~>"
46
52
  - !ruby/object:Gem::Version
47
- version: '0'
53
+ version: 0.4.6
48
54
  type: :development
49
55
  prerelease: false
50
56
  version_requirements: !ruby/object:Gem::Requirement
51
57
  requirements:
52
- - - ">="
58
+ - - "~>"
53
59
  - !ruby/object:Gem::Version
54
- version: '0'
60
+ version: 0.4.6
55
61
  - !ruby/object:Gem::Dependency
56
62
  name: pry-byebug
57
63
  requirement: !ruby/object:Gem::Requirement
58
64
  requirements:
59
- - - ">="
65
+ - - "~>"
60
66
  - !ruby/object:Gem::Version
61
- version: '0'
67
+ version: 3.10.1
62
68
  type: :development
63
69
  prerelease: false
64
70
  version_requirements: !ruby/object:Gem::Requirement
65
71
  requirements:
66
- - - ">="
72
+ - - "~>"
67
73
  - !ruby/object:Gem::Version
68
- version: '0'
74
+ version: 3.10.1
69
75
  - !ruby/object:Gem::Dependency
70
76
  name: rake
71
77
  requirement: !ruby/object:Gem::Requirement
72
78
  requirements:
73
- - - ">="
79
+ - - "~>"
74
80
  - !ruby/object:Gem::Version
75
- version: '0'
81
+ version: 13.0.2
76
82
  type: :development
77
83
  prerelease: false
78
84
  version_requirements: !ruby/object:Gem::Requirement
79
85
  requirements:
80
- - - ">="
86
+ - - "~>"
81
87
  - !ruby/object:Gem::Version
82
- version: '0'
88
+ version: 13.0.2
83
89
  - !ruby/object:Gem::Dependency
84
90
  name: rubocop
85
91
  requirement: !ruby/object:Gem::Requirement
86
92
  requirements:
87
- - - ">="
93
+ - - "~>"
88
94
  - !ruby/object:Gem::Version
89
- version: '0'
95
+ version: 1.56.3
90
96
  type: :development
91
97
  prerelease: false
92
98
  version_requirements: !ruby/object:Gem::Requirement
93
99
  requirements:
94
- - - ">="
100
+ - - "~>"
95
101
  - !ruby/object:Gem::Version
96
- version: '0'
102
+ version: 1.56.3
97
103
  - !ruby/object:Gem::Dependency
98
104
  name: simplecov
99
105
  requirement: !ruby/object:Gem::Requirement
100
106
  requirements:
101
- - - ">="
107
+ - - "~>"
102
108
  - !ruby/object:Gem::Version
103
- version: '0'
109
+ version: 0.22.0
104
110
  type: :development
105
111
  prerelease: false
106
112
  version_requirements: !ruby/object:Gem::Requirement
107
113
  requirements:
108
- - - ">="
114
+ - - "~>"
109
115
  - !ruby/object:Gem::Version
110
- version: '0'
116
+ version: 0.22.0
111
117
  - !ruby/object:Gem::Dependency
112
118
  name: mysql2
113
119
  requirement: !ruby/object:Gem::Requirement
114
120
  requirements:
115
- - - ">="
121
+ - - "~>"
116
122
  - !ruby/object:Gem::Version
117
- version: '0'
123
+ version: 0.5.3
118
124
  type: :development
119
125
  prerelease: false
120
126
  version_requirements: !ruby/object:Gem::Requirement
121
127
  requirements:
122
- - - ">="
128
+ - - "~>"
123
129
  - !ruby/object:Gem::Version
124
- version: '0'
130
+ version: 0.5.3
125
131
  - !ruby/object:Gem::Dependency
126
132
  name: pg
127
133
  requirement: !ruby/object:Gem::Requirement
128
134
  requirements:
129
- - - ">="
135
+ - - "~>"
130
136
  - !ruby/object:Gem::Version
131
- version: '0'
137
+ version: 1.5.4
132
138
  type: :development
133
139
  prerelease: false
134
140
  version_requirements: !ruby/object:Gem::Requirement
135
141
  requirements:
136
- - - ">="
142
+ - - "~>"
143
+ - !ruby/object:Gem::Version
144
+ version: 1.5.4
145
+ - !ruby/object:Gem::Dependency
146
+ name: sqlite3
147
+ requirement: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - "~>"
137
150
  - !ruby/object:Gem::Version
138
- version: '0'
151
+ version: 1.6.0
152
+ type: :development
153
+ prerelease: false
154
+ version_requirements: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - "~>"
157
+ - !ruby/object:Gem::Version
158
+ version: 1.6.0
139
159
  description: |2
140
160
  This gem provides a single interface for SEQUENCE functionality
141
161
  in Postgresql and Mysql databases within the Sequel ORM.
@@ -145,8 +165,15 @@ executables: []
145
165
  extensions: []
146
166
  extra_rdoc_files:
147
167
  - README.md
168
+ - LICENSE.md
148
169
  files:
170
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
171
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
172
+ - ".github/PULL_REQUEST_TEMPLATE.md"
173
+ - ".github/dependabot.yml"
174
+ - ".github/workflows/ci.yml"
149
175
  - ".gitignore"
176
+ - ".rubocop.yml"
150
177
  - CHANGELOG.md
151
178
  - CODE_OF_CONDUCT.md
152
179
  - CONTRIBUTING.md
@@ -161,12 +188,15 @@ files:
161
188
  - lib/sequel/sequence/database/mysql.rb
162
189
  - lib/sequel/sequence/database/mysql2.rb
163
190
  - lib/sequel/sequence/database/postgresql.rb
191
+ - lib/sequel/sequence/database/sqlite.rb
164
192
  - lib/sequel/sequence/version.rb
165
193
  - sequel-sequence.gemspec
166
194
  - test/mysql_test_helper.rb
167
195
  - test/postgresql_test_helper.rb
168
196
  - test/sequel/mysql_sequence_test.rb
169
197
  - test/sequel/postgresql_sequence_test.rb
198
+ - test/sequel/sqlite_sequence_test.rb
199
+ - test/sqlite_test_helper.rb
170
200
  - test/test_helper.rb
171
201
  homepage: https://rubygems.org/gems/sequel-sequence
172
202
  licenses: