pgtk 0.18.1 → 0.18.2

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.
data/test/test_retry.rb DELETED
@@ -1,232 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # SPDX-FileCopyrightText: Copyright (c) 2019-2025 Yegor Bugayenko
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'loog'
7
- require 'pg'
8
- require 'qbash'
9
- require 'rake'
10
- require 'tmpdir'
11
- require 'yaml'
12
- require_relative 'test__helper'
13
- require_relative '../lib/pgtk/pool'
14
- require_relative '../lib/pgtk/retry'
15
-
16
- # Retry test.
17
- # Author:: Yegor Bugayenko (yegor256@gmail.com)
18
- # Copyright:: Copyright (c) 2017-2025 Yegor Bugayenko
19
- # License:: MIT
20
- class TestRetry < Pgtk::Test
21
- def test_takes_version
22
- fake_pool do |pool|
23
- v = Pgtk::Retry.new(pool).version
24
- refute_nil(v)
25
- end
26
- end
27
-
28
- def test_executes_select_without_error
29
- fake_pool do |pool|
30
- retry_pool = Pgtk::Retry.new(pool, attempts: 3)
31
- result = retry_pool.exec('SELECT 1 as value')
32
- assert_equal('1', result.first['value'])
33
- end
34
- end
35
-
36
- def test_retries_select_on_failure
37
- fake_pool do |pool|
38
- counter = 0
39
- stub_pool = Object.new
40
- def stub_pool.version
41
- 'stub'
42
- end
43
- stub_pool.define_singleton_method(:exec) do |sql, *args|
44
- counter += 1
45
- raise PG::Error, 'Connection lost' if counter < 3
46
- pool.exec(sql, *args)
47
- end
48
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
49
- result = retry_pool.exec('SELECT 2 as num')
50
- assert_equal('2', result.first['num'])
51
- assert_equal(3, counter)
52
- end
53
- end
54
-
55
- def test_fails_after_max_attempts
56
- fake_pool do |_pool|
57
- stub_pool = Object.new
58
- def stub_pool.version
59
- 'stub'
60
- end
61
- stub_pool.define_singleton_method(:exec) do |_sql, *_args|
62
- raise PG::Error, 'Persistent failure'
63
- end
64
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 2)
65
- assert_raises(PG::Error) do
66
- retry_pool.exec('SELECT * FROM users')
67
- end
68
- end
69
- end
70
-
71
- def test_does_not_retry_insert
72
- fake_pool do |_pool|
73
- counter = 0
74
- stub_pool = Object.new
75
- def stub_pool.version
76
- 'stub'
77
- end
78
- stub_pool.define_singleton_method(:exec) do |_sql, *_args|
79
- counter += 1
80
- raise PG::Error, 'Insert failed'
81
- end
82
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
83
- assert_raises(PG::Error) do
84
- retry_pool.exec('INSERT INTO book (title) VALUES ($1)', ['Test Book'])
85
- end
86
- assert_equal(1, counter)
87
- end
88
- end
89
-
90
- def test_does_not_retry_update
91
- fake_pool do |_pool|
92
- counter = 0
93
- stub_pool = Object.new
94
- def stub_pool.version
95
- 'stub'
96
- end
97
- stub_pool.define_singleton_method(:exec) do |_sql, *_args|
98
- counter += 1
99
- raise PG::Error, 'Update failed'
100
- end
101
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
102
- assert_raises(PG::Error) do
103
- retry_pool.exec('UPDATE book SET title = $1 WHERE id = $2', ['New Title', 1])
104
- end
105
- assert_equal(1, counter)
106
- end
107
- end
108
-
109
- def test_does_not_retry_delete
110
- fake_pool do |_pool|
111
- counter = 0
112
- stub_pool = Object.new
113
- def stub_pool.version
114
- 'stub'
115
- end
116
- stub_pool.define_singleton_method(:exec) do |_sql, *_args|
117
- counter += 1
118
- raise PG::Error, 'Delete failed'
119
- end
120
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
121
- assert_raises(PG::Error) do
122
- retry_pool.exec('DELETE FROM book WHERE id = $1', [1])
123
- end
124
- assert_equal(1, counter)
125
- end
126
- end
127
-
128
- def test_handles_select_with_leading_whitespace
129
- fake_pool do |pool|
130
- counter = 0
131
- stub_pool = Object.new
132
- def stub_pool.version
133
- 'stub'
134
- end
135
- stub_pool.define_singleton_method(:exec) do |sql, *args|
136
- counter += 1
137
- raise PG::Error, 'Connection lost' if counter < 2
138
- pool.exec(sql, *args)
139
- end
140
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
141
- result = retry_pool.exec(' SELECT 3 as value')
142
- assert_equal('3', result.first['value'])
143
- assert_equal(2, counter)
144
- end
145
- end
146
-
147
- def test_handles_select_case_insensitive
148
- fake_pool do |pool|
149
- counter = 0
150
- stub_pool = Object.new
151
- def stub_pool.version
152
- 'stub'
153
- end
154
- stub_pool.define_singleton_method(:exec) do |sql, *args|
155
- counter += 1
156
- raise PG::Error, 'Connection lost' if counter < 2
157
- pool.exec(sql, *args)
158
- end
159
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
160
- result = retry_pool.exec('select 4 as value')
161
- assert_equal('4', result.first['value'])
162
- assert_equal(2, counter)
163
- end
164
- end
165
-
166
- def test_handles_array_sql
167
- fake_pool do |pool|
168
- counter = 0
169
- stub_pool = Object.new
170
- def stub_pool.version
171
- 'stub'
172
- end
173
- stub_pool.define_singleton_method(:exec) do |sql, *args|
174
- counter += 1
175
- raise PG::Error, 'Connection lost' if counter < 2
176
- pool.exec(sql, *args)
177
- end
178
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
179
- result = retry_pool.exec(%w[SELECT 5 as value])
180
- assert_equal('5', result.first['value'])
181
- assert_equal(2, counter)
182
- end
183
- end
184
-
185
- def test_transaction_passes_through
186
- fake_pool do |pool|
187
- retry_pool = Pgtk::Retry.new(pool)
188
- retry_pool.transaction do |t|
189
- id = t.exec(
190
- 'INSERT INTO book (title) VALUES ($1) RETURNING id',
191
- ['Transaction Book']
192
- ).first['id'].to_i
193
- assert_predicate(id, :positive?)
194
- end
195
- end
196
- end
197
-
198
- def test_preserves_original_error_type
199
- fake_pool do |_pool|
200
- stub_pool = Object.new
201
- def stub_pool.version
202
- 'stub'
203
- end
204
- stub_pool.define_singleton_method(:exec) do |_sql, *_args|
205
- raise ArgumentError, 'Invalid argument'
206
- end
207
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 2)
208
- assert_raises(ArgumentError) do
209
- retry_pool.exec('SELECT * FROM table')
210
- end
211
- end
212
- end
213
-
214
- def test_retries_with_unicode_query
215
- fake_pool do |pool|
216
- counter = 0
217
- stub_pool = Object.new
218
- def stub_pool.version
219
- 'stub'
220
- end
221
- stub_pool.define_singleton_method(:exec) do |sql, *args|
222
- counter += 1
223
- raise PG::Error, 'Connection lost' if counter < 2
224
- pool.exec(sql, *args)
225
- end
226
- retry_pool = Pgtk::Retry.new(stub_pool, attempts: 3)
227
- result = retry_pool.exec('SELECT \'привет\' as greeting')
228
- assert_equal('привет', result.first['greeting'])
229
- assert_equal(2, counter)
230
- end
231
- end
232
- end
data/test/test_stash.rb DELETED
@@ -1,108 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # SPDX-FileCopyrightText: Copyright (c) 2019-2025 Yegor Bugayenko
4
- # SPDX-License-Identifier: MIT
5
-
6
- require_relative 'test__helper'
7
- require_relative '../lib/pgtk/pool'
8
- require_relative '../lib/pgtk/stash'
9
-
10
- # Pool test.
11
- # Author:: Yegor Bugayenko (yegor256@gmail.com)
12
- # Copyright:: Copyright (c) 2017-2025 Yegor Bugayenko
13
- # License:: MIT
14
- class TestStash < Pgtk::Test
15
- def test_simple_insert
16
- fake_pool do |pool|
17
- id = Pgtk::Stash.new(pool).exec(
18
- 'INSERT INTO book (title) VALUES ($1) RETURNING id',
19
- ['Elegant Objects']
20
- )[0]['id'].to_i
21
- assert_predicate(id, :positive?)
22
- end
23
- end
24
-
25
- def test_non_trivial_queries
26
- fake_pool do |pool|
27
- pg = Pgtk::Stash.new(pool)
28
- [
29
- 'VACUUM FULL',
30
- 'START TRANSACTION',
31
- 'REINDEX TABLE book',
32
- 'TRUNCATE book',
33
- 'CREATE TABLE tmp (id INT)',
34
- 'ALTER TABLE tmp ADD COLUMN foo INT',
35
- 'DROP TABLE tmp',
36
- 'SET client_min_messages TO WARNING',
37
- "SET TIME ZONE 'America/Los_Angeles'"
38
- ].each do |q|
39
- pg.exec(q)
40
- end
41
- end
42
- end
43
-
44
- def test_caching
45
- fake_pool do |pool|
46
- stash = Pgtk::Stash.new(pool)
47
- query = 'SELECT count(*) FROM book'
48
- first_result = stash.exec(query)
49
- second_result = stash.exec(query)
50
- assert_equal(first_result.to_a, second_result.to_a)
51
- assert_same(first_result, second_result)
52
- end
53
- end
54
-
55
- def test_cache_invalidation
56
- fake_pool do |pool|
57
- stash = Pgtk::Stash.new(pool)
58
- query = 'SELECT count(*) FROM book'
59
- first_result = stash.exec(query)
60
- stash.exec('INSERT INTO book (title) VALUES ($1)', ['New Book'])
61
- second_result = stash.exec(query)
62
- refute_same(first_result, second_result)
63
- end
64
- end
65
-
66
- def test_caching_with_params
67
- fake_pool do |pool|
68
- stash = Pgtk::Stash.new(pool)
69
- query = 'SELECT * FROM book WHERE title = $1'
70
- first_result = stash.exec(query, ['Elegant Objects'])
71
- second_result = stash.exec(query, ['Elegant Objects'])
72
- assert_equal(first_result.to_a, second_result.to_a)
73
- assert_same(first_result, second_result)
74
- different_param_result = stash.exec(query, ['Different Title'])
75
- refute_same(first_result, different_param_result)
76
- end
77
- end
78
-
79
- def test_version
80
- fake_pool do |pool|
81
- stash = Pgtk::Stash.new(pool)
82
- assert_match(/^\d+\.\d+/, stash.version)
83
- end
84
- end
85
-
86
- def test_transaction
87
- fake_pool do |pool|
88
- stash = Pgtk::Stash.new(pool)
89
- stash.exec('INSERT INTO book (title) VALUES ($1)', ['Transaction Test'])
90
- stash.transaction do |tx|
91
- result = tx.exec('SELECT title FROM book WHERE title = $1', ['Transaction Test'])
92
- assert_equal('Transaction Test', result[0]['title'])
93
- true
94
- end
95
- end
96
- end
97
-
98
- def test_start
99
- fake_pool do |pool|
100
- stash = Pgtk::Stash.new(pool)
101
- stash.exec('INSERT INTO book (title) VALUES ($1)', ['Start Test'])
102
- new_stash = stash.start(1)
103
- assert_instance_of(Pgtk::Stash, new_stash)
104
- result = new_stash.exec('SELECT title FROM book WHERE title = $1', ['Start Test'])
105
- assert_equal('Start Test', result[0]['title'])
106
- end
107
- end
108
- end
data/test/test_wire.rb DELETED
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # SPDX-FileCopyrightText: Copyright (c) 2019-2025 Yegor Bugayenko
4
- # SPDX-License-Identifier: MIT
5
-
6
- require 'cgi'
7
- require 'yaml'
8
- require_relative 'test__helper'
9
- require_relative '../lib/pgtk/wire'
10
-
11
- # Wire test.
12
- # Author:: Yegor Bugayenko (yegor256@gmail.com)
13
- # Copyright:: Copyright (c) 2017-2025 Yegor Bugayenko
14
- # License:: MIT
15
- class TestWire < Pgtk::Test
16
- def test_connects
17
- fake_config do |f|
18
- wire = Pgtk::Wire::Yaml.new(f)
19
- c = wire.connection
20
- refute_nil(c)
21
- end
22
- end
23
-
24
- def test_connects_via_env_variable
25
- fake_config do |f|
26
- c = YAML.load_file(f)['pgsql']
27
- v = 'DATABASE_URL'
28
- ENV[v] = [
29
- "postgres://#{CGI.escape(c['user'])}:#{CGI.escape(c['password'])}",
30
- "@#{CGI.escape(c['host'])}:#{CGI.escape(c['port'].to_s)}/#{CGI.escape(c['dbname'])}"
31
- ].join
32
- wire = Pgtk::Wire::Env.new(v)
33
- c = wire.connection
34
- refute_nil(c)
35
- end
36
- end
37
- end