Skip to content

Commit

Permalink
Update to make test pass in general and not just for travis. Also hav…
Browse files Browse the repository at this point in the history
…e SFX db models check if their configs are set.
  • Loading branch information
scotdalton committed Oct 11, 2012
1 parent 53c7ce5 commit 906a9c5
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 128 deletions.
5 changes: 0 additions & 5 deletions app/models/sfx4/abstract/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
module Sfx4
module Abstract
module Base
# Was a SFX DB connection set in database.yml to connect directly to sfx?
def connection_configured?
(not (connection_config.nil? or connection_config.blank? or connection_config[:adapter].blank?))
end

# Class method for the module that gets called by the umlaut:load_sfx_urls task.
# Kind of hacky way of trying to extract target URLs from SFX4.
# Will probably be deprecated in the near future.
Expand Down
9 changes: 7 additions & 2 deletions app/models/sfx4/global/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module Sfx4
module Global
class Base < ActiveRecord::Base
self.establish_connection :sfx4_global
# Was a SFX Global DB connection set in database.yml to connect directly to sfx?
def self.connection_configured?
config = ActiveRecord::Base.configurations["sfx4_global"]
(not (config.nil? or config.blank? or config[:adapter].blank?))
end

self.establish_connection :sfx4_global if self.connection_configured?
# ActiveRecord likes it when we tell it this is an abstract
# class only.
self.abstract_class = true
self.abstract_class = true

# All SFX things are read-only!
def readonly?()
Expand Down
9 changes: 8 additions & 1 deletion app/models/sfx4/local/base.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
module Sfx4
module Local
class Base < ActiveRecord::Base
self.establish_connection :sfx_db
# Was a SFX DB connection set in database.yml to connect directly to sfx?
def self.connection_configured?
config = ActiveRecord::Base.configurations["sfx_db"]
(not (config.nil? or config.blank? or config[:adapter].blank?))
end

self.establish_connection :sfx_db if self.connection_configured?
# ActiveRecord likes it when we tell it this is an abstract
# class only.
self.abstract_class = true

extend Sfx4::Abstract::Base

# All SFX things are read-only!
Expand Down
28 changes: 16 additions & 12 deletions test/dummy/db/migrate/20120927163304_sfx4_global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
# DO NOT USE THIS FOR ANYTHING LIKE A REAL SFX DATABASE.
class Sfx4Global < ActiveRecord::Migration
def connection
ActiveRecord::Base.establish_connection(:sfx4_global)
ActiveRecord::Base.connection.initialize_schema_migrations_table
ActiveRecord::Base.connection
if sfx4_mock_instance?
ActiveRecord::Base.establish_connection(:sfx4_global)
ActiveRecord::Base.connection.initialize_schema_migrations_table
ActiveRecord::Base.connection
end
end

def change
unless_testing_raise_error
create_table "KB_OBJECTS", {:id => false} do |t|
t.integer "OBJECT_ID", :default => 0, :null => false, :limit => 8
if sfx4_mock_instance?
create_table "KB_OBJECTS", {:id => false} do |t|
t.integer "OBJECT_ID", :default => 0, :null => false, :limit => 8
end
execute "ALTER TABLE KB_OBJECTS ADD PRIMARY KEY (OBJECT_ID);"
else
puts "Skipping SFX Global migration since SFX global DB specified is not a mock instance."
end
execute "ALTER TABLE KB_OBJECTS ADD PRIMARY KEY (OBJECT_ID);"
end

def unless_testing_raise_error
unless ActiveRecord::Base.configurations["sfx4_global"]["mock_instance"]
raise SecurityError.new("Danger! This is for mock SFX testing only! Do not run this migration against any sort of real SFX database.")
end
def sfx4_mock_instance?
(ActiveRecord::Base.configurations["sfx4_global"] and
ActiveRecord::Base.configurations["sfx4_global"]["mock_instance"])
end
end
end
78 changes: 41 additions & 37 deletions test/dummy/db/migrate/20120927164040_sfx4_local.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,54 @@
# DO NOT USE THIS FOR ANYTHING LIKE A REAL SFX DATABASE.
class Sfx4Local < ActiveRecord::Migration
def connection
ActiveRecord::Base.establish_connection(:sfx_db)
ActiveRecord::Base.connection.initialize_schema_migrations_table
ActiveRecord::Base.connection
if sfx4_mock_instance?
ActiveRecord::Base.establish_connection(:sfx_db)
ActiveRecord::Base.connection.initialize_schema_migrations_table
ActiveRecord::Base.connection
end
end

