1 require 'abstract_unit'
2 require 'fixtures/reference_type'
3 require 'fixtures/reference_code'
5 class TestIds < Test::Unit::TestCase
6 fixtures :reference_types, :reference_codes
10 :class => ReferenceType,
11 :primary_keys => [:reference_type_id],
14 :class => ReferenceCode,
15 :primary_keys => [:reference_type_id, :reference_code],
18 :class => ReferenceCode,
19 :primary_keys => ['reference_type_id', 'reference_code'],
24 self.class.classes = CLASSES
29 assert_equal @first.id, @first.ids if composite?
35 assert_equal first_id_str, @first.id.to_s
36 assert_equal first_id_str, "#{@first.id}"
42 order = @klass.primary_key.is_a?(String) ? @klass.primary_key : @klass.primary_key.join(',')
43 to_test = @klass.find(:all, :order => order)[0..1].map(&:id)
44 assert_equal '(1,1),(1,2)', @klass.ids_to_s(to_test) if @key_test == :dual
45 assert_equal '1,1;1,2', @klass.ids_to_s(to_test, ',', ';', '', '') if @key_test == :dual
49 def test_composite_where_clause
51 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'
52 assert_equal(where, @klass.composite_where_clause([[1, 2], [2, 2]])) if @key_test == :dual
56 def test_set_ids_string
58 array = @primary_keys.collect {|key| 5}
59 expected = composite? ? array.to_composite_keys : array.first
60 @first.id = expected.to_s
61 assert_equal expected, @first.id
65 def test_set_ids_array
67 array = @primary_keys.collect {|key| 5}
68 expected = composite? ? array.to_composite_keys : array.first
70 assert_equal expected, @first.id
76 array = @primary_keys.collect {|key| 5}
77 expected = composite? ? array.to_composite_keys : array.first
79 assert_equal expected, @first.id
86 assert_not_nil @klass.primary_keys
87 assert_equal @primary_keys.map {|key| key.to_sym}, @klass.primary_keys
88 assert_equal @klass.primary_keys, @klass.primary_key
90 assert_not_nil @klass.primary_key
91 assert_equal @primary_keys, [@klass.primary_key.to_sym]
93 assert_equal @primary_keys.join(','), @klass.primary_key.to_s
94 # Need a :primary_keys should be Array with to_s overridden