activerecord-jdbc-adapter 1.3.0.beta2 → 1.3.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (282) hide show
  1. data/.gitignore +14 -8
  2. data/.travis.yml +40 -31
  3. data/.yardopts +4 -0
  4. data/Appraisals +2 -5
  5. data/CONTRIBUTING.md +46 -0
  6. data/Gemfile +21 -4
  7. data/Gemfile.lock +42 -17
  8. data/{History.txt → History.md} +142 -75
  9. data/README.md +102 -104
  10. data/RUNNING_TESTS.md +76 -0
  11. data/Rakefile.jdbc +20 -0
  12. data/activerecord-jdbc-adapter.gemspec +35 -18
  13. data/gemfiles/rails23.gemfile +4 -3
  14. data/gemfiles/rails23.gemfile.lock +9 -6
  15. data/gemfiles/rails30.gemfile +4 -3
  16. data/gemfiles/rails30.gemfile.lock +9 -6
  17. data/gemfiles/rails31.gemfile +4 -3
  18. data/gemfiles/rails31.gemfile.lock +9 -6
  19. data/gemfiles/rails32.gemfile +4 -3
  20. data/gemfiles/rails32.gemfile.lock +17 -14
  21. data/gemfiles/rails40.gemfile +5 -5
  22. data/gemfiles/rails40.gemfile.lock +17 -69
  23. data/lib/active_record/connection_adapters/firebird_adapter.rb +1 -0
  24. data/lib/active_record/connection_adapters/sqlserver_adapter.rb +1 -0
  25. data/lib/arel/visitors/compat.rb +22 -3
  26. data/lib/arel/visitors/db2.rb +8 -4
  27. data/lib/arel/visitors/derby.rb +14 -13
  28. data/lib/arel/visitors/firebird.rb +5 -4
  29. data/lib/arel/visitors/hsqldb.rb +11 -9
  30. data/lib/arel/visitors/sql_server.rb +89 -61
  31. data/lib/arjdbc.rb +1 -1
  32. data/lib/arjdbc/db2/adapter.rb +181 -212
  33. data/lib/arjdbc/db2/as400.rb +31 -18
  34. data/lib/arjdbc/db2/column.rb +167 -0
  35. data/lib/arjdbc/db2/connection_methods.rb +2 -0
  36. data/lib/arjdbc/derby/adapter.rb +206 -107
  37. data/lib/arjdbc/derby/connection_methods.rb +4 -9
  38. data/lib/arjdbc/firebird.rb +1 -0
  39. data/lib/arjdbc/firebird/adapter.rb +202 -64
  40. data/lib/arjdbc/firebird/connection_methods.rb +20 -0
  41. data/lib/arjdbc/h2/adapter.rb +56 -36
  42. data/lib/arjdbc/hsqldb/adapter.rb +99 -68
  43. data/lib/arjdbc/jdbc/adapter.rb +474 -265
  44. data/lib/arjdbc/jdbc/adapter_java.jar +0 -0
  45. data/lib/arjdbc/jdbc/adapter_require.rb +8 -7
  46. data/lib/arjdbc/jdbc/arel_support.rb +132 -0
  47. data/lib/arjdbc/jdbc/base_ext.rb +8 -7
  48. data/lib/arjdbc/jdbc/callbacks.rb +16 -10
  49. data/lib/arjdbc/jdbc/column.rb +25 -3
  50. data/lib/arjdbc/jdbc/connection.rb +28 -55
  51. data/lib/arjdbc/jdbc/extension.rb +14 -14
  52. data/lib/arjdbc/jdbc/java.rb +6 -3
  53. data/lib/arjdbc/jdbc/jdbc.rake +1 -1
  54. data/lib/arjdbc/jdbc/quoted_primary_key.rb +2 -2
  55. data/lib/arjdbc/jdbc/rake_tasks.rb +1 -1
  56. data/lib/arjdbc/jdbc/type_converter.rb +5 -2
  57. data/lib/arjdbc/mssql/adapter.rb +160 -280
  58. data/lib/arjdbc/mssql/column.rb +182 -0
  59. data/lib/arjdbc/mssql/connection_methods.rb +37 -4
  60. data/lib/arjdbc/mssql/explain_support.rb +13 -21
  61. data/lib/arjdbc/mssql/limit_helpers.rb +79 -42
  62. data/lib/arjdbc/mssql/lock_methods.rb +77 -0
  63. data/lib/arjdbc/mssql/utils.rb +11 -11
  64. data/lib/arjdbc/mysql/adapter.rb +165 -247
  65. data/lib/arjdbc/mysql/column.rb +123 -0
  66. data/lib/arjdbc/mysql/connection_methods.rb +3 -6
  67. data/lib/arjdbc/oracle/adapter.rb +282 -288
  68. data/lib/arjdbc/oracle/column.rb +122 -0
  69. data/lib/arjdbc/oracle/connection_methods.rb +3 -0
  70. data/lib/arjdbc/postgresql/adapter.rb +336 -574
  71. data/lib/arjdbc/postgresql/column.rb +458 -0
  72. data/lib/arjdbc/postgresql/connection_methods.rb +1 -2
  73. data/lib/arjdbc/postgresql/schema_creation.rb +38 -0
  74. data/lib/arjdbc/sqlite3/adapter.rb +189 -145
  75. data/lib/arjdbc/sqlite3/explain_support.rb +1 -1
  76. data/lib/arjdbc/tasks/oracle/enhanced_structure_dump.rb +8 -8
  77. data/lib/arjdbc/util/quoted_cache.rb +60 -0
  78. data/lib/arjdbc/util/table_copier.rb +110 -0
  79. data/lib/arjdbc/version.rb +6 -7
  80. data/pom.xml +56 -2
  81. data/rakelib/02-test.rake +72 -83
  82. data/rakelib/db.rake +29 -17
  83. data/src/java/arjdbc/ArJdbcModule.java +21 -18
  84. data/src/java/arjdbc/db2/DB2RubyJdbcConnection.java +84 -12
  85. data/src/java/arjdbc/derby/DerbyModule.java +140 -143
  86. data/src/java/arjdbc/derby/DerbyRubyJdbcConnection.java +58 -7
  87. data/src/java/arjdbc/h2/H2Module.java +43 -0
  88. data/src/java/arjdbc/informix/InformixRubyJdbcConnection.java +7 -6
  89. data/src/java/arjdbc/jdbc/RubyJdbcConnection.java +1223 -648
  90. data/src/java/arjdbc/mssql/MSSQLRubyJdbcConnection.java +24 -23
  91. data/src/java/arjdbc/mysql/MySQLModule.java +33 -32
  92. data/src/java/arjdbc/mysql/MySQLRubyJdbcConnection.java +147 -30
  93. data/src/java/arjdbc/oracle/OracleModule.java +13 -13
  94. data/src/java/arjdbc/oracle/OracleRubyJdbcConnection.java +114 -6
  95. data/src/java/arjdbc/postgresql/PostgreSQLRubyJdbcConnection.java +166 -36
  96. data/src/java/arjdbc/sqlite3/SQLite3RubyJdbcConnection.java +101 -19
  97. data/src/java/arjdbc/util/QuotingUtils.java +19 -19
  98. metadata +240 -394
  99. data/bench/bench_attributes.rb +0 -13
  100. data/bench/bench_attributes_new.rb +0 -14
  101. data/bench/bench_create.rb +0 -12
  102. data/bench/bench_find_all.rb +0 -12
  103. data/bench/bench_find_all_mt.rb +0 -25
  104. data/bench/bench_model.rb +0 -85
  105. data/bench/bench_new.rb +0 -12
  106. data/bench/bench_new_valid.rb +0 -12
  107. data/bench/bench_valid.rb +0 -13
  108. data/lib/arel/engines/sql/compilers/db2_compiler.rb +0 -9
  109. data/lib/arel/engines/sql/compilers/derby_compiler.rb +0 -6
  110. data/lib/arel/engines/sql/compilers/h2_compiler.rb +0 -6
  111. data/lib/arel/engines/sql/compilers/hsqldb_compiler.rb +0 -15
  112. data/lib/arel/engines/sql/compilers/jdbc_compiler.rb +0 -6
  113. data/lib/arel/engines/sql/compilers/mssql_compiler.rb +0 -46
  114. data/lib/arjdbc/jdbc/missing_functionality_helper.rb +0 -98
  115. data/lib/arjdbc/mssql/lock_helpers.rb +0 -76
  116. data/lib/arjdbc/mssql/tsql_methods.rb +0 -58
  117. data/lib/arjdbc/postgresql/column_cast.rb +0 -134
  118. data/test/activerecord/connections/native_jdbc_mysql/connection.rb +0 -25
  119. data/test/activerecord/jall.sh +0 -7
  120. data/test/activerecord/jtest.sh +0 -3
  121. data/test/assets/flowers.jpg +0 -0
  122. data/test/binary.rb +0 -67
  123. data/test/db/db2.rb +0 -43
  124. data/test/db/db2/binary_test.rb +0 -6
  125. data/test/db/db2/has_many_through_test.rb +0 -6
  126. data/test/db/db2/rake_test.rb +0 -82
  127. data/test/db/db2/rake_test_data.sql +0 -35
  128. data/test/db/db2/reset_column_information_test.rb +0 -5
  129. data/test/db/db2/serialize_test.rb +0 -6
  130. data/test/db/db2/simple_test.rb +0 -81
  131. data/test/db/db2/test_helper.rb +0 -6
  132. data/test/db/db2/unit_test.rb +0 -73
  133. data/test/db/derby.rb +0 -12
  134. data/test/db/derby/binary_test.rb +0 -6
  135. data/test/db/derby/migration_test.rb +0 -74
  136. data/test/db/derby/rake_test.rb +0 -96
  137. data/test/db/derby/reset_column_information_test.rb +0 -6
  138. data/test/db/derby/row_locking_test.rb +0 -20
  139. data/test/db/derby/schema_dump_test.rb +0 -5
  140. data/test/db/derby/serialize_test.rb +0 -6
  141. data/test/db/derby/simple_test.rb +0 -173
  142. data/test/db/derby/test_helper.rb +0 -6
  143. data/test/db/derby/unit_test.rb +0 -32
  144. data/test/db/derby/xml_column_test.rb +0 -17
  145. data/test/db/h2.rb +0 -11
  146. data/test/db/h2/binary_test.rb +0 -6
  147. data/test/db/h2/change_column_test.rb +0 -68
  148. data/test/db/h2/identity_column_test.rb +0 -35
  149. data/test/db/h2/offset_test.rb +0 -49
  150. data/test/db/h2/rake_test.rb +0 -98
  151. data/test/db/h2/schema_dump_test.rb +0 -29
  152. data/test/db/h2/serialize_test.rb +0 -6
  153. data/test/db/h2/simple_test.rb +0 -56
  154. data/test/db/hsqldb.rb +0 -11
  155. data/test/db/hsqldb/binary_test.rb +0 -6
  156. data/test/db/hsqldb/rake_test.rb +0 -101
  157. data/test/db/hsqldb/schema_dump_test.rb +0 -19
  158. data/test/db/hsqldb/serialize_test.rb +0 -6
  159. data/test/db/hsqldb/simple_test.rb +0 -17
  160. data/test/db/informix.rb +0 -13
  161. data/test/db/jdbc.rb +0 -16
  162. data/test/db/jdbc_derby.rb +0 -14
  163. data/test/db/jdbc_h2.rb +0 -17
  164. data/test/db/jdbc_mysql.rb +0 -13
  165. data/test/db/jdbc_postgres.rb +0 -23
  166. data/test/db/jndi_config.rb +0 -32
  167. data/test/db/jndi_pooled_config.rb +0 -32
  168. data/test/db/mssql.rb +0 -11
  169. data/test/db/mssql/binary_test.rb +0 -6
  170. data/test/db/mssql/exec_proc_test.rb +0 -46
  171. data/test/db/mssql/identity_insert_test.rb +0 -18
  172. data/test/db/mssql/ignore_system_views_test.rb +0 -40
  173. data/test/db/mssql/limit_offset_test.rb +0 -190
  174. data/test/db/mssql/multibyte_test.rb +0 -16
  175. data/test/db/mssql/multiple_connections_test.rb +0 -71
  176. data/test/db/mssql/rake_test.rb +0 -143
  177. data/test/db/mssql/reset_column_information_test.rb +0 -6
  178. data/test/db/mssql/row_locking_test.rb +0 -7
  179. data/test/db/mssql/serialize_test.rb +0 -6
  180. data/test/db/mssql/simple_test.rb +0 -140
  181. data/test/db/mssql/transaction_test.rb +0 -6
  182. data/test/db/mssql/types_test.rb +0 -205
  183. data/test/db/mssql/unit_test.rb +0 -249
  184. data/test/db/mysql.rb +0 -4
  185. data/test/db/mysql/_rails_test_mysql.32.out +0 -6585
  186. data/test/db/mysql/binary_test.rb +0 -6
  187. data/test/db/mysql/connection_test.rb +0 -51
  188. data/test/db/mysql/index_length_test.rb +0 -58
  189. data/test/db/mysql/multibyte_test.rb +0 -10
  190. data/test/db/mysql/nonstandard_primary_key_test.rb +0 -39
  191. data/test/db/mysql/rake_test.rb +0 -97
  192. data/test/db/mysql/reset_column_information_test.rb +0 -6
  193. data/test/db/mysql/schema_dump_test.rb +0 -228
  194. data/test/db/mysql/serialize_test.rb +0 -6
  195. data/test/db/mysql/simple_test.rb +0 -187
  196. data/test/db/mysql/statement_escaping_test.rb +0 -46
  197. data/test/db/mysql/transaction_test.rb +0 -6
  198. data/test/db/mysql/types_test.rb +0 -30
  199. data/test/db/mysql/unit_test.rb +0 -93
  200. data/test/db/mysql_config.rb +0 -7
  201. data/test/db/oracle.rb +0 -27
  202. data/test/db/oracle/binary_test.rb +0 -6
  203. data/test/db/oracle/limit_test.rb +0 -24
  204. data/test/db/oracle/multibyte_test.rb +0 -22
  205. data/test/db/oracle/rake_test.rb +0 -100
  206. data/test/db/oracle/reset_column_information_test.rb +0 -6
  207. data/test/db/oracle/serialize_test.rb +0 -6
  208. data/test/db/oracle/simple_test.rb +0 -140
  209. data/test/db/oracle/specific_test.rb +0 -180
  210. data/test/db/oracle/transaction_test.rb +0 -31
  211. data/test/db/oracle/unit_test.rb +0 -31
  212. data/test/db/postgres.rb +0 -11
  213. data/test/db/postgres/_rails_test_postgres.32.out +0 -6405
  214. data/test/db/postgres/a_custom_primary_key_test.rb +0 -50
  215. data/test/db/postgres/active_schema_unit_test.rb +0 -68
  216. data/test/db/postgres/array_type_test.rb +0 -101
  217. data/test/db/postgres/binary_test.rb +0 -6
  218. data/test/db/postgres/connection_test.rb +0 -63
  219. data/test/db/postgres/data_types_test.rb +0 -703
  220. data/test/db/postgres/hstore_test.rb +0 -200
  221. data/test/db/postgres/information_schema_leak_test.rb +0 -30
  222. data/test/db/postgres/json_test.rb +0 -86
  223. data/test/db/postgres/ltree_test.rb +0 -51
  224. data/test/db/postgres/mixed_case_test.rb +0 -29
  225. data/test/db/postgres/native_types_test.rb +0 -124
  226. data/test/db/postgres/rake_test.rb +0 -117
  227. data/test/db/postgres/reserved_test.rb +0 -22
  228. data/test/db/postgres/reset_column_information_test.rb +0 -6
  229. data/test/db/postgres/row_locking_test.rb +0 -21
  230. data/test/db/postgres/schema_dump_test.rb +0 -95
  231. data/test/db/postgres/schema_test.rb +0 -115
  232. data/test/db/postgres/simple_test.rb +0 -260
  233. data/test/db/postgres/table_alias_length_test.rb +0 -16
  234. data/test/db/postgres/transaction_test.rb +0 -6
  235. data/test/db/postgres/unit_test.rb +0 -31
  236. data/test/db/postgres_config.rb +0 -10
  237. data/test/db/sqlite3.rb +0 -6
  238. data/test/db/sqlite3/_rails_test_sqlite3.32.out +0 -6274
  239. data/test/db/sqlite3/has_many_though_test.rb +0 -6
  240. data/test/db/sqlite3/rake_test.rb +0 -71
  241. data/test/db/sqlite3/reset_column_information_test.rb +0 -6
  242. data/test/db/sqlite3/schema_dump_test.rb +0 -6
  243. data/test/db/sqlite3/serialize_test.rb +0 -6
  244. data/test/db/sqlite3/simple_test.rb +0 -268
  245. data/test/db/sqlite3/transaction_test.rb +0 -32
  246. data/test/db/sqlite3/type_conversion_test.rb +0 -104
  247. data/test/has_many_through.rb +0 -61
  248. data/test/informix_simple_test.rb +0 -48
  249. data/test/jdbc/db2.rb +0 -36
  250. data/test/jdbc/oracle.rb +0 -34
  251. data/test/jdbc_column_test.rb +0 -23
  252. data/test/jdbc_common.rb +0 -16
  253. data/test/jdbc_connection_test.rb +0 -196
  254. data/test/jndi_callbacks_test.rb +0 -33
  255. data/test/jndi_test.rb +0 -55
  256. data/test/manualTestDatabase.rb +0 -191
  257. data/test/models/add_not_null_column_to_table.rb +0 -9
  258. data/test/models/auto_id.rb +0 -15
  259. data/test/models/binary.rb +0 -18
  260. data/test/models/custom_pk_name.rb +0 -15
  261. data/test/models/data_types.rb +0 -40
  262. data/test/models/entry.rb +0 -41
  263. data/test/models/mixed_case.rb +0 -22
  264. data/test/models/reserved_word.rb +0 -15
  265. data/test/models/rights_and_roles.rb +0 -57
  266. data/test/models/string_id.rb +0 -17
  267. data/test/models/thing.rb +0 -17
  268. data/test/models/topic.rb +0 -32
  269. data/test/models/validates_uniqueness_of_string.rb +0 -19
  270. data/test/rails/mysql.rb +0 -13
  271. data/test/rails/sqlite3/version.rb +0 -6
  272. data/test/rails_stub.rb +0 -31
  273. data/test/rake_test_support.rb +0 -298
  274. data/test/row_locking.rb +0 -102
  275. data/test/schema_dump.rb +0 -182
  276. data/test/serialize.rb +0 -275
  277. data/test/shared_helper.rb +0 -35
  278. data/test/simple.rb +0 -1317
  279. data/test/sybase_jtds_simple_test.rb +0 -28
  280. data/test/sybase_reset_column_information_test.rb +0 -6
  281. data/test/test_helper.rb +0 -304
  282. data/test/transaction.rb +0 -109
