composite_primary_keys 7.0.14 → 7.0.15

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c70848e6827fb3c8454a2de03b225abe8398ef81
4
- data.tar.gz: 475491e54228f0f303e98cf5fd28a2e433a60c50
3
+ metadata.gz: 0655d26f0b98d96ce4e4a5e87d1a6cae68391b7d
4
+ data.tar.gz: c26046f30f2032540ff93522328a40cdbc735d0a
5
5
  SHA512:
6
- metadata.gz: a24dee1f1412596540a92e82b62919c4b7bb915ad8d88a6d0728ef0faf74b27f391a5c3227ee08727a24bb74dc57976e1662fd2b98de31ae2964b8bb234ae319
7
- data.tar.gz: 6bf0353d16d627362941d7063d14279b09910e85a3fd86eec665fe059e1018ea0053afc042a1df9701840462d8a89fd01675fe40a811e517fd30746e240e9e91
6
+ metadata.gz: ecf66e28ac34b92bf9a27a20806bd9fb26e88b1f2b984b2cb43c5e86f325f224f25e5844f4dde8ba8fa80316f85cda8c5d27ae49b616bb48b2bdf6948b56766c
7
+ data.tar.gz: cf120e29e8d8c6d38861505f01364e28cf96430d7fcd1c97c24fa7d6077b65a8d18a4aad3a1f1751d22536ac0c65d8859ff97b6d3b67056872b40b22f7904d67
@@ -1,3 +1,7 @@
1
+ == 7.0.15 (2015-09-02)
2
+
3
+ * Fix bug in calculations - such as average (Charlie Savage)
4
+
1
5
  == 7.0.14 (2015-08-04)
2
6
 
3
7
  * Remove overriden Base#==(comparison_object) which had a different behavior than stock AR method
