Skip to content

Commit 0699b37

Browse files
committed
test against rails
1 parent 36e35b5 commit 0699b37

File tree

8 files changed

+246
-10
lines changed

8 files changed

+246
-10
lines changed

.github/workflows/ruby.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,8 @@ jobs:
2727
- name: Lint
2828
run: bundle exec rake rubocop
2929

30-
- name: Build and test
31-
run: bundle exec rake test
30+
- name: Build
31+
run: bundle exec rake
32+
33+
- name: Test
34+
run: bundle exec rspec

Gemfile.lock

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,172 @@ PATH
77
GEM
88
remote: https://rubygems.org/
99
specs:
10+
actioncable (8.0.2)
11+
actionpack (= 8.0.2)
12+
activesupport (= 8.0.2)
13+
nio4r (~> 2.0)
14+
websocket-driver (>= 0.6.1)
15+
zeitwerk (~> 2.6)
16+
actionmailbox (8.0.2)
17+
actionpack (= 8.0.2)
18+
activejob (= 8.0.2)
19+
activerecord (= 8.0.2)
20+
activestorage (= 8.0.2)
21+
activesupport (= 8.0.2)
22+
mail (>= 2.8.0)
23+
actionmailer (8.0.2)
24+
actionpack (= 8.0.2)
25+
actionview (= 8.0.2)
26+
activejob (= 8.0.2)
27+
activesupport (= 8.0.2)
28+
mail (>= 2.8.0)
29+
rails-dom-testing (~> 2.2)
30+
actionpack (8.0.2)
31+
actionview (= 8.0.2)
32+
activesupport (= 8.0.2)
33+
nokogiri (>= 1.8.5)
34+
rack (>= 2.2.4)
35+
rack-session (>= 1.0.1)
36+
rack-test (>= 0.6.3)
37+
rails-dom-testing (~> 2.2)
38+
rails-html-sanitizer (~> 1.6)
39+
useragent (~> 0.16)
40+
actiontext (8.0.2)
41+
actionpack (= 8.0.2)
42+
activerecord (= 8.0.2)
43+
activestorage (= 8.0.2)
44+
activesupport (= 8.0.2)
45+
globalid (>= 0.6.0)
46+
nokogiri (>= 1.8.5)
47+
actionview (8.0.2)
48+
activesupport (= 8.0.2)
49+
builder (~> 3.1)
50+
erubi (~> 1.11)
51+
rails-dom-testing (~> 2.2)
52+
rails-html-sanitizer (~> 1.6)
53+
activejob (8.0.2)
54+
activesupport (= 8.0.2)
55+
globalid (>= 0.3.6)
56+
activemodel (8.0.2)
57+
activesupport (= 8.0.2)
58+
activerecord (8.0.2)
59+
activemodel (= 8.0.2)
60+
activesupport (= 8.0.2)
61+
timeout (>= 0.4.0)
62+
activestorage (8.0.2)
63+
actionpack (= 8.0.2)
64+
activejob (= 8.0.2)
65+
activerecord (= 8.0.2)
66+
activesupport (= 8.0.2)
67+
marcel (~> 1.0)
68+
activesupport (8.0.2)
69+
base64
70+
benchmark (>= 0.3)
71+
bigdecimal
72+
concurrent-ruby (~> 1.0, >= 1.3.1)
73+
connection_pool (>= 2.2.5)
74+
drb
75+
i18n (>= 1.6, < 2)
76+
logger (>= 1.4.2)
77+
minitest (>= 5.1)
78+
securerandom (>= 0.3)
79+
tzinfo (~> 2.0, >= 2.0.5)
80+
uri (>= 0.13.1)
1081
ast (2.4.2)
82+
base64 (0.2.0)
83+
benchmark (0.4.0)
84+
bigdecimal (3.1.9)
85+
builder (3.3.0)
86+
concurrent-ruby (1.3.5)
87+
connection_pool (2.5.0)
88+
crass (1.0.6)
89+
date (3.4.1)
1190
debug (1.9.2)
1291
irb (~> 1.10)
1392
reline (>= 0.3.8)
1493
diff-lcs (1.6.0)
94+
drb (2.2.1)
95+
erubi (1.13.1)
96+
globalid (1.2.1)
97+
activesupport (>= 6.1)
98+
i18n (1.14.7)
99+
concurrent-ruby (~> 1.0)
15100
io-console (0.7.2)
16101
irb (1.14.1)
17102
rdoc (>= 4.0.0)
18103
reline (>= 0.4.2)
19104
json (2.7.2)
20105
language_server-protocol (3.17.0.3)
21106
lint_roller (1.1.0)
107+
logger (1.6.6)
108+
loofah (2.24.0)
109+
crass (~> 1.0.2)
110+
nokogiri (>= 1.12.0)
111+
mail (2.8.1)
112+
mini_mime (>= 0.1.1)
113+
net-imap
114+
net-pop
115+
net-smtp
116+
marcel (1.0.4)
117+
mini_mime (1.1.5)
22118
mini_portile2 (2.8.8)
119+
minitest (5.25.5)
120+
net-imap (0.5.6)
121+
date
122+
net-protocol
123+
net-pop (0.1.2)
124+
net-protocol
125+
net-protocol (0.2.2)
126+
timeout
127+
net-smtp (0.5.1)
128+
net-protocol
129+
nio4r (2.7.4)
130+
nokogiri (1.18.5-x86_64-linux-gnu)
131+
racc (~> 1.4)
23132
parallel (1.26.3)
24133
parser (3.3.5.0)
25134
ast (~> 2.4.1)
26135
racc
27136
psych (5.1.2)
28137
stringio
29138
racc (1.8.1)
139+
rack (3.1.12)
140+
rack-session (2.1.0)
141+
base64 (>= 0.1.0)
142+
rack (>= 3.0.0)
143+
rack-test (2.2.0)
144+
rack (>= 1.3)
145+
rackup (2.2.1)
146+
rack (>= 3)
147+
rails (8.0.2)
148+
actioncable (= 8.0.2)
149+
actionmailbox (= 8.0.2)
150+
actionmailer (= 8.0.2)
151+
actionpack (= 8.0.2)
152+
actiontext (= 8.0.2)
153+
actionview (= 8.0.2)
154+
activejob (= 8.0.2)
155+
activemodel (= 8.0.2)
156+
activerecord (= 8.0.2)
157+
activestorage (= 8.0.2)
158+
activesupport (= 8.0.2)
159+
bundler (>= 1.15.0)
160+
railties (= 8.0.2)
161+
rails-dom-testing (2.2.0)
162+
activesupport (>= 5.0.0)
163+
minitest
164+
nokogiri (>= 1.6)
165+
rails-html-sanitizer (1.6.2)
166+
loofah (~> 2.21)
167+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
168+
railties (8.0.2)
169+
actionpack (= 8.0.2)
170+
activesupport (= 8.0.2)
171+
irb (~> 1.13)
172+
rackup (>= 1.0.0)
173+
rake (>= 12.2)
174+
thor (~> 1.0, >= 1.2.2)
175+
zeitwerk (~> 2.6)
30176
rainbow (3.1.1)
31177
rake (13.2.1)
32178
rake-compiler (1.2.8)
@@ -69,6 +215,7 @@ GEM
69215
rubocop-rspec (3.4.0)
70216
rubocop (~> 1.61)
71217
ruby-progressbar (1.13.0)
218+
securerandom (0.4.1)
72219
sqlite3 (2.4.0)
73220
mini_portile2 (~> 2.8.0)
74221
standard (1.35.0.1)
@@ -84,13 +231,25 @@ GEM
84231
lint_roller (~> 1.1)
85232
rubocop-performance (~> 1.23.0)
86233
stringio (3.1.1)
234+
thor (1.3.2)
235+
timeout (0.4.3)
236+
tzinfo (2.0.6)
237+
concurrent-ruby (~> 1.0)
87238
unicode-display_width (2.6.0)
239+
uri (1.0.3)
240+
useragent (0.16.11)
241+
websocket-driver (0.7.7)
242+
base64
243+
websocket-extensions (>= 0.1.0)
244+
websocket-extensions (0.1.5)
245+
zeitwerk (2.7.2)
88246