def change
unless_testing_raise_error
create_table "AZ_TITLE", {:id => false} do |t|
t.integer "AZ_TITLE_ID", :default => 0, :null => false
t.string "AZ_PROFILE", :limit => 100, :null => false
t.integer "OBJECT_ID", :default => 0, :null => false, :limit => 8
t.string "TITLE_DISPLAY", :limit => 255, :null => false
t.string "TITLE_SORT", :limit => 200, :null => false
t.string "SCRIPT", :limit => 20, :null => false
end
execute "ALTER TABLE AZ_TITLE ADD PRIMARY KEY (AZ_TITLE_ID);"
if sfx4_mock_instance?
create_table "AZ_TITLE", {:id => false} do |t|
t.integer "AZ_TITLE_ID", :default => 0, :null => false
t.string "AZ_PROFILE", :limit => 100, :null => false
t.integer "OBJECT_ID", :default => 0, :null => false, :limit => 8
t.string "TITLE_DISPLAY", :limit => 255, :null => false
t.string "TITLE_SORT", :limit => 200, :null => false
t.string "SCRIPT", :limit => 20, :null => false
end
execute "ALTER TABLE AZ_TITLE ADD PRIMARY KEY (AZ_TITLE_ID);"

create_table "AZ_EXTRA_INFO", {:id => false} do |t|
t.integer "AZ_EXTRA_INFO_ID", :default => 0, :null => false
t.string "AZ_PROFILE", :limit => 100, :null => false
t.integer "OBJECT_ID", :default => 0, :null => false, :limit => 8
t.text "EXTRA_INFO_XML", :limit => 16777215
end
execute "ALTER TABLE AZ_EXTRA_INFO ADD PRIMARY KEY (AZ_EXTRA_INFO_ID);"
create_table "AZ_EXTRA_INFO", {:id => false} do |t|
t.integer "AZ_EXTRA_INFO_ID", :default => 0, :null => false
t.string "AZ_PROFILE", :limit => 100, :null => false
t.integer "OBJECT_ID", :default => 0, :null => false, :limit => 8
t.text "EXTRA_INFO_XML", :limit => 16777215
end
execute "ALTER TABLE AZ_EXTRA_INFO ADD PRIMARY KEY (AZ_EXTRA_INFO_ID);"

create_table "AZ_TITLE_SEARCH", {:id => false} do |t|
t.integer "AZ_TITLE_SEARCH_ID", :default => 0, :null => false
t.string "AZ_PROFILE", :limit => 100, :null => false
t.integer "AZ_TITLE_ID", :default => 0, :null => false
t.text "TITLE_SEARCH", :null => false
end
execute "ALTER TABLE AZ_TITLE_SEARCH ADD PRIMARY KEY (AZ_TITLE_SEARCH_ID);"
create_table "AZ_TITLE_SEARCH", {:id => false} do |t|
t.integer "AZ_TITLE_SEARCH_ID", :default => 0, :null => false
t.string "AZ_PROFILE", :limit => 100, :null => false
t.integer "AZ_TITLE_ID", :default => 0, :null => false
t.text "TITLE_SEARCH", :null => false
end
execute "ALTER TABLE AZ_TITLE_SEARCH ADD PRIMARY KEY (AZ_TITLE_SEARCH_ID);"

create_table "AZ_LETTER_GROUP", {:id => false} do |t|
t.integer "AZ_LETTER_GROUP_ID", :default => 0, :null => false
t.integer "AZ_TITLE_ID", :default => 0, :null => false
t.string "AZ_LETTER_GROUP_NAME", :limit => 10, :null => false
create_table "AZ_LETTER_GROUP", {:id => false} do |t|
t.integer "AZ_LETTER_GROUP_ID", :default => 0, :null => false
t.integer "AZ_TITLE_ID", :default => 0, :null => false
t.string "AZ_LETTER_GROUP_NAME", :limit => 10, :null => false
end
execute "ALTER TABLE AZ_LETTER_GROUP ADD PRIMARY KEY (AZ_LETTER_GROUP_ID);"
else
puts "Skipping SFX DB migration since the SFX DB specified is not a mock instance."
end
execute "ALTER TABLE AZ_LETTER_GROUP ADD PRIMARY KEY (AZ_LETTER_GROUP_ID);"
end

