-require 'abstract_unit'\r
-require 'fixtures/reference_type'\r
-require 'fixtures/reference_code'\r
-\r
-class TestIds < Test::Unit::TestCase\r
- fixtures :reference_types, :reference_codes\r
- \r
- CLASSES = {\r
- :single => {\r
- :class => ReferenceType,\r
- :primary_keys => [:reference_type_id],\r
- },\r
- :dual => { \r
- :class => ReferenceCode,\r
- :primary_keys => [:reference_type_id, :reference_code],\r
- },\r
- :dual_strs => { \r
- :class => ReferenceCode,\r
- :primary_keys => ['reference_type_id', 'reference_code'],\r
- },\r
- }\r
- \r
- def setup\r
- self.class.classes = CLASSES\r
- end\r
- \r
- def test_id\r
- testing_with do\r
- assert_equal @first.id, @first.ids if composite?\r
- end\r
- end\r
- \r
- def test_id_to_s\r
- testing_with do\r
- assert_equal first_id_str, @first.id.to_s\r
- assert_equal first_id_str, "#{@first.id}"\r
- end\r
- end\r
- \r
- def test_ids_to_s\r
- testing_with do\r
- order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')\r
- to_test = @klass.find(:all, :order => order)[0..1].map(&:id)\r
- assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual\r
- assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual\r
- end\r
- end\r
- \r
- def test_composite_where_clause\r
- testing_with do\r
- where = 'reference_codes.reference_type_id=1 AND reference_codes.reference_code=2) OR (reference_codes.reference_type_id=2 AND reference_codes.reference_code=2'\r
- assert_equal(where, @klass.composite_where_clause([[1, 2], [2, 2]])) if @key_test == :dual\r
- end\r
- end\r
- \r
- def test_set_ids_string\r
- testing_with do\r
- array = @primary_keys.collect {|key| 5}\r
- expected = composite? ? array.to_composite_keys : array.first\r
- @first.id = expected.to_s\r
- assert_equal expected, @first.id\r
- end\r
- end\r
- \r
- def test_set_ids_array\r
- testing_with do\r
- array = @primary_keys.collect {|key| 5}\r
- expected = composite? ? array.to_composite_keys : array.first\r
- @first.id = expected\r
- assert_equal expected, @first.id\r
- end\r
- end\r
- \r
- def test_set_ids_comp\r
- testing_with do\r
- array = @primary_keys.collect {|key| 5}\r
- expected = composite? ? array.to_composite_keys : array.first\r
- @first.id = expected\r
- assert_equal expected, @first.id\r
- end\r
- end\r
- \r
- def test_primary_keys\r
- testing_with do\r
- if composite?\r
- assert_not_nil @klass.primary_keys\r
- assert_equal @primary_keys.map {|key| key.to_sym}, @klass.primary_keys\r
- assert_equal @klass.primary_keys, @klass.primary_key\r
- else\r
- assert_not_nil @klass.primary_key\r
- assert_equal @primary_keys, [@klass.primary_key.to_sym]\r
- end\r
- assert_equal @primary_keys.join(','), @klass.primary_key.to_s\r
- # Need a :primary_keys should be Array with to_s overridden\r
- end\r
- end\r
+require 'abstract_unit'
+require 'fixtures/reference_type'
+require 'fixtures/reference_code'
+
+class TestIds < Test::Unit::TestCase
+ fixtures :reference_types, :reference_codes
+
+ CLASSES = {
+ :single => {
+ :class => ReferenceType,
+ :primary_keys => [:reference_type_id],
+ },
+ :dual => {
+ :class => ReferenceCode,
+ :primary_keys => [:reference_type_id, :reference_code],
+ },
+ :dual_strs => {
+ :class => ReferenceCode,
+ :primary_keys => ['reference_type_id', 'reference_code'],
+ },
+ }
+
+ def setup
+ self.class.classes = CLASSES
+ end
+
+ def test_id
+ testing_with do
+ assert_equal @first.id, @first.ids if composite?
+ end
+ end
+
+ def test_id_to_s
+ testing_with do
+ assert_equal first_id_str, @first.id.to_s
+ assert_equal first_id_str, "#{@first.id}"
+ end
+ end
+
+ def test_ids_to_s
+ testing_with do
+ order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
+ to_test = @klass.find(:all, :order => order)[0..1].map(&:id)
+ assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
+ assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
+ end
+ end
+
+ def test_composite_where_clause
+ testing_with do
+ where = 'reference_codes.reference_type_id=1 AND reference_codes.reference_code=2) OR (reference_codes.reference_type_id=2 AND reference_codes.reference_code=2'
+ assert_equal(where, @klass.composite_where_clause([[1, 2], [2, 2]])) if @key_test == :dual
+ end
+ end
+
+ def test_set_ids_string
+ testing_with do
+ array = @primary_keys.collect {|key| 5}
+ expected = composite? ? array.to_composite_keys : array.first
+ @first.id = expected.to_s
+ assert_equal expected, @first.id
+ end
+ end
+
+ def test_set_ids_array
+ testing_with do
+ array = @primary_keys.collect {|key| 5}
+ expected = composite? ? array.to_composite_keys : array.first
+ @first.id = expected
+ assert_equal expected, @first.id
+ end
+ end
+
+ def test_set_ids_comp
+ testing_with do
+ array = @primary_keys.collect {|key| 5}
+ expected = composite? ? array.to_composite_keys : array.first
+ @first.id = expected
+ assert_equal expected, @first.id
+ end
+ end
+
+ def test_primary_keys
+ testing_with do
+ if composite?
+ assert_not_nil @klass.primary_keys
+ assert_equal @primary_keys.map {|key| key.to_sym}, @klass.primary_keys
+ assert_equal @klass.primary_keys, @klass.primary_key
+ else
+ assert_not_nil @klass.primary_key
+ assert_equal @primary_keys, [@klass.primary_key.to_sym]
+ end
+ assert_equal @primary_keys.join(','), @klass.primary_key.to_s
+ # Need a :primary_keys should be Array with to_s overridden
+ end
+ end
end
\ No newline at end of file