Skip to content

Commit

Permalink
Allow specs to pass on PostgreSQL
Browse files Browse the repository at this point in the history
Allocate only a single Sequel::Database object.  Use a transaction
per test suite and savepoint per test.

Fix various issues, mostly related to using the correct database
type.
  • Loading branch information
jeremyevans committed Jul 9, 2024
1 parent 6a04984 commit 9fc4f18
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion spec/all.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
puts "Running specs with #{ENV['FRAMEWORK']||'roda'} framework"
Dir.new(File.dirname(__FILE__)).each{|f| require_relative f if f.end_with?('_spec.rb')}
puts "Running specs with #{ENV['FRAMEWORK']||'roda'} web framework and #{AutoFormeSpec::DB.database_type} database"
2 changes: 1 addition & 1 deletion spec/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@
click_link 'Albums'
page.current_path.must_equal '/Album/browse'

visit "/Album/association_links/#{Artist.first.id}"
visit "/Album/association_links/#{Album.first.id}"
click_link 'Artist1'
click_button 'Update'
page.current_path.must_match %r{Artist/edit/\d+}
Expand Down
10 changes: 5 additions & 5 deletions spec/basic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -840,15 +840,15 @@ def forme_name
filter do |ds, type, req|
case type
when :edit
ds.where{n0 > 1}
ds.where{n0 > '1'}
when :show
ds.where{n1 > 3}
ds.where{n1 > '3'}
when :delete
ds.where{n2 > 2}
ds.where{n2 > '2'}
when :browse
ds.where{n3 > 6}
ds.where{n3 > '6'}
when :search
ds.where{n4 > 4}
ds.where{n4 > '4'}
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/mtm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@
fill_in 'Name', :with=>'Artist1'
click_button 'Create'

visit '/Artist/mtm_edit/1?association=foo'
visit "/Artist/mtm_edit/#{Artist.first.id}?association=foo"
page.status_code.must_equal 404

visit '/Artist/mtm_edit/1?association=albums'
visit "/Artist/mtm_edit/#{Artist.first.id}?association=albums"
mod.mtm_associations{}
click_button 'Update'
page.status_code.must_equal 404
Expand Down
24 changes: 14 additions & 10 deletions spec/sequel_spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
require 'sequel'
#require 'logger'

Sequel::Model.cache_anonymous_models = false

module AutoFormeSpec
TYPE_MAP = {:string=>String, :integer=>Integer, :decimal=>Numeric, :boolean=>TrueClass}

db_url = ENV['AUTOFORME_SPEC_DATABASE_URL']
db_url ||= defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' ? 'jdbc:sqlite::memory:' : 'sqlite:/'
DB = Sequel.connect(db_url, :identifier_mangling=>false, :cache_schema=>false)
DB.extension :freeze_datasets
if ENV['LOG_SQLS']
require 'logger'
DB.loggers << Logger.new($stdout)
end
DB.freeze

def self.db_setup(tables)
db_url = ENV['DATABASE_URL']
db_url ||= defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby' ? 'jdbc:sqlite::memory:' : 'sqlite:/'
db = Sequel.connect(db_url, :identifier_mangling=>false)
db.extension :freeze_datasets
#db.loggers << Logger.new($stdout)
tables.each do |table, table_spec|
db.create_table(table) do
DB.create_table(table) do
if table_spec.kind_of? Enumerable
primary_key :id
table_spec.each do |name, type, opts|
Expand All @@ -21,9 +28,6 @@ def self.db_setup(tables)
end
end
end

db.freeze
db
end

def self.model_setup(db, models)
Expand Down
18 changes: 12 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
$: << File.expand_path(File.join(__FILE__, '../../lib'))
ENV['FRAMEWORK'] ||= 'roda'

module AutoFormeSpec
end
require_relative 'sequel_spec_helper'

if coverage = ENV.delete('COVERAGE')
require 'coverage'
Expand Down Expand Up @@ -35,16 +34,19 @@ class Minitest::HooksSpec
include Capybara::DSL

attr_reader :app
attr_reader :db
attr_reader :framework
attr_reader :model

def db
AutoFormeSpec::DB
end

def app=(app)
@app = Capybara.app = app
end

def db_setup(tables, &block)
@db = AutoFormeSpec.db_setup(tables)
def db_setup(tables)
AutoFormeSpec.db_setup(tables)
end