def unless_testing_raise_error
unless ActiveRecord::Base.configurations["sfx_db"]["mock_instance"]
raise SecurityError.new("Danger! This is for mock SFX testing only! Do not run this migration against any sort of real SFX database.")
end
def sfx4_mock_instance?
(ActiveRecord::Base.configurations["sfx_db"] and
ActiveRecord::Base.configurations["sfx_db"]["mock_instance"])
end
end
end
58 changes: 32 additions & 26 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,44 @@
ActiveSupport::Deprecation.debug = true

class ActiveSupport::TestCase
# Load SFX 4 fixtures only if we are explicitly creating a mock_instance
# which should really only be the case for travis-ci.org
def self.sfx4_fixtures(*fixture_names)
unless_testing_raise_error
sfx4s = ["Global", "Local"]
sfx4s.each do |sfx4|
# Get the db module associate with this sfx4 instance
sfx4_module = Sfx4.const_get(sfx4.to_sym)
# Get the connection from the :Base class for each sfx4
# Set the path
path = "#{File.dirname(__FILE__)}/fixtures/#{sfx4_module.to_s.underscore}"
# Get class names hash of table_name => class_name
class_names = {}
connection = nil
fixture_names.collect{|t|t.to_s}.each do |table|
next unless sfx4_module.const_defined?(table.classify)
#Find class from table name
klass = sfx4_module.const_get table.classify
connection ||= klass.connection
class_names[klass.table_name.downcase.to_sym] = klass.name
# Load SFX 4 fixtures only if we are explicitly creating a mock_instance
# which should really only be the case for travis-ci.org
if (sfx4_mock_instance?)
sfx4s = ["Global", "Local"]
sfx4s.each do |sfx4|
# Get the db module associate with this sfx4 instance
sfx4_module = Sfx4.const_get(sfx4.to_sym)
# Get the connection from the :Base class for each sfx4
# Set the path
path = "#{File.dirname(__FILE__)}/fixtures/#{sfx4_module.to_s.underscore}"
# Get class names hash of table_name => class_name
class_names = {}
connection = nil
fixture_names.collect{|t|t.to_s}.each do |table|
next unless sfx4_module.const_defined?(table.classify)
#Find class from table name
klass = sfx4_module.const_get table.classify
connection ||= klass.connection
class_names[klass.table_name.downcase.to_sym] = klass.name
end
# Table names are just the keys of the class names
table_names = class_names.keys.collect{|t| t.to_s}
# Create and Instantiate Fixtures
ActiveRecord::Fixtures.create_fixtures(path, table_names, class_names){connection}.first.fixtures
end
# Table names are just the keys of the class names
table_names = class_names.keys.collect{|t| t.to_s}
# Create and Instantiate Fixtures
ActiveRecord::Fixtures.create_fixtures(path, table_names, class_names){connection}.first.fixtures
else
warn "Skipping SFX4 fixtures since the SFX DB specified is not a mock instance."
end
end

