composite_primary_keys 8.1.8 → 9.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. checksums.yaml +5 -5
  2. data/History.rdoc +3 -25
  3. data/README.rdoc +1 -0
  4. data/README_DB2.rdoc +33 -33
  5. data/Rakefile +34 -34
  6. data/lib/composite_primary_keys.rb +4 -11
  7. data/lib/composite_primary_keys/associations/association.rb +14 -12
  8. data/lib/composite_primary_keys/associations/association_scope.rb +27 -54
  9. data/lib/composite_primary_keys/associations/collection_association.rb +22 -8
  10. data/lib/composite_primary_keys/associations/has_many_association.rb +16 -54
  11. data/lib/composite_primary_keys/associations/has_many_through_association.rb +58 -58
  12. data/lib/composite_primary_keys/associations/join_dependency.rb +74 -56
  13. data/lib/composite_primary_keys/associations/join_dependency/join_association.rb +13 -11
  14. data/lib/composite_primary_keys/associations/preloader/association.rb +75 -72
  15. data/lib/composite_primary_keys/associations/singular_association.rb +8 -12
  16. data/lib/composite_primary_keys/attribute_methods.rb +6 -4
  17. data/lib/composite_primary_keys/attribute_methods/primary_key.rb +13 -11
  18. data/lib/composite_primary_keys/attribute_methods/read.rb +16 -15
  19. data/lib/composite_primary_keys/attribute_methods/write.rb +21 -19
  20. data/lib/composite_primary_keys/attribute_set/builder.rb +13 -11
  21. data/lib/composite_primary_keys/base.rb +5 -69
  22. data/lib/composite_primary_keys/composite_arrays.rb +8 -51
  23. data/lib/composite_primary_keys/composite_predicates.rb +7 -16
  24. data/lib/composite_primary_keys/connection_adapters/abstract_adapter.rb +10 -10
  25. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +42 -11
  26. data/lib/composite_primary_keys/core.rb +46 -45
  27. data/lib/composite_primary_keys/dirty.rb +19 -19
  28. data/lib/composite_primary_keys/fixtures.rb +19 -17
  29. data/lib/composite_primary_keys/locking/optimistic.rb +48 -44
  30. data/lib/composite_primary_keys/nested_attributes.rb +64 -53
  31. data/lib/composite_primary_keys/persistence.rb +49 -41
  32. data/lib/composite_primary_keys/relation.rb +22 -47
  33. data/lib/composite_primary_keys/relation/batches.rb +33 -30
  34. data/lib/composite_primary_keys/relation/calculations.rb +3 -7
  35. data/lib/composite_primary_keys/relation/finder_methods.rb +123 -56
  36. data/lib/composite_primary_keys/relation/predicate_builder.rb +18 -29
  37. data/lib/composite_primary_keys/relation/where_clause.rb +33 -0
  38. data/lib/composite_primary_keys/sanitization.rb +45 -38
  39. data/lib/composite_primary_keys/validations/uniqueness.rb +37 -37
  40. data/lib/composite_primary_keys/version.rb +4 -4
  41. data/scripts/console.rb +48 -48
  42. data/scripts/txt2html +76 -76
  43. data/scripts/txt2js +65 -65
  44. data/tasks/databases/mysql.rake +42 -42
  45. data/tasks/databases/postgresql.rake +47 -47
  46. data/tasks/databases/sqlite3.rake +27 -27
  47. data/tasks/website.rake +18 -18
  48. data/test/README_tests.rdoc +56 -56
  49. data/test/abstract_unit.rb +10 -9
  50. data/test/connections/connection_spec.rb +18 -18
  51. data/test/connections/databases.yml +9 -39
  52. data/test/connections/native_ibm_db/connection.rb +18 -18
  53. data/test/connections/native_mysql/connection.rb +17 -17
  54. data/test/connections/native_postgresql/connection.rb +12 -12
  55. data/test/connections/native_sqlite3/connection.rb +9 -9
  56. data/test/db_test.rb +52 -52
  57. data/test/fixtures/article.rb +5 -5
  58. data/test/fixtures/articles.yml +6 -6
  59. data/test/fixtures/capitol.rb +3 -3
  60. data/test/fixtures/capitols.yml +16 -16
  61. data/test/fixtures/comments.yml +15 -15
  62. data/test/fixtures/db_definitions/mysql.sql +2 -12
  63. data/test/fixtures/db_definitions/oracle.sql +1 -2
  64. data/test/fixtures/db_definitions/postgresql.sql +0 -10
  65. data/test/fixtures/db_definitions/sqlite.sql +0 -9
  66. data/test/fixtures/db_definitions/sqlserver.sql +1 -2
  67. data/test/fixtures/department.rb +5 -5
  68. data/test/fixtures/departments.yml +15 -15
  69. data/test/fixtures/dorms.yml +4 -4
  70. data/test/fixtures/employee.rb +1 -2
  71. data/test/fixtures/employees.yml +19 -23
  72. data/test/fixtures/group.rb +2 -2
  73. data/test/fixtures/groups.yml +6 -6
  74. data/test/fixtures/hack.rb +4 -4
  75. data/test/fixtures/hacks.yml +2 -2
  76. data/test/fixtures/membership_status.rb +2 -2
  77. data/test/fixtures/product.rb +9 -9
  78. data/test/fixtures/product_tariff.rb +5 -5
  79. data/test/fixtures/products.yml +11 -11
  80. data/test/fixtures/reading.rb +4 -4
  81. data/test/fixtures/readings.yml +10 -10
  82. data/test/fixtures/reference_code_using_composite_key_alias.rb +8 -8
  83. data/test/fixtures/reference_code_using_simple_key_alias.rb +8 -8
  84. data/test/fixtures/reference_codes.yml +28 -28
  85. data/test/fixtures/reference_type.rb +1 -1
  86. data/test/fixtures/reference_types.yml +9 -9
  87. data/test/fixtures/restaurant.rb +9 -9
  88. data/test/fixtures/restaurants.yml +14 -14
  89. data/test/fixtures/restaurants_suburbs.yml +10 -10
  90. data/test/fixtures/room.rb +11 -11
  91. data/test/fixtures/room_assignment.rb +13 -13
  92. data/test/fixtures/room_assignments.yml +24 -24
  93. data/test/fixtures/room_attribute.rb +2 -2
  94. data/test/fixtures/room_attribute_assignment.rb +4 -4
  95. data/test/fixtures/room_attribute_assignments.yml +4 -4
  96. data/test/fixtures/room_attributes.yml +2 -2
  97. data/test/fixtures/rooms.yml +12 -12
  98. data/test/fixtures/seat.rb +5 -5
  99. data/test/fixtures/seats.yml +8 -8
  100. data/test/fixtures/street.rb +2 -2
  101. data/test/fixtures/streets.yml +16 -16
  102. data/test/fixtures/student.rb +3 -3
  103. data/test/fixtures/students.yml +15 -15
  104. data/test/fixtures/suburbs.yml +14 -14
  105. data/test/fixtures/tariff.rb +5 -5
  106. data/test/fixtures/tariffs.yml +14 -14
  107. data/test/fixtures/user.rb +0 -1
  108. data/test/plugins/pagination.rb +405 -405
  109. data/test/plugins/pagination_helper.rb +135 -135
  110. data/test/setup.rb +50 -50
  111. data/test/test_aliases.rb +18 -18
  112. data/test/test_associations.rb +7 -18
  113. data/test/test_composite_arrays.rb +24 -38
  114. data/test/test_counter_cache.rb +30 -30
  115. data/test/test_create.rb +5 -5
  116. data/test/test_delete_all.rb +7 -13
  117. data/test/test_dup.rb +37 -37
  118. data/test/test_exists.rb +39 -39
  119. data/test/test_find.rb +16 -12
  120. data/test/test_habtm.rb +26 -2
  121. data/test/test_ids.rb +109 -116
  122. data/test/test_miscellaneous.rb +32 -32
  123. data/test/test_pagination.rb +35 -35
  124. data/test/test_polymorphic.rb +0 -7
  125. data/test/test_predicates.rb +9 -28
  126. data/test/test_update.rb +3 -5
  127. data/test/test_validations.rb +13 -13
  128. metadata +24 -32
  129. data/lib/composite_primary_keys/arel/visitors/to_sql.rb +0 -36
  130. data/lib/composite_primary_keys/attribute_methods/dirty.rb +0 -29
  131. data/lib/composite_primary_keys/autosave_association.rb +0 -67
  132. data/lib/composite_primary_keys/connection_adapters/abstract_mysql_adapter.rb +0 -23
  133. data/test/fixtures/pk_called_id.rb +0 -5
  134. data/test/fixtures/pk_called_ids.yml +0 -11
  135. data/test/test_find_in_batches.rb +0 -30
  136. data/test/test_update_all.rb +0 -17
