composite_primary_keys 11.0.0.rc2 → 11.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +4 -4
  2. data/History.rdoc +774 -763
  3. data/Rakefile +37 -37
  4. data/lib/composite_primary_keys.rb +120 -122
  5. data/lib/composite_primary_keys/arel/sqlserver.rb +37 -37
  6. data/lib/composite_primary_keys/arel/to_sql.rb +32 -32
  7. data/lib/composite_primary_keys/associations/association.rb +23 -23
  8. data/lib/composite_primary_keys/associations/association_scope.rb +68 -68
  9. data/lib/composite_primary_keys/associations/collection_association.rb +31 -31
  10. data/lib/composite_primary_keys/associations/foreign_association.rb +15 -15
  11. data/lib/composite_primary_keys/associations/has_many_association.rb +34 -34
  12. data/lib/composite_primary_keys/associations/join_dependency.rb +102 -102
  13. data/lib/composite_primary_keys/associations/preloader/association.rb +117 -117
  14. data/lib/composite_primary_keys/attribute_methods.rb +9 -9
  15. data/lib/composite_primary_keys/attribute_methods/read.rb +27 -27
  16. data/lib/composite_primary_keys/attribute_methods/write.rb +44 -44
  17. data/lib/composite_primary_keys/autosave_association.rb +32 -32
  18. data/lib/composite_primary_keys/base.rb +130 -130
  19. data/lib/composite_primary_keys/composite_arrays.rb +51 -51
  20. data/lib/composite_primary_keys/composite_predicates.rb +69 -69
  21. data/lib/composite_primary_keys/composite_relation.rb +29 -29
  22. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  23. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +29 -29
  24. data/lib/composite_primary_keys/connection_adapters/postgresql/database_statements.rb +26 -26
  25. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +22 -22
  26. data/lib/composite_primary_keys/core.rb +54 -54
  27. data/lib/composite_primary_keys/counter_cache.rb +28 -28
  28. data/lib/composite_primary_keys/fixtures.rb +26 -26
  29. data/lib/composite_primary_keys/persistence.rb +120 -112
  30. data/lib/composite_primary_keys/reflection.rb +20 -20
  31. data/lib/composite_primary_keys/relation.rb +88 -88
  32. data/lib/composite_primary_keys/relation/calculations.rb +79 -79
  33. data/lib/composite_primary_keys/relation/finder_methods.rb +229 -229
  34. data/lib/composite_primary_keys/relation/predicate_builder/association_query_value.rb +20 -20
  35. data/lib/composite_primary_keys/relation/query_methods.rb +42 -42
  36. data/lib/composite_primary_keys/relation/where_clause.rb +24 -24
  37. data/lib/composite_primary_keys/sanitization.rb +42 -42
  38. data/lib/composite_primary_keys/validations/uniqueness.rb +32 -32
  39. data/lib/composite_primary_keys/version.rb +8 -8
  40. data/scripts/console.rb +48 -48
  41. data/scripts/txt2html +76 -76
  42. data/scripts/txt2js +65 -65
  43. data/tasks/databases/mysql.rake +40 -40
  44. data/tasks/databases/oracle.rake +41 -41
  45. data/tasks/databases/postgresql.rake +38 -38
  46. data/tasks/databases/sqlite.rake +25 -25
  47. data/tasks/databases/sqlserver.rake +43 -43
  48. data/tasks/website.rake +18 -18
  49. data/test/README_tests.rdoc +56 -56
  50. data/test/abstract_unit.rb +120 -120
  51. data/test/connections/connection_spec.rb +27 -27
  52. data/test/connections/databases.ci.yml +16 -16
  53. data/test/connections/databases.example.yml +40 -40
  54. data/test/connections/databases.yml +40 -40
  55. data/test/db_test.rb +52 -52
  56. data/test/fixtures/article.rb +6 -6
  57. data/test/fixtures/articles.yml +6 -6
  58. data/test/fixtures/capitol.rb +3 -3
  59. data/test/fixtures/capitols.yml +16 -16
  60. data/test/fixtures/comment.rb +7 -7
  61. data/test/fixtures/comments.yml +15 -15
  62. data/test/fixtures/db_definitions/db2-create-tables.sql +126 -126
  63. data/test/fixtures/db_definitions/db2-drop-tables.sql +18 -18
  64. data/test/fixtures/db_definitions/mysql.sql +226 -226
  65. data/test/fixtures/db_definitions/oracle.drop.sql +49 -49
  66. data/test/fixtures/db_definitions/oracle.sql +246 -246
  67. data/test/fixtures/db_definitions/postgresql.sql +229 -229
  68. data/test/fixtures/db_definitions/sqlite.sql +214 -214
  69. data/test/fixtures/db_definitions/sqlserver.sql +211 -211
  70. data/test/fixtures/department.rb +5 -5
  71. data/test/fixtures/departments.yml +15 -15
  72. data/test/fixtures/dorm.rb +2 -2
  73. data/test/fixtures/dorms.yml +4 -4
  74. data/test/fixtures/employee.rb +10 -10
  75. data/test/fixtures/employees.yml +18 -18
  76. data/test/fixtures/group.rb +2 -2
  77. data/test/fixtures/groups.yml +6 -6
  78. data/test/fixtures/hack.rb +4 -4
  79. data/test/fixtures/hacks.yml +2 -2
  80. data/test/fixtures/membership.rb +6 -6
  81. data/test/fixtures/membership_status.rb +2 -2
  82. data/test/fixtures/membership_statuses.yml +16 -16
  83. data/test/fixtures/memberships.yml +10 -10
  84. data/test/fixtures/pk_called_id.rb +5 -5
  85. data/test/fixtures/pk_called_ids.yml +11 -11
  86. data/test/fixtures/product.rb +9 -9
  87. data/test/fixtures/product_tariff.rb +5 -5
  88. data/test/fixtures/product_tariffs.yml +14 -14
  89. data/test/fixtures/products.yml +11 -11
  90. data/test/fixtures/reading.rb +4 -4
  91. data/test/fixtures/readings.yml +10 -10
  92. data/test/fixtures/reference_code.rb +7 -7
  93. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  94. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  95. data/test/fixtures/reference_codes.yml +28 -28
  96. data/test/fixtures/reference_type.rb +12 -12
  97. data/test/fixtures/reference_types.yml +9 -9
  98. data/test/fixtures/restaurant.rb +9 -9
  99. data/test/fixtures/restaurants.yml +14 -14
  100. data/test/fixtures/restaurants_suburb.rb +2 -2
  101. data/test/fixtures/restaurants_suburbs.yml +10 -10
  102. data/test/fixtures/room.rb +11 -11
  103. data/test/fixtures/room_assignment.rb +13 -13
  104. data/test/fixtures/room_assignments.yml +24 -24
  105. data/test/fixtures/room_attribute.rb +2 -2
  106. data/test/fixtures/room_attribute_assignment.rb +4 -4
  107. data/test/fixtures/room_attribute_assignments.yml +4 -4
  108. data/test/fixtures/room_attributes.yml +2 -2
  109. data/test/fixtures/rooms.yml +12 -12
  110. data/test/fixtures/salary.rb +5 -5
  111. data/test/fixtures/seat.rb +5 -5
  112. data/test/fixtures/seats.yml +8 -8
  113. data/test/fixtures/street.rb +2 -2
  114. data/test/fixtures/streets.yml +16 -16
  115. data/test/fixtures/student.rb +3 -3
  116. data/test/fixtures/students.yml +15 -15
  117. data/test/fixtures/suburb.rb +5 -5
  118. data/test/fixtures/suburbs.yml +14 -14
  119. data/test/fixtures/tariff.rb +5 -5
  120. data/test/fixtures/tariffs.yml +14 -14
  121. data/test/fixtures/topic.rb +5 -5
  122. data/test/fixtures/topic_source.rb +6 -6
  123. data/test/fixtures/topic_sources.yml +3 -3
  124. data/test/fixtures/topics.yml +8 -8
  125. data/test/fixtures/user.rb +10 -10
  126. data/test/fixtures/users.yml +10 -10
  127. data/test/plugins/pagination.rb +405 -405
  128. data/test/plugins/pagination_helper.rb +135 -135
  129. data/test/setup.rb +50 -50
  130. data/test/test_aliases.rb +18 -18
  131. data/test/test_associations.rb +345 -345
  132. data/test/test_attribute_methods.rb +63 -63
  133. data/test/test_attributes.rb +60 -60
  134. data/test/test_calculations.rb +42 -42
  135. data/test/test_callbacks.rb +99 -99
  136. data/test/test_composite_arrays.rb +24 -24
  137. data/test/test_counter_cache.rb +30 -30
  138. data/test/test_create.rb +164 -164
  139. data/test/test_delete.rb +167 -167
  140. data/test/test_dumpable.rb +15 -15
  141. data/test/test_dup.rb +37 -37
  142. data/test/test_enum.rb +21 -21
  143. data/test/test_equal.rb +26 -26
  144. data/test/test_exists.rb +39 -39
  145. data/test/test_find.rb +137 -137
  146. data/test/test_habtm.rb +141 -141
  147. data/test/test_ids.rb +113 -113
  148. data/test/test_miscellaneous.rb +32 -32
  149. data/test/test_nested_attributes.rb +124 -124
  150. data/test/test_optimistic.rb +18 -18
  151. data/test/test_pagination.rb +35 -35
  152. data/test/test_polymorphic.rb +27 -27
  153. data/test/test_predicates.rb +59 -59
  154. data/test/test_preload.rb +94 -94
  155. data/test/test_santiago.rb +23 -23
  156. data/test/test_suite.rb +34 -34
  157. data/test/test_touch.rb +23 -23
  158. data/test/test_tutorial_example.rb +25 -25
  159. data/test/test_update.rb +76 -76
  160. data/test/test_validations.rb +13 -13
  161. metadata +32 -33
  162. data/lib/composite_primary_keys/locking/optimistic.rb +0 -56
