1 require 'abstract_unit'
2 require 'fixtures/reference_type'
3 require 'fixtures/reference_code'
4 require 'fixtures/street'
5 require 'fixtures/suburb'
7 class TestCreate < Test::Unit::TestCase
8 fixtures :reference_types, :reference_codes, :streets, :suburbs
12 :class => ReferenceType,
13 :primary_keys => :reference_type_id,
14 :create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
17 :class => ReferenceCode,
18 :primary_keys => [:reference_type_id, :reference_code],
19 :create => {:reference_type_id => 1, :reference_code => 20, :code_label => 'NEW_CODE', :abbreviation => 'New Code'}
24 self.class.classes = CLASSES
29 assert_not_nil @klass_info[:create]
35 assert new_obj = @klass.create(@klass_info[:create])
36 assert !new_obj.new_record?
43 @obj = @klass.create(@klass_info[:create].block(@klass.primary_key))
44 @successful = !composite?
45 rescue CompositePrimaryKeys::ActiveRecord::CompositeKeyError
48 flunk "Incorrect exception raised: #{$!}, #{$!.class}"
50 assert_equal composite?, !@successful, "Create should have failed for composites; #{@obj.inspect}"
54 def test_create_on_association
55 suburb = Suburb.find(:first)
56 suburb.streets.create(:name => "my street")
57 street = Street.find_by_name('my street')
58 assert_equal(suburb.city_id, street.city_id)
59 assert_equal(suburb.suburb_id, street.suburb_id)
62 def test_create_on_association_when_belongs_to_is_single_key
63 rt = ReferenceType.find(:first)
64 rt.reference_codes.create(:reference_code => 4321, :code_label => 'foo', :abbreviation => 'bar')
65 rc = ReferenceCode.find_by_reference_code(4321)
66 assert_equal(rc.reference_type_id, rt.reference_type_id)