]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/composite_primary_keys-2.2.2/test/test_delete.rb
Handle Timeout::Error exceptions which are caught by ActiveRecord.
[rails.git] / vendor / gems / composite_primary_keys-2.2.2 / test / test_delete.rb
1 require 'abstract_unit'\r
2 require 'fixtures/reference_type'\r
3 require 'fixtures/reference_code'\r
4 require 'fixtures/department'\r
5 require 'fixtures/employee'\r
6 \r
7 class TestDelete < Test::Unit::TestCase\r
8   fixtures :reference_types, :reference_codes, :departments, :employees\r
9   \r
10   CLASSES = {\r
11     :single => {\r
12       :class => ReferenceType,\r
13       :primary_keys => :reference_type_id,\r
14     },\r
15     :dual   => { \r
16       :class => ReferenceCode,\r
17       :primary_keys => [:reference_type_id, :reference_code],\r
18     },\r
19   }\r
20   \r
21   def setup\r
22     self.class.classes = CLASSES\r
23   end\r
24   \r
25   def test_destroy_one\r
26     testing_with do\r
27       #assert @first.destroy\r
28       assert true\r
29     end\r
30   end\r
31   \r
32   def test_destroy_one_via_class\r
33     testing_with do\r
34       assert @klass.destroy(*@first.id)\r
35     end\r
36   end\r
37   \r
38   def test_destroy_one_alone_via_class\r
39     testing_with do\r
40       assert @klass.destroy(@first.id)\r
41     end\r
42   end\r
43   \r
44   def test_delete_one\r
45     testing_with do\r
46       assert @klass.delete(*@first.id) if composite?\r
47     end\r
48   end\r
49   \r
50   def test_delete_one_alone\r
51     testing_with do\r
52       assert @klass.delete(@first.id)\r
53     end\r
54   end\r
55   \r
56   def test_delete_many\r
57     testing_with do\r
58       to_delete = @klass.find(:all)[0..1]\r
59       assert_equal 2, to_delete.length\r
60     end\r
61   end\r
62   \r
63   def test_delete_all\r
64     testing_with do\r
65       @klass.delete_all\r
66     end\r
67   end\r
68 \r
69   def test_clear_association\r
70       department = Department.find(1,1)\r
71       assert_equal 2, department.employees.size, "Before clear employee count should be 2."\r
72       department.employees.clear\r
73       assert_equal 0, department.employees.size, "After clear employee count should be 0."\r
74       department.reload\r
75       assert_equal 0, department.employees.size, "After clear and a reload from DB employee count should be 0."\r
76   end\r
77 \r
78   def test_delete_association\r
79       department = Department.find(1,1)\r
80       assert_equal 2, department.employees.size , "Before delete employee count should be 2."\r
81       first_employee = department.employees[0]\r
82       department.employees.delete(first_employee)\r
83       assert_equal 1, department.employees.size, "After delete employee count should be 1."\r
84       department.reload\r
85       assert_equal 1, department.employees.size, "After delete and a reload from DB employee count should be 1."\r
86   end\r
87 \r
88   def test_delete_records_for_has_many_association_with_composite_primary_key\r
89       reference_type  = ReferenceType.find(1)\r
90       codes_to_delete = reference_type.reference_codes[0..1]\r
91       assert_equal 3, reference_type.reference_codes.size, "Before deleting records reference_code count should be 3."\r
92       reference_type.reference_codes.delete_records(codes_to_delete)\r
93       reference_type.reload\r
94       assert_equal 1, reference_type.reference_codes.size, "After deleting 2 records and a reload from DB reference_code count should be 1."\r
95   end\r
96 end\r