unit_record 0.4.1 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -0
- data/LICENSE +20 -0
- data/README.markdown +137 -0
- data/Rakefile +63 -56
- data/lib/active_record/connection_adapters/unit_record_adapter.rb +97 -0
- data/lib/unit_record.rb +8 -1
- data/lib/unit_record/association_stubbing.rb +39 -0
- data/lib/unit_record/column_extension.rb +11 -0
- data/lib/unit_record/disconnected_active_record.rb +10 -32
- data/lib/unit_record/disconnected_test_case.rb +6 -0
- data/test/active_record/connection_adapters/unit_record_adapter_test.rb +103 -0
- data/test/db/schema.rb +9 -0
- data/test/sample_spec.rb +40 -0
- data/test/test_helper.rb +28 -4
- data/test/unit_record/association_stubbing_test.rb +37 -0
- data/test/unit_record/column_cacher_test.rb +26 -0
- data/test/{unit → unit_record}/column_extension_test.rb +1 -1
- data/test/{functional → unit_record}/column_test.rb +1 -1
- data/test/{functional → unit_record}/controller_test.rb +1 -1
- data/test/unit_record/disconnected_active_record_test.rb +52 -0
- data/test/{functional → unit_record}/disconnected_fixtures_test.rb +1 -1
- data/test/unit_record/disconnected_test_case_test.rb +19 -0
- data/vendor/dust-0.1.6/lib/array_extension.rb +5 -0
- data/vendor/dust-0.1.6/lib/definition_error.rb +20 -0
- data/vendor/dust-0.1.6/lib/dust.rb +8 -0
- data/vendor/dust-0.1.6/lib/nil_extension.rb +5 -0
- data/vendor/dust-0.1.6/lib/object_extension.rb +62 -0
- data/vendor/dust-0.1.6/lib/string_extension.rb +5 -0
- data/vendor/dust-0.1.6/lib/symbol_extension.rb +5 -0
- data/vendor/dust-0.1.6/lib/test_case_extension.rb +76 -0
- data/vendor/dust-0.1.6/rakefile.rb +50 -0
- data/vendor/dust-0.1.6/test/all_tests.rb +1 -0
- data/vendor/dust-0.1.6/test/failing_with_helper_unit_test.rb +16 -0
- data/vendor/dust-0.1.6/test/failing_with_setup_unit_test.rb +16 -0
- data/vendor/dust-0.1.6/test/functional_test.rb +12 -0
- data/vendor/dust-0.1.6/test/passing_unit_test.rb +11 -0
- data/vendor/dust-0.1.6/test/passing_with_helper_unit_test.rb +10 -0
- data/vendor/dust-0.1.6/test/passing_with_helpers_unit_test.rb +13 -0
- data/vendor/dust-0.1.6/test/passing_with_setup_unit_test.rb +10 -0
- data/vendor/dust-0.1.6/test/test_helper.rb +1 -0
- metadata +75 -52
- data/README +0 -60
- data/lib/unit_record/column_cacher.rb +0 -45
- data/test/functional/column_cacher_test.rb +0 -19
- data/test/functional/disconnected_active_record_test.rb +0 -33
- data/test/functional/disconnected_test_case_test.rb +0 -7
- data/test/functional/functional_test_helper.rb +0 -4
- data/test/unit/unit_test_helper.rb +0 -5
data/README
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
= UnitRecord
|
2
|
-
|
3
|
-
This plugin provides unit testing for ActiveRecord by disconnecting tests from the database.
|
4
|
-
|
5
|
-
== The Benefits
|
6
|
-
|
7
|
-
Rationale: http://www.dcmanges.com/blog/rails-unit-record-test-without-the-database
|
8
|
-
|
9
|
-
One huge benefit to disconnecting unit tests from the database is having a faster test suite. Here is the benchmark from one of my current projects:
|
10
|
-
|
11
|
-
Finished in 11.541093 seconds.
|
12
|
-
3001 tests, 5325 assertions, 0 failures, 0 errors
|
13
|
-
|
14
|
-
== Installation
|
15
|
-
|
16
|
-
gem install unit_record
|
17
|
-
|
18
|
-
== Usage
|
19
|
-
|
20
|
-
=== Restructuring the Rails Test Directory
|
21
|
-
|
22
|
-
The Rails test directory typically places testing for models under <tt>test/unit</tt> and tests for controllers under <tt>test/functional</tt>. However, we need to change the definition of unit and functional. Controllers can be unit tested (mocking out models and not rendering the view). Models can be functionally tested (hitting the database). Also, each type of test needs its own test_helper. I recommend restructuring your test directory like this:
|
23
|
-
test
|
24
|
-
test_helper.rb
|
25
|
-
unit
|
26
|
-
unit_test_helper.rb
|
27
|
-
controllers
|
28
|
-
models
|
29
|
-
functional
|
30
|
-
functional_test_helper.rb
|
31
|
-
controllers
|
32
|
-
models
|
33
|
-
|
34
|
-
You should move existing functional tests into functional/controllers. You will also need to change the require line at the top of those tests to require the functional_test_helper.rb file instead of the test_helper.rb file.
|
35
|
-
|
36
|
-
The <tt>functional_test_helper.rb</tt> file only needs to require <tt>test_helper.rb</tt>:
|
37
|
-
require File.dirname(__FILE__) + "/../test_helper"
|
38
|
-
|
39
|
-
For moving unit tests, you have a few options. I recommend moving them to unit/models and then disconnecting your unit tests from the database. Any tests that fail should then be modified to not hit the database or moved to functional/models.
|
40
|
-
|
41
|
-
== Disconnecting
|
42
|
-
|
43
|
-
In the <tt>test/unit/unit_test_helper.rb</tt> file you created when restructuring your test directory, you should add these lines:
|
44
|
-
require File.dirname(__FILE__) + "/../test_helper"
|
45
|
-
require "unit_record"
|
46
|
-
ActiveRecord::Base.disconnect!
|
47
|
-
|
48
|
-
The <tt>disconnect!</tt> method will do everything necessary to run your unit tests without hitting the database.
|
49
|
-
|
50
|
-
== Thanks
|
51
|
-
Thanks to Jay Fields for the original implementation:
|
52
|
-
http://blog.jayfields.com/2007/03/rails-activerecord-unit-testing-part-ii.html
|
53
|
-
|
54
|
-
== Contributors
|
55
|
-
|
56
|
-
* Rob Sanheim
|
57
|
-
|
58
|
-
== License
|
59
|
-
Released under Ruby's license.
|
60
|
-
http://www.ruby-lang.org/en/LICENSE.txt
|
@@ -1,45 +0,0 @@
|
|
1
|
-
module UnitRecord
|
2
|
-
class ColumnCacher
|
3
|
-
include ActiveRecord::ConnectionAdapters::SchemaStatements
|
4
|
-
|
5
|
-
def self.cache(schema_file)
|
6
|
-
(class << ActiveRecord::Schema; self; end).class_eval do
|
7
|
-
def define(options={}, &block)
|
8
|
-
UnitRecord::ColumnCacher.new.instance_eval(&block)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
load schema_file
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_index(table_name, column_name, options = {})
|
15
|
-
end
|
16
|
-
|
17
|
-
def create_table(table_name, options={})
|
18
|
-
table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self)
|
19
|
-
table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false
|
20
|
-
yield table_definition
|
21
|
-
ActiveRecord::Base.cached_columns[table_name.to_s] =
|
22
|
-
table_definition.columns.map do |c|
|
23
|
-
ActiveRecord::ConnectionAdapters::Column.new(c.name.to_s, c.default, c.sql_type, c.null)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def native_database_types
|
28
|
-
# Copied from the MysqlAdapter so ColumnDefinition#sql_type will work
|
29
|
-
{
|
30
|
-
:primary_key => "int(11) DEFAULT NULL auto_increment PRIMARY KEY",
|
31
|
-
:string => { :name => "varchar", :limit => 255 },
|
32
|
-
:text => { :name => "text" },
|
33
|
-
:integer => { :name => "int", :limit => 11 },
|
34
|
-
:float => { :name => "float" },
|
35
|
-
:decimal => { :name => "decimal" },
|
36
|
-
:datetime => { :name => "datetime" },
|
37
|
-
:timestamp => { :name => "datetime" },
|
38
|
-
:time => { :name => "time" },
|
39
|
-
:date => { :name => "date" },
|
40
|
-
:binary => { :name => "blob" },
|
41
|
-
:boolean => { :name => "tinyint", :limit => 1 }
|
42
|
-
}
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/functional_test_helper"
|
2
|
-
|
3
|
-
functional_tests do
|
4
|
-
test "caching columns with no defaults or not nulls" do
|
5
|
-
assert_equal [
|
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
|
-
], Person.columns
|
10
|
-
end
|
11
|
-
|
12
|
-
test "caching column with default" do
|
13
|
-
assert_equal ActiveRecord::ConnectionAdapters::Column.new("show_help", true, "tinyint(1)", nil), Preference.columns.detect { |c| c.name == "show_help" }
|
14
|
-
end
|
15
|
-
|
16
|
-
test "add_index does not blow up" do
|
17
|
-
assert_nothing_raised { UnitRecord::ColumnCacher.new.add_index("people", "first_name") }
|
18
|
-
end
|
19
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__) + "/functional_test_helper"
|
2
|
-
|
3
|
-
functional_tests do
|
4
|
-
test "accessing connection raises" do
|
5
|
-
assert_raises(RuntimeError) { ActiveRecord::Base.connection }
|
6
|
-
end
|
7
|
-
|
8
|
-
test "accessing connection gives exception message with class name" do
|
9
|
-
exception = nil
|
10
|
-
begin
|
11
|
-
Person.connection
|
12
|
-
rescue => exception
|
13
|
-
end
|
14
|
-
assert_not_nil exception
|
15
|
-
assert_equal "(from Person): ActiveRecord is disconnected; database access is unavailable in unit tests.", exception.message
|
16
|
-
end
|
17
|
-
|
18
|
-
test "connected? is false" do
|
19
|
-
assert_equal false, ActiveRecord::Base.connected?
|
20
|
-
end
|
21
|
-
|
22
|
-
test "disconnected? is true" do
|
23
|
-
assert_equal true, ActiveRecord::Base.disconnected?
|
24
|
-
end
|
25
|
-
|
26
|
-
test "inspect does not blow up" do
|
27
|
-
assert_nothing_raised { Person.inspect }
|
28
|
-
end
|
29
|
-
|
30
|
-
test "table_exists?" do
|
31
|
-
assert_equal true, Person.table_exists?
|
32
|
-
end
|
33
|
-
end
|