dan-manges-unit_record 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +25 -0
- data/Rakefile +85 -0
- data/lib/active_record/connection_adapters/unit_record_adapter.rb +93 -0
- data/lib/unit_record/association_stubbing.rb +37 -0
- data/lib/unit_record/column_extension.rb +18 -0
- data/lib/unit_record/disconnected_active_record.rb +22 -0
- data/lib/unit_record/disconnected_fixtures.rb +10 -0
- data/lib/unit_record/disconnected_test_case.rb +13 -0
- data/lib/unit_record.rb +19 -0
- data/test/active_record/connection_adapters/unit_record_adapter_test.rb +90 -0
- data/test/db/schema.rb +26 -0
- data/test/test_helper.rb +67 -0
- data/test/unit_record/association_stubbing_test.rb +22 -0
- data/test/unit_record/column_cacher_test.rb +26 -0
- data/test/unit_record/column_extension_test.rb +33 -0
- data/test/unit_record/column_test.rb +40 -0
- data/test/unit_record/controller_test.rb +41 -0
- data/test/unit_record/disconnected_active_record_test.rb +48 -0
- data/test/unit_record/disconnected_fixtures_test.rb +7 -0
- data/test/unit_record/disconnected_test_case_test.rb +19 -0
- metadata +78 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
*HEAD
|
2
|
+
|
3
|
+
* Added UnitRecord::AssociationStubbing module to stub associations. Is not included by default.
|
4
|
+
* Give exception message if trying to use fixtures instead of failing silently.
|
5
|
+
|
6
|
+
*0.4.1* (December 10th, 2007)
|
7
|
+
|
8
|
+
* Stub caching for compatibility with ActionController::Caching::SqlCache in Rails 2.
|
9
|
+
|
10
|
+
*0.4.0* (December 9th, 2007)
|
11
|
+
|
12
|
+
* Rails 2.0 compatibility.
|
13
|
+
|
14
|
+
*0.3.0* (August 22nd, 2007)
|
15
|
+
|
16
|
+
* Works with models using non-conventional table names.
|
17
|
+
|
18
|
+
*0.2.0* (August 16th, 2007)
|
19
|
+
|
20
|
+
* Cache columns based on schema.rb
|
21
|
+
|
22
|
+
*0.1.0* (August 15th, 2007)
|
23
|
+
|
24
|
+
* Converted plugin into a gem.
|
25
|
+
* No longer depend on Rake task to dump and cache columns.
|
data/Rakefile
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
require 'rake/contrib/sshpublisher'
|
6
|
+
|
7
|
+
desc "Default: run tests"
|
8
|
+
task :default => :test
|
9
|
+
|
10
|
+
Rake::TestTask.new("test") do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Generate documentation"
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = "doc"
|
19
|
+
rdoc.title = "UnitRecord"
|
20
|
+
rdoc.options << '--line-numbers'
|
21
|
+
rdoc.rdoc_files.include('README.rdoc', 'CHANGELOG')
|
22
|
+
end
|
23
|
+
|
24
|
+
desc "Upload RDoc to RubyForge"
|
25
|
+
task :publish_rdoc => [:rdoc] do
|
26
|
+
Rake::SshDirPublisher.new("dcmanges@rubyforge.org", "/var/www/gforge-projects/unit-test-ar", "doc").upload
|
27
|
+
end
|
28
|
+
|
29
|
+
Gem.manage_gems
|
30
|
+
require "date"
|
31
|
+
|
32
|
+
gem_spec = Gem::Specification.new do |s|
|
33
|
+
s.name = "unit_record"
|
34
|
+
s.summary = "UnitRecord enables unit testing without hitting the database."
|
35
|
+
s.version = "0.4.1"
|
36
|
+
s.author = "Dan Manges"
|
37
|
+
s.description = "UnitRecord enables unit testing without hitting the database."
|
38
|
+
s.email = "daniel.manges@gmail.com"
|
39
|
+
s.homepage = "http://unit-test-ar.rubyforge.org"
|
40
|
+
s.rubyforge_project = "unit-test-ar"
|
41
|
+
|
42
|
+
s.has_rdoc = true
|
43
|
+
s.extra_rdoc_files = ['README.rdoc', 'CHANGELOG']
|
44
|
+
s.rdoc_options << '--title' << "UnitRecord" << '--main' << 'README.rdoc' << '--line-numbers'
|
45
|
+
|
46
|
+
s.autorequire = "unit_record"
|
47
|
+
s.files = FileList['{lib,test}/**/*.rb', 'CHANGELOG', 'README.rdoc', 'Rakefile'].to_a
|
48
|
+
end
|
49
|
+
|
50
|
+
Rake::GemPackageTask.new(gem_spec) do |package|
|
51
|
+
package.need_zip = false
|
52
|
+
package.need_tar = false
|
53
|
+
end
|
54
|
+
Rake::Task["gem"].prerequisites.unshift "test:multi"
|
55
|
+
|
56
|
+
namespace :gemspec do
|
57
|
+
desc "generates unit_record.gemspec"
|
58
|
+
task :generate do
|
59
|
+
File.open("unit_record.gemspec", "w") do |f|
|
60
|
+
f.puts "# this file is generated by rake gemspec:generate for github"
|
61
|
+
f.write gem_spec.to_ruby
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
task :readme do
|
67
|
+
require "rubygems"; gem "BlueCloth"; require "BlueCloth"; require 'tmpdir'
|
68
|
+
file = "#{Dir.tmpdir}/readme.html"
|
69
|
+
File.open(file, "w") { |f| f.write BlueCloth.new(File.read("README.markdown")).to_html }
|
70
|
+
sh "open #{file}"
|
71
|
+
end
|
72
|
+
|
73
|
+
RAILS_VERSIONS = %w[1.2.6 2.0.2 2.1.0 2.1.1]
|
74
|
+
|
75
|
+
namespace :test do
|
76
|
+
desc "test with multiple versions of rails"
|
77
|
+
task :multi do
|
78
|
+
RAILS_VERSIONS.each do |rails_version|
|
79
|
+
sh "RAILS_VERSION='#{rails_version}' rake test > /dev/null 2>&1"
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
desc "pre-commit task"
|
85
|
+
task :pc => %w[test:multi gemspec:generate]
|
@@ -0,0 +1,93 @@
|
|
1
|
+
class ActiveRecord::ConnectionAdapters::UnitRecordAdapter < ::ActiveRecord::ConnectionAdapters::AbstractAdapter
|
2
|
+
EXCEPTION_MESSAGE = "ActiveRecord is disconnected; database access is unavailable in unit tests."
|
3
|
+
|
4
|
+
def initialize(config = {})
|
5
|
+
super
|
6
|
+
@strategy = config[:strategy] || :raise
|
7
|
+
@cached_columns = {"schema_info" => []}
|
8
|
+
end
|
9
|
+
|
10
|
+
def columns(table_name, name = nil)#:nodoc:
|
11
|
+
@cached_columns[table_name.to_s] ||
|
12
|
+
raise("Columns are not cached for '#{table_name}' - check schema.rb")
|
13
|
+
end
|
14
|
+
|
15
|
+
def create_table(table_name, options={})
|
16
|
+
table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self)
|
17
|
+
table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false
|
18
|
+
yield table_definition
|
19
|
+
@cached_columns[table_name.to_s] =
|
20
|
+
table_definition.columns.map do |c|
|
21
|
+
ActiveRecord::ConnectionAdapters::Column.new(c.name.to_s, c.default, c.sql_type, c.null)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def native_database_types
|
26
|
+
# Copied from the MysqlAdapter so ColumnDefinition#sql_type will work
|
27
|
+
{
|
28
|
+
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
|
29
|
+
:string => { :name => "varchar", :limit => 255 },
|
30
|
+
:text => { :name => "text" },
|
31
|
+
:integer => { :name => "int", :limit => 11 },
|
32
|
+
:float => { :name => "float" },
|
33
|
+
:decimal => { :name => "decimal" },
|
34
|
+
:datetime => { :name => "datetime" },
|
35
|
+
:timestamp => { :name => "datetime" },
|
36
|
+
:time => { :name => "time" },
|
37
|
+
:date => { :name => "date" },
|
38
|
+
:binary => { :name => "blob" },
|
39
|
+
:boolean => { :name => "tinyint", :limit => 1 }
|
40
|
+
}
|
41
|
+
end
|
42
|
+
|
43
|
+
def change_strategy(new_strategy, &block)
|
44
|
+
unless [:noop, :raise].include?(new_strategy.to_sym)
|
45
|
+
raise ArgumentError, "#{new_strategy.inspect} is not a valid strategy - valid values are :noop and :raise"
|
46
|
+
end
|
47
|
+
begin
|
48
|
+
old_strategy = @strategy
|
49
|
+
@strategy = new_strategy.to_sym
|
50
|
+
yield
|
51
|
+
ensure
|
52
|
+
@strategy = old_strategy
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def execute(sql, name = nil)
|
57
|
+
raise_or_noop
|
58
|
+
end
|
59
|
+
|
60
|
+
def select_rows(sql, name = nil)
|
61
|
+
raise_or_noop []
|
62
|
+
end
|
63
|
+
|
64
|
+
def rename_table(table_name, new_name)
|
65
|
+
raise_or_noop
|
66
|
+
end
|
67
|
+
|
68
|
+
def change_column(table_name, column_name, type, options = {})
|
69
|
+
raise_or_noop
|
70
|
+
end
|
71
|
+
|
72
|
+
def change_column_default(table_name, column_name, default)
|
73
|
+
raise_or_noop
|
74
|
+
end
|
75
|
+
|
76
|
+
def rename_column(table_name, column_name, new_column_name)
|
77
|
+
raise_or_noop
|
78
|
+
end
|
79
|
+
|
80
|
+
def tables
|
81
|
+
@cached_columns.keys
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
|
86
|
+
def raise_or_noop(noop_return_value = nil)
|
87
|
+
@strategy == :raise ? raise(EXCEPTION_MESSAGE) : noop_return_value
|
88
|
+
end
|
89
|
+
|
90
|
+
def select(sql, name = nil)
|
91
|
+
raise_or_noop []
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module UnitRecord
|
2
|
+
module AssociationStubbing
|
3
|
+
|
4
|
+
private
|
5
|
+
|
6
|
+
def initialize_with_association_stubbing(attributes = {})
|
7
|
+
attributes ||= {}
|
8
|
+
associations = extract_associations attributes
|
9
|
+
initialize_without_association_stubbing attributes
|
10
|
+
stub_associations associations
|
11
|
+
end
|
12
|
+
|
13
|
+
protected
|
14
|
+
|
15
|
+
def extract_associations(attributes = {})
|
16
|
+
attributes.inject({}) do |associations,(attr,value)|
|
17
|
+
next associations unless self.class.reflections.keys.include? attr
|
18
|
+
associations[attr] = attributes.delete attr
|
19
|
+
associations
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def stub_associations(associations = {})
|
24
|
+
associations.each do |attr,value|
|
25
|
+
self.stubs(attr).returns(value)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.included(klass)
|
30
|
+
klass.class_eval do
|
31
|
+
unless (instance_methods + private_instance_methods).include?("initialize_without_association_stubbing")
|
32
|
+
alias_method_chain :initialize, :association_stubbing
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module UnitRecord
|
2
|
+
module ColumnExtension
|
3
|
+
def self.included(base)
|
4
|
+
base.class_eval do
|
5
|
+
alias_method_chain :simplified_type, :boolean
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def simplified_type_with_boolean(field_type)
|
10
|
+
return :boolean if field_type.to_s.downcase.index("tinyint(1)")
|
11
|
+
simplified_type_without_boolean field_type
|
12
|
+
end
|
13
|
+
|
14
|
+
def ==(other)
|
15
|
+
other && instance_variables.all? { |ivar| instance_variable_get(ivar) == other.instance_variable_get(ivar) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module UnitRecord
|
2
|
+
module DisconnectedActiveRecord
|
3
|
+
def disconnected?
|
4
|
+
false
|
5
|
+
end
|
6
|
+
|
7
|
+
def disconnect!(options = {})
|
8
|
+
return if disconnected?
|
9
|
+
establish_connection options.merge(:adapter => "unit_record")
|
10
|
+
(class << self; self; end).class_eval do
|
11
|
+
def disconnected?
|
12
|
+
true
|
13
|
+
end
|
14
|
+
end
|
15
|
+
Fixtures.disconnect!
|
16
|
+
Test::Unit::TestCase.disconnect!
|
17
|
+
ActiveRecord::Base.connection.change_strategy(:noop) do
|
18
|
+
load(RAILS_ROOT + "/db/schema.rb")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module UnitRecord
|
2
|
+
module DisconnectedTestCase
|
3
|
+
def disconnect!
|
4
|
+
self.use_transactional_fixtures = false
|
5
|
+
|
6
|
+
class_eval do
|
7
|
+
def self.fixtures(*args)
|
8
|
+
raise "Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests."
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/unit_record.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "unit_record/association_stubbing"
|
2
|
+
require "unit_record/column_extension"
|
3
|
+
require "unit_record/disconnected_active_record"
|
4
|
+
require "unit_record/disconnected_test_case"
|
5
|
+
require "unit_record/disconnected_fixtures"
|
6
|
+
require "active_record/connection_adapters/unit_record_adapter"
|
7
|
+
|
8
|
+
require "active_record/fixtures"
|
9
|
+
|
10
|
+
ActiveRecord::ConnectionAdapters::Column.send :include, UnitRecord::ColumnExtension
|
11
|
+
ActiveRecord::Base.extend UnitRecord::DisconnectedActiveRecord
|
12
|
+
Test::Unit::TestCase.extend UnitRecord::DisconnectedTestCase
|
13
|
+
Fixtures.extend UnitRecord::DisconnectedFixtures
|
14
|
+
|
15
|
+
ActiveRecord::Base.class_eval do
|
16
|
+
def self.unit_record_connection(config)
|
17
|
+
ActiveRecord::ConnectionAdapters::UnitRecordAdapter.new(config)
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../test_helper"
|
2
|
+
|
3
|
+
functional_tests do
|
4
|
+
|
5
|
+
test "find(:all)" do
|
6
|
+
ActiveRecord::Base.connection.change_strategy(:raise) do
|
7
|
+
assert_raises(RuntimeError) { Person.find(:all) }
|
8
|
+
end
|
9
|
+
ActiveRecord::Base.connection.change_strategy(:noop) do
|
10
|
+
assert_equal [], Person.find(:all)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
test "execute raises an exception" do
|
15
|
+
assert_raises_disconnected_exception do
|
16
|
+
ActiveRecord::Base.connection.execute "SELECT 1"
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
test "select_rows raises an exception" do
|
21
|
+
assert_raises_disconnected_exception do
|
22
|
+
ActiveRecord::Base.connection.select_rows "SELECT * FROM people"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
test "select raises an exception" do
|
27
|
+
assert_raises_disconnected_exception do
|
28
|
+
ActiveRecord::Base.connection.send :select, "SELECT * FROM people"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
test "rename_table raises an exception" do
|
33
|
+
assert_raises_disconnected_exception do
|
34
|
+
ActiveRecord::Base.connection.rename_table "people", "persons"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
test "change_column raises an exception" do
|
39
|
+
assert_raises_disconnected_exception do
|
40
|
+
ActiveRecord::Base.connection.change_column "people", "first_name", :string, :null => false
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
test "change_column_default raises an exception" do
|
45
|
+
assert_raises_disconnected_exception do
|
46
|
+
ActiveRecord::Base.connection.change_column_default "people", "first_person", "george"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
test "rename_column raises an exception" do
|
51
|
+
assert_raises_disconnected_exception do
|
52
|
+
ActiveRecord::Base.connection.rename_column "people", "first_name", "name_first"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
test "initialize can set strategy" do
|
57
|
+
ActiveRecord::Base.establish_connection :adapter => "unit_record", :strategy => :noop
|
58
|
+
assert_nil ActiveRecord::Base.connection.execute("SELECT 1")
|
59
|
+
ActiveRecord::Base.establish_connection :adapter => "unit_record", :strategy => :raise
|
60
|
+
assert_raises(RuntimeError) { ActiveRecord::Base.connection.execute("SELECT 1") }
|
61
|
+
end
|
62
|
+
|
63
|
+
test "noop" do
|
64
|
+
ActiveRecord::Base.connection.change_strategy(:noop) do
|
65
|
+
assert_nil ActiveRecord::Base.connection.execute("SELECT 1")
|
66
|
+
assert_equal [], ActiveRecord::Base.connection.select_rows("SELECT * FROM people")
|
67
|
+
assert_equal [], ActiveRecord::Base.connection.send(:select, "SELECT * FROM people")
|
68
|
+
assert_nil ActiveRecord::Base.connection.rename_table("people", "persons")
|
69
|
+
assert_nil ActiveRecord::Base.connection.change_column("people", "first_name", :string, :null => false)
|
70
|
+
assert_nil ActiveRecord::Base.connection.change_column_default("people", "first_person", "george")
|
71
|
+
assert_nil ActiveRecord::Base.connection.rename_column("people", "first_name", "name_first")
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
test "change_strategy raises if invalid strategy" do
|
76
|
+
assert_nothing_raised { ActiveRecord::Base.connection.change_strategy(:noop) {} }
|
77
|
+
assert_nothing_raised { ActiveRecord::Base.connection.change_strategy(:raise) {} }
|
78
|
+
assert_raises(ArgumentError) { ActiveRecord::Base.connection.change_strategy(:bogus) {} }
|
79
|
+
end
|
80
|
+
|
81
|
+
def assert_raises_disconnected_exception(&block)
|
82
|
+
exception = nil
|
83
|
+
begin
|
84
|
+
yield
|
85
|
+
rescue Exception => exception
|
86
|
+
end
|
87
|
+
assert_not_nil exception
|
88
|
+
assert_equal "ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message
|
89
|
+
end
|
90
|
+
end
|
data/test/db/schema.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :preferences, :force => true do |t|
|
3
|
+
t.column :some_count, :integer
|
4
|
+
t.column :show_help, :boolean, :default => true
|
5
|
+
end
|
6
|
+
|
7
|
+
create_table :people, :force => true do |t|
|
8
|
+
t.column :first_name, :string
|
9
|
+
t.column :last_name, :string
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table "profiles", :force => true do |t|
|
13
|
+
t.column "description", :string
|
14
|
+
t.column "person_id", :integer
|
15
|
+
end
|
16
|
+
|
17
|
+
create_table :pets, :force => true do |t|
|
18
|
+
t.column :name, :string
|
19
|
+
end
|
20
|
+
|
21
|
+
create_table :foofoo, :force => true do |t|
|
22
|
+
t.column :bar, :string
|
23
|
+
end
|
24
|
+
|
25
|
+
add_index "people", ["first_name"]
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
unless defined?(TEST_HELPER_LOADED)
|
2
|
+
TEST_HELPER_LOADED = true
|
3
|
+
$:.unshift(File.dirname(__FILE__) + '/../lib')
|
4
|
+
RAILS_ROOT = File.dirname(__FILE__)
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'test/unit'
|
8
|
+
|
9
|
+
if rails_version = ENV['RAILS_VERSION']
|
10
|
+
gem "rails", rails_version
|
11
|
+
end
|
12
|
+
require "rails/version"
|
13
|
+
puts "==== Testing with Rails #{Rails::VERSION::STRING} ===="
|
14
|
+
require 'active_record'
|
15
|
+
require 'active_record/fixtures'
|
16
|
+
require "action_controller"
|
17
|
+
if Rails::VERSION::MAJOR == 2
|
18
|
+
require "action_controller/test_case"
|
19
|
+
end
|
20
|
+
require "action_controller/test_process"
|
21
|
+
|
22
|
+
begin
|
23
|
+
require 'mocha'
|
24
|
+
require 'dust'
|
25
|
+
rescue LoadError
|
26
|
+
raise "Need Mocha and Dust gems to Test"
|
27
|
+
end
|
28
|
+
Test::Unit::TestCase.disallow_setup!
|
29
|
+
|
30
|
+
require "#{File.dirname(__FILE__)}/../init"
|
31
|
+
|
32
|
+
Test::Unit::TestCase.use_transactional_fixtures = true
|
33
|
+
|
34
|
+
# Needed because of this line in setup_with_fixtures and teardown_with_fixtures:
|
35
|
+
# return unless defined?(ActiveRecord::Base) && !ActiveRecord::Base.configurations.blank?
|
36
|
+
ActiveRecord::Base.configurations = {"irrelevant" => {:adapter => "stub"}}
|
37
|
+
|
38
|
+
ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing
|
39
|
+
|
40
|
+
class Preference < ActiveRecord::Base
|
41
|
+
end
|
42
|
+
|
43
|
+
class Person < ActiveRecord::Base
|
44
|
+
has_many :pets
|
45
|
+
has_one :profile
|
46
|
+
end
|
47
|
+
|
48
|
+
class Profile < ActiveRecord::Base
|
49
|
+
belongs_to :person
|
50
|
+
end
|
51
|
+
|
52
|
+
class Pet < ActiveRecord::Base
|
53
|
+
belongs_to :person
|
54
|
+
end
|
55
|
+
|
56
|
+
class Foo < ActiveRecord::Base
|
57
|
+
set_table_name :foofoo
|
58
|
+
end
|
59
|
+
|
60
|
+
class DoesNotExist < ActiveRecord::Base
|
61
|
+
set_table_name "table_does_not_exist"
|
62
|
+
end
|
63
|
+
|
64
|
+
ActiveRecord::Base.disconnect! :strategy => :raise
|
65
|
+
# make sure calling disconnect multiple times does not cause problems
|
66
|
+
ActiveRecord::Base.disconnect!
|
67
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
functional_tests do
|
4
|
+
|
5
|
+
test "stubbing a has_many" do
|
6
|
+
pets = [stub, stub]
|
7
|
+
person = Person.new :pets => pets
|
8
|
+
assert_equal pets, person.pets
|
9
|
+
end
|
10
|
+
|
11
|
+
test "stubbing a belongs_to" do
|
12
|
+
person = stub
|
13
|
+
pet = Pet.new :person => person
|
14
|
+
assert_equal person, pet.person
|
15
|
+
end
|
16
|
+
|
17
|
+
test "multiple includes doesn't hurt" do
|
18
|
+
ActiveRecord::Base.send :include, UnitRecord::AssociationStubbing
|
19
|
+
Person.new
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
functional_tests do
|
4
|
+
test "caching columns with no defaults or not nulls" do
|
5
|
+
expected = [
|
6
|
+
ActiveRecord::ConnectionAdapters::Column.new("id", nil, "int(11) DEFAULT NULL auto_increment PRIMARY KEY", nil),
|
7
|
+
ActiveRecord::ConnectionAdapters::Column.new("first_name", nil, "varchar(255)", nil),
|
8
|
+
ActiveRecord::ConnectionAdapters::Column.new("last_name", nil, "varchar(255)", nil)
|
9
|
+
]
|
10
|
+
expected[0].primary = true
|
11
|
+
expected[1..-1].each { |column| column.primary = false }
|
12
|
+
assert_equal expected, Person.columns
|
13
|
+
end
|
14
|
+
|
15
|
+
test "caching column with default" do
|
16
|
+
expected = ActiveRecord::ConnectionAdapters::Column.new("show_help", true, "tinyint(1)", nil)
|
17
|
+
expected.primary = false
|
18
|
+
assert_equal expected, Preference.columns.detect { |c| c.name == "show_help" }
|
19
|
+
end
|
20
|
+
|
21
|
+
test "boolean columns" do
|
22
|
+
expected = ActiveRecord::ConnectionAdapters::Column.new("show_help", true, "tinyint(1)", nil)
|
23
|
+
expected.primary = false
|
24
|
+
assert_equal :boolean, Preference.columns.detect { |c| c.name == "show_help" }.type
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
unit_tests do
|
4
|
+
test "equality" do
|
5
|
+
column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil)
|
6
|
+
column2 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil)
|
7
|
+
assert_equal column1, column2
|
8
|
+
end
|
9
|
+
|
10
|
+
test "non-equality on name" do
|
11
|
+
column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil)
|
12
|
+
column2 = ActiveRecord::ConnectionAdapters::Column.new("different name", nil, :string, nil)
|
13
|
+
assert column1 != column2
|
14
|
+
end
|
15
|
+
|
16
|
+
test "non-equality on sql_type" do
|
17
|
+
column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil)
|
18
|
+
column2 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :text, nil)
|
19
|
+
assert column1 != column2
|
20
|
+
end
|
21
|
+
|
22
|
+
test "non-equality on default" do
|
23
|
+
column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil)
|
24
|
+
column2 = ActiveRecord::ConnectionAdapters::Column.new("name", "Dan", :string, nil)
|
25
|
+
assert column1 != column2
|
26
|
+
end
|
27
|
+
|
28
|
+
test "non-equality on null" do
|
29
|
+
column1 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, nil)
|
30
|
+
column2 = ActiveRecord::ConnectionAdapters::Column.new("name", nil, :string, true)
|
31
|
+
assert column1 != column2
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
functional_tests do
|
4
|
+
test "instantiating" do
|
5
|
+
person = Person.new :first_name => "Dan", :last_name => "Manges"
|
6
|
+
assert_equal "Dan", person.first_name
|
7
|
+
assert_equal "Manges", person.last_name
|
8
|
+
end
|
9
|
+
|
10
|
+
test "using model with column with a default" do
|
11
|
+
record = Preference.new
|
12
|
+
assert_equal true, record.show_help?
|
13
|
+
end
|
14
|
+
|
15
|
+
test "typecasting happens for integer attributes" do
|
16
|
+
record = Preference.new
|
17
|
+
record.some_count = "42"
|
18
|
+
assert_equal 42, record.some_count
|
19
|
+
end
|
20
|
+
|
21
|
+
test "a model with a non-convential table name does not blow up" do
|
22
|
+
assert_nothing_raised { Foo.columns }
|
23
|
+
end
|
24
|
+
|
25
|
+
test "using attribute on a model with a non-conventional table name" do
|
26
|
+
foo = Foo.new
|
27
|
+
foo.bar = "baz"
|
28
|
+
assert_equal "baz", foo.bar
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should get a descriptive error message if no cached columns" do
|
32
|
+
exception = nil
|
33
|
+
begin
|
34
|
+
DoesNotExist.columns
|
35
|
+
rescue => exception
|
36
|
+
end
|
37
|
+
assert_not_nil exception
|
38
|
+
assert_equal "Columns are not cached for 'table_does_not_exist' - check schema.rb", exception.message
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class SampleController < ActionController::Base
|
4
|
+
def sample_action
|
5
|
+
render :text => "OK"
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
ActionController::Routing::Routes.add_route "/sample/sample_action", :controller => "sample", :action => "sample_action"
|
10
|
+
|
11
|
+
if defined?(ActionController::TestCase) # Rails 2
|
12
|
+
|
13
|
+
class ControllerTest < ActionController::TestCase
|
14
|
+
tests SampleController
|
15
|
+
|
16
|
+
test "render" do
|
17
|
+
get :sample_action
|
18
|
+
assert_equal "OK", @response.body
|
19
|
+
end
|
20
|
+
|
21
|
+
test "sql caching is enabled" do
|
22
|
+
assert_equal true, (SampleController < ActionController::Caching::SqlCache)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
else # Rails 1.x
|
27
|
+
|
28
|
+
class ControllerTest < Test::Unit::TestCase
|
29
|
+
def setup
|
30
|
+
@controller = SampleController.new
|
31
|
+
@request = ActionController::TestRequest.new
|
32
|
+
@response = ActionController::TestResponse.new
|
33
|
+
end
|
34
|
+
|
35
|
+
test "render" do
|
36
|
+
get :sample_action
|
37
|
+
assert_equal "OK", @response.body
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
functional_tests do
|
4
|
+
test "find_by_sql gives disconnected exception message" do
|
5
|
+
exception = nil
|
6
|
+
begin
|
7
|
+
Person.find_by_sql "SELECT * FROM people"
|
8
|
+
rescue => exception
|
9
|
+
end
|
10
|
+
assert_not_nil exception
|
11
|
+
assert_equal "ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message
|
12
|
+
end
|
13
|
+
|
14
|
+
test "connected? is true" do
|
15
|
+
assert_equal true, ActiveRecord::Base.connected?
|
16
|
+
end
|
17
|
+
|
18
|
+
test "disconnected? is true" do
|
19
|
+
assert_equal true, ActiveRecord::Base.disconnected?
|
20
|
+
end
|
21
|
+
|
22
|
+
test "inspect does not blow up" do
|
23
|
+
assert_nothing_raised { Person.inspect }
|
24
|
+
end
|
25
|
+
|
26
|
+
test "table_exists?" do
|
27
|
+
assert_equal true, Person.table_exists?
|
28
|
+
if ActiveRecord::Base.connection.respond_to?(:table_exists?)
|
29
|
+
assert_equal false, ActiveRecord::Base.connection.table_exists?("bogus_table")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
test "setting a has_one association" do
|
34
|
+
person = Person.new
|
35
|
+
person.profile = Profile.new
|
36
|
+
end
|
37
|
+
|
38
|
+
test "boolean columns do type casting" do
|
39
|
+
pref = Preference.new
|
40
|
+
pref.show_help = "0"
|
41
|
+
assert_equal false, pref.send(:read_attribute, :show_help)
|
42
|
+
assert_equal false, pref.show_help
|
43
|
+
assert_equal false, pref.show_help?
|
44
|
+
pref.show_help = "1"
|
45
|
+
assert_equal true, pref.show_help
|
46
|
+
assert_equal true, pref.show_help?
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../test_helper"
|
2
|
+
|
3
|
+
functional_tests do
|
4
|
+
test "use_transactional_fixtures is false" do
|
5
|
+
assert_equal false, Test::Unit::TestCase.use_transactional_fixtures
|
6
|
+
end
|
7
|
+
|
8
|
+
test "trying to use fixtures gives useful message" do
|
9
|
+
exception = nil
|
10
|
+
begin
|
11
|
+
Class.new(Test::Unit::TestCase) do
|
12
|
+
fixtures :users
|
13
|
+
end
|
14
|
+
rescue => exception
|
15
|
+
end
|
16
|
+
assert_not_nil exception
|
17
|
+
assert_equal "Fixtures cannot be used with UnitRecord. ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dan-manges-unit_record
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dan Manges
|
8
|
+
autorequire: unit_record
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-10 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: UnitRecord enables unit testing without hitting the database.
|
17
|
+
email: daniel.manges@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- CHANGELOG
|
25
|
+
files:
|
26
|
+
- lib/active_record/connection_adapters/unit_record_adapter.rb
|
27
|
+
- lib/unit_record/association_stubbing.rb
|
28
|
+
- lib/unit_record/column_extension.rb
|
29
|
+
- lib/unit_record/disconnected_active_record.rb
|
30
|
+
- lib/unit_record/disconnected_fixtures.rb
|
31
|
+
- lib/unit_record/disconnected_test_case.rb
|
32
|
+
- lib/unit_record.rb
|
33
|
+
- test/active_record/connection_adapters/unit_record_adapter_test.rb
|
34
|
+
- test/db/schema.rb
|
35
|
+
- test/test_helper.rb
|
36
|
+
- test/unit_record/association_stubbing_test.rb
|
37
|
+
- test/unit_record/column_cacher_test.rb
|
38
|
+
- test/unit_record/column_extension_test.rb
|
39
|
+
- test/unit_record/column_test.rb
|
40
|
+
- test/unit_record/controller_test.rb
|
41
|
+
- test/unit_record/disconnected_active_record_test.rb
|
42
|
+
- test/unit_record/disconnected_fixtures_test.rb
|
43
|
+
- test/unit_record/disconnected_test_case_test.rb
|
44
|
+
- CHANGELOG
|
45
|
+
- README.rdoc
|
46
|
+
- Rakefile
|
47
|
+
has_rdoc: true
|
48
|
+
homepage: http://unit-test-ar.rubyforge.org
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --title
|
52
|
+
- UnitRecord
|
53
|
+
- --main
|
54
|
+
- README.rdoc
|
55
|
+
- --line-numbers
|
56
|
+
require_paths:
|
57
|
+
- lib
|
58
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: "0"
|
63
|
+
version:
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: "0"
|
69
|
+
version:
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: unit-test-ar
|
73
|
+
rubygems_version: 1.2.0
|
74
|
+
signing_key:
|
75
|
+
specification_version: 2
|
76
|
+
summary: UnitRecord enables unit testing without hitting the database.
|
77
|
+
test_files: []
|
78
|
+
|