Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

importing and adapting my database-driven test setup

  • Loading branch information...
commit e5d7bb41446f3beb1266e1ed6d0589117e7c0ec2 1 parent e1a484f
@cainlevy authored
View
2  init.rb
@@ -1 +1 @@
-# Include hook code here
+ActiveRecord::Base.class_eval do include NestedAssignment end
View
4 lib/nested_assignment.rb
@@ -1,7 +1,7 @@
# NestedAssignment
module NestedAssignment
def self.included(base)
- base.class_eval do extend ClassMethods end
+ base.class_eval { extend ClassMethods }
end
module ClassMethods
@@ -58,7 +58,7 @@ def deletable_associated
end
def changed_associated
- instantiated_associated.select(&:changed?
+ instantiated_associated.select(&:changed?)
end
def instantiated_associated
View
3  test/db/database.yml
@@ -0,0 +1,3 @@
+nested_assignment_test:
+ adapter: sqlite3
+ database: ':memory:'
View
38 test/db/models.rb
@@ -0,0 +1,38 @@
+# A set of classes design to offer not only every association, but every pair of associations.
+# For example, there's a has_one paired with a normal belongs_to, and another has_one paired with a polymorphic belongs_to.
+# To use, mix the PluginTestModels module into the TestCase
+module PluginTestModels
+ def self.included(base)
+ base.set_fixture_class({
+ :addresses => PluginTestModels::Address,
+ :users => PluginTestModels::User,
+ :services => PluginTestModels::Service,
+ :subscriptions => PluginTestModels::Subscription,
+ :roles => PluginTestModels::Role
+ })
+ end
+
+ class Address < ActiveRecord::Base
+ belongs_to :addressable, :polymorphic => true
+ end
+
+ class User < ActiveRecord::Base
+ has_and_belongs_to_many :roles
+ has_one :subscription
+ has_one :address, :as => :addressable
+ end
+
+ class Service < ActiveRecord::Base
+ has_many :subscriptions
+ has_many :users, :through => :subscriptions
+ end
+
+ class Subscription < ActiveRecord::Base
+ belongs_to :service
+ belongs_to :user
+ end
+
+ class Role < ActiveRecord::Base
+ has_and_belongs_to_many :users
+ end
+end
View
31 test/db/schema.rb
@@ -0,0 +1,31 @@
+ActiveRecord::Schema.define(:version => 1) do
+
+ create_table :addresses, :force => true do |t|
+ t.column :name, :string
+ t.column :addressable_id, :integer
+ t.column :addressable_type, :string
+ end
+
+ create_table :users, :force => true do |t|
+ t.column :name, :string
+ end
+
+ create_table :roles_users, :force => true, :id => false do |t|
+ t.column :user_id, :integer
+ t.column :role_id, :integer
+ end
+
+ create_table :services, :force => true do |t|
+ t.column :name, :string
+ end
+
+ create_table :subscriptions, :force => true do |t|
+ t.column :name, :string
+ t.column :service_id, :integer
+ t.column :user_id, :integer
+ end
+
+ create_table :roles, :force => true do |t|
+ t.column :name, :string
+ end
+end
View
15 test/fixtures/addresses.yml
@@ -0,0 +1,15 @@
+bobs_home:
+ id: 1
+ name: Home
+ addressable_id: 1
+ addressable_type: 'PluginTestModels::User'
+bobs_work:
+ id: 2
+ name: Work
+ addressable_id: 1
+ addressable_type: 'PluginTestModels::User'
+sues_home:
+ id: 3
+ name: Home
+ addressable_id: 2
+ addressable_type: 'PluginTestModels::User'
View
4 test/fixtures/roles.yml
@@ -0,0 +1,4 @@
+cook:
+ name: Cook
+gardener:
+ name: Gardener
View
6 test/fixtures/roles_users.yml
@@ -0,0 +1,6 @@
+bob_is_a_cook:
+ role_id: 1
+ user_id: 1
+sue_is_a_gardener:
+ role_id: 2
+ user_id: 2
View
6 test/fixtures/services.yml
@@ -0,0 +1,6 @@
+free:
+ id: 1
+ name: Free
+premium:
+ id: 2
+ name: Premium
View
10 test/fixtures/subscriptions.yml
@@ -0,0 +1,10 @@
+bob_is_free:
+ id: 1
+ name: Bob/Free
+ service_id: 1
+ user_id: 1
+sue_is_premium:
+ id: 2
+ name: Sue/Premium
+ service_id: 2
+ user_id: 2
View
6 test/fixtures/users.yml
@@ -0,0 +1,6 @@
+bob:
+ id: 1
+ name: Bob
+sue:
+ id: 2
+ name: Sue
View
5 test/nested_assignment_test.rb
@@ -1,5 +0,0 @@
-require 'test_helper'
-
-class NestedAssignmentTest < ActiveSupport::TestCase
-
-end
View
37 test/test_helper.rb
@@ -1,3 +1,36 @@
+ENV["RAILS_ENV"] = "test"
+
+# load the support libraries
+require 'test/unit'
require 'rubygems'
-require 'active_support'
-require 'active_support/test_case'
+require 'active_record'
+require 'active_record/fixtures'
+
+# establish the database connection
+ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/db/database.yml'))
+ActiveRecord::Base.establish_connection('nested_assignment_test')
+
+# capture the logging
+ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/test.log")
+
+# load the schema ... silently
+ActiveRecord::Migration.verbose = false
+load(File.dirname(__FILE__) + "/db/schema.rb")
+
+# load the ActiveRecord models
+require File.dirname(__FILE__) + '/db/models'
+
+# configure the TestCase settings
+class ActiveSupport::TestCase
+ include PluginTestModels
+
+ self.use_transactional_fixtures = true
+ self.use_instantiated_fixtures = false
+ self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
+
+ fixtures :all
+end
+
+# load the code-to-be-tested
+ActiveSupport::Dependencies.load_paths << File.dirname(__FILE__) + '/../lib/'
+require File.dirname(__FILE__) + '/../init'
View
11 test/unit/nested_assignment_test.rb
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class NestedAssignmentTest < ActiveSupport::TestCase
+ def test_fixtures
+ assert !User.find(:all).empty?
+ end
+
+ def test_failure
+ flunk
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.