def self.unless_testing_raise_error
unless (ActiveRecord::Base.configurations["sfx_db"]["mock_instance"] and
ActiveRecord::Base.configurations["sfx_db"]["mock_instance"] and
def self.sfx4_mock_instance?
(ActiveRecord::Base.configurations["sfx_db"] and
ActiveRecord::Base.configurations["sfx_db"]["mock_instance"] and
ActiveRecord::Base.configurations["sfx4_global"] and
ActiveRecord::Base.configurations["sfx4_global"]["mock_instance"])
raise SecurityError.new("Danger! These fixtures are for mock SFX testing only! Do not run fixtures against any sort of real SFX database.")
end
end
end

Expand Down
101 changes: 59 additions & 42 deletions test/unit/sfx4_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,72 +15,89 @@ class Sfx4SearchTest < ActiveSupport::TestCase
@page = 1
end

# test "fetch urls?" do
# assert(SearchMethods::Sfx4.fetch_urls?)
# end
#
# test "fetch urls" do
# urls = SearchMethods::Sfx4.fetch_urls
# assert_instance_of(Array, urls)
# urls.each {|url|
# assert_instance_of(String, url)
# end
# end

test "find by title contains" do
# Indices aren't set up in test instance
# so commenting out this test for now.
# @title_query_param = "Air"
# @search_type_param = "contains"
# (context_objects, count) = find_by_title
# assert_equal(3, count)
# assert_instance_of(Array, context_objects)
# context_objects.each {|context_object|
# assert_instance_of(OpenURL::ContextObject, context_object)
# assert_match(/Air/i, context_object.referent.get_metadata("jtitle"))
# assert_not_nil(context_object.referent.get_metadata("object_id"))
# }
# # (context_objects, count) = find_by_title
# # Only run the assertions if we are using our mock instance
# # otherwise just check that this doesn't raise errors since
# # it's non-deterministic.
# if(self.class.sfx4_mock_instance?)
# assert_equal(3, count)
# assert_instance_of(Array, context_objects)
# context_objects.each {|context_object|
# assert_instance_of(OpenURL::ContextObject, context_object)
# assert_match(/Air/i, context_object.referent.get_metadata("jtitle"))
# assert_not_nil(context_object.referent.get_metadata("object_id"))
# }
# end
end

test "find by title begins with" do
return unless sfx4_connection_configured?
@title_query_param = "Account"
@search_type_param = "begins"
(context_objects, count) = find_by_title
assert_equal(5, count)
assert_instance_of(Array, context_objects)
context_objects.each {|context_object|
assert_instance_of(OpenURL::ContextObject, context_object)
assert_match(/^(The )?Account/, context_object.referent.get_metadata("jtitle"))
assert_not_nil(context_object.referent.get_metadata("object_id"))
}
# Only run the assertions if we are using our mock instance
# otherwise just check that this doesn't raise errors since
# it's non-deterministic.
if(self.class.sfx4_mock_instance?)
assert_equal(5, count)
assert_instance_of(Array, context_objects)
context_objects.each {|context_object|
assert_instance_of(OpenURL::ContextObject, context_object)
assert_match(/^(The )?Account/, context_object.referent.get_metadata("jtitle"))
assert_not_nil(context_object.referent.get_metadata("object_id"))
}
end
end

test "find by title exact" do
return unless sfx4_connection_configured?
@title_query_param = "Advances in Applied Probability"
@search_type_param = "exact"
(context_objects, count) = find_by_title
assert_equal(1, count)
assert_instance_of(Array, context_objects)
context_objects.each {|context_object|
assert_instance_of(OpenURL::ContextObject, context_object)
assert_equal("Advances in Applied Probability", context_object.referent.get_metadata("jtitle"))
assert_not_nil(context_object.referent.get_metadata("object_id"))
}
# Only run the assertions if we are using our mock instance
# otherwise just check that this doesn't raise errors since
# it's non-deterministic.
if(self.class.sfx4_mock_instance?)
assert_equal(1, count)
assert_instance_of(Array, context_objects)
context_objects.each {|context_object|
assert_instance_of(OpenURL::ContextObject, context_object)
assert_equal("Advances in Applied Probability", context_object.referent.get_metadata("jtitle"))
assert_not_nil(context_object.referent.get_metadata("object_id"))
}
end
end

test"find by group" do
return unless sfx4_connection_configured?
@params[:id] = "A"
(context_objects, count) = find_by_group
assert_equal(16, count)
assert_instance_of(Array, context_objects)
context_objects.each {|context_object|
assert_instance_of(OpenURL::ContextObject, context_object)
assert_match(/^(The )?A/, context_object.referent.get_metadata("jtitle"))
assert_not_nil(context_object.referent.get_metadata("object_id"))
}
# Only run the assertions if we are using our mock instance
# otherwise just check that this doesn't raise errors since
# it's non-deterministic.
if(self.class.sfx4_mock_instance?)
assert_equal(16, count)
assert_instance_of(Array, context_objects)
context_objects.each {|context_object|
assert_instance_of(OpenURL::ContextObject, context_object)
assert_match(/^(The )?A/, context_object.referent.get_metadata("jtitle"))
assert_not_nil(context_object.referent.get_metadata("object_id"))
}
end
end

test "fetch_urls?" do
assert(SearchMethods::Sfx4.fetch_urls?)
assert(SearchMethods::Sfx4.fetch_urls?,
":sfx_db is not configured in database.yml, and 'SFX controlled' URLs will not be loaded.")
end

private
def sfx4_connection_configured?
Sfx4::Local::AzTitle.connection_configured?
end
end
Loading

0 comments on commit 906a9c5

Please sign in to comment.