activerecord_authorails 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (270) hide show
  1. data/CHANGELOG +3043 -0
  2. data/README +360 -0
  3. data/RUNNING_UNIT_TESTS +64 -0
  4. data/Rakefile +226 -0
  5. data/examples/associations.png +0 -0
  6. data/examples/associations.rb +87 -0
  7. data/examples/shared_setup.rb +15 -0
  8. data/examples/validation.rb +85 -0
  9. data/install.rb +30 -0
  10. data/lib/active_record.rb +85 -0
  11. data/lib/active_record/acts/list.rb +244 -0
  12. data/lib/active_record/acts/nested_set.rb +211 -0
  13. data/lib/active_record/acts/tree.rb +89 -0
  14. data/lib/active_record/aggregations.rb +191 -0
  15. data/lib/active_record/associations.rb +1637 -0
  16. data/lib/active_record/associations/association_collection.rb +190 -0
  17. data/lib/active_record/associations/association_proxy.rb +158 -0
  18. data/lib/active_record/associations/belongs_to_association.rb +56 -0
  19. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +50 -0
  20. data/lib/active_record/associations/has_and_belongs_to_many_association.rb +169 -0
  21. data/lib/active_record/associations/has_many_association.rb +210 -0
  22. data/lib/active_record/associations/has_many_through_association.rb +247 -0
  23. data/lib/active_record/associations/has_one_association.rb +80 -0
  24. data/lib/active_record/attribute_methods.rb +75 -0
  25. data/lib/active_record/base.rb +2164 -0
  26. data/lib/active_record/calculations.rb +270 -0
  27. data/lib/active_record/callbacks.rb +367 -0
  28. data/lib/active_record/connection_adapters/abstract/connection_specification.rb +279 -0
  29. data/lib/active_record/connection_adapters/abstract/database_statements.rb +130 -0
  30. data/lib/active_record/connection_adapters/abstract/quoting.rb +58 -0
  31. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +343 -0
  32. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +310 -0
  33. data/lib/active_record/connection_adapters/abstract_adapter.rb +161 -0
  34. data/lib/active_record/connection_adapters/db2_adapter.rb +228 -0
  35. data/lib/active_record/connection_adapters/firebird_adapter.rb +728 -0
  36. data/lib/active_record/connection_adapters/frontbase_adapter.rb +861 -0
  37. data/lib/active_record/connection_adapters/mysql_adapter.rb +414 -0
  38. data/lib/active_record/connection_adapters/openbase_adapter.rb +350 -0
  39. data/lib/active_record/connection_adapters/oracle_adapter.rb +689 -0
  40. data/lib/active_record/connection_adapters/postgresql_adapter.rb +584 -0
  41. data/lib/active_record/connection_adapters/sqlite_adapter.rb +407 -0
  42. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +591 -0
  43. data/lib/active_record/connection_adapters/sybase_adapter.rb +662 -0
  44. data/lib/active_record/deprecated_associations.rb +104 -0
  45. data/lib/active_record/deprecated_finders.rb +44 -0
  46. data/lib/active_record/fixtures.rb +628 -0
  47. data/lib/active_record/locking/optimistic.rb +106 -0
  48. data/lib/active_record/locking/pessimistic.rb +77 -0
  49. data/lib/active_record/migration.rb +394 -0
  50. data/lib/active_record/observer.rb +178 -0
  51. data/lib/active_record/query_cache.rb +64 -0
  52. data/lib/active_record/reflection.rb +222 -0
  53. data/lib/active_record/schema.rb +58 -0
  54. data/lib/active_record/schema_dumper.rb +149 -0
  55. data/lib/active_record/timestamp.rb +51 -0
  56. data/lib/active_record/transactions.rb +136 -0
  57. data/lib/active_record/validations.rb +843 -0
  58. data/lib/active_record/vendor/db2.rb +362 -0
  59. data/lib/active_record/vendor/mysql.rb +1214 -0
  60. data/lib/active_record/vendor/simple.rb +693 -0
  61. data/lib/active_record/version.rb +9 -0
  62. data/lib/active_record/wrappers/yaml_wrapper.rb +15 -0
  63. data/lib/active_record/wrappings.rb +58 -0
  64. data/lib/active_record/xml_serialization.rb +308 -0
  65. data/test/aaa_create_tables_test.rb +59 -0
  66. data/test/abstract_unit.rb +77 -0
  67. data/test/active_schema_test_mysql.rb +31 -0
  68. data/test/adapter_test.rb +87 -0
  69. data/test/adapter_test_sqlserver.rb +81 -0
  70. data/test/aggregations_test.rb +95 -0
  71. data/test/all.sh +8 -0
  72. data/test/ar_schema_test.rb +33 -0
  73. data/test/association_inheritance_reload.rb +14 -0
  74. data/test/associations/callbacks_test.rb +126 -0
  75. data/test/associations/cascaded_eager_loading_test.rb +138 -0
  76. data/test/associations/eager_test.rb +393 -0
  77. data/test/associations/extension_test.rb +42 -0
  78. data/test/associations/join_model_test.rb +497 -0
  79. data/test/associations_test.rb +1809 -0
  80. data/test/attribute_methods_test.rb +49 -0
  81. data/test/base_test.rb +1586 -0
  82. data/test/binary_test.rb +37 -0
  83. data/test/calculations_test.rb +219 -0
  84. data/test/callbacks_test.rb +377 -0
  85. data/test/class_inheritable_attributes_test.rb +32 -0
  86. data/test/column_alias_test.rb +17 -0
  87. data/test/connection_test_firebird.rb +8 -0
  88. data/test/connections/native_db2/connection.rb +25 -0
  89. data/test/connections/native_firebird/connection.rb +26 -0
  90. data/test/connections/native_frontbase/connection.rb +27 -0
  91. data/test/connections/native_mysql/connection.rb +24 -0
  92. data/test/connections/native_openbase/connection.rb +21 -0
  93. data/test/connections/native_oracle/connection.rb +27 -0
  94. data/test/connections/native_postgresql/connection.rb +23 -0
  95. data/test/connections/native_sqlite/connection.rb +34 -0
  96. data/test/connections/native_sqlite3/connection.rb +34 -0
  97. data/test/connections/native_sqlite3/in_memory_connection.rb +18 -0
  98. data/test/connections/native_sqlserver/connection.rb +23 -0
  99. data/test/connections/native_sqlserver_odbc/connection.rb +25 -0
  100. data/test/connections/native_sybase/connection.rb +23 -0
  101. data/test/copy_table_sqlite.rb +64 -0
  102. data/test/datatype_test_postgresql.rb +52 -0
  103. data/test/default_test_firebird.rb +16 -0
  104. data/test/defaults_test.rb +60 -0
  105. data/test/deprecated_associations_test.rb +396 -0
  106. data/test/deprecated_finder_test.rb +151 -0
  107. data/test/empty_date_time_test.rb +25 -0
  108. data/test/finder_test.rb +504 -0
  109. data/test/fixtures/accounts.yml +28 -0
  110. data/test/fixtures/author.rb +99 -0
  111. data/test/fixtures/author_favorites.yml +4 -0
  112. data/test/fixtures/authors.yml +7 -0
  113. data/test/fixtures/auto_id.rb +4 -0
  114. data/test/fixtures/bad_fixtures/attr_with_numeric_first_char +1 -0
  115. data/test/fixtures/bad_fixtures/attr_with_spaces +1 -0
  116. data/test/fixtures/bad_fixtures/blank_line +3 -0
  117. data/test/fixtures/bad_fixtures/duplicate_attributes +3 -0
  118. data/test/fixtures/bad_fixtures/missing_value +1 -0
  119. data/test/fixtures/binary.rb +2 -0
  120. data/test/fixtures/categories.yml +14 -0
  121. data/test/fixtures/categories/special_categories.yml +9 -0
  122. data/test/fixtures/categories/subsubdir/arbitrary_filename.yml +4 -0
  123. data/test/fixtures/categories_ordered.yml +7 -0
  124. data/test/fixtures/categories_posts.yml +23 -0
  125. data/test/fixtures/categorization.rb +5 -0
  126. data/test/fixtures/categorizations.yml +17 -0
  127. data/test/fixtures/category.rb +20 -0
  128. data/test/fixtures/column_name.rb +3 -0
  129. data/test/fixtures/comment.rb +23 -0
  130. data/test/fixtures/comments.yml +59 -0
  131. data/test/fixtures/companies.yml +55 -0
  132. data/test/fixtures/company.rb +107 -0
  133. data/test/fixtures/company_in_module.rb +59 -0
  134. data/test/fixtures/computer.rb +3 -0
  135. data/test/fixtures/computers.yml +4 -0
  136. data/test/fixtures/course.rb +3 -0
  137. data/test/fixtures/courses.yml +7 -0
  138. data/test/fixtures/customer.rb +55 -0
  139. data/test/fixtures/customers.yml +17 -0
  140. data/test/fixtures/db_definitions/db2.drop.sql +32 -0
  141. data/test/fixtures/db_definitions/db2.sql +231 -0
  142. data/test/fixtures/db_definitions/db22.drop.sql +2 -0
  143. data/test/fixtures/db_definitions/db22.sql +5 -0
  144. data/test/fixtures/db_definitions/firebird.drop.sql +63 -0
  145. data/test/fixtures/db_definitions/firebird.sql +304 -0
  146. data/test/fixtures/db_definitions/firebird2.drop.sql +2 -0
  147. data/test/fixtures/db_definitions/firebird2.sql +6 -0
  148. data/test/fixtures/db_definitions/frontbase.drop.sql +32 -0
  149. data/test/fixtures/db_definitions/frontbase.sql +268 -0
  150. data/test/fixtures/db_definitions/frontbase2.drop.sql +1 -0
  151. data/test/fixtures/db_definitions/frontbase2.sql +4 -0
  152. data/test/fixtures/db_definitions/mysql.drop.sql +32 -0
  153. data/test/fixtures/db_definitions/mysql.sql +234 -0
  154. data/test/fixtures/db_definitions/mysql2.drop.sql +2 -0
  155. data/test/fixtures/db_definitions/mysql2.sql +5 -0
  156. data/test/fixtures/db_definitions/openbase.drop.sql +2 -0
  157. data/test/fixtures/db_definitions/openbase.sql +302 -0
  158. data/test/fixtures/db_definitions/openbase2.drop.sql +2 -0
  159. data/test/fixtures/db_definitions/openbase2.sql +7 -0
  160. data/test/fixtures/db_definitions/oracle.drop.sql +65 -0
  161. data/test/fixtures/db_definitions/oracle.sql +325 -0
  162. data/test/fixtures/db_definitions/oracle2.drop.sql +2 -0
  163. data/test/fixtures/db_definitions/oracle2.sql +6 -0
  164. data/test/fixtures/db_definitions/postgresql.drop.sql +37 -0
  165. data/test/fixtures/db_definitions/postgresql.sql +263 -0
  166. data/test/fixtures/db_definitions/postgresql2.drop.sql +2 -0
  167. data/test/fixtures/db_definitions/postgresql2.sql +5 -0
  168. data/test/fixtures/db_definitions/schema.rb +60 -0
  169. data/test/fixtures/db_definitions/sqlite.drop.sql +32 -0
  170. data/test/fixtures/db_definitions/sqlite.sql +215 -0
  171. data/test/fixtures/db_definitions/sqlite2.drop.sql +2 -0
  172. data/test/fixtures/db_definitions/sqlite2.sql +5 -0
  173. data/test/fixtures/db_definitions/sqlserver.drop.sql +34 -0
  174. data/test/fixtures/db_definitions/sqlserver.sql +243 -0
  175. data/test/fixtures/db_definitions/sqlserver2.drop.sql +2 -0
  176. data/test/fixtures/db_definitions/sqlserver2.sql +5 -0
  177. data/test/fixtures/db_definitions/sybase.drop.sql +34 -0
  178. data/test/fixtures/db_definitions/sybase.sql +218 -0
  179. data/test/fixtures/db_definitions/sybase2.drop.sql +4 -0
  180. data/test/fixtures/db_definitions/sybase2.sql +5 -0
  181. data/test/fixtures/default.rb +2 -0
  182. data/test/fixtures/developer.rb +52 -0
  183. data/test/fixtures/developers.yml +21 -0
  184. data/test/fixtures/developers_projects.yml +17 -0
  185. data/test/fixtures/developers_projects/david_action_controller +3 -0
  186. data/test/fixtures/developers_projects/david_active_record +3 -0
  187. data/test/fixtures/developers_projects/jamis_active_record +2 -0
  188. data/test/fixtures/edge.rb +5 -0
  189. data/test/fixtures/edges.yml +6 -0
  190. data/test/fixtures/entrant.rb +3 -0
  191. data/test/fixtures/entrants.yml +14 -0
  192. data/test/fixtures/fk_test_has_fk.yml +3 -0
  193. data/test/fixtures/fk_test_has_pk.yml +2 -0
  194. data/test/fixtures/flowers.jpg +0 -0
  195. data/test/fixtures/funny_jokes.yml +10 -0
  196. data/test/fixtures/joke.rb +6 -0
  197. data/test/fixtures/keyboard.rb +3 -0
  198. data/test/fixtures/legacy_thing.rb +3 -0
  199. data/test/fixtures/legacy_things.yml +3 -0
  200. data/test/fixtures/migrations/1_people_have_last_names.rb +9 -0
  201. data/test/fixtures/migrations/2_we_need_reminders.rb +12 -0
  202. data/test/fixtures/migrations/3_innocent_jointable.rb +12 -0
  203. data/test/fixtures/migrations_with_decimal/1_give_me_big_numbers.rb +15 -0
  204. data/test/fixtures/migrations_with_duplicate/1_people_have_last_names.rb +9 -0
  205. data/test/fixtures/migrations_with_duplicate/2_we_need_reminders.rb +12 -0
  206. data/test/fixtures/migrations_with_duplicate/3_foo.rb +7 -0
  207. data/test/fixtures/migrations_with_duplicate/3_innocent_jointable.rb +12 -0
  208. data/test/fixtures/migrations_with_missing_versions/1000_people_have_middle_names.rb +9 -0
  209. data/test/fixtures/migrations_with_missing_versions/1_people_have_last_names.rb +9 -0
  210. data/test/fixtures/migrations_with_missing_versions/3_we_need_reminders.rb +12 -0
  211. data/test/fixtures/migrations_with_missing_versions/4_innocent_jointable.rb +12 -0
  212. data/test/fixtures/mixed_case_monkey.rb +3 -0
  213. data/test/fixtures/mixed_case_monkeys.yml +6 -0
  214. data/test/fixtures/mixin.rb +63 -0
  215. data/test/fixtures/mixins.yml +127 -0
  216. data/test/fixtures/movie.rb +5 -0
  217. data/test/fixtures/movies.yml +7 -0
  218. data/test/fixtures/naked/csv/accounts.csv +1 -0
  219. data/test/fixtures/naked/yml/accounts.yml +1 -0
  220. data/test/fixtures/naked/yml/companies.yml +1 -0
  221. data/test/fixtures/naked/yml/courses.yml +1 -0
  222. data/test/fixtures/order.rb +4 -0
  223. data/test/fixtures/people.yml +3 -0
  224. data/test/fixtures/person.rb +4 -0
  225. data/test/fixtures/post.rb +58 -0
  226. data/test/fixtures/posts.yml +48 -0
  227. data/test/fixtures/project.rb +27 -0
  228. data/test/fixtures/projects.yml +7 -0
  229. data/test/fixtures/reader.rb +4 -0
  230. data/test/fixtures/readers.yml +4 -0
  231. data/test/fixtures/reply.rb +37 -0
  232. data/test/fixtures/subject.rb +4 -0
  233. data/test/fixtures/subscriber.rb +6 -0
  234. data/test/fixtures/subscribers/first +2 -0
  235. data/test/fixtures/subscribers/second +2 -0
  236. data/test/fixtures/tag.rb +7 -0
  237. data/test/fixtures/tagging.rb +6 -0
  238. data/test/fixtures/taggings.yml +18 -0
  239. data/test/fixtures/tags.yml +7 -0
  240. data/test/fixtures/task.rb +3 -0
  241. data/test/fixtures/tasks.yml +7 -0
  242. data/test/fixtures/topic.rb +25 -0
  243. data/test/fixtures/topics.yml +22 -0
  244. data/test/fixtures/vertex.rb +9 -0
  245. data/test/fixtures/vertices.yml +4 -0
  246. data/test/fixtures_test.rb +401 -0
  247. data/test/inheritance_test.rb +205 -0
  248. data/test/lifecycle_test.rb +137 -0
  249. data/test/locking_test.rb +190 -0
  250. data/test/method_scoping_test.rb +416 -0
  251. data/test/migration_test.rb +768 -0
  252. data/test/migration_test_firebird.rb +124 -0
  253. data/test/mixin_nested_set_test.rb +196 -0
  254. data/test/mixin_test.rb +550 -0
  255. data/test/modules_test.rb +34 -0
  256. data/test/multiple_db_test.rb +60 -0
  257. data/test/pk_test.rb +104 -0
  258. data/test/readonly_test.rb +107 -0
  259. data/test/reflection_test.rb +159 -0
  260. data/test/schema_authorization_test_postgresql.rb +75 -0
  261. data/test/schema_dumper_test.rb +96 -0
  262. data/test/schema_test_postgresql.rb +64 -0
  263. data/test/synonym_test_oracle.rb +17 -0
  264. data/test/table_name_test_sqlserver.rb +23 -0
  265. data/test/threaded_connections_test.rb +48 -0
  266. data/test/transactions_test.rb +230 -0
  267. data/test/unconnected_test.rb +32 -0
  268. data/test/validations_test.rb +1097 -0
  269. data/test/xml_serialization_test.rb +125 -0
  270. metadata +365 -0