@@ -1,15 +1,15 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestDumpable < ActiveSupport::TestCase
4
- fixtures :articles, :readings, :users
5
-
6
- def test_marshal_with_simple_preload
7
- articles = Article.preload(:readings).where(id: 1).to_a
8
- assert_equal(Marshal.load(Marshal.dump(articles)), articles)
9
- end
10
-
11
- def test_marshal_with_comples_preload
12
- articles = Article.preload({ readings: :user }).where(id: 1).to_a
13
- assert_equal(Marshal.load(Marshal.dump(articles)), articles)
14
- end
15
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestDumpable < ActiveSupport::TestCase
4
+ fixtures :articles, :readings, :users
5
+
6
+ def test_marshal_with_simple_preload
7
+ articles = Article.preload(:readings).where(id: 1).to_a
8
+ assert_equal(Marshal.load(Marshal.dump(articles)), articles)
9
+ end
10
+
11
+ def test_marshal_with_comples_preload
12
+ articles = Article.preload({ readings: :user }).where(id: 1).to_a
13
+ assert_equal(Marshal.load(Marshal.dump(articles)), articles)
14
+ end
15
+ end
@@ -1,38 +1,38 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestClone < ActiveSupport::TestCase
4
- fixtures :reference_types, :reference_codes
5
-
6
- CLASSES = {
7
- :single => {
8
- :class => ReferenceType,
9
- :primary_keys => :reference_type_id,
10
- },
11
- :dual => {
12
- :class => ReferenceCode,
13
- :primary_keys => [:reference_type_id, :reference_code],
14
- },
15
- }
16
-
17
- def setup
18
- self.class.classes = CLASSES
19
- end
20
-
21
- def test_dup
22
- testing_with do
23
- clone = @first.dup
24
-
25
- remove_keys = Array(@klass.primary_key).map(&:to_s)
26
- remove_keys << Array(@klass.primary_key) # Rails 4 adds the PK to the attributes, so we want to remove it as well
27
- assert_equal(@first.attributes.except(*remove_keys), clone.attributes.except(*remove_keys))
28
-
29
- if composite?
30
- @klass.primary_key.each do |key|
31
- assert_nil(clone[key], "Primary key '#{key}' should be nil")
32
- end
33
- else
34
- assert_nil(clone[@klass.primary_key], "Sole primary key should be nil")
35
- end
36
- end
37
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestClone < ActiveSupport::TestCase
4
+ fixtures :reference_types, :reference_codes
5
+
6
+ CLASSES = {
7
+ :single => {
8
+ :class => ReferenceType,
9
+ :primary_keys => :reference_type_id,
10
+ },
11
+ :dual => {
12
+ :class => ReferenceCode,
13
+ :primary_keys => [:reference_type_id, :reference_code],
14
+ },
15
+ }
16
+
17
+ def setup
18
+ self.class.classes = CLASSES
19
+ end
20
+
21
+ def test_dup
22
+ testing_with do
23
+ clone = @first.dup
24
+
25
+ remove_keys = Array(@klass.primary_key).map(&:to_s)
26
+ remove_keys << Array(@klass.primary_key) # Rails 4 adds the PK to the attributes, so we want to remove it as well
27
+ assert_equal(@first.attributes.except(*remove_keys), clone.attributes.except(*remove_keys))
28
+
29
+ if composite?
30
+ @klass.primary_key.each do |key|
31
+ assert_nil(clone[key], "Primary key '#{key}' should be nil")
32
+ end
33
+ else
34
+ assert_nil(clone[@klass.primary_key], "Sole primary key should be nil")
35
+ end
36
+ end
37
+ end
38
38
  end