@@ -1,35 +0,0 @@
1
- # Defining helper methods useful both in the definition of test tasks, as well as in the execution
2
- # of tests themselves. Do not require test/unit within this file, as that'll make the rake process
3
- # attempt to run tests itself. Test-specific helpers should go in test/jdbc_common.rb.
4
-
5
- module Kernel
6
- def find_executable?(name)
7
- (ENV['PATH'] || '').split(File::PATH_SEPARATOR).
8
- detect { |p| File.executable?(File.join(p, name)) }
9
- end
10
- end
11
-
12
- module PostgresHelper
13
- def self.pg_cmdline_params
14
- params = ""
15
- params += "-h #{ENV['PGHOST']} " if ENV['PGHOST']
16
- params += "-p #{ENV['PGPORT']} " if ENV['PGPORT']
17
- params
18
- end
19
-
20
- def self.have_postgres?(warn = nil)
21
- if find_executable?("psql")
22
- if `psql -c '\\l' -U postgres #{pg_cmdline_params}2>&1` && $?.exitstatus == 0
23
- true
24
- else
25
- if warn.nil?
26
- warn = "No \"postgres\" role? You might need to execute `createuser postgres -drs' first."
27
- end
28
- send(:warn, warn) if warn # warn == false disables warnings
29
- false
30
- end
31
- end
32
- end
33
- end
34
-
35
- require 'fileutils'
data/test/simple.rb DELETED
@@ -1,1317 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- require 'test_helper'
3
-
4
- require 'set'
5
-
6
- require 'models/data_types'
7
- require 'models/entry'
8
- require 'models/auto_id'
9
- require 'models/string_id'
10
- require 'models/thing'
11
- require 'models/custom_pk_name'
12
- require 'models/validates_uniqueness_of_string'
13
- require 'models/add_not_null_column_to_table'
14
-
15
- ActiveRecord::Schema.verbose = false
16
- ActiveRecord::Base.time_zone_aware_attributes = true if ActiveRecord::Base.respond_to?(:time_zone_aware_attributes)
17
- ActiveRecord::Base.default_timezone = :utc
18
-
19
- module MigrationSetup
20
-
21
- def setup
22
- setup!
23
- end
24
-
25
- def teardown
26
- teardown!
27
- end
28
-
29
- def setup!
30
- MigrationSetup.setup!
31
- end
32
-
33
- def teardown!
34
- MigrationSetup.teardown!
35
- end
36
-
37
- def self.setup!
38
- DbTypeMigration.up
39
- CreateStringIds.up
40
- CreateEntries.up
41
- CreateUsers.up
42
- CreateAutoIds.up
43
- CreateValidatesUniquenessOf.up
44
- CreateThings.up
45
- CreateCustomPkName.up
46
- end
47
-
48
- def self.teardown!
49
- DbTypeMigration.down
50
- CreateStringIds.down
51
- CreateEntries.down
52
- CreateUsers.down
53
- CreateAutoIds.down
54
- CreateValidatesUniquenessOf.down
55
- CreateThings.down
56
- CreateCustomPkName.down
57
- end
58
-
59
- end
60
-
61
- module FixtureSetup
62
- include MigrationSetup
63
-
64
- @@_time_zone = Time.respond_to?(:zone) ? Time.zone : nil
65
-
66
- def setup
67
- super
68
- #
69
- # just a random zone, unlikely to be local, and not UTC
70
- Time.zone = 'Moscow' if Time.respond_to?(:zone)
71
- end
72
-
73
- def teardown
74
- super
75
- #
76
- Time.zone = @@_time_zone if Time.respond_to?(:zone)
77
- end
78
-
79
- end
80
-
81
- module ColumnNameQuotingTests
82
-
83
- def self.included(base)
84
- base.class_eval do
85
- @@column_quote_char = "\""
86
-
87
- def self.column_quote_char(char)
88
- @@column_quote_char = char
89
- end
90
- end
91
- end
92
-
93
- def test_column_names_are_escaped
94
- conn = ActiveRecord::Base.connection
95
- quoted = conn.quote_column_name "foo#{column_quote_char}bar"
96
- assert_equal "#{column_quote_char}foo#{column_quote_char * 2}bar#{column_quote_char}", quoted
97
- end
98
-
99
- protected
100
-
101
- def column_quote_char
102
- @@column_quote_char || "\""
103
- end
104
-
105
- end
106
-
107
- module DirtyAttributeTests
108
-
109
- def test_partial_update_with_updated_at
110
- user = User.create!(:login => 'cicina')
111
- old_updated_at = 61.minutes.ago.in_time_zone
112
-
113
- do_update_all(User, { :updated_at => old_updated_at }, :login => user.login)
114
-
115
- with_partial_updates User, false do
116
- assert_queries(1) { user.save! }
117
- end
118
-
119
- do_update_all(User, { :updated_at => old_updated_at }, :login => user.login)
120
-
121
- with_partial_updates User, true do
122
- assert_queries(0) { user.save! }
123
- assert_datetime_equal old_updated_at, user.reload.updated_at
124
-
125
- assert_queries(1) { user.login = 'cicinbrus'; user.save! }
126
- assert_datetime_not_equal old_updated_at, user.reload.updated_at
127
- end
128
- end
129
-
130
- def test_partial_update_with_updated_on
131
- entry = Entry.create!(:title => 'foo')
132
- old_updated_on = 25.hours.ago.beginning_of_day.in_time_zone
133
-
134
- do_update_all(Entry, { :updated_on => old_updated_on }, :id => entry.id)
135
-
136
- with_partial_updates Entry, false do
137
- assert_queries(2) { 2.times { entry.save! } }
138
- end
139
-
140
- do_update_all(Entry, { :updated_on => old_updated_on }, :id => entry.id)
141
-
142
- with_partial_updates Entry, true do
143
- assert_queries(0) { 2.times { entry.save! } }
144
- assert_date_equal old_updated_on, entry.reload.updated_on
145
-
146
- assert_queries(1) { entry.title = 'bar'; entry.save! }
147
- assert_date_not_equal old_updated_on, entry.reload.updated_on
148
- end
149
- end
150
-
151
- private
152
-
153
- if ActiveRecord::VERSION::MAJOR > 3
154
-
155
- def with_partial_updates(klass, on = true)
156
- old = klass.partial_writes?
157
- klass.partial_writes = on
158
- yield
159
- ensure
160
- klass.partial_writes = old
161
- end
162
-
163
- else
164
-
165
- def with_partial_updates(klass, on = true)
166
- old = klass.partial_updates?
167
- klass.partial_updates = on
168
- yield
169
- ensure
170
- klass.partial_updates = old
171
- end
172
-
173
- end
174
-
175
- def do_update_all(model, values, conditions)
176
- if ar_version('3.2')
177
- model.where(conditions).update_all(values)
178
- else # User.update_all values, conditions deprecated on 4.0
179
- model.update_all(values, conditions)
180
- end
181
- end
182
-
183
- end
184
-
185
- module SimpleTestMethods
186
- include FixtureSetup
187
-
188
- def test_tables
189
- assert_not_empty ActiveRecord::Base.connection.tables
190
- tables = ActiveRecord::Base.connection.tables
191
- assert tables.find { |t| t =~ /^entries$/i }, "entries not created: #{tables.inspect}"
192
- assert tables.map(&:downcase).include?('users'), "users table not found: #{tables.inspect}"
193
- end
194
-
195
- def test_table_exists?
196
- assert_true ActiveRecord::Base.connection.table_exists? 'entries'
197
- assert_false ActiveRecord::Base.connection.table_exists? 'blahbls'
198
- end
199
-
200
- def test_entries_empty
201
- Entry.delete_all
202
- assert_equal 0, Entry.count
203
- end
204
-
205
- def test_find_with_string_slug
206
- new_entry = Entry.create(:title => "Blah")
207
- entry = Entry.find(new_entry.to_param)
208
- assert_equal new_entry.id, entry.id
209
- end
210
-
211
- def test_insert_returns_id
212
- value = ActiveRecord::Base.connection.insert("INSERT INTO entries (title, content, rating) VALUES('insert_title', 'some content', 1)")
213
- assert !value.nil?
214
- entry = Entry.find_by_title('insert_title')
215
- assert_equal entry.id, value
216
-
217
- # Ensure we get the id even if the PK column is not named 'id'
218
- 1.upto(4) do |i|
219
- cpn_name = "return id test#{i}"
220
- cpn = CustomPkName.new
221
- cpn.name = cpn_name
222
- cpn.save
223
- value = cpn.custom_id
224
- assert !value.nil?
225
- cpn = CustomPkName.find_by_name(cpn_name)
226
- assert_equal cpn.custom_id, value
227
- end
228
- end
229
-
230
- def test_create_new_entry
231
- Entry.delete_all
232
-
233
- title = "First post!"
234
- content = "Hello from JRuby on Rails!"
235
- rating = 205.76
236
-
237
- post = Entry.new
238
- post.title = title
239
- post.content = content
240
- post.rating = rating
241
- post.save
242
-
243
- assert_equal 1, Entry.count
244
- end
245
-
246
- def test_create_partial_new_entry
247
- Entry.create(:title => "Blah")
248
- Entry.create(:title => "Bloh")
249
- end
250
-
251
- def test_find_and_update_entry
252
- title = "First post!"
253
- content = "Hello from JRuby on Rails!"
254
- new_title = "First post updated title"
255
- rating = 205.76
256
- user = User.create! :login => "something"
257
- Entry.create! :title => title, :content => content, :rating => rating, :user => user
258
-
259
- post = Entry.first
260
- assert_equal title, post.title
261
- assert_equal content, post.content
262
- assert_equal rating, post.rating
263
-
264
- post.title = new_title
265
- post.save
266
-
267
- post = Entry.first
268
- assert_equal new_title, post.title
269
- end
270
-
271
- def test_destroy_entry
272
- user = User.create! :login => "something"
273
- Entry.create! :title => '1', :content => '', :rating => 1.0, :user => user
274
- Entry.create! :title => '2', :content => '', :rating => 2.0, :user => user
275
-
276
- prev_count = Entry.count
277
- entry = Entry.first
278
- entry.destroy
279
-
280
- assert_equal prev_count - 1, Entry.count
281
- end
282
-
283
- if Entry.respond_to?(:limit)
284
- def test_limit
285
- Entry.limit(10).to_a
286
- end
287
-
288
- def test_count_with_limit
289
- assert_equal Entry.count, Entry.limit(10).count
290
- end
291
- end
292
-
293
- if Time.respond_to?(:zone)
294
-
295
- def test_save_time_with_utc
296
- current_zone = Time.zone
297
- default_zone = ActiveRecord::Base.default_timezone
298
- ActiveRecord::Base.default_timezone = Time.zone = :utc
299
- now = Time.now
300
- my_time = Time.local now.year, now.month, now.day, now.hour, now.min, now.sec
301
- m = DbType.create! :sample_datetime => my_time
302
- m.reload
303
-
304
- assert_equal my_time, m.sample_datetime
305
- rescue
306
- Time.zone = current_zone
307
- ActiveRecord::Base.default_timezone = default_zone
308
- end
309
-
310
- def test_save_time_with_zone
311
- t = Time.now
312
- # precision will only be expected to the second :
313
- original_time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
314
- time = original_time.in_time_zone
315
- e = DbType.create! :sample_datetime => time
316
- assert_equal time, e.reload.sample_datetime
317
- end
318
-
319
- def test_save_date_time
320
- t = Time.now
321
- # precision will only be expected to the second :
322
- time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
323
- e = DbType.create! :sample_datetime => time.to_datetime
324
- assert_equal time, e.reload.sample_datetime.localtime
325
- end
326
-
327
- end
328
-
329
- def test_save_time
330
- # Ruby doesn't have a plain Time class without a date.
331
- time = Time.utc(2012, 12, 18, 21, 10, 15, 0)
332
- e = DbType.new
333
- e.sample_time = time
334
- e.save!
335
-
336
- assert_time_equal time, e.reload.sample_time
337
- end
338
-
339
- def test_save_timestamp
340
- timestamp = Time.utc(2012, 12, 18, 21, 10, 15, 0)
341
- e = DbType.create! :sample_datetime => Time.now
342
- e.sample_timestamp = timestamp
343
- e.save!
344
- assert_timestamp_equal timestamp, e.reload.sample_timestamp
345
- end
346
-
347
- def test_save_timestamp_with_usec
348
- pend 'todo: support precision beyond seconds !?'
349
- timestamp = Time.utc(1942, 11, 30, 01, 53, 59, 123_456)
350
- e = DbType.create! :sample_timestamp => timestamp
351
- assert_timestamp_equal timestamp, e.reload.sample_timestamp
352
- end
353
-
354
- def test_save_date
355
- date = Date.new(2007)
356
- e = DbType.new
357
- e.sample_date = date
358
- e.save!; e.reload
359
- assert_date_type e.sample_date
360
- assert_date_equal date, e.sample_date
361
- end
362
-
363
- def test_save_float
364
- e = DbType.new :sample_float => 12.0
365
- e.save!
366
- assert_equal 12.0, e.reload.sample_float
367
- end
368
-
369
- def test_boolean
370
- e = DbType.create! :sample_float => 0
371
- assert_nil e.reload.sample_boolean # unset boolean should default to nil
372
-
373
- e.update_attributes :sample_boolean => false
374
- assert_equal false, e.reload.sample_boolean
375
-
376
- e.sample_boolean = true
377
- e.save!
378
- assert_equal true, e.reload.sample_boolean
379
- end
380
-
381
- def test_integer
382
- e = DbType.create! :sample_boolean => false
383
- assert_nil e.reload.sample_integer
384
-
385
- e.sample_integer = 10
386
- e.save!
387
- assert_equal 10, e.reload.sample_integer
388
- end
389
-
390
- def test_text
391
- e = DbType.create! :sample_boolean => false
392
- assert_null_text e.reload.sample_text
393
-
394
- e.sample_text = "ooop?"
395
- e.save!
396
- assert_equal "ooop?", e.reload.sample_text
397
- end
398
-
399
- def test_string
400
- e = DbType.create! :sample_boolean => false
401
- assert_empty_string e.reload.sample_string
402
-
403
- e.sample_string = "ooop?"
404
- e.save!
405
- assert_equal "ooop?", e.reload.sample_string
406
- end
407
-
408
- def test_save_binary
409
- # string is 60_000 bytes
410
- binary_string = "\000ABCDEFGHIJKLMNOPQRSTUVWXYZ'\001\003" * 1 # 2_000
411
- e = DbType.new
412
- e.sample_binary = binary_string
413
- e.save!
414
- assert_equal binary_string, e.reload.sample_binary
415
- end
416
-
417
- def test_small_decimal
418
- test_value = 7.3
419
- db_type = DbType.new(:sample_small_decimal => test_value)
420
- db_type.save!
421
- db_type = DbType.find(db_type.id)
422
- assert_kind_of BigDecimal, db_type.sample_small_decimal
423
- assert_equal BigDecimal.new(test_value.to_s), db_type.sample_small_decimal
424
-
425
- test_value = BigDecimal('1.23')
426
- db_type = DbType.create!(:sample_small_decimal => test_value)
427
- if ar_version('3.0')
428
- assert_equal 1, DbType.where("sample_small_decimal < ?", 1.5).count
429
- else
430
- assert_equal 1, DbType.find(:all, :conditions => ["sample_small_decimal < ?", 1.5]).size
431
- end
432
- end
433
-
434
- def test_decimal # _with_zero_scale
435
- test_value = 7000.0
436
- db_type = DbType.create!(:sample_decimal => test_value)
437
- db_type = DbType.find(db_type.id)
438
- assert_kind_of Integer, db_type.sample_decimal
439
- assert_equal test_value.to_i, db_type.sample_decimal
440
- end
441
-
442
- def test_decimal_with_scale
443
- test_value = BigDecimal("100023400056.795")
444
- db_type = DbType.create!(:decimal_with_scale => test_value)
445
- assert_equal test_value, db_type.reload.decimal_with_scale
446
- end
447
-
448
- def test_big_decimal
449
- test_value = 9876543210_9876543210_9876543210.0
450
- db_type = DbType.create!(:big_decimal => test_value)
451
- db_type = DbType.find(db_type.id)
452
- assert_kind_of Bignum, db_type.big_decimal
453
- assert_equal test_value, db_type.big_decimal
454
- end
455
-
456
- # NOTE: relevant on 4.0 as it started using empty_insert_statement_value
457
- def test_empty_insert_statement
458
- DbType.create!
459
- assert DbType.first
460
- assert_not_nil DbType.first.id
461
- end
462
-
463
- def test_negative_default_value
464
- assert_equal(-1, DbType.columns_hash['sample_integer_neg_default'].default)
465
- assert_equal(-1, DbType.new.sample_integer_neg_default)
466
- end
467
-
468
- def test_indexes
469
- indexes = connection.indexes(:entries)
470
- assert_equal 0, indexes.size
471
-
472
- connection.add_index :entries, :updated_on
473
- connection.add_index :entries, [ :title, :user_id ], :unique => true,
474
- :name => 'x_entries_on_title_and_user_id' # <= 30 chars
475
-
476
- indexes = connection.indexes(:entries)
477
- assert_equal 2, indexes.size
478
-
479
- assert_not_nil title_index = indexes.find { |index| index.unique }
480
-
481
- assert_equal "entries", title_index.table.to_s
482
- assert_true title_index.unique
483
- assert_equal [ 'title', 'user_id' ], title_index.columns
484
-
485
- updated_index = (indexes - [ title_index ]).first
486
-
487
- assert_equal "entries", updated_index.table.to_s
488
- assert_equal "index_entries_on_updated_on", updated_index.name
489
- assert ! updated_index.unique
490
- assert_equal [ 'updated_on' ], updated_index.columns
491
-
492
- connection.remove_index :entries, :updated_on
493
- indexes = connection.indexes(:entries)
494
- assert_equal 1, indexes.size
495
- end
496
-
497
- def test_nil_values
498
- e = DbType.create! :sample_integer => '', :sample_string => 'sample'
499
- assert_nil e.reload.sample_integer
500
- end
501
-
502
- # These should make no difference, but might due to the wacky regexp SQL rewriting we do.
503
- def test_save_value_containing_sql
504
- e = DbType.new :sample_string => 'sample'
505
- e.save!
506
-
507
- e.sample_string = e.sample_text = "\n\nselect from nothing where id = 'foo'"
508
- e.save!
509
- end
510
-
511
- def test_invalid
512
- title = "First post!"
513
- content = "Hello from JRuby on Rails!"
514
- rating = 205.76
515
- user = User.create! :login => "something"
516
- Entry.create! :title => title, :content => content, :rating => rating, :user => user
517
-
518
- e = Entry.new(:title => title, :content => content, :rating => ' ')
519
- assert e.valid?
520
- end
521
-
522
- def test_reconnect
523
- DbType.create! :sample_string => 'sample'
524
- assert_equal 1, DbType.count
525
- ActiveRecord::Base.connection.reconnect!
526
- assert_equal 1, DbType.count
527
- end
528
-
529
- if defined?(JRUBY_VERSION)
530
- def test_connection_valid
531
- assert_raise(ActiveRecord::JDBCError) do
532
- connection = ActiveRecord::Base.connection
533
- connection.raw_connection.with_connection_retry_guard do |c|
534
- begin
535
- stmt = c.createStatement
536
- stmt.execute "bogus sql"
537
- ensure
538
- stmt.close rescue nil
539
- end
540
- end
541
- end
542
- end
543
-
544
- class Animal < ActiveRecord::Base; end
545
-
546
- def test_fetching_columns_for_nonexistent_table
547
- disable_logger(Animal.connection) do
548
- assert_raise(ActiveRecord::StatementInvalid, ActiveRecord::JDBCError) do
549
- Animal.columns
550
- end
551
- end
552
- end
553
- end
554
-
555
- def test_disconnect
556
- DbType.create! :sample_string => 'sample'
557
- assert_equal 1, DbType.count
558
- ActiveRecord::Base.clear_active_connections!
559
- ActiveRecord::Base.connection_pool.disconnect! if ActiveRecord::Base.respond_to?(:connection_pool)
560
- assert !ActiveRecord::Base.connected?
561
- assert_equal 1, DbType.count
562
- assert ActiveRecord::Base.connected?
563
- end
564
-
565
- def test_add_not_null_column_to_table
566
- AddNotNullColumnToTable.up
567
- AddNotNullColumnToTable.down
568
- end
569
-
570
- def test_add_null_column_with_default
571
- Entry.connection.add_column :entries, :color, :string, :null => false, :default => "blue"
572
- created_columns = Entry.connection.columns('entries')
573
-
574
- color = created_columns.detect { |c| c.name == 'color' }
575
- assert !color.null
576
- end
577
-
578
- def test_add_null_column_with_no_default
579
- # You must specify a default value with most databases
580
- if ActiveRecord::Base.connection.adapter_name =~ /mysql/i
581
- Entry.connection.add_column :entries, :color, :string, :null => false
582
- created_columns = Entry.connection.columns('entries')
583
-
584
- color = created_columns.detect { |c| c.name == 'color' }
585
- assert !color.null
586
- end
587
- end
588
-
589
- def test_add_null_column_with_nil_default
590
- # You must specify a default value with most databases
591
- if ActiveRecord::Base.connection.adapter_name =~ /mysql/i
592
- Entry.connection.add_column :entries, :color, :string, :null => false, :default => nil
593
- created_columns = Entry.connection.columns('entries')
594
-
595
- color = created_columns.detect { |c| c.name == 'color' }
596
- assert !color.null
597
- end
598
- end
599
-
600
- def test_validates_uniqueness_of_strings_case_sensitive
601
- name_lower = ValidatesUniquenessOfString.new(:cs_string => "name", :ci_string => '1')
602
- name_lower.save!
603
-
604
- name_upper = ValidatesUniquenessOfString.new(:cs_string => "NAME", :ci_string => '2')
605
- assert_nothing_raised do
606
- name_upper.save!
607
- end
608
-
609
- name_lower_collision = ValidatesUniquenessOfString.new(:cs_string => "name", :ci_string => '3')
610
- assert_raise ActiveRecord::RecordInvalid do
611
- name_lower_collision.save!
612
- end
613
-
614
- name_upper_collision = ValidatesUniquenessOfString.new(:cs_string => "NAME", :ci_string => '4')
615
- assert_raise ActiveRecord::RecordInvalid do
616
- name_upper_collision.save!
617
- end
618
- end
619
-
620
- def test_validates_uniqueness_of_strings_case_insensitive
621
- name_lower = ValidatesUniquenessOfString.new(:cs_string => '1', :ci_string => "name")
622
- name_lower.save!
623
-
624
- name_upper = ValidatesUniquenessOfString.new(:cs_string => '2', :ci_string => "NAME")
625
- assert_raise ActiveRecord::RecordInvalid do
626
- name_upper.save!
627
- end
628
-
629
- name_lower_collision = ValidatesUniquenessOfString.new(:cs_string => '3', :ci_string => "name")
630
- assert_raise ActiveRecord::RecordInvalid do
631
- name_lower_collision.save!
632
- end
633
-
634
- alternate_name_upper = ValidatesUniquenessOfString.new(:cs_string => '4', :ci_string => "ALTERNATE_NAME")
635
- assert_nothing_raised do
636
- alternate_name_upper.save!
637
- end
638
-
639
- alternate_name_upper_collision = ValidatesUniquenessOfString.new(:cs_string => '5', :ci_string => "ALTERNATE_NAME")
640
- assert_raise ActiveRecord::RecordInvalid do
641
- alternate_name_upper_collision.save!
642
- end
643
-
644
- alternate_name_lower = ValidatesUniquenessOfString.new(:cs_string => '6', :ci_string => "alternate_name")
645
- assert_raise ActiveRecord::RecordInvalid do
646
- alternate_name_lower.save!
647
- end
648
- end
649
-
650
- def test_substitute_binds_has_no_side_effect_on_binds_parameter
651
- binds = [ [ Entry.columns_hash['title'], 'test1' ] ]
652
- binds_dup = binds.dup
653
- sql = 'SELECT * FROM entries WHERE title = ?'
654
- Entry.connection.send :substitute_binds, sql, binds
655
- assert_equal binds_dup, binds
656
- end
657
-
658
- def test_find_by_sql_with_binds
659
- Entry.create!(:title => 'qqq', :content => '', :rating => 4)
660
- Entry.create!(:title => 'www', :content => '', :rating => 5)
661
- Entry.create!(:title => 'www', :content => '', :rating => 6)
662
- #ActiveRecord::Base.logger.level = Logger::DEBUG
663
- sql = 'SELECT * FROM entries WHERE ( title = ? OR title = ? ) AND rating < ? AND rating > ?'
664
- entries = Entry.find_by_sql [ sql, 'qqq', 'www', 6, 4 ]
665
- assert_equal 1, entries.size
666
- ensure
667
- #ActiveRecord::Base.logger.level = Logger::WARN
668
- end
669
-
670
- def test_find_by_sql_with_named_binds
671
- Entry.create!(:title => 'qqq', :content => '', :rating => 4)
672
- Entry.create!(:title => 'www', :content => '', :rating => 5)
673
- Entry.create!(:title => 'www', :content => '', :rating => 6)
674
- #ActiveRecord::Base.logger.level = Logger::DEBUG
675
- sql = 'SELECT * FROM entries WHERE ( title = :title OR title = :title ) AND rating < :upper AND rating > :lower'
676
- entries = Entry.find_by_sql [ sql, { :title => 'www', :upper => 6, :lower => 4 } ]
677
- assert_equal 1, entries.size
678
- ensure
679
- #ActiveRecord::Base.logger.level = Logger::WARN
680
- end
681
-
682
- def test_create_bind_param_with_q_mark
683
- str = "Don' botharrr talkin' like one, savvy? Right?!?"
684
- db_type = DbType.create! :sample_string => str.dup
685
- assert_equal str, db_type.reload.sample_string
686
-
687
- entry = Entry.create! :title => 'foo!', :content => 'bar?'
688
- assert_equal 'foo!', entry.reload.title
689
- assert_equal 'bar?', entry.content
690
- end
691
-
692
- def test_exec_update_bind_param_with_q_mark
693
- entry = Entry.create! :title => 'foo!'
694
-
695
- sql = "UPDATE entries SET title = ? WHERE id = #{entry.id}"
696
- connection.exec_update sql, 'UPDATE(with_q_mark)', [ [ nil, "bar?" ] ]
697
- assert_equal 'bar?', entry.reload.title
698
- end
699
-
700
- def test_exec_insert_bind_param_with_q_mark
701
- sql = "INSERT INTO entries(title) VALUES (?)"
702
- connection.exec_insert sql, 'INSERT(with_q_mark)', [ [ nil, "bar?!?" ] ]
703
-
704
- entries = Entry.find_by_sql "SELECT * FROM entries WHERE title = 'bar?!?'"
705
- assert entries.first
706
- end
707
-
708
- def test_raw_insert_bind_param_with_q_mark
709
- sql = "INSERT INTO entries(title) VALUES (?)"
710
- name = "INSERT(raw_with_q_mark)"
711
- pk = nil; id_value = nil; sequence_name = nil
712
- connection.insert sql, name, pk, id_value, sequence_name, [ [ nil, "?!huu!?" ] ]
713
- assert Entry.exists?([ 'title LIKE ?', "%?!huu!?%" ])
714
- end if Test::Unit::TestCase.ar_version('3.1') # no binds argument for <= 3.0
715
-
716
- def test_raw_update_bind_param_with_q_mark
717
- entry = Entry.create! :title => 'foo!'
718
-
719
- sql = "UPDATE entries SET title = ? WHERE id = #{entry.id}"
720
- name = "UPDATE(raw_with_q_mark)"
721
- title_column = Entry.columns.find { |n| n.to_s == 'title' }
722
- connection.update sql, name, [ [ title_column, "bar?" ] ]
723
- assert_equal 'bar?', entry.reload.title
724
-
725
- sql = "UPDATE entries SET title = ? WHERE id = ?"
726
- connection.update sql, name, [ [ nil, "?baz?!?" ], [ nil, entry.id ] ]
727
- assert_equal '?baz?!?', entry.reload.title
728
- end if Test::Unit::TestCase.ar_version('3.1') # no binds argument for <= 3.0
729
-
730
- def test_raw_delete_bind_param_with_q_mark
731
- entry = Entry.create! :title => 'foo?!?'
732
-
733
- sql = "DELETE FROM entries WHERE title = ?"
734
- name = "DELETE(raw_with_q_mark)"
735
- connection.delete sql, name, [ [ nil, "foo?!?" ] ]
736
- assert ! Entry.exists?(entry.id)
737
- end if Test::Unit::TestCase.ar_version('3.1') # no binds argument for <= 3.0
738
-
739
- class ChangeEntriesTable < ActiveRecord::Migration
740
- def self.up
741
- change_table :entries do |t|
742
- t.string :author
743
- end
744
- end
745
- def self.down
746
- change_table :entries do |t|
747
- t.remove :author
748
- end
749
- end
750
- end
751
-
752
- def test_change_table
753
- attributes = {
754
- :title => 'welcome to the real world',
755
- :content => '... TO BE CONTINUED ...',
756
- :author => 'kares'
757
- }
758
- assert_raise ActiveRecord::UnknownAttributeError do
759
- Entry.create! attributes
760
- end
761
-
762
- ChangeEntriesTable.up
763
- Entry.reset_column_information
764
- begin
765
- Entry.create! attributes
766
- if Entry.respond_to?(:where)
767
- e = Entry.where :author => 'kares'
768
- else # 2.3
769
- e = Entry.all :conditions => { :author => 'kares' }
770
- end
771
- assert e.first
772
- ensure
773
- ChangeEntriesTable.down # rescue nil
774
- Entry.reset_column_information
775
- end
776
- end # if Test::Unit::TestCase.ar_version('3.0')
777
-
778
- def test_string_id
779
- f = StringId.new
780
- f.id = "some_string"
781
- f.save
782
- f = StringId.first #reload is essential
783
- assert_equal "some_string", f.id
784
- end
785
-
786
- def test_handles_quotes_inside_of_strings
787
- content_json = {
788
- "comments" => [
789
- "First I was like, \"What, you've got crazy pants\"",
790
- "And then he was like, \"Yeah dude, total crazy pantalones\""
791
- ]
792
- }.to_json
793
-
794
- post = Entry.new :title => 'comment entry'
795
- post.content = content_json
796
- post.save!
797
-
798
- assert_equal content_json, post.reload.content
799
- end
800
-
801
- def test_exec_insert
802
- name_column = Thing.columns.detect { |column| column.name.to_s == 'name' }
803
- created_column = Thing.columns.detect { |column| column.name.to_s == 'created_at' }
804
- updated_column = Thing.columns.detect { |column| column.name.to_s == 'updated_at' }
805
- now = Time.zone.now
806
-
807
- binds = [ [ name_column, 'ferko' ], [ created_column, now ], [ updated_column, now ] ]
808
- connection.exec_insert "INSERT INTO things VALUES (?,?,?)", 'INSERT_1', binds
809
- assert Thing.find_by_name 'ferko'
810
- # NOTE: #exec_insert accepts 5 arguments on AR-4.0 :
811
- binds = [ [ name_column, 'jozko' ], [ created_column, now ], [ updated_column, now ] ]
812
- connection.exec_insert "INSERT INTO things VALUES (?,?,?)", 'INSERT_2', binds, nil, nil
813
- assert Thing.find_by_name 'jozko'
814
- end
815
-
816
- def test_exec_query_result
817
- Entry.delete_all
818
- user1 = User.create! :login => 'user1'
819
- user2 = User.create! :login => 'user2'
820
- Entry.create! :title => 'user11', :user_id => user1.id
821
- Entry.create! :title => 'user12', :user_id => user1.id
822
- Entry.create! :title => 'user21', :user_id => user2.id
823
-
824
- result = Entry.connection.exec_query 'SELECT * FROM entries'
825
-
826
- if ar_version('3.1')
827
- assert_instance_of ActiveRecord::Result, result
828
- assert_not_empty result.columns
829
- columns = Entry.columns.map { |column| column.name.to_s }
830
- assert_equal Set.new(columns), Set.new(result.columns)
831
-
832
- assert_equal 3, result.rows.size
833
- assert_instance_of Array, result.rows[0]
834
- assert_equal 'user11', result.rows[0][1]
835
- assert_equal 'user12', result.rows[1][1]
836
- else
837
- assert_instance_of Array, result
838
- assert_equal 3, result.size
839
- assert_instance_of Hash, result[0]
840
- assert_equal 'user11', result[0]['title']
841
- assert_equal user1.id, result[0]['user_id']
842
- end
843
- end
844
-
845
- def test_exec_query_empty_result
846
- Entry.delete_all; User.delete_all
847
-
848
- result = User.connection.exec_query 'SELECT * FROM users'
849
-
850
- if ar_version('3.1')
851
- assert_instance_of ActiveRecord::Result, result
852
- assert_not_empty result.columns
853
- columns = User.columns.map { |column| column.name.to_s }
854
- assert_equal Set.new(columns), Set.new(result.columns)
855
-
856
- assert_equal 0, result.rows.size
857
- else
858
- assert_instance_of Array, result
859
- assert_equal 0, result.size
860
- end
861
- end
862
-
863
- def test_exec_query_raw
864
- User.delete_all
865
- User.create! :login => 'user1'
866
- User.create! :login => 'user2'
867
-
868
- result = User.connection.exec_query_raw 'SELECT * FROM users'
869
-
870
- assert_instance_of Array, result
871
- assert_equal 2, result.size
872
- assert_instance_of Hash, result[0]
873
- assert_equal 'user1', result[0]['login']
874
- assert_equal 'user2', result[1]['login']
875
- end
876
-
877
- def test_exec_query_raw_yields
878
- User.create! :login => 'user3'
879
- User.create! :login => 'user4'
880
-
881
- arel = User.select('id, login, created_at').where("login = 'user3' or login = 'user4'")
882
- yielded = 0
883
- ActiveRecord::Base.connection.exec_query_raw(arel) do |*args| # id, login, created_at
884
- assert_equal 3, args.size
885
- yielded += 1
886
- case yielded
887
- when 1
888
- assert_equal 'user3', args[1]
889
- when 2
890
- assert_equal 'user4', args[1]
891
- else
892
- fail "yielded 3 times"
893
- end
894
- end
895
- assert yielded == 2
896
- end if Test::Unit::TestCase.ar_version('3.0')
897
-
898
- def test_select
899
- Entry.delete_all
900
- user = User.create! :login => 'select'
901
- Entry.create! :title => 'title 1', :content => 'content 1', :user_id => user.id, :rating => 1.0
902
- Entry.create! :title => 'title 2', :content => 'content 2', :user_id => user.id, :rating => 2.0
903
-
904
- # rows = connection.execute 'SELECT * FROM entries'
905
- # column_order = rows.first.keys
906
-
907
- result = connection.select 'SELECT * FROM entries'
908
-
909
- if ar_version('4.0')
910
- assert_instance_of ActiveRecord::Result, result
911
- assert_equal 2, result.rows.size
912
- else
913
- assert_instance_of Array, result
914
- assert_equal 2, result.size
915
- end
916
- end
917
-
918
- def test_select_rows
919
- Entry.delete_all
920
- user = User.create! :login => 'select_rows'
921
- Entry.create! :title => 'title 1', :content => 'content 1', :user_id => user.id
922
- Entry.create! :title => 'title 2', :content => 'content 2', :user_id => user.id, :rating => 1.0
923
-
924
- rows = connection.execute 'SELECT * FROM entries'
925
- column_order = rows.first.keys
926
-
927
- rows = connection.select_rows 'SELECT * FROM entries'
928
- assert_instance_of Array, rows
929
- assert_equal 2, rows.size
930
-
931
- row = rows[0]
932
- column_order.each_with_index do |column, i|
933
- case column.to_s
934
- when 'id' then assert_not_nil row[i]
935
- when 'title' then assert_equal 'title 1', row[i]
936
- when 'content' then assert_equal 'content 1', row[i]
937
- when 'user_id' then assert_equal user.id, row[i]
938
- when 'rating' then assert_nil row[i]
939
- when 'updated_on' then assert_not_nil row[i]
940
- else raise "unexpected entries row: #{column.inspect}"
941
- end
942
- end
943
-
944
- row = rows[1]
945
- column_order.each_with_index do |column, i|
946
- case column.to_s
947
- when 'id' then assert_not_nil row[i]
948
- when 'title' then assert_equal 'title 2', row[i]
949
- when 'content' then assert_equal 'content 2', row[i]
950
- when 'user_id' then assert_equal user.id, row[i]
951
- when 'rating' then assert_not_nil row[i]
952
- when 'updated_on' then assert_not_nil row[i]
953
- else raise "unexpected entries row: #{column.inspect}"
954
- end
955
- end
956
- end
957
-
958
- def test_update
959
- user = User.create! :login => 'update'
960
-
961
- User.update(user.id, :login => 'UPDATEd')
962
- assert_equal 'UPDATEd', user.reload.login
963
- end
964
-
965
- def test_connection_alive_sql
966
- connection = ActiveRecord::Base.connection
967
- if alive_sql = connection.config[:connection_alive_sql]
968
- connection.execute alive_sql
969
- end
970
- # if no alive SQL than JDBC 4.0 driver's "alive" test will be used
971
- end
972
-
973
- def test_connection_valid
974
- connection = ActiveRecord::Base.connection
975
- assert connection.active? # JDBC connection.isValid (if alive_sql not set)
976
- end
977
-
978
- def test_query_cache
979
- user_1 = User.create! :login => 'query_cache_1'
980
- user_2 = User.create! :login => 'query_cache_2'
981
- user_3 = User.create! :login => 'query_cache_3'
982
- # NOTE: on 3.1 AR::Base.cache does not cache if AR not configured,
983
- # due : `if ActiveRecord::Base.configurations.blank?; yield ...`
984
- User.connection.cache do # instead of simply `User.cache`
985
- id1 = user_1.id; id2 = user_2.id
986
- assert_queries(2) { User.find(id1); User.find(id1); User.find(id2); User.find(id1) }
987
- end
988
- User.connection.uncached do
989
- id1 = user_1.id; id3 = user_3.id
990
- assert_queries(3) { User.find(id3); User.find(id1); User.find(id3) }
991
- end
992
- end
993
-
994
- protected
995
-
996
- def assert_date_type(value)
997
- assert_instance_of Date, value
998
- end
999
-
1000
- end
1001
-
1002
- module MultibyteTestMethods
1003
- include MigrationSetup
1004
-
1005
- if defined?(JRUBY_VERSION)
1006
- def setup
1007
- super
1008
- config = ActiveRecord::Base.connection.config
1009
- jdbc_driver = ActiveRecord::ConnectionAdapters::JdbcDriver.new(config[:driver])
1010
- @java_connection = jdbc_driver.connection(config[:url], config[:username], config[:password])
1011
- @java_connection.setAutoCommit(true)
1012
- end
1013
-
1014
- def teardown
1015
- @java_connection.close
1016
- super
1017
- end
1018
-
1019
- def test_select_multibyte_string
1020
- @java_connection.createStatement().
1021
- execute("insert into entries (id, title, content) values (1, 'テスト', '本文')")
1022
- entry = Entry.first
1023
- assert_equal "テスト", entry.title
1024
- assert_equal "本文", entry.content
1025
- assert_equal entry, Entry.find_by_title("テスト")
1026
- end
1027
-
1028
- def test_update_multibyte_string
1029
- Entry.create!(:title => "テスト", :content => "本文")
1030
- rs = @java_connection.createStatement().
1031
- executeQuery("select title, content from entries")
1032
- assert rs.next
1033
- assert_equal "テスト", rs.getString(1)
1034
- assert_equal "本文", rs.getString(2)
1035
- end
1036
- end
1037
-
1038
- def test_multibyte_aliasing
1039
- str = "テスト"
1040
- quoted_alias = Entry.connection.quote_column_name(str)
1041
- sql = "SELECT title AS #{quoted_alias} from entries"
1042
- records = Entry.connection.select_all(sql)
1043
- records.each do |rec|
1044
- rec.keys.each do |key|
1045
- assert_equal str, key
1046
- end
1047
- end
1048
- end
1049
-
1050
- def test_chinese_word
1051
- chinese_word = '中文'
1052
- new_entry = Entry.create(:title => chinese_word)
1053
- new_entry.reload
1054
- assert_equal chinese_word, new_entry.title
1055
- end
1056
- end
1057
-
1058
- module NonUTF8EncodingMethods
1059
- def setup
1060
- @connection = ActiveRecord::Base.remove_connection
1061
- latin2_connection = @connection.dup
1062
- latin2_connection[:encoding] = 'latin2'
1063
- latin2_connection.delete(:url) # pre-gen url gets stashed; remove to re-gen
1064
- ActiveRecord::Base.establish_connection latin2_connection
1065
- CreateEntries.up
1066
- end
1067
-
1068
- def teardown
1069
- CreateEntries.down
1070
- ActiveRecord::Base.establish_connection @connection
1071
- end
1072
-
1073
- def test_nonutf8_encoding_in_entry
1074
- prague_district = 'hradčany'
1075
- new_entry = Entry.create :title => prague_district
1076
- new_entry.reload
1077
- assert_equal prague_district, new_entry.title
1078
- end
1079
- end
1080
-
1081
- module XmlColumnTests
1082
-
1083
- def self.included(base)
1084
- base.send :include, TestMethods if base.ar_version('3.1')
1085
- end
1086
-
1087
- class XmlModel < ActiveRecord::Base; end
1088
-
1089
- module TestMethods
1090
-
1091
- def test_create_xml_column
1092
- create_xml_models! do |t|
1093
- skip('TableDefinition#xml not-implemented') unless t.respond_to?(:xml)
1094
- end
1095
-
1096
- xml_column = connection.columns(:xml_models).detect do |c|
1097
- c.name == "xml_col"
1098
- end
1099
-
1100
- assert_xml_type xml_column.sql_type
1101
- ensure
1102
- drop_xml_models! rescue false
1103
- end
1104
-
1105
- def test_use_xml_column
1106
- if created = ( ( create_xml_models! || true ) rescue nil )
1107
-
1108
- XmlModel.create! :xml_col => "<xml><LoVE><![CDATA[Rubyist's <3 XML!]]></LoVE></xml>"
1109
-
1110
- assert xml_model = XmlModel.first
1111
-
1112
- unless xml_sql_type =~ /text/i
1113
- require 'rexml/document'
1114
- doc = REXML::Document.new xml_model.xml_col
1115
- assert_equal "Rubyist's <3 XML!", doc.root.elements.first.text
1116
- end
1117
-
1118
- else
1119
- skip('TableDefinition#xml not-implemented')
1120
- end
1121
- ensure
1122
- drop_xml_models! if created
1123
- end
1124
-
1125
- protected
1126
-
1127
- def assert_xml_type sql_type
1128
- assert_equal xml_sql_type, sql_type
1129
- end
1130
-
1131
- def xml_sql_type
1132
- 'text'
1133
- end
1134
-
1135
- private
1136
-
1137
- def create_xml_models!
1138
- connection.create_table(:xml_models) do |t|
1139
- yield(t) if block_given?
1140
- t.xml :xml_col
1141
- end
1142
- end
1143
-
1144
- def drop_xml_models!
1145
- disable_logger(connection) do
1146
- connection.drop_table(:xml_models)
1147
- end
1148
- end
1149
-
1150
- end
1151
-
1152
- end
1153
-
1154
- module ActiveRecord3TestMethods
1155
-
1156
- def self.included(base)
1157
- base.send :include, TestMethods if base.ar_version('3.0')
1158
- end
1159
-
1160
- module TestMethods
1161
-
1162
- def test_visitor_accessor
1163
- adapter = Entry.connection
1164
- adapter_spec = adapter.config[:adapter_spec]
1165
- visitor_type = adapter_spec.arel2_visitors(adapter.config).values.first
1166
- assert_not_nil adapter.visitor
1167
- assert_kind_of visitor_type, adapter.visitor
1168
- end if Test::Unit::TestCase.ar_version('3.2') # >= 3.2
1169
-
1170
- def test_where
1171
- user = User.create! :login => "blogger"
1172
- entry = Entry.create! :title => 'something', :content => 'JRuby on Rails !', :rating => 42.1, :user => user
1173
-
1174
- entries = Entry.where(:title => entry.title)
1175
- assert_equal entry, entries.first
1176
- end
1177
-
1178
- def test_remove_nonexistent_index
1179
- assert_raise(ArgumentError, ActiveRecord::StatementInvalid, ActiveRecord::JDBCError) do
1180
- connection.remove_index :entries, :nonexistent_index
1181
- end
1182
- end
1183
-
1184
- def test_add_index_with_invalid_name_length
1185
- index_name = 'x' * (connection.index_name_length + 1)
1186
- assert_raise(ArgumentError) do
1187
- connection.add_index "entries", "title", :name => index_name
1188
- end
1189
- end
1190
-
1191
- def test_model_with_no_id
1192
- assert_nothing_raised do
1193
- Thing.create! :name => "a thing"
1194
- end
1195
- assert_equal 1, Thing.count
1196
- end
1197
-
1198
- end
1199
-
1200
- end
1201
-
1202
- module ResetColumnInformationTestMethods
1203
- class Fhqwhgad < ActiveRecord::Base
1204
- end
1205
-
1206
- def test_reset_column_information
1207
- drop_fhqwhgads_table!
1208
- create_fhqwhgads_table_1!
1209
- Fhqwhgad.reset_column_information
1210
- assert_equal ["id", "come_on"].sort, Fhqwhgad.columns.map{|c| c.name}.sort, "columns should be correct the first time"
1211
-
1212
- drop_fhqwhgads_table!
1213
- create_fhqwhgads_table_2!
1214
- Fhqwhgad.reset_column_information
1215
- assert_equal ["id", "to_the_limit"].sort, Fhqwhgad.columns.map{|c| c.name}.sort, "columns should be correct the second time"
1216
- ensure
1217
- drop_fhqwhgads_table!
1218
- end
1219
-
1220
- private
1221
-
1222
- def drop_fhqwhgads_table!
1223
- ActiveRecord::Schema.define do
1224
- suppress_messages do
1225
- drop_table :fhqwhgads if table_exists? :fhqwhgads
1226
- end
1227
- end
1228
- end
1229
-
1230
- def create_fhqwhgads_table_1!
1231
- ActiveRecord::Schema.define do
1232
- suppress_messages do
1233
- create_table :fhqwhgads do |t|
1234
- t.string :come_on
1235
- end
1236
- end
1237
- end
1238
- end
1239
-
1240
- def create_fhqwhgads_table_2!
1241
- ActiveRecord::Schema.define do
1242
- suppress_messages do
1243
- create_table :fhqwhgads do |t|
1244
- t.string :to_the_limit, :null=>false, :default=>'everybody'
1245
- end
1246
- end
1247
- end
1248
- end
1249
- end
1250
-
1251
- module ExplainSupportTestMethods
1252
-
1253
- PRINT_EXPLAIN_OUTPUT = java.lang.Boolean.getBoolean('explain.support.output')
1254
-
1255
- def test_supports_explain
1256
- assert ActiveRecord::Base.connection.supports_explain?
1257
- end
1258
-
1259
- def test_explain_without_binds
1260
- create_explain_data
1261
-
1262
- pp = ActiveRecord::Base.connection.explain(
1263
- "SELECT * FROM entries JOIN users on entries.user_id = users.id WHERE entries.rating > 0"
1264
- )
1265
- puts "\n"; puts pp if PRINT_EXPLAIN_OUTPUT
1266
- assert_instance_of String, pp
1267
- end
1268
-
1269
- def test_explain_with_binds
1270
- create_explain_data
1271
-
1272
- binds = [ [ Entry.columns.find { |col| col.name.to_s == 'rating' }, 0 ] ]
1273
- pp = ActiveRecord::Base.connection.explain(
1274
- "SELECT * FROM entries JOIN users on entries.user_id = users.id WHERE entries.rating > ?", binds
1275
- )
1276
- puts "\n"; puts pp if PRINT_EXPLAIN_OUTPUT
1277
- assert_instance_of String, pp
1278
- end
1279
-
1280
- private
1281
- def create_explain_data
1282
- user_1 = User.create :login => 'user_1'
1283
- user_2 = User.create :login => 'user_2'
1284
-
1285
- Entry.create :title => 'title_1', :content => 'content_1', :rating => 1, :user_id => user_1.id
1286
- Entry.create :title => 'title_2', :content => 'content_2', :rating => 2, :user_id => user_2.id
1287
- Entry.create :title => 'title_3', :content => 'content', :rating => 0, :user_id => user_1.id
1288
- Entry.create :title => 'title_4', :content => 'content', :rating => 0, :user_id => user_1.id
1289
- end
1290
-
1291
- end
1292
-
1293
- module CustomSelectTestMethods
1294
-
1295
- def test_custom_select_float
1296
- model = DbType.create! :sample_float => 1.42
1297
- if ActiveRecord::VERSION::MAJOR >= 3
1298
- model = DbType.where("id = #{model.id}").select('sample_float AS custom_sample_float').first
1299
- else
1300
- model = DbType.find(:first, :conditions => "id = #{model.id}", :select => 'sample_float AS custom_sample_float')
1301
- end
1302
- assert_equal 1.42, model.custom_sample_float
1303
- assert_instance_of Float, model.custom_sample_float
1304
- end
1305
-
1306
- def test_custom_select_decimal
1307
- model = DbType.create! :sample_small_decimal => ( decimal = BigDecimal.new('5.45') )
1308
- if ActiveRecord::VERSION::MAJOR >= 3
1309
- model = DbType.where("id = #{model.id}").select('sample_small_decimal AS custom_decimal').first
1310
- else
1311
- model = DbType.find(:first, :conditions => "id = #{model.id}", :select => 'sample_small_decimal AS custom_decimal')
1312
- end
1313
- assert_equal decimal, model.custom_decimal
1314
- assert_instance_of BigDecimal, model.custom_decimal
1315
- end
1316
-
1317
- end