@@ -0,0 +1,416 @@
1
+ require 'abstract_unit'
2
+ require 'fixtures/developer'
3
+ require 'fixtures/project'
4
+ require 'fixtures/comment'
5
+ require 'fixtures/post'
6
+ require 'fixtures/category'
7
+
8
+ class MethodScopingTest < Test::Unit::TestCase
9
+ fixtures :developers, :projects, :comments, :posts
10
+
11
+ def test_set_conditions
12
+ Developer.with_scope(:find => { :conditions => 'just a test...' }) do
13
+ assert_equal 'just a test...', Developer.send(:current_scoped_methods)[:find][:conditions]
14
+ end
15
+ end
16
+
17
+ def test_scoped_find
18
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
19
+ assert_nothing_raised { Developer.find(1) }
20
+ end
21
+ end
22
+
23
+ def test_scoped_find_first
24
+ Developer.with_scope(:find => { :conditions => "salary = 100000" }) do
25
+ assert_equal Developer.find(10), Developer.find(:first, :order => 'name')
26
+ end
27
+ end
28
+
29
+ def test_scoped_find_combines_conditions
30
+ Developer.with_scope(:find => { :conditions => "salary = 9000" }) do
31
+ assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => "name = 'Jamis'")
32
+ end
33
+ end
34
+
35
+ def test_scoped_find_sanitizes_conditions
36
+ Developer.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
37
+ assert_equal developers(:poor_jamis), Developer.find(:first)
38
+ end
39
+ end
40
+
41
+ def test_scoped_find_combines_and_sanitizes_conditions
42
+ Developer.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
43
+ assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
44
+ end
45
+ end
46
+
47
+ def test_scoped_find_all
48
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
49
+ assert_equal [developers(:david)], Developer.find(:all)
50
+ end
51
+ end
52
+
53
+ def test_scoped_count
54
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
55
+ assert_equal 1, Developer.count
56
+ end
57
+
58
+ Developer.with_scope(:find => { :conditions => 'salary = 100000' }) do
59
+ assert_equal 8, Developer.count
60
+ assert_equal 1, Developer.count(:conditions => "name LIKE 'fixture_1%'")
61
+ end
62
+ end
63
+
64
+ def test_scoped_find_include
65
+ # with the include, will retrieve only developers for the given project
66
+ scoped_developers = Developer.with_scope(:find => { :include => :projects }) do
67
+ Developer.find(:all, :conditions => 'projects.id = 2')
68
+ end
69
+ assert scoped_developers.include?(developers(:david))
70
+ assert !scoped_developers.include?(developers(:jamis))
71
+ assert_equal 1, scoped_developers.size
72
+ end
73
+
74
+ def test_scoped_count_include
75
+ # with the include, will retrieve only developers for the given project
76
+ Developer.with_scope(:find => { :include => :projects }) do
77
+ assert_equal 1, Developer.count(:conditions => 'projects.id = 2')
78
+ end
79
+ end
80
+
81
+ def test_scoped_create
82
+ new_comment = nil
83
+
84
+ VerySpecialComment.with_scope(:create => { :post_id => 1 }) do
85
+ assert_equal({ :post_id => 1 }, VerySpecialComment.send(:current_scoped_methods)[:create])
86
+ new_comment = VerySpecialComment.create :body => "Wonderful world"
87
+ end
88
+
89
+ assert Post.find(1).comments.include?(new_comment)
90
+ end
91
+
92
+ def test_immutable_scope
93
+ options = { :conditions => "name = 'David'" }
94
+ Developer.with_scope(:find => options) do
95
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
96
+ options[:conditions] = "name != 'David'"
97
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
98
+ end
99
+
100
+ scope = { :find => { :conditions => "name = 'David'" }}
101
+ Developer.with_scope(scope) do
102
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
103
+ scope[:find][:conditions] = "name != 'David'"
104
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
105
+ end
106
+ end
107
+
108
+ def test_scoped_with_duck_typing
109
+ scoping = Struct.new(:method_scoping).new(:find => { :conditions => ["name = ?", 'David'] })
110
+ Developer.with_scope(scoping) do
111
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
112
+ end
113
+ end
114
+
115
+ def test_ensure_that_method_scoping_is_correctly_restored
116
+ scoped_methods = Developer.instance_eval('current_scoped_methods')
117
+
118
+ begin
119
+ Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
120
+ raise "an exception"
121
+ end
122
+ rescue
123
+ end
124
+ assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
125
+ end
126
+ end
127
+
128
+ class NestedScopingTest < Test::Unit::TestCase
129
+ fixtures :developers, :projects, :comments, :posts
130
+
131
+ def test_merge_options
132
+ Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do
133
+ Developer.with_scope(:find => { :limit => 10 }) do
134
+ merged_option = Developer.instance_eval('current_scoped_methods')[:find]
135
+ assert_equal({ :conditions => 'salary = 80000', :limit => 10 }, merged_option)
136
+ end
137
+ end
138
+ end
139
+
140
+ def test_replace_options
141
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
142
+ Developer.with_exclusive_scope(:find => { :conditions => "name = 'Jamis'" }) do
143
+ assert_equal({:find => { :conditions => "name = 'Jamis'" }}, Developer.instance_eval('current_scoped_methods'))
144
+ assert_equal({:find => { :conditions => "name = 'Jamis'" }}, Developer.send(:scoped_methods)[-1])
145
+ end
146
+ end
147
+ end
148
+
149
+ def test_append_conditions
150
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
151
+ Developer.with_scope(:find => { :conditions => 'salary = 80000' }) do
152
+ appended_condition = Developer.instance_eval('current_scoped_methods')[:find][:conditions]
153
+ assert_equal("( name = 'David' ) AND ( salary = 80000 )", appended_condition)
154
+ assert_equal(1, Developer.count)
155
+ end
156
+ Developer.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
157
+ assert_equal(0, Developer.count)
158
+ end
159
+ end
160
+ end
161
+
162
+ def test_merge_and_append_options
163
+ Developer.with_scope(:find => { :conditions => 'salary = 80000', :limit => 10 }) do
164
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
165
+ merged_option = Developer.instance_eval('current_scoped_methods')[:find]
166
+ assert_equal({ :conditions => "( salary = 80000 ) AND ( name = 'David' )", :limit => 10 }, merged_option)
167
+ end
168
+ end
169
+ end
170
+
171
+ def test_nested_scoped_find
172
+ Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
173
+ Developer.with_exclusive_scope(:find => { :conditions => "name = 'David'" }) do
174
+ assert_nothing_raised { Developer.find(1) }
175
+ assert_equal('David', Developer.find(:first).name)
176
+ end
177
+ assert_equal('Jamis', Developer.find(:first).name)
178
+ end
179
+ end
180
+
181
+ def test_nested_scoped_find_include
182
+ Developer.with_scope(:find => { :include => :projects }) do
183
+ Developer.with_scope(:find => { :conditions => "projects.id = 2" }) do
184
+ assert_nothing_raised { Developer.find(1) }
185
+ assert_equal('David', Developer.find(:first).name)
186
+ end
187
+ end
188
+ end
189
+
190
+ def test_nested_scoped_find_merged_include
191
+ # :include's remain unique and don't "double up" when merging
192
+ Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do
193
+ Developer.with_scope(:find => { :include => :projects }) do
194
+ assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length
195
+ assert_equal('David', Developer.find(:first).name)
196
+ end
197
+ end
198
+
199
+ # the nested scope doesn't remove the first :include
200
+ Developer.with_scope(:find => { :include => :projects, :conditions => "projects.id = 2" }) do
201
+ Developer.with_scope(:find => { :include => [] }) do
202
+ assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length
203
+ assert_equal('David', Developer.find(:first).name)
204
+ end
205
+ end
206
+
207
+ # mixing array and symbol include's will merge correctly
208
+ Developer.with_scope(:find => { :include => [:projects], :conditions => "projects.id = 2" }) do
209
+ Developer.with_scope(:find => { :include => :projects }) do
210
+ assert_equal 1, Developer.instance_eval('current_scoped_methods')[:find][:include].length
211
+ assert_equal('David', Developer.find(:first).name)
212
+ end
213
+ end
214
+ end
215
+
216
+ def test_nested_scoped_find_replace_include
217
+ Developer.with_scope(:find => { :include => :projects }) do
218
+ Developer.with_exclusive_scope(:find => { :include => [] }) do
219
+ assert_equal 0, Developer.instance_eval('current_scoped_methods')[:find][:include].length
220
+ end
221
+ end
222
+ end
223
+
224
+ def test_three_level_nested_exclusive_scoped_find
225
+ Developer.with_scope(:find => { :conditions => "name = 'Jamis'" }) do
226
+ assert_equal('Jamis', Developer.find(:first).name)
227
+
228
+ Developer.with_exclusive_scope(:find => { :conditions => "name = 'David'" }) do
229
+ assert_equal('David', Developer.find(:first).name)
230
+
231
+ Developer.with_exclusive_scope(:find => { :conditions => "name = 'Maiha'" }) do
232
+ assert_equal(nil, Developer.find(:first))
233
+ end
234
+
235
+ # ensure that scoping is restored
236
+ assert_equal('David', Developer.find(:first).name)
237
+ end
238
+
239
+ # ensure that scoping is restored
240
+ assert_equal('Jamis', Developer.find(:first).name)
241
+ end
242
+ end
243
+
244
+ def test_merged_scoped_find
245
+ poor_jamis = developers(:poor_jamis)
246
+ Developer.with_scope(:find => { :conditions => "salary < 100000" }) do
247
+ Developer.with_scope(:find => { :offset => 1 }) do
248
+ assert_equal(poor_jamis, Developer.find(:first, :order => 'id asc'))
249
+ end
250
+ end
251
+ end
252
+
253
+ def test_merged_scoped_find_sanitizes_conditions
254
+ Developer.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
255
+ Developer.with_scope(:find => { :conditions => ['salary = ?', 9000] }) do
256
+ assert_raise(ActiveRecord::RecordNotFound) { developers(:poor_jamis) }
257
+ end
258
+ end
259
+ end
260
+
261
+ def test_nested_scoped_find_combines_and_sanitizes_conditions
262
+ Developer.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
263
+ Developer.with_exclusive_scope(:find => { :conditions => ['salary = ?', 9000] }) do
264
+ assert_equal developers(:poor_jamis), Developer.find(:first)
265
+ assert_equal developers(:poor_jamis), Developer.find(:first, :conditions => ['name = ?', 'Jamis'])
266
+ end
267
+ end
268
+ end
269
+
270
+ def test_merged_scoped_find_combines_and_sanitizes_conditions
271
+ Developer.with_scope(:find => { :conditions => ["name = ?", 'David'] }) do
272
+ Developer.with_scope(:find => { :conditions => ['salary > ?', 9000] }) do
273
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
274
+ end
275
+ end
276
+ end
277
+
278
+ def test_immutable_nested_scope
279
+ options1 = { :conditions => "name = 'Jamis'" }
280
+ options2 = { :conditions => "name = 'David'" }
281
+ Developer.with_scope(:find => options1) do
282
+ Developer.with_exclusive_scope(:find => options2) do
283
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
284
+ options1[:conditions] = options2[:conditions] = nil
285
+ assert_equal %w(David), Developer.find(:all).map { |d| d.name }
286
+ end
287
+ end
288
+ end
289
+
290
+ def test_immutable_merged_scope
291
+ options1 = { :conditions => "name = 'Jamis'" }
292
+ options2 = { :conditions => "salary > 10000" }
293
+ Developer.with_scope(:find => options1) do
294
+ Developer.with_scope(:find => options2) do
295
+ assert_equal %w(Jamis), Developer.find(:all).map { |d| d.name }
296
+ options1[:conditions] = options2[:conditions] = nil
297
+ assert_equal %w(Jamis), Developer.find(:all).map { |d| d.name }
298
+ end
299
+ end
300
+ end
301
+
302
+ def test_ensure_that_method_scoping_is_correctly_restored
303
+ Developer.with_scope(:find => { :conditions => "name = 'David'" }) do
304
+ scoped_methods = Developer.instance_eval('current_scoped_methods')
305
+ begin
306
+ Developer.with_scope(:find => { :conditions => "name = 'Maiha'" }) do
307
+ raise "an exception"
308
+ end
309
+ rescue
310
+ end
311
+ assert_equal scoped_methods, Developer.instance_eval('current_scoped_methods')
312
+ end
313
+ end
314
+ end
315
+
316
+ class HasManyScopingTest< Test::Unit::TestCase
317
+ fixtures :comments, :posts
318
+
319
+ def setup
320
+ @welcome = Post.find(1)
321
+ end
322
+
323
+ def test_forwarding_of_static_methods
324
+ assert_equal 'a comment...', Comment.what_are_you
325
+ assert_equal 'a comment...', @welcome.comments.what_are_you
326
+ end
327
+
328
+ def test_forwarding_to_scoped
329
+ assert_equal 4, Comment.search_by_type('Comment').size
330
+ assert_equal 2, @welcome.comments.search_by_type('Comment').size
331
+ end
332
+
333
+ def test_forwarding_to_dynamic_finders
334
+ assert_equal 4, Comment.find_all_by_type('Comment').size
335
+ assert_equal 2, @welcome.comments.find_all_by_type('Comment').size
336
+ end
337
+
338
+ def test_nested_scope
339
+ Comment.with_scope(:find => { :conditions => '1=1' }) do
340
+ assert_equal 'a comment...', @welcome.comments.what_are_you
341
+ end
342
+ end
343
+ end
344
+
345
+
346
+ class HasAndBelongsToManyScopingTest< Test::Unit::TestCase
347
+ fixtures :posts, :categories, :categories_posts
348
+
349
+ def setup
350
+ @welcome = Post.find(1)
351
+ end
352
+
353
+ def test_forwarding_of_static_methods
354
+ assert_equal 'a category...', Category.what_are_you
355
+ assert_equal 'a category...', @welcome.categories.what_are_you
356
+ end
357
+
358
+ def test_forwarding_to_dynamic_finders
359
+ assert_equal 4, Category.find_all_by_type('SpecialCategory').size
360
+ assert_equal 0, @welcome.categories.find_all_by_type('SpecialCategory').size
361
+ assert_equal 2, @welcome.categories.find_all_by_type('Category').size
362
+ end
363
+
364
+ def test_nested_scope
365
+ Category.with_scope(:find => { :conditions => '1=1' }) do
366
+ assert_equal 'a comment...', @welcome.comments.what_are_you
367
+ end
368
+ end
369
+ end
370
+
371
+
372
+ =begin
373
+ # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case
374
+
375
+
376
+ class BelongsToScopingTest< Test::Unit::TestCase
377
+ fixtures :comments, :posts
378
+
379
+ def setup
380
+ @greetings = Comment.find(1)
381
+ end
382
+
383
+ def test_forwarding_of_static_method
384
+ assert_equal 'a post...', Post.what_are_you
385
+ assert_equal 'a post...', @greetings.post.what_are_you
386
+ end
387
+
388
+ def test_forwarding_to_dynamic_finders
389
+ assert_equal 4, Post.find_all_by_type('Post').size
390
+ assert_equal 1, @greetings.post.find_all_by_type('Post').size
391
+ end
392
+
393
+ end
394
+
395
+
396
+ class HasOneScopingTest< Test::Unit::TestCase
397
+ fixtures :comments, :posts
398
+
399
+ def setup
400
+ @sti_comments = Post.find(4)
401
+ end
402
+
403
+ def test_forwarding_of_static_methods
404
+ assert_equal 'a comment...', Comment.what_are_you
405
+ assert_equal 'a very special comment...', @sti_comments.very_special_comment.what_are_you
406
+ end
407
+
408
+ def test_forwarding_to_dynamic_finders
409
+ assert_equal 1, Comment.find_all_by_type('VerySpecialComment').size
410
+ assert_equal 1, @sti_comments.very_special_comment.find_all_by_type('VerySpecialComment').size
411
+ assert_equal 0, @sti_comments.very_special_comment.find_all_by_type('Comment').size
412
+ end
413
+
414
+ end
415
+
416
+ =end
@@ -0,0 +1,768 @@
1
+ require 'abstract_unit'
2
+ require 'bigdecimal/util'
3
+
4
+ require 'fixtures/person'
5
+ require 'fixtures/topic'
6
+ require File.dirname(__FILE__) + '/fixtures/migrations/1_people_have_last_names'
7
+ require File.dirname(__FILE__) + '/fixtures/migrations/2_we_need_reminders'
8
+ require File.dirname(__FILE__) + '/fixtures/migrations_with_decimal/1_give_me_big_numbers'
9
+
10
+ if ActiveRecord::Base.connection.supports_migrations?
11
+ class BigNumber < ActiveRecord::Base; end
12
+
13
+ class Reminder < ActiveRecord::Base; end
14
+
15
+ class ActiveRecord::Migration
16
+ class <<self
17
+ attr_accessor :message_count
18
+ def puts(text="")
19
+ self.message_count ||= 0
20
+ self.message_count += 1
21
+ end
22
+ end
23
+ end
24
+
25
+ class MigrationTest < Test::Unit::TestCase
26
+ self.use_transactional_fixtures = false
27
+
28
+ def setup
29
+ ActiveRecord::Migration.verbose = true
30
+ PeopleHaveLastNames.message_count = 0
31
+ end
32
+
33
+ def teardown
34
+ ActiveRecord::Base.connection.initialize_schema_information
35
+ ActiveRecord::Base.connection.update "UPDATE #{ActiveRecord::Migrator.schema_info_table_name} SET version = 0"
36
+
37
+ %w(reminders people_reminders prefix_reminders_suffix).each do |table|
38
+ Reminder.connection.drop_table(table) rescue nil
39
+ end
40
+ Reminder.reset_column_information
41
+
42
+ %w(last_name key bio age height wealth birthday favorite_day
43
+ male administrator).each do |column|
44
+ Person.connection.remove_column('people', column) rescue nil
45
+ end
46
+ Person.connection.remove_column("people", "first_name") rescue nil
47
+ Person.connection.remove_column("people", "middle_name") rescue nil
48
+ Person.connection.add_column("people", "first_name", :string, :limit => 40)
49
+ Person.reset_column_information
50
+ end
51
+
52
+ def test_add_index
53
+ # Limit size of last_name and key columns to support Firebird index limitations
54
+ Person.connection.add_column "people", "last_name", :string, :limit => 100
55
+ Person.connection.add_column "people", "key", :string, :limit => 100
56
+ Person.connection.add_column "people", "administrator", :boolean
57
+
58
+ assert_nothing_raised { Person.connection.add_index("people", "last_name") }
59
+ assert_nothing_raised { Person.connection.remove_index("people", "last_name") }
60
+
61
+ # Orcl nds shrt indx nms. Sybs 2.
62
+ unless current_adapter?(:OracleAdapter, :SybaseAdapter)
63
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
64
+ assert_nothing_raised { Person.connection.remove_index("people", :column => ["last_name", "first_name"]) }
65
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
66
+ assert_nothing_raised { Person.connection.remove_index("people", :name => "index_people_on_last_name_and_first_name") }
67
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
68
+ assert_nothing_raised { Person.connection.remove_index("people", "last_name_and_first_name") }
69
+ assert_nothing_raised { Person.connection.add_index("people", ["last_name", "first_name"]) }
70
+ assert_nothing_raised { Person.connection.remove_index("people", ["last_name", "first_name"]) }
71
+ end
72
+
73
+ # quoting
74
+ # Note: changed index name from "key" to "key_idx" since "key" is a Firebird reserved word
75
+ assert_nothing_raised { Person.connection.add_index("people", ["key"], :name => "key_idx", :unique => true) }
76
+ assert_nothing_raised { Person.connection.remove_index("people", :name => "key_idx", :unique => true) }
77
+
78
+ # Sybase adapter does not support indexes on :boolean columns
79
+ unless current_adapter?(:SybaseAdapter)
80
+ assert_nothing_raised { Person.connection.add_index("people", %w(last_name first_name administrator), :name => "named_admin") }
81
+ assert_nothing_raised { Person.connection.remove_index("people", :name => "named_admin") }
82
+ end
83
+ end
84
+
85
+ def test_create_table_adds_id
86
+ Person.connection.create_table :testings do |t|
87
+ t.column :foo, :string
88
+ end
89
+
90
+ assert_equal %w(foo id),
91
+ Person.connection.columns(:testings).map { |c| c.name }.sort
92
+ ensure
93
+ Person.connection.drop_table :testings rescue nil
94
+ end
95
+
96
+ def test_create_table_with_not_null_column
97
+ assert_nothing_raised do
98
+ Person.connection.create_table :testings do |t|
99
+ t.column :foo, :string, :null => false
100
+ end
101
+ end
102
+
103
+ assert_raises(ActiveRecord::StatementInvalid) do
104
+ Person.connection.execute "insert into testings (foo) values (NULL)"
105
+ end
106
+ ensure
107
+ Person.connection.drop_table :testings rescue nil
108
+ end
109
+
110
+ def test_create_table_with_defaults
111
+ Person.connection.create_table :testings do |t|
112
+ t.column :one, :string, :default => "hello"
113
+ t.column :two, :boolean, :default => true
114
+ t.column :three, :boolean, :default => false
115
+ t.column :four, :integer, :default => 1
116
+ end
117
+
118
+ columns = Person.connection.columns(:testings)
119
+ one = columns.detect { |c| c.name == "one" }
120
+ two = columns.detect { |c| c.name == "two" }
121
+ three = columns.detect { |c| c.name == "three" }
122
+ four = columns.detect { |c| c.name == "four" }
123
+
124
+ assert_equal "hello", one.default
125
+ assert_equal true, two.default
126
+ assert_equal false, three.default
127
+ assert_equal 1, four.default
128
+
129
+ ensure
130
+ Person.connection.drop_table :testings rescue nil
131
+ end
132
+
133
+ def test_create_table_with_limits
134
+ assert_nothing_raised do
135
+ Person.connection.create_table :testings do |t|
136
+ t.column :foo, :string, :limit => 255
137
+
138
+ t.column :default_int, :integer
139
+
140
+ t.column :one_int, :integer, :limit => 1
141
+ t.column :four_int, :integer, :limit => 4
142
+ t.column :eight_int, :integer, :limit => 8
143
+ end
144
+ end
145
+
146
+ columns = Person.connection.columns(:testings)
147
+ foo = columns.detect { |c| c.name == "foo" }
148
+ assert_equal 255, foo.limit
149
+
150
+ default = columns.detect { |c| c.name == "default_int" }
151
+ one = columns.detect { |c| c.name == "one_int" }
152
+ four = columns.detect { |c| c.name == "four_int" }
153
+ eight = columns.detect { |c| c.name == "eight_int" }
154
+
155
+ if current_adapter?(:PostgreSQLAdapter)
156
+ assert_equal 'integer', default.sql_type
157
+ assert_equal 'smallint', one.sql_type
158
+ assert_equal 'integer', four.sql_type
159
+ assert_equal 'bigint', eight.sql_type
160
+ elsif current_adapter?(:OracleAdapter)
161
+ assert_equal 'NUMBER(38)', default.sql_type
162
+ assert_equal 'NUMBER(1)', one.sql_type
163
+ assert_equal 'NUMBER(4)', four.sql_type
164
+ assert_equal 'NUMBER(8)', eight.sql_type
165
+ end
166
+ ensure
167
+ Person.connection.drop_table :testings rescue nil
168
+ end
169
+
170
+ # SQL Server and Sybase will not allow you to add a NOT NULL column
171
+ # to a table without specifying a default value, so the
172
+ # following test must be skipped
173
+ unless current_adapter?(:SQLServerAdapter, :SybaseAdapter)
174
+ def test_add_column_not_null_without_default
175
+ Person.connection.create_table :testings do |t|
176
+ t.column :foo, :string
177
+ end
178
+ Person.connection.add_column :testings, :bar, :string, :null => false
179
+
180
+ assert_raises(ActiveRecord::StatementInvalid) do
181
+ Person.connection.execute "insert into testings (foo, bar) values ('hello', NULL)"
182
+ end
183
+ ensure
184
+ Person.connection.drop_table :testings rescue nil
185
+ end
186
+ end
187
+
188
+ def test_add_column_not_null_with_default
189
+ Person.connection.create_table :testings do |t|
190
+ t.column :foo, :string
191
+ end
192
+
193
+ con = Person.connection
194
+ Person.connection.enable_identity_insert("testings", true) if current_adapter?(:SybaseAdapter)
195
+ Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}) values (1, 'hello')"
196
+ Person.connection.enable_identity_insert("testings", false) if current_adapter?(:SybaseAdapter)
197
+ assert_nothing_raised {Person.connection.add_column :testings, :bar, :string, :null => false, :default => "default" }
198
+
199
+ assert_raises(ActiveRecord::StatementInvalid) do
200
+ Person.connection.execute "insert into testings (#{con.quote_column_name('id')}, #{con.quote_column_name('foo')}, #{con.quote_column_name('bar')}) values (2, 'hello', NULL)"
201
+ end
202
+ ensure
203
+ Person.connection.drop_table :testings rescue nil
204
+ end
205
+
206
+ # We specifically do a manual INSERT here, and then test only the SELECT
207
+ # functionality. This allows us to more easily catch INSERT being broken,
208
+ # but SELECT actually working fine.
209
+ def test_native_decimal_insert_manual_vs_automatic
210
+ # SQLite3 always uses float in violation of SQL
211
+ # 16 decimal places
212
+ correct_value = (current_adapter?(:SQLiteAdapter) ? '0.123456789012346E20' : '0012345678901234567890.0123456789').to_d
213
+
214
+ Person.delete_all
215
+ Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
216
+ Person.reset_column_information
217
+
218
+ # Do a manual insertion
219
+ if current_adapter?(:OracleAdapter)
220
+ Person.connection.execute "insert into people (id, wealth) values (people_seq.nextval, 12345678901234567890.0123456789)"
221
+ else
222
+ Person.connection.execute "insert into people (wealth) values (12345678901234567890.0123456789)"
223
+ end
224
+
225
+ # SELECT
226
+ row = Person.find(:first)
227
+ assert_kind_of BigDecimal, row.wealth
228
+
229
+ # If this assert fails, that means the SELECT is broken!
230
+ assert_equal correct_value, row.wealth
231
+
232
+ # Reset to old state
233
+ Person.delete_all
234
+
235
+ # Now use the Rails insertion
236
+ assert_nothing_raised { Person.create :wealth => BigDecimal.new("12345678901234567890.0123456789") }
237
+
238
+ # SELECT
239
+ row = Person.find(:first)
240
+ assert_kind_of BigDecimal, row.wealth
241
+
242
+ # If these asserts fail, that means the INSERT (create function, or cast to SQL) is broken!
243
+ assert_equal correct_value, row.wealth
244
+
245
+ # Reset to old state
246
+ Person.connection.del_column "people", "wealth" rescue nil
247
+ Person.reset_column_information
248
+ end
249
+
250
+ def test_native_types
251
+ Person.delete_all
252
+ Person.connection.add_column "people", "last_name", :string
253
+ Person.connection.add_column "people", "bio", :text
254
+ Person.connection.add_column "people", "age", :integer
255
+ Person.connection.add_column "people", "height", :float
256
+ Person.connection.add_column "people", "wealth", :decimal, :precision => '30', :scale => '10'
257
+ Person.connection.add_column "people", "birthday", :datetime
258
+ Person.connection.add_column "people", "favorite_day", :date
259
+ Person.connection.add_column "people", "male", :boolean
260
+ assert_nothing_raised { Person.create :first_name => 'bob', :last_name => 'bobsen', :bio => "I was born ....", :age => 18, :height => 1.78, :wealth => BigDecimal.new("12345678901234567890.0123456789"), :birthday => 18.years.ago, :favorite_day => 10.days.ago, :male => true }
261
+ bob = Person.find(:first)
262
+
263
+ assert_equal 'bob', bob.first_name
264
+ assert_equal 'bobsen', bob.last_name
265
+ assert_equal "I was born ....", bob.bio
266
+ assert_equal 18, bob.age
267
+
268
+ # Test for 30 significent digits (beyond the 16 of float), 10 of them
269
+ # after the decimal place.
270
+ if current_adapter?(:SQLiteAdapter)
271
+ # SQLite3 uses float in violation of SQL. Test for 16 decimal places.
272
+ assert_equal BigDecimal.new('0.123456789012346E20'), bob.wealth
273
+ else
274
+ assert_equal BigDecimal.new("0012345678901234567890.0123456789"), bob.wealth
275
+ end
276
+
277
+ assert_equal true, bob.male?
278
+
279
+ assert_equal String, bob.first_name.class
280
+ assert_equal String, bob.last_name.class
281
+ assert_equal String, bob.bio.class
282
+ assert_equal Fixnum, bob.age.class
283
+ assert_equal Time, bob.birthday.class
284
+
285
+ if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter)
286
+ # Sybase, and Oracle don't differentiate between date/time
287
+ assert_equal Time, bob.favorite_day.class
288
+ else
289
+ assert_equal Date, bob.favorite_day.class
290
+ end
291
+
292
+ assert_equal TrueClass, bob.male?.class
293
+ assert_kind_of BigDecimal, bob.wealth
294
+ end
295
+
296
+ def test_add_remove_single_field_using_string_arguments
297
+ assert !Person.column_methods_hash.include?(:last_name)
298
+
299
+ ActiveRecord::Migration.add_column 'people', 'last_name', :string
300
+
301
+ Person.reset_column_information
302
+ assert Person.column_methods_hash.include?(:last_name)
303
+
304
+ ActiveRecord::Migration.remove_column 'people', 'last_name'
305
+
306
+ Person.reset_column_information
307
+ assert !Person.column_methods_hash.include?(:last_name)
308
+ end
309
+
310
+ def test_add_remove_single_field_using_symbol_arguments
311
+ assert !Person.column_methods_hash.include?(:last_name)
312
+
313
+ ActiveRecord::Migration.add_column :people, :last_name, :string
314
+
315
+ Person.reset_column_information
316
+ assert Person.column_methods_hash.include?(:last_name)
317
+
318
+ ActiveRecord::Migration.remove_column :people, :last_name
319
+
320
+ Person.reset_column_information
321
+ assert !Person.column_methods_hash.include?(:last_name)
322
+ end
323
+
324
+ def test_add_rename
325
+ Person.delete_all
326
+
327
+ begin
328
+ Person.connection.add_column "people", "girlfriend", :string
329
+ Person.create :girlfriend => 'bobette'
330
+
331
+ Person.connection.rename_column "people", "girlfriend", "exgirlfriend"
332
+
333
+ Person.reset_column_information
334
+ bob = Person.find(:first)
335
+
336
+ assert_equal "bobette", bob.exgirlfriend
337
+ ensure
338
+ Person.connection.remove_column("people", "girlfriend") rescue nil
339
+ Person.connection.remove_column("people", "exgirlfriend") rescue nil
340
+ end
341
+
342
+ end
343
+
344
+ def test_rename_column_using_symbol_arguments
345
+ begin
346
+ Person.connection.rename_column :people, :first_name, :nick_name
347
+ Person.reset_column_information
348
+ assert Person.column_names.include?("nick_name")
349
+ ensure
350
+ Person.connection.remove_column("people","nick_name")
351
+ Person.connection.add_column("people","first_name", :string)
352
+ end
353
+ end
354
+
355
+ def test_rename_column
356
+ begin
357
+ Person.connection.rename_column "people", "first_name", "nick_name"
358
+ Person.reset_column_information
359
+ assert Person.column_names.include?("nick_name")
360
+ ensure
361
+ Person.connection.remove_column("people","nick_name")
362
+ Person.connection.add_column("people","first_name", :string)
363
+ end
364
+ end
365
+
366
+ def test_rename_table
367
+ begin
368
+ ActiveRecord::Base.connection.create_table :octopuses do |t|
369
+ t.column :url, :string
370
+ end
371
+ ActiveRecord::Base.connection.rename_table :octopuses, :octopi
372
+
373
+ # Using explicit id in insert for compatibility across all databases
374
+ con = ActiveRecord::Base.connection
375
+ con.enable_identity_insert("octopi", true) if current_adapter?(:SybaseAdapter)
376
+ assert_nothing_raised { con.execute "INSERT INTO octopi (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" }
377
+ con.enable_identity_insert("octopi", false) if current_adapter?(:SybaseAdapter)
378
+
379
+ assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
380
+
381
+ ensure
382
+ ActiveRecord::Base.connection.drop_table :octopuses rescue nil
383
+ ActiveRecord::Base.connection.drop_table :octopi rescue nil
384
+ end
385
+ end
386
+
387
+ def test_rename_table_with_an_index
388
+ begin
389
+ ActiveRecord::Base.connection.create_table :octopuses do |t|
390
+ t.column :url, :string
391
+ end
392
+ ActiveRecord::Base.connection.add_index :octopuses, :url
393
+
394
+ ActiveRecord::Base.connection.rename_table :octopuses, :octopi
395
+
396
+ # Using explicit id in insert for compatibility across all databases
397
+ con = ActiveRecord::Base.connection
398
+ con.enable_identity_insert("octopi", true) if current_adapter?(:SybaseAdapter)
399
+ assert_nothing_raised { con.execute "INSERT INTO octopi (#{con.quote_column_name('id')}, #{con.quote_column_name('url')}) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')" }
400
+ con.enable_identity_insert("octopi", false) if current_adapter?(:SybaseAdapter)
401
+
402
+ assert_equal 'http://www.foreverflying.com/octopus-black7.jpg', ActiveRecord::Base.connection.select_value("SELECT url FROM octopi WHERE id=1")
403
+ assert ActiveRecord::Base.connection.indexes(:octopi).first.columns.include?("url")
404
+ ensure
405
+ ActiveRecord::Base.connection.drop_table :octopuses rescue nil
406
+ ActiveRecord::Base.connection.drop_table :octopi rescue nil
407
+ end
408
+ end
409
+
410
+ def test_change_column
411
+ Person.connection.add_column 'people', 'age', :integer
412
+ old_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
413
+ assert old_columns.find { |c| c.name == 'age' and c.type == :integer }
414
+
415
+ assert_nothing_raised { Person.connection.change_column "people", "age", :string }
416
+
417
+ new_columns = Person.connection.columns(Person.table_name, "#{name} Columns")
418
+ assert_nil new_columns.find { |c| c.name == 'age' and c.type == :integer }
419
+ assert new_columns.find { |c| c.name == 'age' and c.type == :string }
420
+
421
+ old_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
422
+ assert old_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
423
+ assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => false }
424
+ new_columns = Topic.connection.columns(Topic.table_name, "#{name} Columns")
425
+ assert_nil new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == true }
426
+ assert new_columns.find { |c| c.name == 'approved' and c.type == :boolean and c.default == false }
427
+ assert_nothing_raised { Topic.connection.change_column :topics, :approved, :boolean, :default => true }
428
+ end
429
+
430
+ def test_change_column_with_nil_default
431
+ Person.connection.add_column "people", "contributor", :boolean, :default => true
432
+ Person.reset_column_information
433
+ assert Person.new.contributor?
434
+
435
+ assert_nothing_raised { Person.connection.change_column "people", "contributor", :boolean, :default => nil }
436
+ Person.reset_column_information
437
+ assert !Person.new.contributor?
438
+ assert_nil Person.new.contributor
439
+ end
440
+
441
+ def test_change_column_with_new_default
442
+ Person.connection.add_column "people", "administrator", :boolean, :default => true
443
+ Person.reset_column_information
444
+ assert Person.new.administrator?
445
+
446
+ assert_nothing_raised { Person.connection.change_column "people", "administrator", :boolean, :default => false }
447
+ Person.reset_column_information
448
+ assert !Person.new.administrator?
449
+ end
450
+
451
+ def test_change_column_default
452
+ Person.connection.change_column_default "people", "first_name", "Tester"
453
+ Person.reset_column_information
454
+ assert_equal "Tester", Person.new.first_name
455
+ end
456
+
457
+ def test_change_column_default_to_null
458
+ Person.connection.change_column_default "people", "first_name", nil
459
+ Person.reset_column_information
460
+ assert_nil Person.new.first_name
461
+ end
462
+
463
+ def test_add_table
464
+ assert !Reminder.table_exists?
465
+
466
+ WeNeedReminders.up
467
+
468
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
469
+ assert_equal "hello world", Reminder.find(:first).content
470
+
471
+ WeNeedReminders.down
472
+ assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
473
+ end
474
+
475
+ def test_add_table_with_decimals
476
+ Person.connection.drop_table :big_numbers rescue nil
477
+
478
+ assert !BigNumber.table_exists?
479
+ GiveMeBigNumbers.up
480
+
481
+ assert BigNumber.create(
482
+ :bank_balance => 1586.43,
483
+ :big_bank_balance => BigDecimal("1000234000567.95"),
484
+ :world_population => 6000000000,
485
+ :my_house_population => 3,
486
+ :value_of_e => BigDecimal("2.7182818284590452353602875")
487
+ )
488
+
489
+ b = BigNumber.find(:first)
490
+ assert_not_nil b
491
+
492
+ assert_not_nil b.bank_balance
493
+ assert_not_nil b.big_bank_balance
494
+ assert_not_nil b.world_population
495
+ assert_not_nil b.my_house_population
496
+ assert_not_nil b.value_of_e
497
+
498
+ # TODO: set world_population >= 2**62 to cover 64-bit platforms and test
499
+ # is_a?(Bignum)
500
+ assert_kind_of Integer, b.world_population
501
+ assert_equal 6000000000, b.world_population
502
+ assert_kind_of Fixnum, b.my_house_population
503
+ assert_equal 3, b.my_house_population
504
+ assert_kind_of BigDecimal, b.bank_balance
505
+ assert_equal BigDecimal("1586.43"), b.bank_balance
506
+ assert_kind_of BigDecimal, b.big_bank_balance
507
+ assert_equal BigDecimal("1000234000567.95"), b.big_bank_balance
508
+
509
+ # This one is fun. The 'value_of_e' field is defined as 'DECIMAL' with
510
+ # precision/scale explictly left out. By the SQL standard, numbers
511
+ # assigned to this field should be truncated but that's seldom respected.
512
+ if current_adapter?(:PostgreSQLAdapter, :SQLite2Adapter)
513
+ # - PostgreSQL changes the SQL spec on columns declared simply as
514
+ # "decimal" to something more useful: instead of being given a scale
515
+ # of 0, they take on the compile-time limit for precision and scale,
516
+ # so the following should succeed unless you have used really wacky
517
+ # compilation options
518
+ # - SQLite2 has the default behavior of preserving all data sent in,
519
+ # so this happens there too
520
+ assert_kind_of BigDecimal, b.value_of_e
521
+ assert_equal BigDecimal("2.7182818284590452353602875"), b.value_of_e
522
+ elsif current_adapter?(:SQLiteAdapter)
523
+ # - SQLite3 stores a float, in violation of SQL
524
+ assert_kind_of BigDecimal, b.value_of_e
525
+ assert_equal BigDecimal("2.71828182845905"), b.value_of_e
526
+ elsif current_adapter?(:SQLServer)
527
+ # - SQL Server rounds instead of truncating
528
+ assert_kind_of Fixnum, b.value_of_e
529
+ assert_equal 3, b.value_of_e
530
+ else
531
+ # - SQL standard is an integer
532
+ assert_kind_of Fixnum, b.value_of_e
533
+ assert_equal 2, b.value_of_e
534
+ end
535
+
536
+ GiveMeBigNumbers.down
537
+ assert_raises(ActiveRecord::StatementInvalid) { BigNumber.find(:first) }
538
+ end
539
+
540
+ def test_migrator
541
+ assert !Person.column_methods_hash.include?(:last_name)
542
+ assert !Reminder.table_exists?
543
+
544
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
545
+
546
+ assert_equal 3, ActiveRecord::Migrator.current_version
547
+ Person.reset_column_information
548
+ assert Person.column_methods_hash.include?(:last_name)
549
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
550
+ assert_equal "hello world", Reminder.find(:first).content
551
+
552
+ ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/')
553
+
554
+ assert_equal 0, ActiveRecord::Migrator.current_version
555
+ Person.reset_column_information
556
+ assert !Person.column_methods_hash.include?(:last_name)
557
+ assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
558
+ end
559
+
560
+ def test_migrator_one_up
561
+ assert !Person.column_methods_hash.include?(:last_name)
562
+ assert !Reminder.table_exists?
563
+
564
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
565
+
566
+ Person.reset_column_information
567
+ assert Person.column_methods_hash.include?(:last_name)
568
+ assert !Reminder.table_exists?
569
+
570
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 2)
571
+
572
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
573
+ assert_equal "hello world", Reminder.find(:first).content
574
+ end
575
+
576
+ def test_migrator_one_down
577
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/')
578
+
579
+ ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
580
+
581
+ Person.reset_column_information
582
+ assert Person.column_methods_hash.include?(:last_name)
583
+ assert !Reminder.table_exists?
584
+ end
585
+
586
+ def test_migrator_one_up_one_down
587
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
588
+ ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
589
+
590
+ assert !Person.column_methods_hash.include?(:last_name)
591
+ assert !Reminder.table_exists?
592
+ end
593
+
594
+ def test_migrator_verbosity
595
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
596
+ assert PeopleHaveLastNames.message_count > 0
597
+ PeopleHaveLastNames.message_count = 0
598
+
599
+ ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
600
+ assert PeopleHaveLastNames.message_count > 0
601
+ PeopleHaveLastNames.message_count = 0
602
+ end
603
+
604
+ def test_migrator_verbosity_off
605
+ PeopleHaveLastNames.verbose = false
606
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
607
+ assert PeopleHaveLastNames.message_count.zero?
608
+ ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
609
+ assert PeopleHaveLastNames.message_count.zero?
610
+ end
611
+
612
+ def test_migrator_going_down_due_to_version_target
613
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
614
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
615
+
616
+ assert !Person.column_methods_hash.include?(:last_name)
617
+ assert !Reminder.table_exists?
618
+
619
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/')
620
+
621
+ Person.reset_column_information
622
+ assert Person.column_methods_hash.include?(:last_name)
623
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
624
+ assert_equal "hello world", Reminder.find(:first).content
625
+ end
626
+
627
+ def test_schema_info_table_name
628
+ ActiveRecord::Base.table_name_prefix = "prefix_"
629
+ ActiveRecord::Base.table_name_suffix = "_suffix"
630
+ Reminder.reset_table_name
631
+ assert_equal "prefix_schema_info_suffix", ActiveRecord::Migrator.schema_info_table_name
632
+ ActiveRecord::Base.table_name_prefix = ""
633
+ ActiveRecord::Base.table_name_suffix = ""
634
+ Reminder.reset_table_name
635
+ assert_equal "schema_info", ActiveRecord::Migrator.schema_info_table_name
636
+ ensure
637
+ ActiveRecord::Base.table_name_prefix = ""
638
+ ActiveRecord::Base.table_name_suffix = ""
639
+ end
640
+
641
+ def test_proper_table_name
642
+ assert_equal "table", ActiveRecord::Migrator.proper_table_name('table')
643
+ assert_equal "table", ActiveRecord::Migrator.proper_table_name(:table)
644
+ assert_equal "reminders", ActiveRecord::Migrator.proper_table_name(Reminder)
645
+ Reminder.reset_table_name
646
+ assert_equal Reminder.table_name, ActiveRecord::Migrator.proper_table_name(Reminder)
647
+
648
+ # Use the model's own prefix/suffix if a model is given
649
+ ActiveRecord::Base.table_name_prefix = "ARprefix_"
650
+ ActiveRecord::Base.table_name_suffix = "_ARsuffix"
651
+ Reminder.table_name_prefix = 'prefix_'
652
+ Reminder.table_name_suffix = '_suffix'
653
+ Reminder.reset_table_name
654
+ assert_equal "prefix_reminders_suffix", ActiveRecord::Migrator.proper_table_name(Reminder)
655
+ Reminder.table_name_prefix = ''
656
+ Reminder.table_name_suffix = ''
657
+ Reminder.reset_table_name
658
+
659
+ # Use AR::Base's prefix/suffix if string or symbol is given
660
+ ActiveRecord::Base.table_name_prefix = "prefix_"
661
+ ActiveRecord::Base.table_name_suffix = "_suffix"
662
+ Reminder.reset_table_name
663
+ assert_equal "prefix_table_suffix", ActiveRecord::Migrator.proper_table_name('table')
664
+ assert_equal "prefix_table_suffix", ActiveRecord::Migrator.proper_table_name(:table)
665
+ ActiveRecord::Base.table_name_prefix = ""
666
+ ActiveRecord::Base.table_name_suffix = ""
667
+ Reminder.reset_table_name
668
+ end
669
+
670
+ def test_add_drop_table_with_prefix_and_suffix
671
+ assert !Reminder.table_exists?
672
+ ActiveRecord::Base.table_name_prefix = 'prefix_'
673
+ ActiveRecord::Base.table_name_suffix = '_suffix'
674
+ Reminder.reset_table_name
675
+ Reminder.reset_sequence_name
676
+ WeNeedReminders.up
677
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
678
+ assert_equal "hello world", Reminder.find(:first).content
679
+
680
+ WeNeedReminders.down
681
+ assert_raises(ActiveRecord::StatementInvalid) { Reminder.find(:first) }
682
+ ensure
683
+ ActiveRecord::Base.table_name_prefix = ''
684
+ ActiveRecord::Base.table_name_suffix = ''
685
+ Reminder.reset_table_name
686
+ Reminder.reset_sequence_name
687
+ end
688
+
689
+ # FrontBase does not support default values on BLOB/CLOB columns
690
+ unless current_adapter?(:FrontBaseAdapter)
691
+ def test_create_table_with_binary_column
692
+ Person.connection.drop_table :binary_testings rescue nil
693
+
694
+ assert_nothing_raised {
695
+ Person.connection.create_table :binary_testings do |t|
696
+ t.column "data", :binary, :default => "", :null => false
697
+ end
698
+ }
699
+
700
+ columns = Person.connection.columns(:binary_testings)
701
+ data_column = columns.detect { |c| c.name == "data" }
702
+
703
+ if current_adapter?(:OracleAdapter)
704
+ assert_equal "empty_blob()", data_column.default
705
+ else
706
+ assert_equal "", data_column.default
707
+ end
708
+
709
+ Person.connection.drop_table :binary_testings rescue nil
710
+ end
711
+ end
712
+ def test_migrator_with_duplicates
713
+ assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
714
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_duplicate/', nil)
715
+ end
716
+ end
717
+
718
+ def test_migrator_with_missing_version_numbers
719
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 500)
720
+ assert !Person.column_methods_hash.include?(:middle_name)
721
+ assert_equal 4, ActiveRecord::Migrator.current_version
722
+
723
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations_with_missing_versions/', 2)
724
+ assert !Reminder.table_exists?
725
+ assert Person.column_methods_hash.include?(:last_name)
726
+ assert_equal 2, ActiveRecord::Migrator.current_version
727
+ end
728
+
729
+ def test_create_table_with_custom_sequence_name
730
+ return unless current_adapter? :OracleAdapter
731
+
732
+ # table name is 29 chars, the standard sequence name will
733
+ # be 33 chars and fail
734
+ assert_raises(ActiveRecord::StatementInvalid) do
735
+ begin
736
+ Person.connection.create_table :table_with_name_thats_just_ok do |t|
737
+ t.column :foo, :string, :null => false
738
+ end
739
+ ensure
740
+ Person.connection.drop_table :table_with_name_thats_just_ok rescue nil
741
+ end
742
+ end
743
+
744
+ # should be all good w/ a custom sequence name
745
+ assert_nothing_raised do
746
+ begin
747
+ Person.connection.create_table :table_with_name_thats_just_ok,
748
+ :sequence_name => 'suitably_short_seq' do |t|
749
+ t.column :foo, :string, :null => false
750
+ end
751
+
752
+ Person.connection.execute("select suitably_short_seq.nextval from dual")
753
+
754
+ ensure
755
+ Person.connection.drop_table :table_with_name_thats_just_ok,
756
+ :sequence_name => 'suitably_short_seq' rescue nil
757
+ end
758
+ end
759
+
760
+ # confirm the custom sequence got dropped
761
+ assert_raises(ActiveRecord::StatementInvalid) do
762
+ Person.connection.execute("select suitably_short_seq.nextval from dual")
763
+ end
764
+ end
765
+
766
+ end
767
+ end
768
+