@@ -1,129 +1,129 @@
1
- module ActiveRecord
2
- class CompositeKeyError < StandardError #:nodoc:
3
- end
4
-
5
- class Base
6
- INVALID_FOR_COMPOSITE_KEYS = 'Not appropriate for composite primary keys'
7
- NOT_IMPLEMENTED_YET = 'Not implemented for composite primary keys yet'
8
-
9
- class << self
10
- def primary_keys
11
- unless defined?(@primary_keys)
12
- reset_primary_keys
13
- end
14
- @primary_keys
15
- end
16
-
17
- # Don't like this method name, but its modeled after how AR does it
18
- def reset_primary_keys
19
- if self != base_class
20
- self.primary_keys = base_class.primary_keys
21
- end
22
- end
23
-
24
- def primary_key_with_composite_key_support=(keys)
25
- unless keys.kind_of?(Array)
26
- self.primary_key_without_composite_key_support = keys
27
- return
28
- end
29
-
30
- @primary_keys = keys.map { |k| k.to_s }.to_composite_keys
31
-
32
- class_eval <<-EOV
33
- extend CompositeClassMethods
34
- include CompositeInstanceMethods
35
- EOV
36
- end
37
- alias_method_chain :primary_key=, :composite_key_support
38
- alias_method :primary_keys=, :primary_key=
39
-
40
- def set_primary_keys(*keys)
41
- ActiveSupport::Deprecation.warn(
42
- "Calling set_primary_keys is deprecated. Please use `self.primary_keys = keys` instead."
43
- )
44
-
45
- keys = keys.first if keys.first.is_a?(Array)
46
- if keys.length == 1
47
- self.primary_key = keys.first
48
- else
49
- self.primary_keys = keys
50
- end
51
- end
52
-
53
- def composite?
54
- false
55
- end
56
- end
57
-
58
- def composite?
59
- self.class.composite?
60
- end
61
-
62
- module CompositeClassMethods
63
- def primary_key
64
- primary_keys
65
- end
66
-
67
- def primary_key=(keys)
68
- primary_keys = keys
69
- end
70
-
71
- def composite?
72
- true
73
- end
74
-
75
- #ids_to_s([[1,2],[7,3]]) -> "(1,2),(7,3)"
76
- #ids_to_s([[1,2],[7,3]], ',', ';') -> "1,2;7,3"
77
- def ids_to_s(many_ids, id_sep = CompositePrimaryKeys::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')')
78
- many_ids.map {|ids| "#{left_bracket}#{CompositePrimaryKeys::CompositeKeys.new(ids)}#{right_bracket}"}.join(list_sep)
79
- end
80
- end
81
-
82
- module CompositeInstanceMethods
83
- # A model instance's primary keys is always available as model.ids
84
- # whether you name it the default 'id' or set it to something else.
85
- def id
86
- attr_names = self.class.primary_keys
87
- ::CompositePrimaryKeys::CompositeKeys.new(attr_names.map { |attr_name| read_attribute(attr_name) })
88
- end
89
- alias_method :ids, :id
90
-
91
- def ids_hash
92
- self.class.primary_key.zip(ids).inject(Hash.new) do |hash, (key, value)|
93
- hash[key] = value
94
- hash
95
- end
96
- end
97
-
98
- def id_before_type_cast
99
- self.class.primary_keys.map do |key|
100
- self.send("#{key.to_s}_before_type_cast")
101
- end
102
- end
103
-
104
- # Sets the primary ID.
105
- def id=(ids)
106
- ids = CompositePrimaryKeys::CompositeKeys.parse(ids)
107
- unless ids.length == self.class.primary_keys.length
108
- raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids"
109
- end
110
- [self.class.primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)}
111
- id
112
- end
113
-
114
- def can_change_primary_key_values?
115
- false
116
- end
117
-
118
- # Returns this record's primary keys values in an Array
119
- # if any value is available
120
- def to_key
121
- ids.to_a if !ids.compact.empty? # XXX Maybe use primary_keys with send instead of ids
122
- end
123
-
124
- def to_param
125
- persisted? ? to_key.join(CompositePrimaryKeys::ID_SEP) : nil
126
- end
127
- end
128
- end
129
- end
1
+ module ActiveRecord
2
+ class CompositeKeyError < StandardError #:nodoc:
3
+ end
4
+
5
+ class Base
6
+ INVALID_FOR_COMPOSITE_KEYS = 'Not appropriate for composite primary keys'
7
+ NOT_IMPLEMENTED_YET = 'Not implemented for composite primary keys yet'
8
+
9
+ class << self
10
+ def primary_keys
11
+ unless defined?(@primary_keys)
12
+ reset_primary_keys
13
+ end
14
+ @primary_keys
15
+ end
16
+
17
+ # Don't like this method name, but its modeled after how AR does it
18
+ def reset_primary_keys
19
+ if self != base_class
20
+ self.primary_keys = base_class.primary_keys
21
+ end
22
+ end
23
+
24
+ def primary_key_with_composite_key_support=(keys)
25
+ unless keys.kind_of?(Array)
26
+ self.primary_key_without_composite_key_support = keys
27
+ return
28
+ end
29
+
30
+ @primary_keys = keys.map { |k| k.to_s }.to_composite_keys
31
+
32
+ class_eval <<-EOV
33
+ extend CompositeClassMethods
34
+ include CompositeInstanceMethods
35
+ EOV
36
+ end
37
+ alias_method_chain :primary_key=, :composite_key_support
38
+ alias_method :primary_keys=, :primary_key=
39
+
40
+ def set_primary_keys(*keys)
41
+ ActiveSupport::Deprecation.warn(
42
+ "Calling set_primary_keys is deprecated. Please use `self.primary_keys = keys` instead."
43
+ )
44
+
45
+ keys = keys.first if keys.first.is_a?(Array)
46
+ if keys.length == 1
47
+ self.primary_key = keys.first
48
+ else
49
+ self.primary_keys = keys
50
+ end
51
+ end
52
+
53
+ def composite?
54
+ false
55
+ end
56
+ end
57
+
58
+ def composite?
59
+ self.class.composite?
60
+ end
61
+
62
+ module CompositeClassMethods
63
+ def primary_key
64
+ primary_keys
65
+ end
66
+
67
+ def primary_key=(keys)
68
+ primary_keys = keys
69
+ end
70
+
71
+ def composite?
72
+ true
73
+ end
74
+
75
+ #ids_to_s([[1,2],[7,3]]) -> "(1,2),(7,3)"
76
+ #ids_to_s([[1,2],[7,3]], ',', ';') -> "1,2;7,3"
77
+ def ids_to_s(many_ids, id_sep = CompositePrimaryKeys::ID_SEP, list_sep = ',', left_bracket = '(', right_bracket = ')')
78
+ many_ids.map {|ids| "#{left_bracket}#{CompositePrimaryKeys::CompositeKeys.new(ids)}#{right_bracket}"}.join(list_sep)
79
+ end
80
+ end
81
+
82
+ module CompositeInstanceMethods
83
+ # A model instance's primary keys is always available as model.ids
84
+ # whether you name it the default 'id' or set it to something else.
85
+ def id
86
+ attr_names = self.class.primary_keys
87
+ ::CompositePrimaryKeys::CompositeKeys.new(attr_names.map { |attr_name| read_attribute(attr_name) })
88
+ end
89
+ alias_method :ids, :id
90
+
91
+ def ids_hash
92
+ self.class.primary_key.zip(ids).inject(Hash.new) do |hash, (key, value)|
93
+ hash[key] = value
94
+ hash
95
+ end
96
+ end
97
+
98
+ def id_before_type_cast
99
+ self.class.primary_keys.map do |key|
100
+ self.send("#{key.to_s}_before_type_cast")
101
+ end
102
+ end
103
+
104
+ # Sets the primary ID.
105
+ def id=(ids)
106
+ ids = CompositePrimaryKeys::CompositeKeys.parse(ids)
107
+ unless ids.length == self.class.primary_keys.length
108
+ raise "#{self.class}.id= requires #{self.class.primary_keys.length} ids"
109
+ end
110
+ [self.class.primary_keys, ids].transpose.each {|key, an_id| write_attribute(key , an_id)}
111
+ id
112
+ end
113
+
114
+ def can_change_primary_key_values?
115
+ false
116
+ end
117
+
118
+ # Returns this record's primary keys values in an Array
119
+ # if any value is available
120
+ def to_key
121
+ ids.to_a if !ids.compact.empty? # XXX Maybe use primary_keys with send instead of ids
122
+ end
123
+
124
+ def to_param
125
+ persisted? ? to_key.join(CompositePrimaryKeys::ID_SEP) : nil
126
+ end
127
+ end
128
+ end
129
+ end
@@ -29,8 +29,8 @@ module CompositePrimaryKeys
29
29
  else