@@ -1,21 +1,21 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestEnum < ActiveSupport::TestCase
4
- fixtures :comments
5
-
6
- def test_enum_was
7
- comment = Comment.first
8
- assert_nil comment.shown
9
- assert_equal({}, comment.changed_attributes)
10
- comment.shown = :true
11
- assert_equal({ "shown" => nil }, comment.changed_attributes)
12
- assert_equal 'true', comment.shown
13
- assert_nil comment.shown_was
14
-
15
- comment.save
16
-
17
- comment.shown = :false
18
- assert_equal 'false', comment.shown
19
- assert_equal 'true', comment.shown_was
20
- end
21
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestEnum < ActiveSupport::TestCase
4
+ fixtures :comments
5
+
6
+ def test_enum_was
7
+ comment = Comment.first
8
+ assert_nil comment.shown
9
+ assert_equal({}, comment.changed_attributes)
10
+ comment.shown = :true
11
+ assert_equal({ "shown" => nil }, comment.changed_attributes)
12
+ assert_equal 'true', comment.shown
13
+ assert_nil comment.shown_was
14
+
15
+ comment.save
16
+
17
+ comment.shown = :false
18
+ assert_equal 'false', comment.shown
19
+ assert_equal 'true', comment.shown_was
20
+ end
21
+ 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,40 +1,40 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestExists < ActiveSupport::TestCase
4
- fixtures :articles, :departments, :capitols
5
-
6
- def test_id
7
- assert(Article.exists?(1))
8
- assert(!Article.exists?(-1))
9
- end
10
-
11
- def test_array
12
- assert(Article.exists?(['name = ?', 'Article One']))
13
- assert(!Article.exists?(['name = ?', 'Article -1']))
14
- end
15
-
16
- def test_hash
17
- assert(Article.exists?('name' => 'Article One'))
18
- assert(!Article.exists?('name' => 'Article -1'))
19
- end
20
-
21
- def test_cpk_id
22
- assert(Department.exists?(CompositePrimaryKeys::CompositeKeys.new([1,1])))
23
- assert(!Department.exists?(CompositePrimaryKeys::CompositeKeys.new([1,-1])))
24
- end
25
-
26
- def test_cpk_array_id
27
- assert(Department.exists?([1,1]))
28
- assert(!Department.exists?([1,-1]))
29
- end
30
-
31
- def test_cpk_array_condition
32
- assert(Department.exists?(['department_id = ? and location_id = ?', 1, 1]))
33
- assert(!Department.exists?(['department_id = ? and location_id = ?', 1, -1]))
34
- end
35
-
36
- def test_cpk_array_string_id
37
- assert(Capitol.exists?(['The Netherlands', 'Amsterdam']))
38
- assert(!Capitol.exists?(['The Netherlands', 'Paris']))
39
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestExists < ActiveSupport::TestCase
4
+ fixtures :articles, :departments, :capitols
5
+
6
+ def test_id
7
+ assert(Article.exists?(1))
8
+ assert(!Article.exists?(-1))
9
+ end
10
+
11
+ def test_array
12
+ assert(Article.exists?(['name = ?', 'Article One']))
13
+ assert(!Article.exists?(['name = ?', 'Article -1']))
14
+ end
15
+
16
+ def test_hash
17
+ assert(Article.exists?('name' => 'Article One'))
18
+ assert(!Article.exists?('name' => 'Article -1'))
19
+ end
20
+
21
+ def test_cpk_id
22
+ assert(Department.exists?(CompositePrimaryKeys::CompositeKeys.new([1,1])))
23
+ assert(!Department.exists?(CompositePrimaryKeys::CompositeKeys.new([1,-1])))
24
+ end
25
+
26
+ def test_cpk_array_id
27
+ assert(Department.exists?([1,1]))
28
+ assert(!Department.exists?([1,-1]))
29
+ end
30
+
31
+ def test_cpk_array_condition
32
+ assert(Department.exists?(['department_id = ? and location_id = ?', 1, 1]))
33
+ assert(!Department.exists?(['department_id = ? and location_id = ?', 1, -1]))
34
+ end
35
+
36
+ def test_cpk_array_string_id
37
+ assert(Capitol.exists?(['The Netherlands', 'Amsterdam']))
38
+ assert(!Capitol.exists?(['The Netherlands', 'Paris']))
39
+ end
40
40
  end
