]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/composite_primary_keys-1.1.0/test/test_create.rb
Patch composite_primary_keys to work around issue when one component of
[rails.git] / vendor / gems / composite_primary_keys-1.1.0 / test / test_create.rb
1 require 'abstract_unit'
2 require 'fixtures/reference_type'
3 require 'fixtures/reference_code'
4 require 'fixtures/street'
5 require 'fixtures/suburb'
6
7 class TestCreate < Test::Unit::TestCase
8   fixtures :reference_types, :reference_codes, :streets, :suburbs
9   
10   CLASSES = {
11     :single => {
12       :class => ReferenceType,
13       :primary_keys => :reference_type_id,
14       :create => {:reference_type_id => 10, :type_label => 'NEW_TYPE', :abbreviation => 'New Type'}
15     },
16     :dual   => { 
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'}
20     },
21   }
22   
23   def setup
24     self.class.classes = CLASSES
25   end
26   
27   def test_setup
28     testing_with do
29       assert_not_nil @klass_info[:create]
30     end
31   end
32   
33   def test_create
34     testing_with do
35       assert new_obj = @klass.create(@klass_info[:create])
36       assert !new_obj.new_record?
37     end
38   end
39   
40   def test_create_no_id
41     testing_with do
42       begin
43         @obj = @klass.create(@klass_info[:create].block(@klass.primary_key))
44         @successful = !composite?
45       rescue CompositePrimaryKeys::ActiveRecord::CompositeKeyError
46         @successful = false
47       rescue
48         flunk "Incorrect exception raised: #{$!}, #{$!.class}"
49       end
50       assert_equal composite?, !@successful, "Create should have failed for composites; #{@obj.inspect}"
51     end
52   end
53   
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)
60   end
61   
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)
67   end
68 end