@@ -1,31 +1,31 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class TestCalculations < ActiveSupport::TestCase
4
- fixtures :tariffs
5
-
6
- def test_update_counter
7
- tariff = tariffs(:flat)
8
- assert_equal(50, tariff.amount)
9
- Tariff.update_counters(tariff.id, :amount => 1)
10
- tariff.reload
11
- assert_equal(51, tariff.amount)
12
- end
13
-
14
- def test_increment_counter
15
- tariff = tariffs(:flat)
16
- assert_equal(50, tariff.amount)
17
- Tariff.increment_counter(:amount, tariff.id)
18
-
19
- tariff.reload
20
- assert_equal(51, tariff.amount)
21
- end
22
-
23
- def test_decrement_counter
24
- tariff = tariffs(:flat)
25
- assert_equal(50, tariff.amount)
26
- Tariff.decrement_counter(:amount, tariff.id)
27
-
28
- tariff.reload
29
- assert_equal(49, tariff.amount)
30
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class TestCalculations < ActiveSupport::TestCase
4
+ fixtures :tariffs
5
+
6
+ def test_update_counter
7
+ tariff = tariffs(:flat)
8
+ assert_equal(50, tariff.amount)
9
+ Tariff.update_counters(tariff.id, :amount => 1)
10
+ tariff.reload
11
+ assert_equal(51, tariff.amount)
12
+ end
13
+
14
+ def test_increment_counter
15
+ tariff = tariffs(:flat)
16
+ assert_equal(50, tariff.amount)
17
+ Tariff.increment_counter(:amount, tariff.id)
18
+
19
+ tariff.reload
20
+ assert_equal(51, tariff.amount)
21
+ end
22
+
23
+ def test_decrement_counter
24
+ tariff = tariffs(:flat)
25
+ assert_equal(50, tariff.amount)
26
+ Tariff.decrement_counter(:amount, tariff.id)
27
+
28
+ tariff.reload
29
+ assert_equal(49, tariff.amount)
30
+ end
31
31
  end
