composite_primary_keys 14.0.1 → 14.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/History.rdoc +900 -894
- data/README.rdoc +182 -182
- data/lib/composite_primary_keys/associations/{join_dependency.rb → join_association.rb} +137 -137
- data/lib/composite_primary_keys/associations/preloader/association.rb +68 -68
- data/lib/composite_primary_keys/composite_predicates.rb +71 -71
- data/lib/composite_primary_keys/persistence.rb +96 -96
- data/lib/composite_primary_keys/relation/calculations.rb +110 -110
- data/lib/composite_primary_keys/version.rb +8 -8
- data/lib/composite_primary_keys.rb +118 -118
- data/test/abstract_unit.rb +118 -118
- data/test/fixtures/department.rb +16 -16
- data/test/fixtures/membership.rb +8 -8
- data/test/test_associations.rb +372 -372
- metadata +2 -2
data/test/abstract_unit.rb
CHANGED
@@ -1,118 +1,118 @@
|
|
1
|
-
spec_name = ENV['ADAPTER'] || 'postgresql'
|
2
|
-
require 'bundler'
|
3
|
-
require 'minitest/autorun'
|
4
|
-
|
5
|
-
Bundler.setup(:default, spec_name.to_sym)
|
6
|
-
Bundler.require(:default, spec_name.to_sym)
|
7
|
-
require 'composite_primary_keys'
|
8
|
-
|
9
|
-
# Require the connection spec
|
10
|
-
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
11
|
-
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
12
|
-
|
13
|
-
spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
|
14
|
-
puts "Loaded #{spec_name}"
|
15
|
-
|
16
|
-
# And now connect to the database
|
17
|
-
ActiveRecord::Base.establish_connection(spec)
|
18
|
-
|
19
|
-
# Tell active record about the configuration
|
20
|
-
ActiveRecord::Base.configurations = {test: spec}
|
21
|
-
|
22
|
-
# Tell ActiveRecord where to find models
|
23
|
-
ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
|
24
|
-
Dir[File.join(PROJECT_ROOT, 'test', 'fixtures', '*.rb')].each do |file_path|
|
25
|
-
require_file = file_path.sub(PROJECT_ROOT, '..').sub('.rb', '')
|
26
|
-
require_relative require_file
|
27
|
-
end
|
28
|
-
|
29
|
-
I18n.config.enforce_available_locales = true
|
30
|
-
|
31
|
-
class ActiveSupport::TestCase
|
32
|
-
include ActiveRecord::TestFixtures
|
33
|
-
|
34
|
-
self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
|
35
|
-
self.use_instantiated_fixtures = false
|
36
|
-
self.use_transactional_tests = true
|
37
|
-
self.test_order = :random
|
38
|
-
|
39
|
-
def assert_date_from_db(expected, actual, message = nil)
|
40
|
-
# SQL Server doesn't have a separate column type just for dates,
|
41
|
-
# so the time is in the string and incorrectly formatted
|
42
|
-
if current_adapter?(:SQLServerAdapter)
|
43
|
-
assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
|
44
|
-
elsif current_adapter?(:SybaseAdapter)
|
45
|
-
assert_equal expected.to_s, actual.to_date.to_s, message
|
46
|
-
else
|
47
|
-
assert_equal expected.to_s, actual.to_s, message
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def assert_queries(num = 1)
|
52
|
-
ActiveRecord::Base.connection.class.class_eval do
|
53
|
-
self.query_count = 0
|
54
|
-
alias_method :execute, :execute_with_query_counting
|
55
|
-
end
|
56
|
-
yield
|
57
|
-
ensure
|
58
|
-
ActiveRecord::Base.connection.class.class_eval do
|
59
|
-
alias_method :execute, :execute_without_query_counting
|
60
|
-
end
|
61
|
-
assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
|
62
|
-
end
|
63
|
-
|
64
|
-
def assert_no_queries(&block)
|
65
|
-
assert_queries(0, &block)
|
66
|
-
end
|
67
|
-
|
68
|
-
cattr_accessor :classes
|
69
|
-
|
70
|
-
protected
|
71
|
-
|
72
|
-
def testing_with(&block)
|
73
|
-
classes.keys.each do |key_test|
|
74
|
-
@key_test = key_test
|
75
|
-
@klass_info = classes[@key_test]
|
76
|
-
@klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
|
77
|
-
order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
|
78
|
-
@first = @klass.order(order).first
|
79
|
-
yield
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
def first_id
|
84
|
-
ids = (1..@primary_keys.length).map {|num| 1}
|
85
|
-
composite? ? ids.to_composite_ids : ids.first
|
86
|
-
end
|
87
|
-
|
88
|
-
def composite?
|
89
|
-
@key_test != :single
|
90
|
-
end
|
91
|
-
|
92
|
-
# Oracle metadata is in all caps.
|
93
|
-
def with_quoted_identifiers(s)
|
94
|
-
s.gsub(/'(\w+)'/) { |m|
|
95
|
-
if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
|
96
|
-
m.upcase
|
97
|
-
else
|
98
|
-
m
|
99
|
-
end
|
100
|
-
}
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
def current_adapter?(type)
|
105
|
-
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
|
106
|
-
ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
|
107
|
-
end
|
108
|
-
|
109
|
-
ActiveRecord::Base.connection.class.class_eval do
|
110
|
-
cattr_accessor :query_count
|
111
|
-
alias_method :execute_without_query_counting, :execute
|
112
|
-
def execute_with_query_counting(sql, name = nil)
|
113
|
-
self.query_count += 1
|
114
|
-
execute_without_query_counting(sql, name)
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
|
1
|
+
spec_name = ENV['ADAPTER'] || 'postgresql'
|
2
|
+
require 'bundler'
|
3
|
+
require 'minitest/autorun'
|
4
|
+
|
5
|
+
Bundler.setup(:default, spec_name.to_sym)
|
6
|
+
Bundler.require(:default, spec_name.to_sym)
|
7
|
+
require 'composite_primary_keys'
|
8
|
+
|
9
|
+
# Require the connection spec
|
10
|
+
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
11
|
+
require File.join(PROJECT_ROOT, 'test', 'connections', 'connection_spec')
|
12
|
+
|
13
|
+
spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
|
14
|
+
puts "Loaded #{spec_name}"
|
15
|
+
|
16
|
+
# And now connect to the database
|
17
|
+
ActiveRecord::Base.establish_connection(spec)
|
18
|
+
|
19
|
+
# Tell active record about the configuration
|
20
|
+
ActiveRecord::Base.configurations = {test: spec}
|
21
|
+
|
22
|
+
# Tell ActiveRecord where to find models
|
23
|
+
ActiveSupport::Dependencies.autoload_paths << File.join(PROJECT_ROOT, 'test', 'fixtures')
|
24
|
+
Dir[File.join(PROJECT_ROOT, 'test', 'fixtures', '*.rb')].each do |file_path|
|
25
|
+
require_file = file_path.sub(PROJECT_ROOT, '..').sub('.rb', '')
|
26
|
+
require_relative require_file
|
27
|
+
end
|
28
|
+
|
29
|
+
I18n.config.enforce_available_locales = true
|
30
|
+
|
31
|
+
class ActiveSupport::TestCase
|
32
|
+
include ActiveRecord::TestFixtures
|
33
|
+
|
34
|
+
self.fixture_path = File.dirname(__FILE__) + '/fixtures/'
|
35
|
+
self.use_instantiated_fixtures = false
|
36
|
+
self.use_transactional_tests = true
|
37
|
+
self.test_order = :random
|
38
|
+
|
39
|
+
def assert_date_from_db(expected, actual, message = nil)
|
40
|
+
# SQL Server doesn't have a separate column type just for dates,
|
41
|
+
# so the time is in the string and incorrectly formatted
|
42
|
+
if current_adapter?(:SQLServerAdapter)
|
43
|
+
assert_equal expected.strftime('%Y/%m/%d 00:00:00'), actual.strftime('%Y/%m/%d 00:00:00')
|
44
|
+
elsif current_adapter?(:SybaseAdapter)
|
45
|
+
assert_equal expected.to_s, actual.to_date.to_s, message
|
46
|
+
else
|
47
|
+
assert_equal expected.to_s, actual.to_s, message
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def assert_queries(num = 1)
|
52
|
+
ActiveRecord::Base.connection.class.class_eval do
|
53
|
+
self.query_count = 0
|
54
|
+
alias_method :execute, :execute_with_query_counting
|
55
|
+
end
|
56
|
+
yield
|
57
|
+
ensure
|
58
|
+
ActiveRecord::Base.connection.class.class_eval do
|
59
|
+
alias_method :execute, :execute_without_query_counting
|
60
|
+
end
|
61
|
+
assert_equal num, ActiveRecord::Base.connection.query_count, '#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed.'
|
62
|
+
end
|
63
|
+
|
64
|
+
def assert_no_queries(&block)
|
65
|
+
assert_queries(0, &block)
|
66
|
+
end
|
67
|
+
|
68
|
+
cattr_accessor :classes
|
69
|
+
|
70
|
+
protected
|
71
|
+
|
72
|
+
def testing_with(&block)
|
73
|
+
classes.keys.each do |key_test|
|
74
|
+
@key_test = key_test
|
75
|
+
@klass_info = classes[@key_test]
|
76
|
+
@klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
|
77
|
+
order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
|
78
|
+
@first = @klass.order(order).first
|
79
|
+
yield
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def first_id
|
84
|
+
ids = (1..@primary_keys.length).map {|num| 1}
|
85
|
+
composite? ? ids.to_composite_ids : ids.first
|
86
|
+
end
|
87
|
+
|
88
|
+
def composite?
|
89
|
+
@key_test != :single
|
90
|
+
end
|
91
|
+
|
92
|
+
# Oracle metadata is in all caps.
|
93
|
+
def with_quoted_identifiers(s)
|
94
|
+
s.gsub(/'(\w+)'/) { |m|
|
95
|
+
if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
|
96
|
+
m.upcase
|
97
|
+
else
|
98
|
+
m
|
99
|
+
end
|
100
|
+
}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def current_adapter?(type)
|
105
|
+
ActiveRecord::ConnectionAdapters.const_defined?(type) &&
|
106
|
+
ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
|
107
|
+
end
|
108
|
+
|
109
|
+
ActiveRecord::Base.connection.class.class_eval do
|
110
|
+
cattr_accessor :query_count
|
111
|
+
alias_method :execute_without_query_counting, :execute
|
112
|
+
def execute_with_query_counting(sql, name = nil)
|
113
|
+
self.query_count += 1
|
114
|
+
execute_without_query_counting(sql, name)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
|
data/test/fixtures/department.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
class Department < ActiveRecord::Base
|
2
|
-
self.primary_keys = :id, :location_id
|
3
|
-
|
4
|
-
has_many :employees,
|
5
|
-
# We intentionally redefine primary key for test purposes. #455
|
6
|
-
:primary_key => [:id, :location_id],
|
7
|
-
:foreign_key => [:department_id, :location_id]
|
8
|
-
|
9
|
-
has_many :comments, :through => :employees
|
10
|
-
|
11
|
-
has_one :head, :class_name => 'Employee', :autosave => true, :dependent => :delete,
|
12
|
-
# We intentionally redefine primary key for test purposes. #455
|
13
|
-
:primary_key => [:id, :location_id],
|
14
|
-
:foreign_key => [:department_id, :location_id]
|
15
|
-
|
16
|
-
end
|
1
|
+
class Department < ActiveRecord::Base
|
2
|
+
self.primary_keys = :id, :location_id
|
3
|
+
|
4
|
+
has_many :employees,
|
5
|
+
# We intentionally redefine primary key for test purposes. #455
|
6
|
+
:primary_key => [:id, :location_id],
|
7
|
+
:foreign_key => [:department_id, :location_id]
|
8
|
+
|
9
|
+
has_many :comments, :through => :employees
|
10
|
+
|
11
|
+
has_one :head, :class_name => 'Employee', :autosave => true, :dependent => :delete,
|
12
|
+
# We intentionally redefine primary key for test purposes. #455
|
13
|
+
:primary_key => [:id, :location_id],
|
14
|
+
:foreign_key => [:department_id, :location_id]
|
15
|
+
|
16
|
+
end
|
data/test/fixtures/membership.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
class Membership < ActiveRecord::Base
|
2
|
-
self.primary_keys = :user_id, :group_id
|
3
|
-
belongs_to :user
|
4
|
-
belongs_to :group
|
5
|
-
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
6
|
-
has_many :active_statuses, -> { where('membership_statuses.status = ?', 'Active') },
|
7
|
-
:class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
8
|
-
has_many :readings, :primary_key => :user_id, :foreign_key => :user_id
|
1
|
+
class Membership < ActiveRecord::Base
|
2
|
+
self.primary_keys = :user_id, :group_id
|
3
|
+
belongs_to :user
|
4
|
+
belongs_to :group
|
5
|
+
has_many :statuses, :class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
6
|
+
has_many :active_statuses, -> { where('membership_statuses.status = ?', 'Active') },
|
7
|
+
:class_name => 'MembershipStatus', :foreign_key => [:user_id, :group_id]
|
8
|
+
has_many :readings, :primary_key => :user_id, :foreign_key => :user_id
|
9
9
|
end
|