Skip to content

Commit

Permalink
Start adding tests by adding ActiveRecord test harness
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegeek committed Oct 12, 2022
1 parent 96968b6 commit bc056c7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ Gemfile.lock
/.gem_rbs_collection/
rbs_collection.lock.yaml
.tool-versions
test.db
test.log
6 changes: 5 additions & 1 deletion lib/encoded_id/rails/with_encoded_id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
module EncodedId
module Rails
module WithEncodedId
class << self
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
# Find by encoded ID and optionally ensure record ID is the same as constraint (can be slugged)
def find_by_encoded_id(slugged_encoded_id, with_id: nil)
encoded_id = extract_id_part(slugged_encoded_id)
Expand Down
4 changes: 3 additions & 1 deletion test/encoded_id/test_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ def test_that_it_has_a_version_number
end

def test_it_does_something_useful
assert false
m = MyModel.create(foo: "bar", bar: 123.5)
assert m.persisted?
asset_equal "bar", m.encoded_id
end
end
5 changes: 5 additions & 0 deletions test/support/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class MyModel < ::ActiveRecord::Base
include EncodedId::WithEncodedId
end
10 changes: 10 additions & 0 deletions test/support/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

ActiveRecord::Schema.define do
create_table :my_models, force: true do |t|
t.column :foo, :string
t.column :bar, :numeric
t.datetime :created_at
t.datetime :updated_at
end
end
24 changes: 24 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,27 @@
require "encoded_id/rails"

require "minitest/autorun"

# Thanks to https://github.com/zdennis/activerecord-import/tree/master/test for the config here
require "active_record"

ActiveRecord::Base.logger = Logger.new("test.log")
ActiveRecord::Base.logger.level = Logger::DEBUG

config = {
adapter: "sqlite3",
database: "test.db"
}
db_config = ActiveRecord::DatabaseConfigurations::HashConfig.new("test", "sqlite3", config)
ActiveRecord::Base.configurations.configurations << db_config

ActiveRecord::Base.default_timezone = :utc

ActiveRecord::Base.establish_connection :test

ActiveSupport::Notifications.subscribe(/active_record.sql/) do |_, _, _, _, hsh|
ActiveRecord::Base.logger.info hsh[:sql]
end

require_relative "support/schema"
require_relative "support/model"

0 comments on commit bc056c7

Please sign in to comment.