30
30
  column = aggregate_column(column_name)
31
31
 
32
- column_alias = select_value.alias
33
32
  select_value = operation_over_aggregate_column(column, operation, distinct)
33
+ column_alias = select_value.alias
34
34
 
35
35
  relation.select_values = [select_value]
36
36
 
@@ -2,7 +2,7 @@ module CompositePrimaryKeys
2
2
  module VERSION
3
3
  MAJOR = 7
4
4
  MINOR = 0
5
- TINY = 14
5
+ TINY = 15
6
6
  STRING = [MAJOR, MINOR, TINY].join('.')
7
7
  end
8
8
  end
@@ -1,113 +1,113 @@
1
- PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
2
-
3
- # To make debugging easier, test within this source tree versus an installed gem
4
- $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
5
- require 'composite_primary_keys'
6
- require 'minitest/autorun'
7
- require 'active_support/test_case'
8
-
9
- # Now load the connection spec
10
- require File.join(PROJECT_ROOT, "test", "connections", "connection_spec")
11
-
12
- spec_name = ENV['ADAPTER'] || 'postgresql'
13
- spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
14
-
15
- # And now connect to the database
16
- adapter = spec['adapter']
17
- require File.join(PROJECT_ROOT, "test", "connections", "native_#{spec_name}", "connection")
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
-
25
- I18n.config.enforce_available_locales = true
26
-
27
- class ActiveSupport::TestCase
28
- include ActiveRecord::TestFixtures
29
-
30
- self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
31
- self.use_instantiated_fixtures = false
32
- self.use_transactional_fixtures = true
33
-
34
- def assert_date_from_db(expected, actual, message = nil)
35
- # SQL Server doesn't have a separate column type just for dates,
36
- # so the time is in the string and incorrectly formatted
37
- if current_adapter?(:SQLServerAdapter)
38
- assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
39
- elsif current_adapter?(:SybaseAdapter)
40
- assert_equal expected.to_s, actual.to_date.to_s, message
41
- else
42
- assert_equal expected.to_s, actual.to_s, message
43
- end
44
- end
45
-
46
- def assert_queries(num = 1)
47
- ActiveRecord::Base.connection.class.class_eval do
48
- self.query_count = 0
49
- alias_method :execute, :execute_with_query_counting
50
- end
51
- yield
52
- ensure
53
- ActiveRecord::Base.connection.class.class_eval do
54
- alias_method :execute, :execute_without_query_counting
55
- end
56
- assert_equal num, ActiveRecord::Base.connection.query_count, "#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed."
57
- end
58
-
59
- def assert_no_queries(&block)
60
- assert_queries(0, &block)
61
- end
62
-
63
- cattr_accessor :classes
64
-
65
- protected
66
-
67
- def testing_with(&block)
68
- classes.keys.each do |key_test|
69
- @key_test = key_test
70
- @klass_info = classes[@key_test]
71
- @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
72
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
73
- @first = @klass.order(order).first
74
- yield
75
- end
76
- end
77
-
78
- def first_id
79
- ids = (1..@primary_keys.length).map {|num| 1}
80
- composite? ? ids.to_composite_ids : ids.first
81
- end
82
-
83
- def composite?
84
- @key_test != :single
85
- end
86
-
87
- # Oracle metadata is in all caps.
88
- def with_quoted_identifiers(s)
89
- s.gsub(/"(\w+)"/) { |m|
90
- if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
91
- m.upcase
92
- else
93
- m
94
- end
95
- }
96
- end
97
- end
98
-
99
- def current_adapter?(type)
100
- ActiveRecord::ConnectionAdapters.const_defined?(type) &&
101
- ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
102
- end
103
-
104
- ActiveRecord::Base.connection.class.class_eval do
105
- cattr_accessor :query_count
106
- alias_method :execute_without_query_counting, :execute
107
- def execute_with_query_counting(sql, name = nil)
108
- self.query_count += 1
109
- execute_without_query_counting(sql, name)
110
- end
111
- end
112
-
113
- ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
1
+ PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
2
+
3
+ # To make debugging easier, test within this source tree versus an installed gem
4
+ $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
5
+ require 'composite_primary_keys'
6
+ require 'minitest/autorun'
7
+ require 'active_support/test_case'
8
+
9
+ # Now load the connection spec
10
+ require File.join(PROJECT_ROOT, "test", "connections", "connection_spec")
11
+
12
+ spec_name = ENV['ADAPTER'] || 'postgresql'
13
+ spec = CompositePrimaryKeys::ConnectionSpec[spec_name]
14
+
15
+ # And now connect to the database
16
+ adapter = spec['adapter']
17
+ require File.join(PROJECT_ROOT, "test", "connections", "native_#{spec_name}", "connection")
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
+
25
+ I18n.config.enforce_available_locales = true
26
+
27
+ class ActiveSupport::TestCase
28
+ include ActiveRecord::TestFixtures
29
+
30
+ self.fixture_path = File.dirname(__FILE__) + "/fixtures/"
31
+ self.use_instantiated_fixtures = false
32
+ self.use_transactional_fixtures = true
33
+
34
+ def assert_date_from_db(expected, actual, message = nil)
35
+ # SQL Server doesn't have a separate column type just for dates,
36
+ # so the time is in the string and incorrectly formatted
37
+ if current_adapter?(:SQLServerAdapter)
38
+ assert_equal expected.strftime("%Y/%m/%d 00:00:00"), actual.strftime("%Y/%m/%d 00:00:00")
39
+ elsif current_adapter?(:SybaseAdapter)
40
+ assert_equal expected.to_s, actual.to_date.to_s, message
41
+ else
42
+ assert_equal expected.to_s, actual.to_s, message
43
+ end
44
+ end
45
+
46
+ def assert_queries(num = 1)
47
+ ActiveRecord::Base.connection.class.class_eval do
48
+ self.query_count = 0
49
+ alias_method :execute, :execute_with_query_counting
50
+ end
51
+ yield
52
+ ensure
53
+ ActiveRecord::Base.connection.class.class_eval do
54
+ alias_method :execute, :execute_without_query_counting
55
+ end
56
+ assert_equal num, ActiveRecord::Base.connection.query_count, "#{ActiveRecord::Base.connection.query_count} instead of #{num} queries were executed."
57
+ end
58
+
59
+ def assert_no_queries(&block)
60
+ assert_queries(0, &block)
61
+ end
62
+
63
+ cattr_accessor :classes
64
+
65
+ protected
66
+
67
+ def testing_with(&block)
68
+ classes.keys.each do |key_test|
69
+ @key_test = key_test
70
+ @klass_info = classes[@key_test]
71
+ @klass, @primary_keys = @klass_info[:class], @klass_info[:primary_keys]
72
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
73
+ @first = @klass.order(order).first
74
+ yield
75
+ end
76
+ end
77
+
78
+ def first_id
79
+ ids = (1..@primary_keys.length).map {|num| 1}
80
+ composite? ? ids.to_composite_ids : ids.first
81
+ end
82
+
83
+ def composite?
84
+ @key_test != :single
85
+ end
86
+
87
+ # Oracle metadata is in all caps.
88
+ def with_quoted_identifiers(s)
89
+ s.gsub(/"(\w+)"/) { |m|
90
+ if ActiveRecord::Base.configurations[:test]['adapter'] =~ /oracle/i
91
+ m.upcase
92
+ else
93
+ m
94
+ end
95
+ }
96
+ end
97
+ end
98
+
99
+ def current_adapter?(type)
100
+ ActiveRecord::ConnectionAdapters.const_defined?(type) &&
101
+ ActiveRecord::Base.connection.instance_of?(ActiveRecord::ConnectionAdapters.const_get(type))
102
+ end
103
+
104
+ ActiveRecord::Base.connection.class.class_eval do
105
+ cattr_accessor :query_count
106
+ alias_method :execute_without_query_counting, :execute
107
+ def execute_with_query_counting(sql, name = nil)
108
+ self.query_count += 1
109
+ execute_without_query_counting(sql, name)
110
+ end
111
+ end
112
+
113
+ ActiveRecord::Base.logger = Logger.new(ENV['CPK_LOGFILE'] || STDOUT)
@@ -34,4 +34,9 @@ class TestCalculations < ActiveSupport::TestCase
34
34
  count = Tariff.includes(:product_tariffs).references(:product_tariffs).where("product_tariffs.tariff_id = ?", 2).count