89247
PLATFORMS
90248
x86_64-linux-gnu
91249

92250
DEPENDENCIES
93251
debug (>= 1.0.0)
252+
rails (= 8.0.2)
94253
rake
95254
rake-compiler
96255
rspec

Rakefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ require "rubocop/rake_task"
77
RuboCop::RakeTask.new
88

99
require "rspec/core/rake_task"
10-
RSpec::Core::RakeTask.new(:spec)
10+
RSpec::Core::RakeTask.new(:spec) do |c|
11+
# loading rails in this file breaks `rake`
12+
c.exclude_pattern = "spec/sqlite_extensions/rails_spec.rb"
13+
end
1114

1215
Rake::ExtensionTask.new("sqlite_extensions/uuid") do |ext|
16+
ext.gem_spec = Gem::Specification.load("sqlite_extensions-uuid.gemspec")
1317
ext.name = "uuid"
1418
ext.lib_dir = "lib/sqlite_extensions/uuid"
1519
end

config/database.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
production:
2+
database: ':memory:'
3+
adapter: sqlite3
4+
extensions:
5+
- <%= SqliteExtensions::UUID.to_path %>

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@
88
end
99

1010
require "sqlite_extensions-uuid"
11+
require "debug"
12+
13+
# good enough for testing
14+
UUID_REGEX = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
# not required by our gem, but needed for these tests
4+
require "sqlite3"
5+
require "active_record/railtie"
6+
require "minitest/autorun"
7+
8+
# Adapted from https://github.com/rails/rails/blob/v8.0.2/guides/bug_report_templates/active_record_migrations.rb
9+
#
10+
RSpec.describe "Rails usage" do # rubocop:disable RSpec/DescribeClass
11+
before do
12+
gemspec = instance_double(
13+
Gem::Specification,
14+
require_path: File.join(__dir__, "../../lib")
15+
)
16+
allow(Gem).to receive(:loaded_specs).and_return("sqlite_extensions-uuid" => gemspec)
17+
18+
ENV["RAILS_ENV"] = "production"
19+
20+
# config/application.rb
21+
stub_const("TestApp", Class.new(Rails::Application) do
22+
config.load_defaults Rails::VERSION::STRING.to_f
23+
config.eager_load = false
24+
config.secret_key_base = "secret_key_base"
25+
end)
26+
Rails.application.initialize!
27+
28+
# db/migrate/yyyymmddhhmmss_create_users.rb
29+
stub_const("CreateUsers", Class.new(ActiveRecord::Migration[8.0]) do
30+
def change
31+
create_table :users, id: :string, default: -> { "uuid()" }, limit: 36 do |t|
32+
t.string :email_address, null: false
33+
t.timestamps
34+
end
35+
add_index :users, :email_address, unique: true
36+
end
37+
end)
38+
39+
# app/models/user.rb
40+
stub_const("User", Class.new(ActiveRecord::Base))
41+
end
42+
43+
it "allows Rails to support UUIDs for SQLite" do
44+
log_file = StringIO.new
45+
46+
# TODO: prevent Rails from logging to stdout entirely
47+
allow(Rails.logger).to receive(:debug).and_wrap_original do |m, msg|
48+
log_file.write msg
49+
end
50+
51+
CreateUsers.migrate(:up)
52+
53+
log_file.rewind
54+
output = log_file.read
55+
56+
expect(output).to include('CREATE TABLE "users"')
57+
expect(output).to include("varchar(36) DEFAULT (uuid())")
58+
59+
id_column = User.columns.find { |column| column.name == "id" }
60+
expect(id_column.sql_type).to eq "varchar(36)"
61+
62+
user = User.create!(email_address: "[email protected]")
63+
expect(user.id).to match(UUID_REGEX)
64+
end
65+
end

