unit_record 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,12 @@
1
+ *0.3.0* (August 22nd, 2007)
2
+
3
+ * Works with models using non-conventional table names.
4
+
5
+ *0.2.0* (August 16th, 2007)
6
+
7
+ * Cache columns based on schema.rb
8
+
9
+ *0.1.0* (August 15th, 2007)
10
+
11
+ * Converted plugin into a gem.
12
+ * No longer depend on Rake task to dump and cache columns.
data/README CHANGED
@@ -23,7 +23,7 @@ One huge benefit to disconnecting unit tests from the database is having a faste
23
23
 
24
24
  === Restructuring the Rails Test Directory
25
25
 
26
- 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. You should restructure your test directory like this:
26
+ 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:
27
27
  test
28
28
  test_helper.rb
29
29
  unit
@@ -37,7 +37,7 @@ The Rails test directory typically places testing for models under <tt>test/unit
37
37
 
38
38
  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.
39
39
 
40
- The <tt>functional_test_helper.rb</tt> file should require <tt>test_helper.rb</tt> for now:
40
+ The <tt>functional_test_helper.rb</tt> file only needs to require <tt>test_helper.rb</tt>:
41
41
  require File.dirname(__FILE__) + "/../test_helper"
42
42
 
43
43
  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.
@@ -49,11 +49,7 @@ In the <tt>test/unit/unit_test_helper.rb</tt> file you created when restructurin
49
49
  require "unit_record"
50
50
  ActiveRecord::Base.disconnect!
51
51
 
52
- The <tt>disconnect!</tt> method will do everything necessary to run your unit tests without hitting the database. For now, it connects to the database and caches the columns in a file (db/columns.rb) before disconnecting. A later version will not need to connect at all.
53
-
54
- == Limitations
55
-
56
- * UnitRecord currently only works when all models use convention for table names.
52
+ The <tt>disconnect!</tt> method will do everything necessary to run your unit tests without hitting the database.
57
53
 
58
54
  == Thanks
59
55
  Thanks to Jay Fields for the original implementation:
data/Rakefile CHANGED
@@ -27,7 +27,7 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
27
27
  rdoc.rdoc_dir = "doc"
28
28
  rdoc.title = "UnitRecord"
29
29
  rdoc.options << '--line-numbers'
30
- rdoc.rdoc_files.include('README')
30
+ rdoc.rdoc_files.include('README', 'CHANGELOG')
31
31
  end
32
32
 
33
33
  desc "Upload RDoc to RubyForge"
@@ -40,7 +40,7 @@ Gem::manage_gems
40
40
  specification = Gem::Specification.new do |s|
41
41
  s.name = "unit_record"
42
42
  s.summary = "UnitRecord enables unit testing without hitting the database."
43
- s.version = "0.2.0"
43
+ s.version = "0.3.0"
44
44
  s.author = "Dan Manges"
45
45
  s.description = "UnitRecord enables unit testing without hitting the database."
46
46
  s.email = "daniel.manges@gmail.com"
@@ -48,11 +48,11 @@ specification = Gem::Specification.new do |s|
48
48
  s.rubyforge_project = "unit-test-ar"
49
49
 
50
50
  s.has_rdoc = true
51
- s.extra_rdoc_files = ['README']
51
+ s.extra_rdoc_files = ['README', 'CHANGELOG']
52
52
  s.rdoc_options << '--title' << "UnitRecord" << '--main' << 'README' << '--line-numbers'
53
53
 
54
54
  s.autorequire = "unit_record"
55
- s.files = FileList['{lib,test}/**/*.rb', '^[A-Z]+$', 'Rakefile', 'init.rb'].to_a
55
+ s.files = FileList['{lib,test}/**/*.rb', 'CHANGELOG', 'README', 'Rakefile'].to_a
56
56
  end
57
57
 
58
58
  Rake::GemPackageTask.new(specification) do |package|
@@ -2,13 +2,13 @@ module UnitRecord
2
2
  class ColumnCacher
3
3
  include ActiveRecord::ConnectionAdapters::SchemaStatements
4
4
 
5
- def self.cache(file)
5
+ def self.cache(schema_file)
6
6
  (class << ActiveRecord::Schema; self; end).class_eval do
7
7
  def define(options={}, &block)
8
8
  UnitRecord::ColumnCacher.new.instance_eval(&block)
