mingusbabcock-composite_primary_keys 2.2.2 → 2.2.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (122) hide show
  1. data/History.txt +156 -0
  2. data/Manifest.txt +122 -0
  3. data/README.txt +41 -0
  4. data/README_DB2.txt +33 -0
  5. data/Rakefile +65 -0
  6. data/init.rb +2 -0
  7. data/install.rb +30 -0
  8. data/lib/adapter_helper/base.rb +63 -0
  9. data/lib/adapter_helper/mysql.rb +13 -0
  10. data/lib/adapter_helper/oracle.rb +12 -0
  11. data/lib/adapter_helper/postgresql.rb +13 -0
  12. data/lib/adapter_helper/sqlite3.rb +13 -0
  13. data/lib/composite_primary_keys.rb +56 -0
  14. data/lib/composite_primary_keys/association_preload.rb +253 -0
  15. data/lib/composite_primary_keys/associations.rb +428 -0
  16. data/lib/composite_primary_keys/attribute_methods.rb +84 -0
  17. data/lib/composite_primary_keys/base.rb +341 -0
  18. data/lib/composite_primary_keys/calculations.rb +69 -0
  19. data/lib/composite_primary_keys/composite_arrays.rb +30 -0
  20. data/lib/composite_primary_keys/connection_adapters/ibm_db_adapter.rb +21 -0
  21. data/lib/composite_primary_keys/connection_adapters/oracle_adapter.rb +15 -0
  22. data/lib/composite_primary_keys/connection_adapters/postgresql_adapter.rb +53 -0
  23. data/lib/composite_primary_keys/connection_adapters/sqlite3_adapter.rb +15 -0
  24. data/lib/composite_primary_keys/fixtures.rb +8 -0
  25. data/lib/composite_primary_keys/migration.rb +20 -0
  26. data/lib/composite_primary_keys/reflection.rb +19 -0
  27. data/lib/composite_primary_keys/version.rb +8 -0
  28. data/loader.rb +24 -0
  29. data/local/database_connections.rb.sample +10 -0
  30. data/local/paths.rb.sample +2 -0
  31. data/local/tasks.rb.sample +2 -0
  32. data/scripts/console.rb +48 -0
  33. data/scripts/txt2html +67 -0
  34. data/scripts/txt2js +59 -0
  35. data/tasks/activerecord_selection.rake +43 -0
  36. data/tasks/databases.rake +12 -0
  37. data/tasks/databases/mysql.rake +30 -0
  38. data/tasks/databases/oracle.rake +25 -0
  39. data/tasks/databases/postgresql.rake +26 -0
  40. data/tasks/databases/sqlite3.rake +28 -0
  41. data/tasks/deployment.rake +22 -0
  42. data/tasks/local_setup.rake +13 -0
  43. data/tasks/website.rake +18 -0
  44. data/test/README_tests.txt +67 -0
  45. data/test/abstract_unit.rb +94 -0
  46. data/test/connections/native_ibm_db/connection.rb +23 -0
  47. data/test/connections/native_mysql/connection.rb +13 -0
  48. data/test/connections/native_oracle/connection.rb +14 -0
  49. data/test/connections/native_postgresql/connection.rb +9 -0
  50. data/test/connections/native_sqlite/connection.rb +9 -0
  51. data/test/fixtures/article.rb +5 -0
  52. data/test/fixtures/articles.yml +6 -0
  53. data/test/fixtures/comment.rb +6 -0
  54. data/test/fixtures/comments.yml +16 -0
  55. data/test/fixtures/db_definitions/db2-create-tables.sql +113 -0
  56. data/test/fixtures/db_definitions/db2-drop-tables.sql +16 -0
  57. data/test/fixtures/db_definitions/mysql.sql +174 -0
  58. data/test/fixtures/db_definitions/oracle.drop.sql +39 -0
  59. data/test/fixtures/db_definitions/oracle.sql +188 -0
  60. data/test/fixtures/db_definitions/postgresql.sql +199 -0
  61. data/test/fixtures/db_definitions/sqlite.sql +160 -0
  62. data/test/fixtures/department.rb +5 -0
  63. data/test/fixtures/departments.yml +3 -0
  64. data/test/fixtures/employee.rb +4 -0
  65. data/test/fixtures/employees.yml +9 -0
  66. data/test/fixtures/group.rb +3 -0
  67. data/test/fixtures/groups.yml +3 -0
  68. data/test/fixtures/hack.rb +6 -0
  69. data/test/fixtures/hacks.yml +2 -0
  70. data/test/fixtures/membership.rb +7 -0
  71. data/test/fixtures/membership_status.rb +3 -0
  72. data/test/fixtures/membership_statuses.yml +10 -0
  73. data/test/fixtures/memberships.yml +6 -0
  74. data/test/fixtures/product.rb +7 -0
  75. data/test/fixtures/product_tariff.rb +5 -0
  76. data/test/fixtures/product_tariffs.yml +12 -0
  77. data/test/fixtures/products.yml +6 -0
  78. data/test/fixtures/reading.rb +4 -0
  79. data/test/fixtures/readings.yml +10 -0
  80. data/test/fixtures/reference_code.rb +7 -0
  81. data/test/fixtures/reference_codes.yml +28 -0
  82. data/test/fixtures/reference_type.rb +7 -0
  83. data/test/fixtures/reference_types.yml +9 -0
  84. data/test/fixtures/street.rb +3 -0
  85. data/test/fixtures/streets.yml +15 -0
  86. data/test/fixtures/suburb.rb +6 -0
  87. data/test/fixtures/suburbs.yml +9 -0
  88. data/test/fixtures/tariff.rb +6 -0
  89. data/test/fixtures/tariffs.yml +13 -0
  90. data/test/fixtures/user.rb +10 -0
  91. data/test/fixtures/users.yml +6 -0
  92. data/test/hash_tricks.rb +34 -0
  93. data/test/plugins/pagination.rb +405 -0
  94. data/test/plugins/pagination_helper.rb +135 -0
  95. data/test/test_associations.rb +160 -0
  96. data/test/test_attribute_methods.rb +22 -0
  97. data/test/test_attributes.rb +84 -0
  98. data/test/test_clone.rb +34 -0
  99. data/test/test_composite_arrays.rb +51 -0
  100. data/test/test_create.rb +68 -0
  101. data/test/test_delete.rb +96 -0
  102. data/test/test_dummy.rb +28 -0
  103. data/test/test_exists.rb +29 -0
  104. data/test/test_find.rb +73 -0
  105. data/test/test_ids.rb +97 -0
  106. data/test/test_miscellaneous.rb +39 -0
  107. data/test/test_pagination.rb +38 -0
  108. data/test/test_polymorphic.rb +31 -0
  109. data/test/test_santiago.rb +27 -0
  110. data/test/test_tutorial_examle.rb +26 -0
  111. data/test/test_update.rb +40 -0
  112. data/website/index.html +199 -0
  113. data/website/index.txt +159 -0
  114. data/website/javascripts/rounded_corners_lite.inc.js +285 -0
  115. data/website/stylesheets/screen.css +126 -0
  116. data/website/template.js +3 -0
  117. data/website/template.rhtml +53 -0
  118. data/website/version-raw.js +3 -0
  119. data/website/version-raw.txt +2 -0
  120. data/website/version.js +4 -0
  121. data/website/version.txt +3 -0
  122. metadata +180 -18