spec/sqlite_extensions/sqlite_spec.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212
allow(Gem).to receive(:loaded_specs).and_return("sqlite_extensions-uuid" => gemspec)
1313
end
1414

15-
# good enough for testing
16-
let(:uuid_regex) do
17-
/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i
18-
end
19-
2015
it "works when passed as an extension to SQLite3::Database.new" do
2116
db = SQLite3::Database.new(":memory:", extensions: [SqliteExtensions::UUID.to_path])
2217

2318
result = db.execute("select uuid()")
2419

2520
expect(result).to match([[an_instance_of(String)]])
2621
uuid = result.first.first
27-
expect(uuid).to match(uuid_regex)
22+
expect(uuid).to match(UUID_REGEX)
2823
end
2924

3025
it "works when using `enable_load_extension`" do
@@ -36,6 +31,6 @@
3631

3732
expect(result).to match([[an_instance_of(String)]])
3833
uuid = result.first.first
39-
expect(uuid).to match(uuid_regex)
34+
expect(uuid).to match(UUID_REGEX)
4035
end
4136
end

sqlite_extensions-uuid.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
2727
spec.add_development_dependency "rubocop-rake"
2828
spec.add_development_dependency "rubocop-rspec"
2929
spec.add_development_dependency "rubocop-performance"
30+
spec.add_development_dependency "rails", "8.0.2"
3031
end

0 commit comments

Comments
 (0)