9
9
  end
10
10
  end
11
- load file
11
+ load schema_file
12
12
  end
13
13
 
14
14
  def add_index(table_name, column_name, options = {})
@@ -18,15 +18,10 @@ module UnitRecord
18
18
  table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(self)
19
19
  table_definition.primary_key(options[:primary_key] || "id") unless options[:id] == false
20
20
  yield table_definition
21
- if model = model_for_table(table_name)
22
- (class << model; self; end).class_eval do
23
- define_method(:columns) do
24
- table_definition.columns.map do |c|
25
- ActiveRecord::ConnectionAdapters::Column.new(c.name.to_s, c.default, c.sql_type, c.null)
26
- end
27
- end
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)
28
24
  end
29
- end
30
25
  end
31
26
 
32
27
  def native_database_types
@@ -46,9 +41,5 @@ module UnitRecord
46
41
  :boolean => { :name => "tinyint", :limit => 1 }
47
42
  }
48
43
  end
49
-
50
- def model_for_table(table)
51
- table.to_s.classify.constantize rescue nil
52
- end
53
44
  end
54
45
  end
@@ -6,6 +6,13 @@ module UnitRecord
6
6
  def disconnect!
7
7
  return if disconnected?
8
8
  (class << self; self; end).class_eval do
9
+ def cached_columns
10
+ @@_cached_columns ||= {}
11
+ end
12
+ def columns
13
+ cached_columns[table_name] ||
14
+ raise("Columns are not cached for '#{table_name}' - check schema.rb")
15
+ end
9
16
  def connection
10
17
  raise "ActiveRecord is disconnected; database access is unavailable in unit tests."
11
18
  end
@@ -18,8 +25,6 @@ module UnitRecord
18
25
  end
19
26
  Fixtures.disconnect!
20
27
  Test::Unit::TestCase.disconnect!
21
- old_columns_file = File.join(RAILS_ROOT, 'db', 'columns.rb')
22
- File.delete old_columns_file if File.exists?(old_columns_file)
23
28
  ColumnCacher.cache(RAILS_ROOT + "/db/schema.rb")
24
29
  end
25
30
  end
data/test/db/schema.rb CHANGED
@@ -9,5 +9,9 @@ ActiveRecord::Schema.define(:version => 0) do
9
9
  t.column :last_name, :string
10
10
  end
11
11
 
12
+ create_table :foofoo, :force => true do |t|
13
+ t.column :bar, :string
14
+ end
15
+
12
16
  add_index "people", ["first_name"]
13
17
  end
@@ -18,6 +18,23 @@ functional_tests do
18
18
  assert_equal 42, record.some_count
19
19
  end
20
20
 
21
- test "" do
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
22
39
  end
23
40
  end
data/test/test_helper.rb CHANGED
@@ -23,3 +23,9 @@ class Preference < ActiveRecord::Base
23
23
  end
24
24
  class Person < ActiveRecord::Base
25
25
  end
26
+ class Foo < ActiveRecord::Base
27
+ set_table_name :foofoo
28
+ end
29
+ class DoesNotExist < ActiveRecord::Base
30
+ set_table_name "table_does_not_exist"
31
+ end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.3
3
3
  specification_version: 1
4
4
  name: unit_record
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2007-08-16 00:00:00 -04:00
6
+ version: 0.3.0
7
+ date: 2007-08-22 00:00:00 -04:00
8
8
  summary: UnitRecord enables unit testing without hitting the database.
9
9
  require_paths:
10
10
  - lib
@@ -44,9 +44,9 @@ files:
44
44
  - test/functional/disconnected_test_case_test.rb
45
45
  - test/functional/functional_test_helper.rb
46
46
  - test/unit/column_extension_test.rb
47
- - Rakefile
48
- - init.rb
47
+ - CHANGELOG
49
48
  - README
49
+ - Rakefile
50
50
  test_files: []
51
51
 
52
52
  rdoc_options:
@@ -57,6 +57,7 @@ rdoc_options:
57
57
  - --line-numbers
58
58
  extra_rdoc_files:
59
59
  - README
60
+ - CHANGELOG
60
61
  executables: []
61
62
 
62
63
  extensions: []
data/init.rb DELETED
@@ -1 +0,0 @@
1
- require "unit_record"