35
35
  assert_equal(1, count)
36
36
  end
37
+
38
+ def test_average
39
+ average = Tariff.average(:amount)
40
+ assert_equal(50, average)
41
+ end
37
42
  end
@@ -1,26 +1,26 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestEqual < ActiveSupport::TestCase
4
- fixtures :capitols
5
-
6
- def test_new
7
- assert_equal(Capitol.new, Capitol.new)
8
- end
9
-
10
- def test_same_new
11
- it = Capitol.new
12
- assert_equal(it, it)
13
- end
14
-
15
- def test_same
16
- first = Capitol.find(['Canada', 'Ottawa'])
17
- second = Capitol.find(['Canada', 'Ottawa'])
18
- assert_equal(first, second)
19
- end
20
-
21
- def test_different
22
- first = Capitol.find(['Mexico', 'Mexico City'])
23
- second = Capitol.find(['Canada', 'Ottawa'])
24
- assert_not_equal(first, second)
25
- end
26
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestEqual < ActiveSupport::TestCase
4
+ fixtures :capitols
5
+
6
+ def test_new
7
+ assert_equal(Capitol.new, Capitol.new)
8
+ end
9
+
10
+ def test_same_new
11
+ it = Capitol.new
12
+ assert_equal(it, it)
13
+ end
14
+
15
+ def test_same
16
+ first = Capitol.find(['Canada', 'Ottawa'])
17
+ second = Capitol.find(['Canada', 'Ottawa'])
18
+ assert_equal(first, second)
19
+ end
20
+
21
+ def test_different
22
+ first = Capitol.find(['Mexico', 'Mexico City'])
23
+ second = Capitol.find(['Canada', 'Ottawa'])
24
+ assert_not_equal(first, second)
25
+ end
26
+ end
@@ -1,118 +1,118 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- # Testing the find action on composite ActiveRecords with two primary keys
4
- class TestFind < ActiveSupport::TestCase
5
- fixtures :capitols, :departments, :reference_types, :reference_codes, :suburbs
6
-
7
- def test_find_first
8
- ref_code = ReferenceCode.order('reference_type_id, reference_code').first
9
- assert_kind_of(ReferenceCode, ref_code)
10
- assert_equal([1,1], ref_code.id)
11
- end
12
-
13
- def test_find_last
14
- ref_code = ReferenceCode.order('reference_type_id, reference_code').last
15
- assert_kind_of(ReferenceCode, ref_code)
16
- assert_equal([2,2], ref_code.id)
17
- end
18
-
19
- def test_find_one
20
- ref_code = ReferenceCode.find([1,3])
21
- assert_not_nil(ref_code)
22
- assert_equal([1,3], ref_code.id)
23
- end
24
-
25
- def test_find_some
26
- ref_codes = ReferenceCode.find([1,3], [2,1])
27
- assert_kind_of(Array, ref_codes)
28
- assert_equal(2, ref_codes.length)
29
-
30
- ref_code = ref_codes[0]
31
- assert_equal([1,3], ref_code.id)
32
-
33
- ref_code = ref_codes[1]
34
- assert_equal([2,1], ref_code.id)
35
- end
36
-
37
- def test_find_with_strings_as_composite_keys
38
- capitol = Capitol.find(['The Netherlands', 'Amsterdam'])
39
- assert_kind_of(Capitol, capitol)
40
- assert_equal(['The Netherlands', 'Amsterdam'], capitol.id)
41
- end
42
-
43
- def test_find_each
44
- room_assignments = []
45
- RoomAssignment.find_each(:batch_size => 2) do |assignment|
46
- room_assignments << assignment
47
- end
48
-
49
- assert_equal(RoomAssignment.count, room_assignments.uniq.length)
50
- end
51
-
52
- def test_find_each_with_scope
53
- scoped_departments = Department.where("department_id <> 3")
54
- scoped_departments.find_each(:batch_size => 2) do |department|
55
- assert department.id != 3
56
- end
57
- end
58
-
59
- def test_not_found
60
- error = assert_raise(::ActiveRecord::RecordNotFound) do
61
- ReferenceCode.find(['999', '999'])
62
- end
63
-
64
- connection = ActiveRecord::Base.connection
65
- ref_type_quoted = "#{connection.quote_table_name('reference_codes')}.#{connection.quote_column_name('reference_type_id')}"
66
- ref_code_quoted = "#{connection.quote_table_name('reference_codes')}.#{connection.quote_column_name('reference_code')}"
67
-
68
- expected = "Couldn't find all ReferenceCodes with 'reference_type_id,reference_code': (999, 999) (found 0 results, but was looking for 1)"
69
- assert_equal(with_quoted_identifiers(expected), error.message)
70
- end
71
-
72
- def test_find_last_suburb
73
- suburb = Suburb.last
74
- assert_equal([2,1], suburb.id)
75
- end
76
-
77
- def test_find_last_suburb_with_order
78
- # Rails actually changes city_id DESC to city_id ASC
79
- suburb = Suburb.order('suburbs.city_id DESC').last
80
- assert_equal([1,1], suburb.id)
81
- end
82
-
83
- def test_find_in_batches
84
- Department.find_in_batches do |batch|
85
- assert_equal(Department.count, batch.size)
86
- end
87
- end
88
-
89
- def test_expand
90
- department = departments(:engineering)
91
- employees = Employee.where(:department => department)
92
- assert_equal(2, employees.count)
93
- end
94
-
95
- def test_find_one_with_params_id
96
- params_id = ReferenceCode.find([1,3]).to_param
97
- assert_equal params_id, "1,3"
98
-
99
- ref_code = ReferenceCode.find(params_id)
100
- assert_not_nil(ref_code)
101
- assert_equal([1,3], ref_code.id)
102
- end
103
-
104
- def test_find_some_with_array_of_params_id
105
- params_ids = ReferenceCode.find([1,3], [2,1]).map(&:to_param)
106
- assert_equal ["1,3", "2,1"], params_ids
107
-
108
- ref_codes = ReferenceCode.find(params_ids)
109
- assert_kind_of(Array, ref_codes)
110
- assert_equal(2, ref_codes.length)
111
-
112
- ref_code = ref_codes[0]
113
- assert_equal([1,3], ref_code.id)
114
-
115
- ref_code = ref_codes[1]
116
- assert_equal([2,1], ref_code.id)
117
- end
118
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ # Testing the find action on composite ActiveRecords with two primary keys
4
+ class TestFind < ActiveSupport::TestCase
5
+ fixtures :capitols, :departments, :reference_types, :reference_codes, :suburbs
6
+
7
+ def test_find_first
8
+ ref_code = ReferenceCode.order('reference_type_id, reference_code').first
9
+ assert_kind_of(ReferenceCode, ref_code)
10
+ assert_equal([1,1], ref_code.id)
11
+ end
12
+
13
+ def test_find_last
14
+ ref_code = ReferenceCode.order('reference_type_id, reference_code').last
15
+ assert_kind_of(ReferenceCode, ref_code)
16
+ assert_equal([2,2], ref_code.id)
17
+ end
18
+
19
+ def test_find_one
20
+ ref_code = ReferenceCode.find([1,3])
21
+ assert_not_nil(ref_code)
22
+ assert_equal([1,3], ref_code.id)
23
+ end
24
+
25
+ def test_find_some
26
+ ref_codes = ReferenceCode.find([1,3], [2,1])
27
+ assert_kind_of(Array, ref_codes)
28
+ assert_equal(2, ref_codes.length)
29
+
30
+ ref_code = ref_codes[0]
31
+ assert_equal([1,3], ref_code.id)
32
+
33
+ ref_code = ref_codes[1]
34
+ assert_equal([2,1], ref_code.id)
35
+ end
36
+
37
+ def test_find_with_strings_as_composite_keys
38
+ capitol = Capitol.find(['The Netherlands', 'Amsterdam'])
39
+ assert_kind_of(Capitol, capitol)
40
+ assert_equal(['The Netherlands', 'Amsterdam'], capitol.id)
41
+ end
42
+
43
+ def test_find_each
44
+ room_assignments = []
45
+ RoomAssignment.find_each(:batch_size => 2) do |assignment|
46
+ room_assignments << assignment
47
+ end
48
+
49
+ assert_equal(RoomAssignment.count, room_assignments.uniq.length)
50
+ end
51
+
52
+ def test_find_each_with_scope
53
+ scoped_departments = Department.where("department_id <> 3")
54
+ scoped_departments.find_each(:batch_size => 2) do |department|
55
+ assert department.id != 3
56
+ end
57
+ end
58
+
59
+ def test_not_found
60
+ error = assert_raise(::ActiveRecord::RecordNotFound) do
61
+ ReferenceCode.find(['999', '999'])
62
+ end
63
+
64
+ connection = ActiveRecord::Base.connection
65
+ ref_type_quoted = "#{connection.quote_table_name('reference_codes')}.#{connection.quote_column_name('reference_type_id')}"
66
+ ref_code_quoted = "#{connection.quote_table_name('reference_codes')}.#{connection.quote_column_name('reference_code')}"
67
+
68
+ expected = "Couldn't find all ReferenceCodes with 'reference_type_id,reference_code': (999, 999) (found 0 results, but was looking for 1)"
69
+ assert_equal(with_quoted_identifiers(expected), error.message)
70
+ end
71
+
72
+ def test_find_last_suburb
73
+ suburb = Suburb.last
74
+ assert_equal([2,1], suburb.id)
75
+ end
76
+
77
+ def test_find_last_suburb_with_order
78
+ # Rails actually changes city_id DESC to city_id ASC
79
+ suburb = Suburb.order('suburbs.city_id DESC').last
80
+ assert_equal([1,1], suburb.id)
81
+ end
82
+
83
+ def test_find_in_batches
84
+ Department.find_in_batches do |batch|
85
+ assert_equal(Department.count, batch.size)
86
+ end
87
+ end
88
+
89
+ def test_expand
90
+ department = departments(:engineering)
91
+ employees = Employee.where(:department => department)
92
+ assert_equal(2, employees.count)
93
+ end
94
+
95
+ def test_find_one_with_params_id
96
+ params_id = ReferenceCode.find([1,3]).to_param
97
+ assert_equal params_id, "1,3"
98
+
99
+ ref_code = ReferenceCode.find(params_id)
100
+ assert_not_nil(ref_code)
101
+ assert_equal([1,3], ref_code.id)
102
+ end
103
+
104
+ def test_find_some_with_array_of_params_id
105
+ params_ids = ReferenceCode.find([1,3], [2,1]).map(&:to_param)
106
+ assert_equal ["1,3", "2,1"], params_ids
107
+
108
+ ref_codes = ReferenceCode.find(params_ids)
109
+ assert_kind_of(Array, ref_codes)
110
+ assert_equal(2, ref_codes.length)
111
+
112
+ ref_code = ref_codes[0]
113
+ assert_equal([1,3], ref_code.id)
114
+
115
+ ref_code = ref_codes[1]
116
+ assert_equal([2,1], ref_code.id)
117
+ end
118
+ end
@@ -1,113 +1,113 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestHabtm < ActiveSupport::TestCase
4
- fixtures :suburbs, :restaurants, :restaurants_suburbs, :products
5
-
6
- def test_has_and_belongs_to_many
7
- @restaurant = Restaurant.find([1,1])
8
- assert_equal 2, @restaurant.suburbs.size
9
-
10
- @restaurant = Restaurant.includes(:suburbs).find([1,1])
11
- assert_equal 2, @restaurant.suburbs.size
12
- end
13
-
14
- def test_include_cpk_both_sides
15
- # assuming the association was set up in the fixtures
16
- # file restaurants_suburbs.yml
17
- mcdonalds = restaurants(:mcdonalds)
18
- # check positive
19
- suburb = mcdonalds.suburbs[0]
20
- assert mcdonalds.suburbs.include?(suburb)
21
- # check negative
22
- suburb_with_no_mcdonalds = suburbs(:no_mcdonalds)
23
- assert !mcdonalds.suburbs.include?(suburb_with_no_mcdonalds)
24
- end
25
-
26
- def test_include_cpk_owner_side_only
27
- subway = restaurants(:subway_one)
28
- product = products(:first_product)
29
- subway.products << product
30
-
31
- # reload
32
- # test positive
33
- subway = restaurants(:subway_one)
34
- assert subway.products.include?(product)
35
-
36
- # test negative
37
- product_two = products(:second_product)
38
- assert !subway.products.include?(product_two)
39
- end
40
-
41
- def test_include_cpk_association_side_only
42
- product = products(:first_product)
43
- subway = restaurants(:subway_one)
44
- product.restaurants << subway
45
-
46
- # reload
47
- # test positive
48
- product = products(:first_product)
49
- assert product.restaurants.include?(subway)
50
-
51
- # test negative
52
- mcdonalds = restaurants(:mcdonalds)
53
- assert !product.restaurants.include?(mcdonalds)
54
- end
55
-
56
- def test_habtm_clear_cpk_both_sides
57
- @restaurant = restaurants(:mcdonalds)
58
- assert_equal 2, @restaurant.suburbs.size
59
- @restaurant.suburbs.clear
60
- assert_equal 0, @restaurant.suburbs.size
61
- end
62
-
63
- def test_habtm_clear_cpk_owner_side_only
64
- subway = restaurants(:subway_one)
65
- first_product = products(:first_product)
66
- second_product = products(:second_product)
67
- subway.products << first_product << second_product
68
- assert_equal 2, subway.products.size
69
- subway.products.clear
70
- # reload to force reload of associations
71
- subway = restaurants(:subway_one)
72
- assert_equal 0, subway.products.size
73
- end
74
-
75
- def test_habtm_clear_cpk_association_side_only
76
- product = products(:first_product)
77
- subway_one = restaurants(:subway_one)
78
- subway_two = restaurants(:subway_two)
79
- product.restaurants << subway_one << subway_two
80
- assert_equal 2, product.restaurants.size
81
- product.restaurants.clear
82
- # reload to force reload of associations
83
- product = products(:first_product)
84
- assert_equal 0, product.restaurants.size
85
- end
86
-
87
- # tests case reported in issue #39 where a bug resulted in
88
- # deletion of incorrect join table records because the owner's id
89
- # was assumed to be an array and is not in this case
90
- # and evaluates to a useable but incorrect value
91
- def test_habtm_clear_cpk_association_side_only_deletes_only_correct_records
92
- product_one = Product.find(1)
93
- product_three = Product.find(3)
94
- subway_one = restaurants(:subway_one)
95
- subway_two = restaurants(:subway_two)
96
- product_one.restaurants << subway_one << subway_two
97
- product_three.restaurants << subway_one << subway_two
98
- assert_equal 2, product_one.restaurants.size
99
- assert_equal 2, product_three.restaurants.size
100
-
101
- # if product_three's id is incorrectly assumed to be
102
- # an array it will be evaluated as 3[0], which is 1, which would
103
- # delete product_one's associations rather than product_three's
104
- product_three.restaurants.clear
105
-
106
- # reload to force reload of associations
107
- product_one = Product.find(1)
108
- assert_equal 2, product_one.restaurants.size
109
-
110
- product_three = Product.find(3)
111
- assert_equal 0, product_three.restaurants.size
112
- end
113
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestHabtm < ActiveSupport::TestCase
4
+ fixtures :suburbs, :restaurants, :restaurants_suburbs, :products
5
+
6
+ def test_has_and_belongs_to_many
7
+ @restaurant = Restaurant.find([1,1])
8
+ assert_equal 2, @restaurant.suburbs.size
9
+
10
+ @restaurant = Restaurant.includes(:suburbs).find([1,1])
11
+ assert_equal 2, @restaurant.suburbs.size
12
+ end
13
+
14
+ def test_include_cpk_both_sides
15
+ # assuming the association was set up in the fixtures
16
+ # file restaurants_suburbs.yml
17
+ mcdonalds = restaurants(:mcdonalds)
18
+ # check positive
19
+ suburb = mcdonalds.suburbs[0]
20
+ assert mcdonalds.suburbs.include?(suburb)
21
+ # check negative
22
+ suburb_with_no_mcdonalds = suburbs(:no_mcdonalds)
23
+ assert !mcdonalds.suburbs.include?(suburb_with_no_mcdonalds)
24
+ end
25
+
26
+ def test_include_cpk_owner_side_only
27
+ subway = restaurants(:subway_one)
28
+ product = products(:first_product)
29
+ subway.products << product
30
+
31
+ # reload
32
+ # test positive
33
+ subway = restaurants(:subway_one)
34
+ assert subway.products.include?(product)
35
+
36
+ # test negative
37
+ product_two = products(:second_product)
38
+ assert !subway.products.include?(product_two)
39
+ end
40
+
41
+ def test_include_cpk_association_side_only
42
+ product = products(:first_product)
43
+ subway = restaurants(:subway_one)
44
+ product.restaurants << subway
45
+
46
+ # reload
47
+ # test positive
48
+ product = products(:first_product)
49
+ assert product.restaurants.include?(subway)
50
+
51
+ # test negative
52
+ mcdonalds = restaurants(:mcdonalds)
53
+ assert !product.restaurants.include?(mcdonalds)
54
+ end
55
+
56
+ def test_habtm_clear_cpk_both_sides
57
+ @restaurant = restaurants(:mcdonalds)
58
+ assert_equal 2, @restaurant.suburbs.size
59
+ @restaurant.suburbs.clear
60
+ assert_equal 0, @restaurant.suburbs.size
61
+ end
62
+
63
+ def test_habtm_clear_cpk_owner_side_only
64
+ subway = restaurants(:subway_one)
65
+ first_product = products(:first_product)
66
+ second_product = products(:second_product)
67
+ subway.products << first_product << second_product
68
+ assert_equal 2, subway.products.size
69
+ subway.products.clear
70
+ # reload to force reload of associations
71
+ subway = restaurants(:subway_one)
72
+ assert_equal 0, subway.products.size
73
+ end
74
+
75
+ def test_habtm_clear_cpk_association_side_only
76
+ product = products(:first_product)
77
+ subway_one = restaurants(:subway_one)
78
+ subway_two = restaurants(:subway_two)
79
+ product.restaurants << subway_one << subway_two
80
+ assert_equal 2, product.restaurants.size
81
+ product.restaurants.clear
82
+ # reload to force reload of associations
83
+ product = products(:first_product)
84
+ assert_equal 0, product.restaurants.size
85
+ end
86
+
87
+ # tests case reported in issue #39 where a bug resulted in
88
+ # deletion of incorrect join table records because the owner's id
89
+ # was assumed to be an array and is not in this case
90
+ # and evaluates to a useable but incorrect value
91
+ def test_habtm_clear_cpk_association_side_only_deletes_only_correct_records
92
+ product_one = Product.find(1)
93
+ product_three = Product.find(3)
94
+ subway_one = restaurants(:subway_one)
95
+ subway_two = restaurants(:subway_two)
96
+ product_one.restaurants << subway_one << subway_two
97
+ product_three.restaurants << subway_one << subway_two
98
+ assert_equal 2, product_one.restaurants.size
99
+ assert_equal 2, product_three.restaurants.size
100
+
101
+ # if product_three's id is incorrectly assumed to be
102
+ # an array it will be evaluated as 3[0], which is 1, which would
103
+ # delete product_one's associations rather than product_three's
104
+ product_three.restaurants.clear
105
+
106
+ # reload to force reload of associations
107
+ product_one = Product.find(1)
108
+ assert_equal 2, product_one.restaurants.size
109
+
110
+ product_three = Product.find(3)
111
+ assert_equal 0, product_three.restaurants.size
112
+ end
113
+ end
@@ -1,26 +1,26 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestPolymorphic < ActiveSupport::TestCase
4
- fixtures :users, :employees, :comments, :hacks
5
-
6
- def test_polymorphic_has_many
7
- comments = Hack.find(7).comments
8
- assert_equal 7, comments[0].person_id
9
- end
10
-
11
- def test_polymorphic_has_one
12
- first_comment = Hack.find(7).first_comment
13
- assert_equal 7, first_comment.person_id
14
- end
15
-
16
- def test_has_many_through
17
- user = users(:santiago)
18
- article_names = user.articles.collect { |a| a.name }.sort
19
- assert_equal ['Article One', 'Article Two'], article_names
20
- end
21
-
22
- def test_polymorphic_has_many_through
23
- user = users(:santiago)
24
- assert_equal(['andrew'], user.hacks.collect { |a| a.name }.sort)
25
- end
26
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestPolymorphic < ActiveSupport::TestCase
4
+ fixtures :users, :employees, :comments, :hacks
5
+
6
+ def test_polymorphic_has_many
7
+ comments = Hack.find(7).comments
8
+ assert_equal 7, comments[0].person_id
9
+ end
10
+
11
+ def test_polymorphic_has_one
12
+ first_comment = Hack.find(7).first_comment
13
+ assert_equal 7, first_comment.person_id
14
+ end
15
+
16
+ def test_has_many_through
17
+ user = users(:santiago)
18
+ article_names = user.articles.collect { |a| a.name }.sort
19
+ assert_equal ['Article One', 'Article Two'], article_names
20
+ end
21
+
22
+ def test_polymorphic_has_many_through
23
+ user = users(:santiago)
24
+ assert_equal(['andrew'], user.hacks.collect { |a| a.name }.sort)
25
+ end
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: composite_primary_keys
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.14
4
+ version: 7.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Charlie Savage
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-04 00:00:00.000000000 Z
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord