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