def model_setup(models)
Expand All @@ -57,8 +59,12 @@ def app_setup(klass=nil, opts={}, &block)
@model = @framework.models[klass.name] if klass
end

around(:all) do |&block|
db.transaction(:rollback=>:always, :auto_savepoint=>true){super(&block)}
end

around do |&block|
db ? db.transaction(:rollback=>:always){super(&block)} : super(&block)
db.transaction(:rollback=>:always, :auto_savepoint=>true){super(&block)}
end

after do
Expand Down
16 changes: 9 additions & 7 deletions spec/unit_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require_relative 'spec_helper'

id_cast = Sequel.cast(:id, String)

describe AutoForme do
before(:all) do
db_setup(:artists=>[[:name, :string]])
Expand Down Expand Up @@ -281,16 +283,16 @@
model.autocomplete(:type=>:show, :query=>'boo').must_equal ["#{b.id} - BooFar"]
model.autocomplete(:type=>:show, :query=>'oo').sort.must_equal ["#{a.id} - FooBar", "#{b.id} - BooFar"]

framework.autocomplete_options :display=>:id
framework.autocomplete_options :display=>id_cast
model.autocomplete(:type=>:show, :query=>a.id.to_s).must_equal ["#{a.id} - #{a.id}"]
framework.autocomplete_options{|mod, type, req| {:limit=>req}}
model.autocomplete(:type=>:show, :query=>'oo', :request=>1).must_equal ["#{a.id} - FooBar"]
model.autocomplete_options :display=>:id
model.autocomplete_options :display=>id_cast
model.autocomplete(:type=>:show, :query=>a.id.to_s).must_equal ["#{a.id} - #{a.id}"]

framework.autocomplete_options({})
model.autocomplete(:type=>:show, :query=>a.id.to_s).must_equal ["#{a.id} - #{a.id}"]
model.autocomplete_options :display=>proc{:id}
model.autocomplete_options :display=>proc{id_cast}
model.autocomplete(:type=>:show, :query=>b.id.to_s).must_equal ["#{b.id} - #{b.id}"]
model.autocomplete_options :limit=>1
model.autocomplete(:type=>:show, :query=>'oo').must_equal ["#{a.id} - FooBar"]
Expand Down Expand Up @@ -348,9 +350,9 @@
b = Artist.create(:name=>'BooFar')
model.autocomplete(:query=>'boo', :association=>:artist).must_equal ["#{b.id} - BooFar"]
model.autocomplete(:query=>'oo', :association=>:artist).sort.must_equal ["#{a.id} - FooBar", "#{b.id} - BooFar"]
artist.autocomplete_options :display=>:id
artist.autocomplete_options :display=>id_cast
model.autocomplete(:query=>a.id.to_s, :association=>:artist).must_equal ["#{a.id} - #{a.id}"]
artist.autocomplete_options :display=>proc{:id}
artist.autocomplete_options :display=>proc{id_cast}
model.autocomplete(:query=>b.id.to_s, :association=>:artist).must_equal ["#{b.id} - #{b.id}"]
artist.autocomplete_options :limit=>1
model.autocomplete(:query=>'oo', :association=>:artist).must_equal ["#{a.id} - FooBar"]
Expand Down Expand Up @@ -427,10 +429,10 @@
c = Album.create(:name=>'Quux')
c.add_artist(a)
model.autocomplete(:query=>'oo', :association=>:artists, :exclude=>c.id).sort.must_equal ["#{b.id} - BooFar"]
artist.autocomplete_options :display=>:id
artist.autocomplete_options :display=>id_cast
model.autocomplete(:query=>a.id.to_s, :association=>:artists).must_equal ["#{a.id} - #{a.id}"]
model.autocomplete(:query=>b.id.to_s, :association=>:artists, :exclude=>c.id).must_equal ["#{b.id} - #{b.id}"]
artist.autocomplete_options :display=>proc{:id}
artist.autocomplete_options :display=>proc{id_cast}
model.autocomplete(:query=>b.id.to_s, :association=>:artists).must_equal ["#{b.id} - #{b.id}"]
model.autocomplete(:query=>a.id.to_s, :association=>:artists, :exclude=>c.id).must_equal []
artist.autocomplete_options :limit=>1
Expand Down

0 comments on commit 9fc4f18

Please sign in to comment.