@@ -2,7 +2,7 @@ require File.expand_path('../abstract_unit', __FILE__)
2
2
 
3
3
  class TestCreate < ActiveSupport::TestCase
4
4
  fixtures :students, :dorms, :rooms, :room_assignments, :reference_types, :reference_codes, :streets, :suburbs
5
-
5
+
6
6
  CLASSES = {
7
7
  :single => {
8
8
  :class => ReferenceType,
@@ -101,7 +101,7 @@ class TestCreate < ActiveSupport::TestCase
101
101
  assert_equal(200, restaurant.store_id)
102
102
  assert_equal("My Store", restaurant.name)
103
103
 
104
- assert_equal(1, restaurant.suburbs(true).length)
104
+ assert_equal(1, restaurant.suburbs.reload.length)
105
105
 
106
106
  # Test suburbs
107
107
  suburb = restaurant.suburbs[0]
@@ -116,14 +116,13 @@ class TestCreate < ActiveSupport::TestCase
116
116
  room.save!
117
117
 
118
118
  student1 = students(:kelly)
119
- student2 = students(:jordan)
120
119
 
121
120
  RoomAssignment.delete_all
122
121
 
123
122
  assignment1 = RoomAssignment.new(:student_id => student1.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
124
123
  assignment1.save!
125
124
 
126
- room.room_assignments = [assignment1]
125
+ room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id]]
127
126
  room.save!
128
127
 
129
128
  assert_equal(1, room.room_assignments.length)
@@ -146,7 +145,8 @@ class TestCreate < ActiveSupport::TestCase
146
145
  assignment2 = RoomAssignment.new(:student_id => student2.id, :dorm_id => room.dorm_id, :room_id => room.room_id)
147
146
  assignment2.save!
148
147
 
149
- room.room_assignments = [assignment1,assignment2]
148
+ room.room_assignment_ids = [[assignment1.student_id, assignment1.dorm_id, assignment1.room_id],
149
+ [assignment2.student_id, assignment2.dorm_id, assignment2.room_id]]
150
150
  room.save!
151
151
 
152
152
  assert_equal(2, room.room_assignments.length)
@@ -1,7 +1,7 @@
1
1
  require File.expand_path('../abstract_unit', __FILE__)
2
2
 
3
3
  class EmployeesGroup < ActiveRecord::Base
4
-
4
+
5
5
  end
6
6
 
7
7
  class TestValidations < ActiveSupport::TestCase
@@ -13,23 +13,17 @@ class TestValidations < ActiveSupport::TestCase
13
13
  EmployeesGroup.create(employee_id: 3, group_id: 103)
14
14
 
15
15
  assert_equal(EmployeesGroup.all.size, 3)
16
- exception = assert_raises(ActiveRecord::StatementInvalid) {
16
+ exception = assert_raises(NoMethodError) {
17
17
  EmployeesGroup.where(employee_id: 1).first.destroy
18
18
  }
19
-
20
- mysql_match = /Unknown column 'employees_groups.' in 'where clause/ =~ exception.message
21
- sqlite3_match = /no such column: employees_groups./ =~ exception.message
22
- postgresql_match = /PG::SyntaxError: ERROR: zero-length delimited identifier/ =~ exception.message
23
- oracle_match = /OCIError: ORA-01741: illegal zero-length identifier/ =~ exception.message
24
19
 
25
- assert(postgresql_match || mysql_match || sqlite3_match || oracle_match)
20
+ assert(/undefined method `to_sym' for nil:NilClass/ =~ exception.message)
26
21
 
27
22
  assert(EmployeesGroup.all.size == 3)
28
23
  end
29
24
 
30
- def test_delete_all_with_joins
31
- # Let's ignore SQLite for this case since multi-column IN clause like (column1, column2) IN (...) is not allowed.
32
- # It cannot work without some dirty fix.
33
- ReferenceCode.joins(:reference_type).where(:reference_type_id => 1).delete_all unless ReferenceCode.connection.adapter_name == "SQLite"
34
- end
25
+ # This test fails, requires fixin arel
26
+ #def test_delete_all_with_joins
27
+ # ReferenceCode.joins(:reference_type).where(:reference_type_id => 1).delete_all
28
+ #end
35
29
  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,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
@@ -41,14 +41,6 @@ class TestFind < ActiveSupport::TestCase
41
41
  assert_equal(['The Netherlands', 'Amsterdam'], capitol.id)
42
42
  end
43
43
 
44
- def test_find_with_strings_with_comma_as_composite_keys
45
- capitol = Capitol.create!(country: 'The USA', city: 'Washington, D.C.')
46
- assert_equal ['The USA', 'Washington, D.C.'], capitol.id
47
-
48
- assert_equal capitol, Capitol.find(['The USA', 'Washington, D.C.'])
49
- assert_equal capitol, Capitol.find(capitol.to_param)
50
- end
51
-
52
44
  def test_find_each
53
45
  room_assignments = []
54
46
  RoomAssignment.find_each(:batch_size => 2) do |assignment|
@@ -70,10 +62,6 @@ class TestFind < ActiveSupport::TestCase
70
62
  ReferenceCode.find(['999', '999'])
71
63
  end
72
64
 
73
- connection = ActiveRecord::Base.connection
74
- ref_type_quoted = "#{connection.quote_table_name('reference_codes')}.#{connection.quote_column_name('reference_type_id')}"
75
- ref_code_quoted = "#{connection.quote_table_name('reference_codes')}.#{connection.quote_column_name('reference_code')}"
76
-
77
65
  expected = "Couldn't find all ReferenceCodes with 'reference_type_id,reference_code': (999, 999) (found 0 results, but was looking for 1)"
78
66
  assert_equal(with_quoted_identifiers(expected), error.message)
79
67
  end
@@ -95,12 +83,28 @@ class TestFind < ActiveSupport::TestCase
95
83
  end
96
84
  end
97
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
+
98
96
  def test_expand
99
97
  department = departments(:engineering)
100
98
  employees = Employee.where(:department => department)
101
99
  assert_equal(2, employees.count)
102
100
  end
103
101
 
102
+ def test_expand_with_multiple
103
+ departments = Department.all
104
+ employees = Employee.where(:department => departments)
105
+ assert_equal(4, employees.count)
106
+ end
107
+
104
108
  def test_find_one_with_params_id
105
109
  params_id = ReferenceCode.find([1,3]).to_param
106
110
  assert_equal params_id, "1,3"
@@ -3,6 +3,30 @@ require File.expand_path('../abstract_unit', __FILE__)
3
3
  class TestHabtm < ActiveSupport::TestCase
4
4
  fixtures :suburbs, :restaurants, :restaurants_suburbs, :products
5
5
 
6
+ def test_no_cpk
7
+ # This test makes sure we don't break anything in standard rails by using CPK
8
+ groups = Group.all
9
+
10
+ # First test records
11
+ employee = Employee.first
12
+ assert_equal(0, employee.groups.length)
13
+ employee.groups = groups
14
+ employee.reload
15
+ assert_equal(groups, employee.groups)
16
+ end
17
+
18
+ def test_no_cpk_ids
19
+ # This test makes sure we don't break anything in standard rails by using CPK
20
+ groups = Group.all
21
+
22
+ employee = Employee.last
23
+ assert_equal(0, employee.groups.length)
24
+ employee.group_ids = groups.map {|group| group.id}
25
+ employee.group_ids = [groups.first.id]
26
+ employee.reload
27
+ assert_equal([groups.first], employee.groups)
28
+ end
29
+
6
30
  def test_has_and_belongs_to_many
7
31
  @restaurant = Restaurant.find([1,1])
8
32
  assert_equal 2, @restaurant.suburbs.size
@@ -42,7 +66,7 @@ class TestHabtm < ActiveSupport::TestCase
42
66
  product = products(:first_product)
43
67
  subway = restaurants(:subway_one)
44
68
  product.restaurants << subway
45
-
69
+
46
70
  # reload
47
71
  # test positive
48
72
  product = products(:first_product)
@@ -106,7 +130,7 @@ class TestHabtm < ActiveSupport::TestCase
106
130
  # an array it will be evaluated as 3[0], which is 1, which would
107
131
  # delete product_one's associations rather than product_three's
108
132
  product_three.restaurants.clear
109
-
133
+
110
134
  # reload to force reload of associations
111
135
  product_one = Product.find(1)
112
136
  assert_equal 2, product_one.restaurants.size
@@ -1,116 +1,109 @@
1
- require File.expand_path('../abstract_unit', __FILE__)
2
-
3
- class ChildCpkTest < ReferenceCode
4
- end
5
-
6
- class TestIds < ActiveSupport::TestCase
7
- fixtures :reference_types, :reference_codes, :pk_called_ids
8
-
9
- CLASSES = {
10
- :single => {
11
- :class => ReferenceType,
12
- :primary_keys => [:reference_type_id],
13
- },
14
- :dual => {
15
- :class => ReferenceCode,
16
- :primary_keys => [:reference_type_id, :reference_code],
17
- },
18
- :dual_strs => {
19
- :class => ReferenceCode,
20
- :primary_keys => ['reference_type_id', 'reference_code'],
21
- },
22
- :pk_called_id => {
23
- :class => PkCalledId,
24
- :primary_keys => ['id', 'reference_code'],
25
- },
26
- }
27
-
28
- def setup
29
- self.class.classes = CLASSES
30
- end
31
-
32
- def test_id
33
- testing_with do
34
- assert_equal @first.id, @first.ids if composite?
35
- assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
36
- end
37
- end
38
-
39
- def test_to_param
40
- testing_with do
41
- assert_equal '1,1', @first.to_param if composite?
42
- end
43
-
44
- capitol = Capitol.create!(country: 'The USA', city: 'Washington, D.C.')
45
- assert_equal 'The USA,Washington^2C D.C.', capitol.to_param
46
- end
47
-
48
- def test_ids_to_s
49
- testing_with do
50
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
51
- to_test = @klass.order(order)[0..1].map(&:id)
52
- assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
53
- assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
54
- end
55
- end
56
-
57
- def test_set_ids_string
58
- testing_with do
59
- array = @primary_keys.collect {|key| 5}
60
- expected = composite? ? array.to_composite_keys : array.first
61
- @first.id = expected.to_s
62
- assert_equal expected, @first.id
63
- end
64
- end
65
-
66
- def test_set_ids_array
67
- testing_with do
68
- array = @primary_keys.collect {|key| 5}
69
- expected = composite? ? array.to_composite_keys : array.first
70
- @first.id = expected
71
- assert_equal expected, @first.id
72
- end
73
- end
74
-
75
- def test_set_ids_comp
76
- testing_with do
77
- array = @primary_keys.collect {|key| 5}
78
- expected = composite? ? array.to_composite_keys : array.first
79
- @first.id = expected
80
- assert_equal expected, @first.id
81
- end
82
- end
83
-
84
- def test_primary_keys
85
- testing_with do
86
- if composite?
87
- assert_not_nil @klass.primary_keys
88
- assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
89
- assert_equal @klass.primary_keys, @klass.primary_key
90
- assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
91
- assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
92
- else
93
- assert_not_nil @klass.primary_key
94
- assert_equal @primary_keys.first, @klass.primary_key.to_sym
95
- assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
96
- end
97
- end
98
- end
99
-
100
- def test_inherited_primary_keys
101
- assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
102
- end
103
-
104
- def test_inherited_ids
105
- cpk_test = ChildCpkTest.new
106
- assert_equal([nil, nil], cpk_test.id)
107
- end
108
-
109
- def test_assign_ids
110
- ref_code = ReferenceCode.new
111
- assert_equal([nil, nil], ref_code.id)
112
-
113
- ref_code.id = [2,1]
114
- assert_equal([2,1], ref_code.id)
115
- end
116
- end
1
+ require File.expand_path('../abstract_unit', __FILE__)
2
+
3
+ class ChildCpkTest < ReferenceCode
4
+ end
5
+
6
+ class TestIds < ActiveSupport::TestCase
7
+ fixtures :reference_types, :reference_codes
8
+
9
+ CLASSES = {
10
+ :single => {
11
+ :class => ReferenceType,
12
+ :primary_keys => [:reference_type_id],
13
+ },
14
+ :dual => {
15
+ :class => ReferenceCode,
16
+ :primary_keys => [:reference_type_id, :reference_code],
17
+ },
18
+ :dual_strs => {
19
+ :class => ReferenceCode,
20
+ :primary_keys => ['reference_type_id', 'reference_code'],
21
+ },
22
+ }
23
+
24
+ def setup
25
+ self.class.classes = CLASSES
26
+ end
27
+
28
+ def test_id
29
+ testing_with do
30
+ assert_equal @first.id, @first.ids if composite?
31
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @first.id) if composite?
32
+ end
33
+ end
34
+
35
+ def test_to_param
36
+ testing_with do
37
+ assert_equal '1,1', @first.to_param if composite?
38
+ end
39
+ end
40
+
41
+ def test_ids_to_s
42
+ testing_with do
43
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
44
+ to_test = @klass.order(order)[0..1].map(&:id)
45
+ assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
46
+ assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
47
+ end
48
+ end
49
+
50
+ def test_set_ids_string
51
+ testing_with do
52
+ array = @primary_keys.collect {|key| 5}
53
+ expected = composite? ? array.to_composite_keys : array.first
54
+ @first.id = expected.to_s
55
+ assert_equal expected, @first.id
56
+ end
57
+ end
58
+
59
+ def test_set_ids_array
60
+ testing_with do
61
+ array = @primary_keys.collect {|key| 5}
62
+ expected = composite? ? array.to_composite_keys : array.first
63
+ @first.id = expected
64
+ assert_equal expected, @first.id
65
+ end
66
+ end
67
+
68
+ def test_set_ids_comp
69
+ testing_with do
70
+ array = @primary_keys.collect {|key| 5}
71
+ expected = composite? ? array.to_composite_keys : array.first
72
+ @first.id = expected
73
+ assert_equal expected, @first.id
74
+ end
75
+ end
76
+
77
+ def test_primary_keys
78
+ testing_with do
79
+ if composite?
80
+ assert_not_nil @klass.primary_keys
81
+ assert_equal @primary_keys.map {|key| key.to_s}, @klass.primary_keys
82
+ assert_equal @klass.primary_keys, @klass.primary_key
83
+ assert_kind_of(CompositePrimaryKeys::CompositeKeys, @klass.primary_keys)
84
+ assert_equal @primary_keys.map {|key| key.to_sym}.join(','), @klass.primary_key.to_s
85
+ else
86
+ assert_not_nil @klass.primary_key
87
+ assert_equal @primary_keys.first, @klass.primary_key.to_sym
88
+ assert_equal @primary_keys.first.to_s, @klass.primary_key.to_s
89
+ end
90
+ end
91
+ end
92
+
93
+ def test_inherited_primary_keys
94
+ assert_equal(["reference_type_id", "reference_code"], ChildCpkTest.primary_keys)
95
+ end
96
+
97
+ def test_inherited_ids
98
+ cpk_test = ChildCpkTest.new
99
+ assert_equal([nil, nil], cpk_test.id)
100
+ end
101
+
102
+ def test_assign_ids
103
+ ref_code = ReferenceCode.new
104
+ assert_equal([nil, nil], ref_code.id)
105
+
106
+ ref_code.id = [2,1]
107
+ assert_equal([2,1], ref_code.id)
108
+ end
109
+ end