PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/test/db/postgres/simple_test.rb

https://github.com/C3/activerecord-jdbc-adapter
Ruby | 258 lines | 202 code | 44 blank | 12 comment | 23 complexity | 274b94be079150adebb0260d1bb6d0ca MD5 | raw file
  1. # To run this script, set up the following postgres user and database:
  2. #
  3. # sudo -u postgres createuser -D -A -P blog
  4. # sudo -u postgres createdb -O blog weblog_development
  5. #
  6. require 'jdbc_common'
  7. require 'db/postgres'
  8. class PostgresSimpleTest < Test::Unit::TestCase
  9. include SimpleTestMethods
  10. include ActiveRecord3TestMethods
  11. include ColumnNameQuotingTests
  12. include DirtyAttributeTests
  13. include XmlColumnTests
  14. include CustomSelectTestMethods
  15. def test_adapter_class_name_equals_native_adapter_class_name
  16. classname = connection.class.name[/[^:]*$/]
  17. assert_equal 'PostgreSQLAdapter', classname
  18. end
  19. def test_schema_search_path
  20. assert_equal connection.schema_search_path, "\"$user\",public"
  21. end
  22. def test_current_schema
  23. assert_equal connection.current_schema, "public"
  24. end
  25. def test_encoding
  26. assert_not_nil connection.encoding
  27. end
  28. def test_multi_statement_support
  29. user = User.create! :login => 'jozko'
  30. Entry.create! :title => 'eee', :user_id => user.id
  31. results = connection.execute "SELECT title FROM entries; SELECT login FROM users"
  32. assert_equal 2, results.length
  33. assert_equal ["title"], results[0].first.keys
  34. assert_equal ["login"], results[1].first.keys
  35. end
  36. def test_find_by_sql_WITH_statement
  37. user = User.create! :login => 'ferko'
  38. Entry.create! :title => 'aaa', :user_id => user.id
  39. entries = Entry.find_by_sql '' +
  40. '( ' +
  41. 'WITH EntryAndUser (title, login, updated_on) AS ' +
  42. ' ( ' +
  43. ' SELECT e.title, u.login, e.updated_on ' +
  44. ' FROM entries e INNER JOIN users u ON e.user_id = u.id ' +
  45. ' ) ' +
  46. 'SELECT * FROM EntryAndUser ORDER BY title ASC ' +
  47. ') '
  48. assert entries.first
  49. assert entries.first.title
  50. assert entries.first.login
  51. end
  52. def test_create_xml_column
  53. return unless PG_VERSION >= 80300
  54. super
  55. end if ar_version('3.1')
  56. def xml_sql_type; 'xml'; end
  57. def test_create_table_with_limits
  58. if ar_version('4.0')
  59. # No integer type has byte size 11. Use a numeric with precision 0 instead.
  60. connection.create_table :testings do |t|
  61. t.column :an_int, :integer, :limit => 8
  62. end
  63. columns = connection.columns(:testings)
  64. an_int = columns.detect { |c| c.name == "an_int" }
  65. assert_equal "bigint", an_int.sql_type
  66. else
  67. assert_nothing_raised do
  68. connection.create_table :testings do |t|
  69. t.column :an_int, :integer, :limit => 11
  70. end
  71. end
  72. columns = connection.columns(:testings)
  73. an_int = columns.detect { |c| c.name == "an_int" }
  74. assert_equal "integer", an_int.sql_type
  75. end
  76. ensure
  77. connection.drop_table :testings rescue nil
  78. end
  79. def test_resolves_correct_columns_default
  80. assert column = DbType.columns.find { |col| col.name == 'sample_small_decimal' }
  81. assert_equal 3.14, column.default
  82. assert column = DbType.columns.find { |col| col.name == 'sample_integer_no_limit' }
  83. assert_equal 42, column.default
  84. assert column = DbType.columns.find { |col| col.name == 'sample_integer_neg_default' }
  85. assert_equal -1, column.default
  86. end
  87. def test_supports_standard_conforming_string
  88. assert([true, false].include?(connection.supports_standard_conforming_strings?))
  89. end
  90. def test_standard_conforming_string_default_set_on_new_connections
  91. c = ActiveRecord::Base.postgresql_connection(POSTGRES_CONFIG)
  92. assert_equal true, c.instance_variable_get("@standard_conforming_strings")
  93. end
  94. def test_default_standard_conforming_string
  95. if connection.supports_standard_conforming_strings?
  96. assert_equal true, connection.standard_conforming_strings?
  97. else
  98. assert_equal false, connection.standard_conforming_strings?
  99. end
  100. end
  101. def test_string_quoting_with_standard_conforming_strings
  102. if connection.supports_standard_conforming_strings?
  103. s = "\\m it's \\M"
  104. assert_equal "'\\m it''s \\M'", connection.quote(s)
  105. end
  106. end
  107. def test_string_quoting_without_standard_conforming_strings
  108. connection.standard_conforming_strings = false
  109. s = "\\m it's \\M"
  110. assert_equal "'\\\\m it''s \\\\M'", connection.quote(s)
  111. connection.standard_conforming_strings = true
  112. end
  113. include ExplainSupportTestMethods if ar_version("3.1")
  114. def test_primary_key
  115. assert_equal 'id', connection.primary_key('entries')
  116. assert_equal 'custom_id', connection.primary_key('custom_pk_names')
  117. # assert_equal 'id', connection.primary_key('auto_ids')
  118. end
  119. def test_primary_key_without_sequence
  120. connection.execute "CREATE TABLE uid_table (uid UUID PRIMARY KEY, name TEXT)"
  121. assert_equal 'uid', connection.primary_key('uid_table')
  122. ensure
  123. connection.execute "DROP TABLE uid_table"
  124. end
  125. def test_extensions
  126. if connection.supports_extensions?
  127. assert_include connection.extensions, 'plpgsql'
  128. assert connection.extension_enabled?('plpgsql')
  129. assert ! connection.extension_enabled?('invalid')
  130. else
  131. assert_empty connection.extensions
  132. end
  133. end
  134. end
  135. class PostgresTimestampTest < Test::Unit::TestCase
  136. def self.startup
  137. DbTypeMigration.up
  138. end
  139. def self.shutdown
  140. DbTypeMigration.down
  141. end
  142. def test_string_is_character_varying
  143. sample_string = DbType.connection.columns(:db_types).detect do |c|
  144. c.name == "sample_string"
  145. end
  146. assert_match(/^character varying/, sample_string.sql_type)
  147. end
  148. def test_select_infinity
  149. d = DbType.find_by_sql("select 'infinity'::timestamp as sample_timestamp").first
  150. assert d.sample_timestamp.infinite?, "timestamp: #{d.sample_timestamp.inspect} should be infinite"
  151. d = DbType.find_by_sql("select '-infinity'::timestamp as sample_timestamp").first
  152. time = d.sample_timestamp
  153. assert time.infinite?, "timestamp: #{time.inspect} should be infinte"
  154. assert_operator time, :<, 0
  155. end
  156. def test_save_infinity
  157. if ar_version('4.0')
  158. # NOTE: likely an AR issue - it only works when time_zone_aware_attributes
  159. # are disabled otherwise TimeZoneConversion's define_method_attribute=(attr_name)
  160. # does the following code ("infinite" time instance ending as nil):
  161. # time_with_zone = time.respond_to?(:in_time_zone) ? time.in_time_zone : nil
  162. tz_aware_attributes = ActiveRecord::Base.time_zone_aware_attributes
  163. begin
  164. ActiveRecord::Base.time_zone_aware_attributes = false
  165. do_test_save_infinity
  166. ensure
  167. ActiveRecord::Base.time_zone_aware_attributes = tz_aware_attributes
  168. end
  169. else
  170. do_test_save_infinity
  171. end
  172. end
  173. def do_test_save_infinity
  174. d = DbType.new
  175. d.sample_datetime = 1.0 / 0.0
  176. d.save!
  177. if ar_version('3.0')
  178. assert_equal 1.0 / 0.0, d.reload.sample_datetime # sample_timestamp
  179. else # 2.3
  180. assert_equal nil, d.reload.sample_datetime # d.sample_timestamp
  181. end
  182. d = DbType.create!(:sample_timestamp => -1.0 / 0.0)
  183. if ar_version('3.0')
  184. assert_equal -1.0 / 0.0, d.sample_timestamp
  185. else # 2.3
  186. assert_equal nil, d.sample_timestamp
  187. end
  188. end
  189. private :do_test_save_infinity
  190. def test_bc_timestamp
  191. omit "Date.new(0) issue on JRuby 1.7.3" if RUBY_VERSION == '1.9.3' && defined?(JRUBY_VERSION) && JRUBY_VERSION == '1.7.3'
  192. # JRuby 1.7.3 (--1.9) bug: `Date.new(0) + 1.seconds` "1753-08-29 22:43:42 +0057"
  193. date = Date.new(0) - 1.second
  194. db_type = DbType.create!(:sample_timestamp => date)
  195. assert_equal date, db_type.reload.sample_timestamp
  196. end if ar_version('3.0')
  197. end
  198. class PostgresDeserializationTest < Test::Unit::TestCase
  199. def self.startup
  200. DbTypeMigration.up
  201. end
  202. def self.shutdown
  203. DbTypeMigration.down
  204. end
  205. def test_should_keep_float_precision
  206. expected = DbType.create(:sample_float => 7.3)
  207. actual = DbType.find(expected.id)
  208. assert_equal expected.sample_float, actual.sample_float
  209. end
  210. end
  211. class PostgresHasManyThroughTest < Test::Unit::TestCase
  212. include HasManyThroughMethods
  213. end