@@ -1,137 +1,137 @@
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,
6
- :suburbs, :employees
7
-
8
- def test_find_first
9
- ref_code = ReferenceCode.order('reference_type_id, reference_code').first
10
- assert_kind_of(ReferenceCode, ref_code)
11
- assert_equal([1,1], ref_code.id)
12
- end
13
-
14
- def test_find_last
15
- ref_code = ReferenceCode.order('reference_type_id, reference_code').last
16
- assert_kind_of(ReferenceCode, ref_code)
17
- assert_equal([2,2], ref_code.id)
18
- end
19
-
20
- def test_find_one
21
- ref_code = ReferenceCode.find([1,3])
22
- assert_not_nil(ref_code)
23
- assert_equal([1,3], ref_code.id)
24
- end
25
-
26
- def test_find_some
27
- ref_codes = ReferenceCode.find([1,3], [2,1])
28
- assert_kind_of(Array, ref_codes)
29
- assert_equal(2, ref_codes.length)
30
-
31
- ref_code = ref_codes[0]
32
- assert_equal([1,3], ref_code.id)
33
-
34
- ref_code = ref_codes[1]
35
- assert_equal([2,1], ref_code.id)
36
- end
37
-
38
- def test_find_with_strings_as_composite_keys
39
- capitol = Capitol.find(['The Netherlands', 'Amsterdam'])
40
- assert_kind_of(Capitol, capitol)
41
- assert_equal(['The Netherlands', 'Amsterdam'], capitol.id)
42
- end
43
-
44
- def test_find_each
45
- room_assignments = []
46
- RoomAssignment.find_each(:batch_size => 2) do |assignment|
47
- room_assignments << assignment
48
- end
49
-
50
- assert_equal(RoomAssignment.count, room_assignments.uniq.length)
51
- end
52
-
53
- def test_find_each_with_scope
54
- scoped_departments = Department.where("department_id <> 3")
55
- scoped_departments.find_each(:batch_size => 2) do |department|
56
- assert department.id != 3
57
- end
58
- end
59
-
60
- def test_not_found
61
- error = assert_raise(::ActiveRecord::RecordNotFound) do
62
- ReferenceCode.find(['999', '999'])
63
- end
64
-
65
- expected = "Couldn't find all ReferenceCodes with 'reference_type_id,reference_code': (999, 999) (found 0 results, but was looking for 1)."
66
- assert_equal(with_quoted_identifiers(expected), error.message)
67
- end
68
-
69
- def test_find_last_suburb
70
- suburb = Suburb.last
71
- assert_equal([2,1], suburb.id)
72
- end
73
-
74
- def test_find_last_suburb_with_order
75
- # Rails actually changes city_id DESC to city_id ASC
76
- suburb = Suburb.order('suburbs.city_id DESC').last
77
- assert_equal([1,1], suburb.id)
78
- end
79
-
80
- def test_find_in_batches
81
- Department.find_in_batches do |batch|
82
- assert_equal(Department.count, batch.size)
83
- end
84
- end
85
-
86
- def test_in_batches_of_1
87
- num_found = 0
88
- Department.in_batches(of: 1) do |batch|
89
- batch.each do |dept|
90
- num_found += 1
91
- end
92
- end
93
- assert_equal(Department.count, num_found)
94
- end
95
-
96
- def test_find_by_one_association
97
- department = departments(:engineering)
98
- employees = Employee.where(:department => department)
99
- assert_equal(2, employees.to_a.count)
100
- end
101
-
102
- def test_find_by_all_associations
103
- departments = Department.all
104
- employees = Employee.where(:department => departments)
105
- assert_equal(4, employees.to_a.count)
106
- end
107
-
108
- def test_expand_all
109
- departments = Department.all
110
- employees = Employee.where(:department => departments)
111
- assert_equal(4, employees.count)
112
- end
113
-
114
- def test_find_one_with_params_id
115
- params_id = ReferenceCode.find([1,3]).to_param
116
- assert_equal params_id, "1,3"
117
-
118
- ref_code = ReferenceCode.find(params_id)
119
- assert_not_nil(ref_code)
120
- assert_equal([1,3], ref_code.id)
121
- end
122
-
123
- def test_find_some_with_array_of_params_id
124
- params_ids = ReferenceCode.find([1,3], [2,1]).map(&:to_param)
125
- assert_equal ["1,3", "2,1"], params_ids
126
-
127
- ref_codes = ReferenceCode.find(params_ids)
128
- assert_kind_of(Array, ref_codes)
129
- assert_equal(2, ref_codes.length)
130
-
131
- ref_code = ref_codes[0]
132
- assert_equal([1,3], ref_code.id)
133
-
134
- ref_code = ref_codes[1]
135
- assert_equal([2,1], ref_code.id)
136
- end
137
- 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,
6
+ :suburbs, :employees
7
+
8
+ def test_find_first
9
+ ref_code = ReferenceCode.order('reference_type_id, reference_code').first
10
+ assert_kind_of(ReferenceCode, ref_code)
11
+ assert_equal([1,1], ref_code.id)
12
+ end
13
+
14
+ def test_find_last
15
+ ref_code = ReferenceCode.order('reference_type_id, reference_code').last
16
+ assert_kind_of(ReferenceCode, ref_code)
17
+ assert_equal([2,2], ref_code.id)
18
+ end
19
+
20
+ def test_find_one
21
+ ref_code = ReferenceCode.find([1,3])
22
+ assert_not_nil(ref_code)
23
+ assert_equal([1,3], ref_code.id)
24
+ end
25
+
26
+ def test_find_some
27
+ ref_codes = ReferenceCode.find([1,3], [2,1])
28
+ assert_kind_of(Array, ref_codes)
29
+ assert_equal(2, ref_codes.length)
30
+
31
+ ref_code = ref_codes[0]
32
+ assert_equal([1,3], ref_code.id)
33
+
34
+ ref_code = ref_codes[1]
35
+ assert_equal([2,1], ref_code.id)
36
+ end
37
+
38
+ def test_find_with_strings_as_composite_keys
39
+ capitol = Capitol.find(['The Netherlands', 'Amsterdam'])
40
+ assert_kind_of(Capitol, capitol)
41
+ assert_equal(['The Netherlands', 'Amsterdam'], capitol.id)
42
+ end
43
+
44
+ def test_find_each
45
+ room_assignments = []
46
+ RoomAssignment.find_each(:batch_size => 2) do |assignment|
47
+ room_assignments << assignment
48
+ end
49
+
50
+ assert_equal(RoomAssignment.count, room_assignments.uniq.length)
51
+ end
52
+
53
+ def test_find_each_with_scope
54
+ scoped_departments = Department.where("department_id <> 3")
55
+ scoped_departments.find_each(:batch_size => 2) do |department|
56
+ assert department.id != 3
57
+ end
58
+ end
59
+
60
+ def test_not_found
61
+ error = assert_raise(::ActiveRecord::RecordNotFound) do
62
+ ReferenceCode.find(['999', '999'])
63
+ end
64
+
65
+ expected = "Couldn't find all ReferenceCodes with 'reference_type_id,reference_code': (999, 999) (found 0 results, but was looking for 1)."
66
+ assert_equal(with_quoted_identifiers(expected), error.message)
67
+ end
68
+
69
+ def test_find_last_suburb
70
+ suburb = Suburb.last
71
+ assert_equal([2,1], suburb.id)
72
+ end
73
+
74
+ def test_find_last_suburb_with_order
75
+ # Rails actually changes city_id DESC to city_id ASC
76
+ suburb = Suburb.order('suburbs.city_id DESC').last
77
+ assert_equal([1,1], suburb.id)
78
+ end
79
+
80
+ def test_find_in_batches
81
+ Department.find_in_batches do |batch|
82
+ assert_equal(Department.count, batch.size)
83
+ end
84
+ end
85
+
86
+ def test_in_batches_of_1
87
+ num_found = 0
88
+ Department.in_batches(of: 1) do |batch|
89
+ batch.each do |dept|
90
+ num_found += 1
91
+ end
92
+ end
93
+ assert_equal(Department.count, num_found)
94
+ end
95
+
96
+ def test_find_by_one_association
97
+ department = departments(:engineering)
98
+ employees = Employee.where(:department => department)
99
+ assert_equal(2, employees.to_a.count)
100
+ end
101
+
102
+ def test_find_by_all_associations
103
+ departments = Department.all
104
+ employees = Employee.where(:department => departments)
105
+ assert_equal(4, employees.to_a.count)
106
+ end
107
+
108
+ def test_expand_all
109
+ departments = Department.all
110
+ employees = Employee.where(:department => departments)
111
+ assert_equal(4, employees.count)
112
+ end
113
+
114
+ def test_find_one_with_params_id
115
+ params_id = ReferenceCode.find([1,3]).to_param
116
+ assert_equal params_id, "1,3"
117
+
118
+ ref_code = ReferenceCode.find(params_id)
119
+ assert_not_nil(ref_code)
120
+ assert_equal([1,3], ref_code.id)
121
+ end
122
+
123
+ def test_find_some_with_array_of_params_id
124
+ params_ids = ReferenceCode.find([1,3], [2,1]).map(&:to_param)
125
+ assert_equal ["1,3", "2,1"], params_ids
126
+
127
+ ref_codes = ReferenceCode.find(params_ids)
128
+ assert_kind_of(Array, ref_codes)
129
+ assert_equal(2, ref_codes.length)
130
+
131
+ ref_code = ref_codes[0]
132
+ assert_equal([1,3], ref_code.id)
133
+
134
+ ref_code = ref_codes[1]
135
+ assert_equal([2,1], ref_code.id)
136
+ end
137
+ end