Skip to content

Commit 4e2d8cc

Browse files
committed
♻️ Refactor spec support harness for ROM
1 parent baa3a81 commit 4e2d8cc

File tree

8 files changed

+139
-95
lines changed

8 files changed

+139
-95
lines changed

.rubocop_gradual.lock

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"README.md:2294530640": [
3-
[275, 3, 100, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3592044714]
2+
"README.md:2478083419": [
3+
[277, 3, 100, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3592044714]
44
],
55
"bin/bundle:247448467": [
66
[64, 5, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2485198147]
@@ -31,13 +31,12 @@
3131
[57, 13, 125, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3604044426],
3232
[69, 13, 83, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 902334745]
3333
],
34-
"lib/omniauth/identity/models/rom.rb:4274893658": [
35-
[23, 9, 804, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2641680277],
34+
"lib/omniauth/identity/models/rom.rb:1077115502": [
35+
[23, 9, 814, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 2641680277],
3636
[34, 33, 14, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 3392992668],
37-
[34, 48, 15, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 3094507655],
38-
[51, 13, 15, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 1768297560],
39-
[67, 18, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2230320645],
40-
[68, 54, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2230320645]
37+
[56, 13, 15, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 1768297560],
38+
[72, 18, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2230320645],
39+
[73, 54, 20, "ThreadSafety/ClassInstanceVariable: Avoid class instance variables.", 2230320645]
4140
],
4241
"lib/omniauth/identity/models/sequel.rb:101522047": [
4342
[44, 9, 3379, "Style/ClassMethodsDefinitions: Use `class << self` to define a class method.", 3595458005]
@@ -103,15 +102,13 @@
103102
[14, 1, 61, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/identity/models/mongoid*_spec.rb`.", 2184534986],
104103
[32, 9, 71, "RSpec/SubjectStub: Do not stub methods of the object under test.", 399334056]
105104
],
106-
"spec_orms/rom_spec.rb:2718230340": [
107-
[42, 1, 57, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/identity/models/rom*_spec.rb`.", 18469108],
108-
[43, 3, 254, "RSpec/LeakyConstantDeclaration: Stub constant instead of declaring explicitly.", 151871654],
109-
[51, 3, 12, "RSpec/BeforeAfterAll: Beware of using `before(:all)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `before(:all)` are not automatically rolled back.", 86334566]
105+
"spec_orms/rom_spec.rb:1364234352": [
106+
[5, 1, 57, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/identity/models/rom*_spec.rb`.", 18469108],
107+
[6, 3, 12, "RSpec/BeforeAfterAll: Beware of using `before(:all)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `before(:all)` are not automatically rolled back.", 86334566]
110108
],
111-
"spec_orms/sequel_spec.rb:1275922107": [
112-
[22, 1, 60, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/identity/models/sequel*_spec.rb`.", 36626431],
113-
[23, 3, 12, "RSpec/BeforeAfterAll: Beware of using `before(:all)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `before(:all)` are not automatically rolled back.", 86334566],
114-
[34, 7, 42, "Layout/EmptyLinesAfterModuleInclusion: Add an empty line after module inclusion.", 1846499429],
115-
[48, 9, 71, "RSpec/SubjectStub: Do not stub methods of the object under test.", 399334056]
109+
"spec_orms/sequel_spec.rb:1276231279": [
110+
[12, 1, 60, "RSpec/SpecFilePathFormat: Spec path should end with `omni_auth/identity/models/sequel*_spec.rb`.", 36626431],
111+
[13, 3, 12, "RSpec/BeforeAfterAll: Beware of using `before(:all)` as it may cause state to leak between tests. If you are using `rspec-rails`, and `use_transactional_fixtures` is enabled, then records created in `before(:all)` are not automatically rolled back.", 86334566],
112+
[39, 9, 71, "RSpec/SubjectStub: Do not stub methods of the object under test.", 399334056]
116113
]
117114
}

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,16 @@ This gem is compatible with a wide range of Ruby versions and Ruby ORMs, as of M
167167
* mongoid 7.3, 7.4, 8.1, 9.0
168168
* bson 4.12, 4.15, 5.0, HEAD
169169
* sequel 5.86+
170-
* At least 5 different database ORM adapters, which connect to 15 different database clients!
170+
* rom-sql (Ruby Object Mapper) 3.7+
171+
* At least 6 different database ORM adapters, which connect to 15 different database clients!
171172

172173
| Databases | Adapter Libraries |
173-
| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
174+
|-----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------|
174175
| MySQL, MariaDB, PostgreSQL, SQLite | [ActiveRecord](https://guides.rubyonrails.org/active_record_basics.html) |
175176
| CouchDB | [CouchPotato](https://github.com/langalex/couch_potato) |
176177
| MongoDB | [Mongoid](https://github.com/mongodb/mongoid) |
177178
| RethinkDB | [NoBrainer](http://nobrainer.io/) |
179+
| ADO, Amalgalite, IBM_DB, JDBC, MySQL, MariaDB, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite, and TinyTDS | [rom-sql](https://rom-rb.org/) |
178180
| ADO, Amalgalite, IBM_DB, JDBC, MySQL, MariaDB, ODBC, Oracle, PostgreSQL, SQLAnywhere, SQLite, and TinyTDS | [Sequel](http://sequel.jeremyevans.net) |
179181

180182
## 🔧 Basic Usage
@@ -298,7 +300,23 @@ end
298300

299301
### Ruby Object Mapper
300302

301-
Would love to add a mixin for the [Ruby Object Mapper (ROM)](https://rom-rb.org/) if anyone wants to work on it!
303+
[ROM](https://rom-rb.org/) is an ORM for pretty much every database.
304+
305+
Include the `OmniAuth::Identity::Models::Rom` mixin and specify
306+
the `rom_container`, everything else is optional.
307+
308+
```ruby
309+
class Identity
310+
include OmniAuth::Identity::Models::Rom
311+
312+
# Configure the ROM container and relation
313+
self.rom_container = -> { MyDatabase.rom } # See spec_orms/rom_spec.rb for example
314+
self.rom_relation_name = :identities # optional, defaults to :idneitities
315+
self.owner_relation_name = :owners # optional, for loading associated owner
316+
self.auth_key_field = :email # optional, defaults to :email
317+
self.password_field = :password_digest # optional, defaults to :password_digest
318+
end
319+
```
302320

303321
### Custom Auth Model
304322

lib/omniauth/identity/models/rom.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
require "bcrypt"
44

5+
require "rom"
6+
require "rom-sql"
7+
58
module OmniAuth
69
module Identity
710
module Models
@@ -14,7 +17,7 @@ module Models
1417
#
1518
# # Configure the ROM container and relation
1619
# self.rom_container = -> { MyDatabase.rom } # See spec_orms/rom_spec.rb for example
17-
# self.rom_relation_name = :identities
20+
# self.rom_relation_name = :identities # optional, defaults to :idneitities
1821
# self.owner_relation_name = :owners # optional, for loading associated owner
1922
# self.auth_key_field = :email # optional, defaults to :email
2023
# self.password_field = :password_digest # optional, defaults to :password_digest
@@ -76,8 +79,6 @@ def locate(conditions)
7679
else
7780
new(identity_data)
7881
end
79-
else
80-
nil
8182
end
8283
end
8384
end

rom_spec_refactored.txt

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
LOADING DEBUGGER: true
2+
3+
Randomized with seed 27610
4+
5+
OmniAuth::Identity::Models::Rom
6+
model
7+
::authenticate
8+
calls locate with additional scopes when provided
9+
recovers gracefully if locate is nil
10+
calls locate and then authenticate
11+
::locate
12+
returns nil if not found
13+
finds a record by auth key
14+
class definition
15+
does not raise an error
16+
owner_relation_name
17+
loads associated owner data when owner_relation_name is configured
18+
::authenticate
19+
returns false with incorrect password
20+
returns false for non-existent user
21+
authenticates with correct password
22+
23+
Finished in 1.89 seconds (files took 0.98284 seconds to load)
24+
10 examples, 0 failures
25+
26+
Randomized with seed 27610
27+

spec_orms/rom_spec.rb

Lines changed: 5 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,27 @@
11
# frozen_string_literal: true
22

3-
# Bugfixes
4-
# JRuby needed an explicit "require 'logger'" for Rails < 7.1
5-
# See: https://github.com/rails/rails/issues/54260#issuecomment-2594650047
6-
# Placing above omniauth because it is a dependency of omniauth,
7-
# which is undeclared in older versions.
8-
require "logger"
9-
10-
# :nocov:
11-
DB = if RUBY_ENGINE == "jruby"
12-
require "jdbc/sqlite3"
13-
require "sequel"
14-
Sequel.connect("jdbc:sqlite::memory:")
15-
else
16-
require "sqlite3"
17-
require "sequel"
18-
Sequel.sqlite
19-
end
20-
# :nocov:
21-
22-
require "rom"
23-
require "rom-sql"
24-
25-
# Define the ROM relations
26-
class RomTestIdentities < ROM::Relation[:sql]
27-
schema(:rom_test_identities) do
28-
attribute :id, ROM::SQL::Types::Serial
29-
attribute :email, ROM::SQL::Types::String
30-
attribute :password_digest, ROM::SQL::Types::String
31-
attribute :owner_id, ROM::SQL::Types::Integer
32-
end
33-
end
34-
35-
class RomTestOwners < ROM::Relation[:sql]
36-
schema(:rom_test_owners) do
37-
attribute :id, ROM::SQL::Types::Serial
38-
attribute :name, ROM::SQL::Types::String
39-
end
40-
end
3+
require_relative "support/rspec_config/rom"
414

425
RSpec.describe(OmniAuth::Identity::Models::Rom, :sqlite3) do
43-
ROM_CONTAINER = begin
44-
# Set up ROM container with Sequel adapter
45-
rom_config = ROM::Configuration.new(:sql, DB)
46-
rom_config.register_relation(RomTestIdentities)
47-
rom_config.register_relation(RomTestOwners)
48-
ROM.container(rom_config)
49-
end
50-
516
before(:all) do
527
# Create the tables
53-
DB.create_table(:rom_test_identities) do
8+
ROM_DB.create_table(:rom_test_identities) do
549
primary_key :id
5510
String :email, null: false
5611
String :password_digest, null: false
5712
Integer :owner_id
5813
end
5914

60-
DB.create_table(:rom_test_owners) do
15+
ROM_DB.create_table(:rom_test_owners) do
6116
primary_key :id
6217
String :name, null: false
6318
end
6419
end
6520

6621
before do
6722
# Clear the tables before each test
68-
DB[:rom_test_identities].delete
69-
DB[:rom_test_owners].delete
70-
71-
rom_test_identity = Class.new do
72-
include OmniAuth::Identity::Models::Rom
73-
74-
self.rom_container = ROM_CONTAINER
75-
self.rom_relation_name = :rom_test_identities
76-
self.auth_key_field = :email
77-
self.password_field = :password_digest
78-
end
79-
stub_const("RomTestIdentity", rom_test_identity)
23+
ROM_DB[:rom_test_identities].delete
24+
ROM_DB[:rom_test_owners].delete
8025
end
8126

8227
describe "model", type: :model do

spec_orms/sequel_spec.rb

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,7 @@
77
# which is undeclared in older versions.
88
require "logger"
99

10-
# :nocov:
11-
DB = if RUBY_ENGINE == "jruby"
12-
require "jdbc/sqlite3"
13-
require "sequel"
14-
Sequel.connect("jdbc:sqlite::memory:")
15-
else
16-
require "sqlite3"
17-
require "sequel"
18-
Sequel.sqlite
19-
end
20-
# :nocov:
10+
require_relative "support/rspec_config/sequel"
2111

2212
RSpec.describe(OmniAuth::Identity::Models::Sequel, :sqlite3) do
2313
before(:all) do
@@ -30,8 +20,9 @@
3020
end
3121

3222
before do
33-
sequel_test_identity = Class.new(Sequel::Model(:sequel_test_identities)) do
23+
sequel_test_identity = Class.new(Sequel::Model(DB[:sequel_test_identities])) do
3424
include OmniAuth::Identity::Models::Sequel
25+
3526
auth_key :email
3627
end
3728
stub_const("SequelTestIdentity", sequel_test_identity)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# frozen_string_literal: true
2+
3+
require "logger"
4+
5+
# :nocov:
6+
ROM_DB = if RUBY_ENGINE == "jruby"
7+
require "jdbc/sqlite3"
8+
require "sequel"
9+
Sequel.connect("jdbc:sqlite::memory:rom")
10+
else
11+
require "sqlite3"
12+
require "sequel"
13+
Sequel.connect("sqlite::memory:rom")
14+
end
15+
# :nocov:
16+
17+
require "rom"
18+
require "rom-sql"
19+
20+
# Define the ROM relations
21+
class RomTestIdentities < ROM::Relation[:sql]
22+
schema(:rom_test_identities) do
23+
attribute :id, ROM::SQL::Types::Serial
24+
attribute :email, ROM::SQL::Types::String
25+
attribute :password_digest, ROM::SQL::Types::String
26+
attribute :owner_id, ROM::SQL::Types::Integer
27+
end
28+
end
29+
30+
class RomTestOwners < ROM::Relation[:sql]
31+
schema(:rom_test_owners) do
32+
attribute :id, ROM::SQL::Types::Serial
33+
attribute :name, ROM::SQL::Types::String
34+
end
35+
end
36+
37+
# Set up ROM container
38+
ROM_CONFIG = ROM::Configuration.new(:sql, ROM_DB)
39+
ROM_CONFIG.register_relation(RomTestIdentities)
40+
ROM_CONFIG.register_relation(RomTestOwners)
41+
ROM_CONTAINER = ROM.container(ROM_CONFIG)
42+
43+
class RomTestIdentity
44+
include OmniAuth::Identity::Models::Rom
45+
46+
self.rom_container = ROM_CONTAINER
47+
self.rom_relation_name = :rom_test_identities
48+
self.auth_key_field = :email
49+
self.password_field = :password_digest
50+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require "logger"
4+
5+
# :nocov:
6+
DB = if RUBY_ENGINE == "jruby"
7+
require "jdbc/sqlite3"
8+
require "sequel"
9+
Sequel.connect("jdbc:sqlite::memory:sequel")
10+
else
11+
require "sqlite3"
12+
require "sequel"
13+
Sequel.connect("sqlite::memory:sequel")
14+
end
15+
# :nocov:

0 commit comments

Comments
 (0)