Repository navigation
Expand file tree
/
Copy pathend2end_sqlite3.rb
More file actions
246 lines (205 loc) · 9.17 KB
/
Copy pathend2end_sqlite3.rb
File metadata and controls
246 lines (205 loc) · 9.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env ruby
gem 'minitest' # ensures using the gem, and not the built-in MT
require 'minitest/autorun'
require 'logger'
require 'stringio'
require 'sqlite3'
require_relative 'consts'
require_relative 'rbs_types'
require_relative '../examples/sqlite3/query_sql'
# End-to-end tests of the code generated for examples/authors/sqlite, against a real (in-memory) SQLite database.
class TestSqlite3 < Minitest::Test
include Sqlite3Codegen
SCHEMA = File.read(File.expand_path('../examples/authors/sqlite/schema.sql', __dir__))
def setup
@db = SQLite3::Database.new(':memory:')
@db.execute_batch(SCHEMA)
@query_sql = QuerySql.new(@db)
end
def create_author(name, bio = nil)
@query_sql.create_author_return_id(name: name, bio: bio)
end
# ---- :one / :many / :exec -------------------------------------------------------------------------------------
def test_flow
first = create_author(Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME)
@query_sql.create_author(name: Consts::DR_SEUSS_AUTHOR, bio: Consts::DR_SEUSS_QUOTE)
assert_equal GetAuthorRow.new(first, Consts::BOJACK_AUTHOR, Consts::BOJACK_THEME),
@query_sql.get_author(id: first)
assert_nil @query_sql.get_author(id: -1)
assert_equal [Consts::BOJACK_AUTHOR, Consts::DR_SEUSS_AUTHOR].sort, @query_sql.list_authors.map(&:name).sort
@query_sql.delete_author(id: first)
assert_equal [Consts::DR_SEUSS_AUTHOR], @query_sql.list_authors.map(&:name)
end
def test_nullable_columns_round_trip_nil
id = create_author('no bio', nil)
assert_nil @query_sql.get_author(id: id).bio
end
# ---- :execlastid / :execrows -----------------------------------------------------------------------------------
def test_execlastid_returns_the_returning_column
first = create_author('a')
second = create_author('b')
assert_kind_of Integer, first
assert_equal first + 1, second
assert_kind_of Integer, @query_sql.create_book(name: 'book', author_id: first)
end
def test_execrows_returns_affected_row_count
create_author('with bio', 'bio')
create_author('with bio 2', 'bio')
create_author('without bio', nil)
assert_equal 2, @query_sql.update_author_bios(bio: 'new bio')
end
# ---- sqlc.arg / sqlc.narg --------------------------------------------------------------------------------------
def test_numbered_parameters_limit_and_offset
%w[a b c d].each { |name| create_author(name) }
page = @query_sql.list_authors_paged(limit: 2, offset: 1)
assert_equal %w[b c], page.map(&:name)
end
def test_nullable_named_arg
%w[abc abd xyz].each { |name| create_author(name) }
assert_equal 3, @query_sql.get_author_by_name_pattern(name_pattern: nil).size
ab = @query_sql.get_author_by_name_pattern(name_pattern: 'ab%')
assert_equal %w[abc abd], ab.map(&:name).sort
end
# ---- sqlc.slice ------------------------------------------------------------------------------------------------
def test_slice
ids = %w[a b c].map { |name| create_author(name) }
got = @query_sql.get_authors_by_ids(ids: ids.first(2))
assert_equal ids.first(2).sort, got.map(&:id).sort
end
def test_empty_slice_matches_nothing
create_author('a')
assert_empty @query_sql.get_authors_by_ids(ids: [])
end
def test_several_slices_in_one_query
ids = %w[a b c].map { |name| create_author(name) }
got = @query_sql.get_authors_by_ids_and_names(ids: ids, names: %w[b c x])
assert_equal %w[b c], got.map(&:name).sort
end
# ---- :copyfrom -------------------------------------------------------------------------------------------------
def test_copyfrom
rows = [
CopyAuthorsArgs.new(name: 'plain', bio: 'bio'),
CopyAuthorsArgs.new(name: 'no bio', bio: nil),
CopyAuthorsArgs.new(name: "comma, 'quotes', \"double\", back\\slash\nnewline", bio: 'special')
]
@query_sql.copy_authors(rows)
assert_equal rows.to_h { |r| [r.name, r.bio] }, @query_sql.list_authors.to_h { |a| [a.name, a.bio] }
end
def test_copyfrom_is_batched_below_the_bound_parameter_limit
@query_sql.copy_authors([])
assert_empty @query_sql.list_authors
@query_sql.copy_authors((1..2500).map { |i| CopyAuthorsArgs.new(name: "author #{i}", bio: i.to_s) })
assert_equal 2500, @query_sql.list_authors.size
end
# ---- sqlc.embed ------------------------------------------------------------------------------------------------
def test_embed
first = create_author('a', 'bio a')
second = create_author('b')
@query_sql.create_book(name: 'book 1', author_id: first)
@query_sql.create_book(name: 'book 2', author_id: second)
rows = @query_sql.list_all_authors_books
assert_equal %w[a b], rows.map { |r| r.author.name }
assert_equal ['book 1', 'book 2'], rows.map { |r| r.book.name }
assert_equal Author.new(first, 'a', 'bio a'), rows.first.author
assert_instance_of Book, rows.first.book
end
def test_embed_with_plain_columns
id = create_author('a', 'bio')
@query_sql.create_book(name: 'the book', author_id: id)
row = @query_sql.get_authors_by_book_name(name: 'the book').first
assert_equal [id, 'a', 'bio'], [row.id, row.name, row.bio]
assert_equal 'the book', row.book.name
end
def test_same_table_embedded_twice
low = create_author('same')
high = create_author('same')
create_author('different')
rows = @query_sql.get_duplicate_authors
assert_equal 1, rows.size
assert_equal [low, high], [rows.first.author.id, rows.first.author2.id]
end
# ---- transactions ----------------------------------------------------------------------------------------------
def test_transaction_commits_and_returns_the_block_value
result = @query_sql.transaction do |tx|
id = tx.create_author_return_id(name: 'in tx', bio: nil)
tx.create_book(name: 'book', author_id: id)
:done
end
assert_equal :done, result
assert_equal ['in tx'], @query_sql.list_authors.map(&:name)
end
def test_transaction_rolls_back_on_error
assert_raises(RuntimeError) do
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'rolled back', bio: nil)
raise 'boom'
end
end
assert_empty @query_sql.list_authors
end
def test_declared_types_match_runtime_values
@db.execute('INSERT INTO authors (id, name, bio) VALUES (1, ?, ?)', ['a', 'b'])
@db.execute('INSERT INTO authors (id, name, bio) VALUES (2, ?, NULL)', ['c'])
rbs = File.expand_path('../examples/sqlite3/query_sql.rbs', __dir__)
@query_sql.list_authors.each { |row| assert_empty RbsTypes.mismatches(row, rbs, 'ListAuthorsRow') }
end
def test_nested_transaction_rolls_back_to_a_savepoint
@query_sql.transaction do |tx|
tx.create_author_return_id(name: 'outer', bio: nil)
assert_raises(RuntimeError) do
tx.transaction do |inner|
inner.create_author_return_id(name: 'inner, rolled back', bio: nil)
raise 'boom'
end
end
tx.create_author_return_id(name: 'outer 2', bio: nil)
end
assert_equal ['outer', 'outer 2'], @query_sql.list_authors.map(&:name).sort
end
def test_nested_transaction_commits_with_the_outer_one
@query_sql.transaction do |tx|
assert_equal 7, tx.transaction { |inner| inner.create_author_return_id(name: 'inner', bio: nil) && 7 }
end
assert_equal ['inner'], @query_sql.list_authors.map(&:name)
assert_raises(RuntimeError) do
@query_sql.transaction do |tx|
tx.transaction { |inner| inner.create_author_return_id(name: 'inner 2', bio: nil) }
raise 'boom'
end
end
assert_equal ['inner'], @query_sql.list_authors.map(&:name)
end
def test_missing_keyword_arguments_are_errors
assert_raises(ArgumentError) { @query_sql.get_author }
assert_raises(ArgumentError) { @query_sql.get_author(id: 1, unknown: 2) }
end
def test_named_nullable_arguments_are_optional
create_author('abc')
assert_equal ['abc'], @query_sql.get_author_by_name_pattern.map(&:name)
end
def test_transaction_mode
assert_equal 1, @query_sql.transaction(mode: :immediate) { 1 }
end
# ---- Ruby conventions: bang finders, logger ----------------------------------------------------------------
def test_bang_finder_raises_not_found
id = create_author('a', nil)
assert_equal 'a', @query_sql.get_author!(id: id).name
error = assert_raises(QuerySql::NotFound) { @query_sql.get_author!(id: -1) }
assert_equal 'GetAuthor returned no row', error.message
assert_nil @query_sql.get_author(id: -1) # the plain finder still returns nil
end
def test_logger_receives_query_names_and_durations
io = StringIO.new
logged = QuerySql.new(@db, logger: Logger.new(io, level: Logger::DEBUG))
logged.list_authors
logged.transaction { |tx| tx.list_authors }
assert_match(/QuerySql#list_authors \(\d+\.\dms\)/, io.string)
assert_match(/QuerySql#transaction \(\d+\.\dms\)/, io.string)
@query_sql.list_authors # no logger: nothing is logged, nothing breaks
end
# ---- models ----------------------------------------------------------------------------------------------------
def test_models_are_generated_for_every_table
assert_equal %i[id name bio], Author.members
assert_equal %i[id name author_id description], Book.members
end
end