]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/composite_primary_keys-1.1.0/test/abstract_unit.rb
Freezing composite primary key so that we can apply TomH's patch to it, and that...
[rails.git] / vendor / gems / composite_primary_keys-1.1.0 / test / abstract_unit.rb
1 $:.unshift(ENV['AR_LOAD_PATH']) if ENV['AR_LOAD_PATH']
2
3 require 'test/unit'
4 require 'hash_tricks'
5 require 'rubygems'
6 require 'active_record'
7 require 'active_record/fixtures'
8 begin
9   require 'connection'
10 rescue MissingSourceFile => e
11   adapter = 'postgresql' #'sqlite'
12   require "#{File.dirname(__FILE__)}/connections/native_#{adapter}/connection"
13 end
14 require 'composite_primary_keys'
15
16 QUOTED_TYPE = ActiveRecord::Base.connection.quote_column_name('type') unless Object.const_defined?(:QUOTED_TYPE)
17
18 class Test::Unit::TestCase #:nodoc:
19   self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
20   self.use_instantiated_fixtures = false
21   self.use_transactional_fixtures = true
22
23   def assert_date_from_db(expected, actual, message = nil)
24     # SQL Server doesn't have a separate column type just for dates, 
25     # so the time is in the string and incorrectly formatted
26     if current_adapter?(:SQLServerAdapter)
27       assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
28     elsif current_adapter?(:SybaseAdapter)
29       assert_equal expected.to_s, actual.to_date.to_s, message
30     else
31       assert_equal expected.to_s, actual.to_s, message
32     end
33   end
34
35   def assert_queries(num = 1)
36     ActiveRecord::Base.connection.class.class_eval do
37       self.query_count = 0
38       alias_method :execute, :execute_with_query_counting
39     end
40     yield
41   ensure
42     ActiveRecord::Base.connection.class.class_eval do
43       alias_method :execute, :execute_without_query_counting
44     end
45     assert_equal num, ActiveRecord::Base.connection.query_count, "#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed."
46   end
47
48   def assert_no_queries(&block)
49     assert_queries(0, &block)
50   end
51   
52   cattr_accessor :classes
53 protected
54   
55   def testing_with(&block)
56     classes.keys.each do |@key_test|
57       @klass_info = classes[@key_test]
58       @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
59       order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
60       @first = @klass.find(:first, :order => order)
61       yield
62     end
63   end
64   
65   def first_id
66     ids = (1..@primary_keys.length).map {|num| 1}
67     composite? ? ids.to_composite_ids : ids.first
68   end
69   
70   def first_id_str
71     composite? ? first_id.join(CompositePrimaryKeys::ID_SEP) : first_id.to_s
72   end
73   
74   def composite?
75     @key_test != :single
76   end  
77 end
78
79 def current_adapter?(type)
80   ActiveRecord::ConnectionAdapters.const_defined?(type) &&
81     ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
82 end
83
84 ActiveRecord::Base.connection.class.class_eval do
85   cattr_accessor :query_count
86   alias_method :execute_without_query_counting, :execute
87   def execute_with_query_counting(sql, name = nil)
88     self.query_count += 1
89     execute_without_query_counting(sql, name)
90   end
91 end
92
93 #ActiveRecord::Base.logger = Logger.new(STDOUT)
94 #ActiveRecord::Base.colorize_logging = false