@@ -0,0 +1,135 @@
1
+ module ActionView
2
+ module Helpers
3
+ # Provides methods for linking to ActionController::Pagination objects using a simple generator API. You can optionally
4
+ # also build your links manually using ActionView::Helpers::AssetHelper#link_to like so:
5
+ #
6
+ # <%= link_to "Previous page", { :page => paginator.current.previous } if paginator.current.previous %>
7
+ # <%= link_to "Next page", { :page => paginator.current.next } if paginator.current.next %>
8
+ module PaginationHelper
9
+ unless const_defined?(:DEFAULT_OPTIONS)
10
+ DEFAULT_OPTIONS = {
11
+ :name => :page,
12
+ :window_size => 2,
13
+ :always_show_anchors => true,
14
+ :link_to_current_page => false,
15
+ :params => {}
16
+ }
17
+ end
18
+
19
+ # Creates a basic HTML link bar for the given +paginator+. Links will be created
20
+ # for the next and/or previous page and for a number of other pages around the current
21
+ # pages position. The +html_options+ hash is passed to +link_to+ when the links are created.
22
+ #
23
+ # ==== Options
24
+ # <tt>:name</tt>:: the routing name for this paginator
25
+ # (defaults to +page+)
26
+ # <tt>:prefix</tt>:: prefix for pagination links
27
+ # (i.e. Older Pages: 1 2 3 4)
28
+ # <tt>:suffix</tt>:: suffix for pagination links
29
+ # (i.e. 1 2 3 4 <- Older Pages)
30
+ # <tt>:window_size</tt>:: the number of pages to show around
31
+ # the current page (defaults to <tt>2</tt>)
32
+ # <tt>:always_show_anchors</tt>:: whether or not the first and last
33
+ # pages should always be shown
34
+ # (defaults to +true+)
35
+ # <tt>:link_to_current_page</tt>:: whether or not the current page
36
+ # should be linked to (defaults to
37
+ # +false+)
38
+ # <tt>:params</tt>:: any additional routing parameters
39
+ # for page URLs
40
+ #
41
+ # ==== Examples
42
+ # # We'll assume we have a paginator setup in @person_pages...
43
+ #
44
+ # pagination_links(@person_pages)
45
+ # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
46
+ #
47
+ # pagination_links(@person_pages, :link_to_current_page => true)
48
+ # # => <a href="/?page=1/">1</a> <a href="/?page=2/">2</a> <a href="/?page=3/">3</a> ... <a href="/?page=10/">10</a>
49
+ #
50
+ # pagination_links(@person_pages, :always_show_anchors => false)
51
+ # # => 1 <a href="/?page=2/">2</a> <a href="/?page=3/">3</a>
52
+ #
53
+ # pagination_links(@person_pages, :window_size => 1)
54
+ # # => 1 <a href="/?page=2/">2</a> ... <a href="/?page=10/">10</a>
55
+ #
56
+ # pagination_links(@person_pages, :params => { :viewer => "flash" })
57
+ # # => 1 <a href="/?page=2&amp;viewer=flash/">2</a> <a href="/?page=3&amp;viewer=flash/">3</a> ...
58
+ # # <a href="/?page=10&amp;viewer=flash/">10</a>
59
+ def pagination_links(paginator, options={}, html_options={})
60
+ name = options[:name] || DEFAULT_OPTIONS[:name]
61
+ params = (options[:params] || DEFAULT_OPTIONS[:params]).clone
62
+
63
+ prefix = options[:prefix] || ''
64
+ suffix = options[:suffix] || ''
65
+
66
+ pagination_links_each(paginator, options, prefix, suffix) do |n|
67
+ params[name] = n
68
+ link_to(n.to_s, params, html_options)
69
+ end
70
+ end
71
+
72
+ # Iterate through the pages of a given +paginator+, invoking a
73
+ # block for each page number that needs to be rendered as a link.
74
+ #
75
+ # ==== Options
76
+ # <tt>:window_size</tt>:: the number of pages to show around
77
+ # the current page (defaults to +2+)
78
+ # <tt>:always_show_anchors</tt>:: whether or not the first and last
79
+ # pages should always be shown
80
+ # (defaults to +true+)
81
+ # <tt>:link_to_current_page</tt>:: whether or not the current page
82
+ # should be linked to (defaults to
83
+ # +false+)
84
+ #
85
+ # ==== Example
86
+ # # Turn paginated links into an Ajax call
87
+ # pagination_links_each(paginator, page_options) do |link|
88
+ # options = { :url => {:action => 'list'}, :update => 'results' }
89
+ # html_options = { :href => url_for(:action => 'list') }
90
+ #
91
+ # link_to_remote(link.to_s, options, html_options)
92
+ # end
93
+ def pagination_links_each(paginator, options, prefix = nil, suffix = nil)
94
+ options = DEFAULT_OPTIONS.merge(options)
95
+ link_to_current_page = options[:link_to_current_page]
96
+ always_show_anchors = options[:always_show_anchors]
97
+
98
+ current_page = paginator.current_page
99
+ window_pages = current_page.window(options[:window_size]).pages
100
+ return if window_pages.length <= 1 unless link_to_current_page
101
+
102
+ first, last = paginator.first, paginator.last
103
+
104
+ html = ''
105
+
106
+ html << prefix if prefix
107
+
108
+ if always_show_anchors and not (wp_first = window_pages[0]).first?
109
+ html << yield(first.number)
110
+ html << ' ... ' if wp_first.number - first.number > 1
111
+ html << ' '
112
+ end
113
+
114
+ window_pages.each do |page|
115
+ if current_page == page && !link_to_current_page
116
+ html << page.number.to_s
117
+ else
118
+ html << yield(page.number)
119
+ end
120
+ html << ' '
121
+ end
122
+
123
+ if always_show_anchors and not (wp_last = window_pages[-1]).last?
124
+ html << ' ... ' if last.number - wp_last.number > 1
125
+ html << yield(last.number)
126
+ end
127
+
128
+ html << suffix if suffix
129
+
130
+ html
131
+ end
132
+
133
+ end # PaginationHelper
134
+ end # Helpers
135
+ end # ActionView
@@ -0,0 +1,160 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/article'
3
+ require 'fixtures/product'
4
+ require 'fixtures/tariff'
5
+ require 'fixtures/product_tariff'
6
+ require 'fixtures/suburb'
7
+ require 'fixtures/street'
8
+ require 'fixtures/restaurant'
9
+ require 'fixtures/dorm'
10
+ require 'fixtures/room'
11
+ require 'fixtures/room_attribute'
12
+ require 'fixtures/room_attribute_assignment'
13
+ require 'fixtures/student'
14
+ require 'fixtures/room_assignment'
15
+ require 'fixtures/user'
16
+ require 'fixtures/reading'
17
+
18
+ class TestAssociations < Test::Unit::TestCase
19
+ fixtures :articles, :products, :tariffs, :product_tariffs, :suburbs, :streets, :restaurants, :restaurants_suburbs,
20
+ :dorms, :rooms, :room_attributes, :room_attribute_assignments, :students, :room_assignments, :users, :readings
21
+
22
+ def test_has_many_through_with_conditions_when_through_association_is_not_composite
23
+ user = User.find(:first)
24
+ assert_equal 1, user.articles.find(:all, :conditions => ["articles.name = ?", "Article One"]).size
25
+ end
26
+
27
+ def test_has_many_through_with_conditions_when_through_association_is_composite
28
+ room = Room.find(:first)
29
+ assert_equal 0, room.room_attributes.find(:all, :conditions => ["room_attributes.name != ?", "keg"]).size
30
+ end
31
+
32
+ def test_has_many_through_on_custom_finder_when_through_association_is_composite_finder_when_through_association_is_not_composite
33
+ user = User.find(:first)
34
+ assert_equal 1, user.find_custom_articles.size
35
+ end
36
+
37
+ def test_has_many_through_on_custom_finder_when_through_association_is_composite
38
+ room = Room.find(:first)
39
+ assert_equal 0, room.find_custom_room_attributes.size
40
+ end
41
+
42
+ def test_count
43
+ assert_equal 2, Product.count(:include => :product_tariffs)
44
+ assert_equal 3, Tariff.count(:include => :product_tariffs)
45
+ assert_equal 2, Tariff.count(:group => :start_date).size
46
+ end
47
+
48
+ def test_products
49
+ assert_not_nil products(:first_product).product_tariffs
50
+ assert_equal 2, products(:first_product).product_tariffs.length
51
+ assert_not_nil products(:first_product).tariffs
52
+ assert_equal 2, products(:first_product).tariffs.length
53
+ assert_not_nil products(:first_product).product_tariff
54
+ end
55
+
56
+ def test_product_tariffs
57
+ assert_not_nil product_tariffs(:first_flat).product
58
+ assert_not_nil product_tariffs(:first_flat).tariff
59
+ assert_equal Product, product_tariffs(:first_flat).product.class
60
+ assert_equal Tariff, product_tariffs(:first_flat).tariff.class
61
+ end
62
+
63
+ def test_tariffs
64
+ assert_not_nil tariffs(:flat).product_tariffs
65
+ assert_equal 1, tariffs(:flat).product_tariffs.length
66
+ assert_not_nil tariffs(:flat).products
67
+ assert_equal 1, tariffs(:flat).products.length
68
+ assert_not_nil tariffs(:flat).product_tariff
69
+ end
70
+
71
+ # Its not generating the instances of associated classes from the rows
72
+ def test_find_includes_products
73
+ assert @products = Product.find(:all, :include => :product_tariffs)
74
+ assert_equal 2, @products.length
75
+ assert_not_nil @products.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
76
+ assert_equal 3, @products.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
77
+ "Incorrect number of product_tariffs returned"
78
+ end
79
+
80
+ def test_find_includes_tariffs
81
+ assert @tariffs = Tariff.find(:all, :include => :product_tariffs)
82
+ assert_equal 3, @tariffs.length
83
+ assert_not_nil @tariffs.first.instance_variable_get('@product_tariffs'), '@product_tariffs not set; should be array'
84
+ assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
85
+ "Incorrect number of product_tariffs returnedturned"
86
+ end
87
+
88
+ def test_find_includes_product
89
+ assert @product_tariffs = ProductTariff.find(:all, :include => :product)
90
+ assert_equal 3, @product_tariffs.length
91
+ assert_not_nil @product_tariffs.first.instance_variable_get('@product'), '@product not set'
92
+ end
93
+
94
+ def test_find_includes_comp_belongs_to_tariff
95
+ assert @product_tariffs = ProductTariff.find(:all, :include => :tariff)
96
+ assert_equal 3, @product_tariffs.length
97
+ assert_not_nil @product_tariffs.first.instance_variable_get('@tariff'), '@tariff not set'
98
+ end
99
+
100
+ def test_find_includes_extended
101
+ assert @products = Product.find(:all, :include => {:product_tariffs => :tariff})
102
+ assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@product_tariffs').length},
103
+ "Incorrect number of product_tariffs returned"
104
+
105
+ assert @tariffs = Tariff.find(:all, :include => {:product_tariffs => :product})
106
+ assert_equal 3, @tariffs.inject(0) {|sum, tariff| sum + tariff.instance_variable_get('@product_tariffs').length},
107
+ "Incorrect number of product_tariffs returned"
108
+ end
109
+
110
+ def test_join_where_clause
111
+ @product = Product.find(:first, :include => :product_tariffs)
112
+ where_clause = @product.product_tariffs.composite_where_clause(
113
+ ['foo','bar'], [1,2]
114
+ )
115
+ assert_equal('(foo=1 AND bar=2)', where_clause)
116
+ end
117
+
118
+ def test_has_many_through
119
+ @products = Product.find(:all, :include => :tariffs)
120
+ assert_equal 3, @products.inject(0) {|sum, product| sum + product.instance_variable_get('@tariffs').length},
121
+ "Incorrect number of tariffs returned"
122
+ end
123
+
124
+ def test_has_many_through_when_not_pre_loaded
125
+ student = Student.find(:first)
126
+ rooms = student.rooms
127
+ assert_equal 1, rooms.size
128
+ assert_equal 1, rooms.first.dorm_id
129
+ assert_equal 1, rooms.first.room_id
130
+ end
131
+
132
+ def test_has_many_through_when_through_association_is_composite
133
+ dorm = Dorm.find(:first)
134
+ assert_equal 1, dorm.rooms.length
135
+ assert_equal 1, dorm.rooms.first.room_attributes.length
136
+ assert_equal 'keg', dorm.rooms.first.room_attributes.first.name
137
+ end
138
+
139
+ def test_associations_with_conditions
140
+ @suburb = Suburb.find([2, 1])
141
+ assert_equal 2, @suburb.streets.size
142
+
143
+ @suburb = Suburb.find([2, 1])
144
+ assert_equal 1, @suburb.first_streets.size
145
+
146
+ @suburb = Suburb.find([2, 1], :include => :streets)
147
+ assert_equal 2, @suburb.streets.size
148
+
149
+ @suburb = Suburb.find([2, 1], :include => :first_streets)
150
+ assert_equal 1, @suburb.first_streets.size
151
+ end
152
+
153
+ def test_has_and_belongs_to_many
154
+ @restaurant = Restaurant.find([1,1])
155
+ assert_equal 2, @restaurant.suburbs.size
156
+
157
+ @restaurant = Restaurant.find([1,1], :include => :suburbs)
158
+ assert_equal 2, @restaurant.suburbs.size
159
+ end
160
+ end
@@ -0,0 +1,22 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/kitchen_sink'
3
+ require 'fixtures/reference_type'
4
+
5
+ class TestAttributeMethods < Test::Unit::TestCase
6
+ fixtures :kitchen_sinks, :reference_types
7
+
8
+ def test_read_attribute_with_single_key
9
+ rt = ReferenceType.find(1)
10
+ assert_equal(1, rt.reference_type_id)
11
+ assert_equal('NAME_PREFIX', rt.type_label)
12
+ assert_equal('Name Prefix', rt.abbreviation)
13
+ end
14
+
15
+ def test_read_attribute_with_composite_keys
16
+ sink = KitchenSink.find(1,2)
17
+ assert_equal(1, sink.id_1)
18
+ assert_equal(2, sink.id_2)
19
+ assert_equal(Date.today, sink.a_date.to_date)
20
+ assert_equal('string', sink.a_string)
21
+ end
22
+ end
@@ -0,0 +1,84 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+ require 'fixtures/product'
5
+ require 'fixtures/tariff'
6
+ require 'fixtures/product_tariff'
7
+
8
+ class TestAttributes < Test::Unit::TestCase
9
+ fixtures :reference_types, :reference_codes, :products, :tariffs, :product_tariffs
10
+
11
+ CLASSES = {
12
+ :single => {
13
+ :class => ReferenceType,
14
+ :primary_keys => :reference_type_id,
15
+ },
16
+ :dual => {
17
+ :class => ReferenceCode,
18
+ :primary_keys => [:reference_type_id, :reference_code],
19
+ },
20
+ }
21
+
22
+ def setup
23
+ self.class.classes = CLASSES
24
+ end
25
+
26
+ def test_brackets
27
+ testing_with do
28
+ @first.attributes.each_pair do |attr_name, value|
29
+ assert_equal value, @first[attr_name]
30
+ end
31
+ end
32
+ end
33
+
34
+ def test_brackets_primary_key
35
+ testing_with do
36
+ assert_equal @first.id, @first[@primary_keys], "[] failing for #{@klass}"
37
+ assert_equal @first.id, @first[@first.class.primary_key]
38
+ end
39
+ end
40
+
41
+ def test_brackets_assignment
42
+ testing_with do
43
+ @first.attributes.each_pair do |attr_name, value|
44
+ @first[attr_name]= !value.nil? ? value * 2 : '1'
45
+ assert_equal !value.nil? ? value * 2 : '1', @first[attr_name]
46
+ end
47
+ end
48
+ end
49
+
50
+ def test_brackets_foreign_key_assignment
51
+ @flat = Tariff.find(1, Date.today.to_s(:db))
52
+ @second_free = ProductTariff.find(2,2,Date.today.to_s(:db))
53
+ @second_free_fk = [:tariff_id, :tariff_start_date]
54
+ @second_free[key = @second_free_fk] = @flat.id
55
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
56
+ assert_equal @flat.id, @second_free[key]
57
+ @second_free[key = @second_free_fk.to_composite_ids] = @flat.id
58
+ assert_equal @flat.id, @second_free[key]
59
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
60
+ @second_free[key = @second_free_fk.to_composite_ids] = @flat.id.to_s
61
+ assert_equal @flat.id, @second_free[key]
62
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
63
+ @second_free[key = @second_free_fk.to_composite_ids] = @flat.id.to_s
64
+ assert_equal @flat.id, @second_free[key]
65
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
66
+ @second_free[key = @second_free_fk.to_composite_ids.to_s] = @flat.id
67
+ assert_equal @flat.id, @second_free[key]
68
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
69
+ @second_free[key = @second_free_fk.to_composite_ids.to_s] = @flat.id.to_s
70
+ assert_equal @flat.id, @second_free[key]
71
+ compare_indexes('@flat', @flat.class.primary_key, '@second_free', @second_free_fk)
72
+ end
73
+ private
74
+ def compare_indexes(obj_name1, indexes1, obj_name2, indexes2)
75
+ obj1, obj2 = eval "[#{obj_name1}, #{obj_name2}]"
76
+ indexes1.length.times do |key_index|
77
+ assert_equal obj1[indexes1[key_index].to_s],
78
+ obj2[indexes2[key_index].to_s],
79
+ "#{obj_name1}[#{indexes1[key_index]}]=#{obj1[indexes1[key_index].to_s].inspect} != " +
80
+ "#{obj_name2}[#{indexes2[key_index]}]=#{obj2[indexes2[key_index].to_s].inspect}; " +
81
+ "#{obj_name2} = #{obj2.inspect}"
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,34 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ class TestClone < Test::Unit::TestCase
6
+ fixtures :reference_types, :reference_codes
7
+
8
+ CLASSES = {
9
+ :single => {
10
+ :class => ReferenceType,
11
+ :primary_keys => :reference_type_id,
12
+ },
13
+ :dual => {
14
+ :class => ReferenceCode,
15
+ :primary_keys => [:reference_type_id, :reference_code],
16
+ },
17
+ }
18
+
19
+ def setup
20
+ self.class.classes = CLASSES
21
+ end
22
+
23
+ def test_truth
24
+ testing_with do
25
+ clone = @first.clone
26
+ assert_equal @first.attributes.block(@klass.primary_key), clone.attributes
27
+ if composite?
28
+ @klass.primary_key.each {|key| assert_nil clone[key], "Primary key '#{key}' should be nil"}
29
+ else
30
+ assert_nil clone[@klass.primary_key], "Sole primary key should be nil"
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,51 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/reference_type'
3
+ require 'fixtures/reference_code'
4
+
5
+ class CompositeArraysTest < Test::Unit::TestCase
6
+
7
+ def test_new_primary_keys
8
+ keys = CompositePrimaryKeys::CompositeKeys.new
9
+ assert_not_nil keys
10
+ assert_equal '', keys.to_s
11
+ assert_equal '', "#{keys}"
12
+ end
13
+
14
+ def test_initialize_primary_keys
15
+ keys = CompositePrimaryKeys::CompositeKeys.new([1,2,3])
16
+ assert_not_nil keys
17
+ assert_equal '1,2,3', keys.to_s
18
+ assert_equal '1,2,3', "#{keys}"
19
+ end
20
+
21
+ def test_to_composite_keys
22
+ keys = [1,2,3].to_composite_keys
23
+ assert_equal CompositePrimaryKeys::CompositeKeys, keys.class
24
+ assert_equal '1,2,3', keys.to_s
25
+ end
26
+
27
+ def test_new_ids
28
+ keys = CompositePrimaryKeys::CompositeIds.new
29
+ assert_not_nil keys
30
+ assert_equal '', keys.to_s
31
+ assert_equal '', "#{keys}"
32
+ end
33
+
34
+ def test_initialize_ids
35
+ keys = CompositePrimaryKeys::CompositeIds.new([1,2,3])
36
+ assert_not_nil keys
37
+ assert_equal '1,2,3', keys.to_s
38
+ assert_equal '1,2,3', "#{keys}"
39
+ end
40
+
41
+ def test_to_composite_ids
42
+ keys = [1,2,3].to_composite_ids
43
+ assert_equal CompositePrimaryKeys::CompositeIds, keys.class
44
+ assert_equal '1,2,3', keys.to_s
45
+ end
46
+
47
+ def test_flatten
48
+ keys = [CompositePrimaryKeys::CompositeIds.new([1,2,3]), CompositePrimaryKeys::CompositeIds.new([4,5,6])]
49
+ assert_equal 6, keys.flatten.size
50
+ end
51
+ end