activerecord-jdbc-adapter 0.9.3-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. data/History.txt +248 -0
  2. data/LICENSE.txt +21 -0
  3. data/Manifest.txt +125 -0
  4. data/README.txt +218 -0
  5. data/Rakefile +10 -0
  6. data/lib/active_record/connection_adapters/cachedb_adapter.rb +1 -0
  7. data/lib/active_record/connection_adapters/derby_adapter.rb +13 -0
  8. data/lib/active_record/connection_adapters/h2_adapter.rb +13 -0
  9. data/lib/active_record/connection_adapters/hsqldb_adapter.rb +13 -0
  10. data/lib/active_record/connection_adapters/informix_adapter.rb +1 -0
  11. data/lib/active_record/connection_adapters/jdbc_adapter.rb +640 -0
  12. data/lib/active_record/connection_adapters/jdbc_adapter_spec.rb +26 -0
  13. data/lib/active_record/connection_adapters/jndi_adapter.rb +1 -0
  14. data/lib/active_record/connection_adapters/mysql_adapter.rb +13 -0
  15. data/lib/active_record/connection_adapters/oracle_adapter.rb +1 -0
  16. data/lib/active_record/connection_adapters/postgresql_adapter.rb +13 -0
  17. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +13 -0
  18. data/lib/generators/jdbc/jdbc_generator.rb +9 -0
  19. data/lib/jdbc_adapter.rb +27 -0
  20. data/lib/jdbc_adapter/jdbc.rake +121 -0
  21. data/lib/jdbc_adapter/jdbc_adapter_internal.jar +0 -0
  22. data/lib/jdbc_adapter/jdbc_cachedb.rb +33 -0
  23. data/lib/jdbc_adapter/jdbc_db2.rb +203 -0
  24. data/lib/jdbc_adapter/jdbc_derby.rb +430 -0
  25. data/lib/jdbc_adapter/jdbc_firebird.rb +109 -0
  26. data/lib/jdbc_adapter/jdbc_hsqldb.rb +218 -0
  27. data/lib/jdbc_adapter/jdbc_informix.rb +147 -0
  28. data/lib/jdbc_adapter/jdbc_mimer.rb +141 -0
  29. data/lib/jdbc_adapter/jdbc_mssql.rb +337 -0
  30. data/lib/jdbc_adapter/jdbc_mysql.rb +236 -0
  31. data/lib/jdbc_adapter/jdbc_oracle.rb +377 -0
  32. data/lib/jdbc_adapter/jdbc_postgre.rb +498 -0
  33. data/lib/jdbc_adapter/jdbc_sqlite3.rb +384 -0
  34. data/lib/jdbc_adapter/jdbc_sybase.rb +50 -0
  35. data/lib/jdbc_adapter/missing_functionality_helper.rb +87 -0
  36. data/lib/jdbc_adapter/rake_tasks.rb +10 -0
  37. data/lib/jdbc_adapter/tsql_helper.rb +60 -0
  38. data/lib/jdbc_adapter/version.rb +5 -0
  39. data/lib/pg.rb +4 -0
  40. data/rails_generators/jdbc_generator.rb +15 -0
  41. data/rails_generators/templates/config/initializers/jdbc.rb +7 -0
  42. data/rails_generators/templates/lib/tasks/jdbc.rake +8 -0
  43. data/rakelib/compile.rake +23 -0
  44. data/rakelib/package.rake +90 -0
  45. data/rakelib/rails.rake +41 -0
  46. data/rakelib/test.rake +76 -0
  47. data/src/java/jdbc_adapter/JdbcAdapterInternalService.java +53 -0
  48. data/src/java/jdbc_adapter/JdbcConnectionFactory.java +36 -0
  49. data/src/java/jdbc_adapter/JdbcDerbySpec.java +293 -0
  50. data/src/java/jdbc_adapter/JdbcMySQLSpec.java +134 -0
  51. data/src/java/jdbc_adapter/MssqlRubyJdbcConnection.java +71 -0
  52. data/src/java/jdbc_adapter/PostgresRubyJdbcConnection.java +55 -0
  53. data/src/java/jdbc_adapter/RubyJdbcConnection.java +1162 -0
  54. data/src/java/jdbc_adapter/SQLBlock.java +27 -0
  55. data/src/java/jdbc_adapter/Sqlite3RubyJdbcConnection.java +41 -0
  56. data/test/abstract_db_create.rb +107 -0
  57. data/test/activerecord/connection_adapters/type_conversion_test.rb +31 -0
  58. data/test/activerecord/connections/native_jdbc_mysql/connection.rb +25 -0
  59. data/test/cachedb_simple_test.rb +6 -0
  60. data/test/db/cachedb.rb +9 -0
  61. data/test/db/db2.rb +9 -0
  62. data/test/db/derby.rb +14 -0
  63. data/test/db/h2.rb +11 -0
  64. data/test/db/hsqldb.rb +12 -0
  65. data/test/db/informix.rb +11 -0
  66. data/test/db/jdbc.rb +11 -0
  67. data/test/db/jndi_config.rb +30 -0
  68. data/test/db/logger.rb +3 -0
  69. data/test/db/mssql.rb +9 -0
  70. data/test/db/mysql.rb +10 -0
  71. data/test/db/oracle.rb +34 -0
  72. data/test/db/postgres.rb +9 -0
  73. data/test/db/sqlite3.rb +15 -0
  74. data/test/db2_simple_test.rb +10 -0
  75. data/test/derby_migration_test.rb +21 -0
  76. data/test/derby_multibyte_test.rb +12 -0
  77. data/test/derby_simple_test.rb +21 -0
  78. data/test/generic_jdbc_connection_test.rb +9 -0
  79. data/test/h2_simple_test.rb +6 -0
  80. data/test/has_many_through.rb +79 -0
  81. data/test/helper.rb +5 -0
  82. data/test/hsqldb_simple_test.rb +6 -0
  83. data/test/informix_simple_test.rb +48 -0
  84. data/test/jdbc_adapter/jdbc_db2_test.rb +26 -0
  85. data/test/jdbc_adapter/jdbc_sybase_test.rb +33 -0
  86. data/test/jdbc_common.rb +25 -0
  87. data/test/jndi_callbacks_test.rb +38 -0
  88. data/test/jndi_test.rb +35 -0
  89. data/test/manualTestDatabase.rb +191 -0
  90. data/test/minirunit.rb +109 -0
  91. data/test/minirunit/testConnect.rb +14 -0
  92. data/test/minirunit/testH2.rb +73 -0
  93. data/test/minirunit/testHsqldb.rb +73 -0
  94. data/test/minirunit/testLoadActiveRecord.rb +3 -0
  95. data/test/minirunit/testMysql.rb +83 -0
  96. data/test/minirunit/testRawSelect.rb +24 -0
  97. data/test/models/add_not_null_column_to_table.rb +12 -0
  98. data/test/models/auto_id.rb +18 -0
  99. data/test/models/data_types.rb +28 -0
  100. data/test/models/entry.rb +23 -0
  101. data/test/models/mixed_case.rb +20 -0
  102. data/test/models/reserved_word.rb +18 -0
  103. data/test/models/string_id.rb +18 -0
  104. data/test/models/validates_uniqueness_of_string.rb +19 -0
  105. data/test/mssql_simple_test.rb +6 -0
  106. data/test/mysql_db_create_test.rb +25 -0
  107. data/test/mysql_multibyte_test.rb +10 -0
  108. data/test/mysql_nonstandard_primary_key_test.rb +42 -0
  109. data/test/mysql_simple_test.rb +32 -0
  110. data/test/oracle_simple_test.rb +29 -0
  111. data/test/pick_rails_version.rb +3 -0
  112. data/test/postgres_db_create_test.rb +21 -0
  113. data/test/postgres_mixed_case_test.rb +19 -0
  114. data/test/postgres_nonseq_pkey_test.rb +40 -0
  115. data/test/postgres_reserved_test.rb +22 -0
  116. data/test/postgres_schema_search_path_test.rb +46 -0
  117. data/test/postgres_simple_test.rb +13 -0
  118. data/test/simple.rb +475 -0
  119. data/test/sqlite3_simple_test.rb +233 -0
  120. data/test/sybase_jtds_simple_test.rb +6 -0
  121. metadata +188 -0
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ class CreateEntries < ActiveRecord::Migration
5
+ def self.up
6
+ create_table "entries", :force => true do |t|
7
+ t.column :title, :string, :limit => 100
8
+ t.column :updated_on, :datetime
9
+ t.column :content, :text
10
+ t.column :rating, :decimal, :precision => 10, :scale => 2
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table "entries"
16
+ end
17
+ end
18
+
19
+ class Entry < ActiveRecord::Base
20
+ def to_param
21
+ "#{id}-#{title.gsub(/[^a-zA-Z0-9]/, '-')}"
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ module Migration
5
+ class MixedCase < ActiveRecord::Migration
6
+
7
+ def self.up
8
+ create_table "mixed_cases" do |t|
9
+ t.column :SOME_value, :string
10
+ end
11
+ end
12
+
13
+ def self.down
14
+ drop_table "mixed_cases"
15
+ end
16
+ end
17
+ end
18
+
19
+ class MixedCase < ActiveRecord::Base
20
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ class CreateReservedWords < ActiveRecord::Migration
5
+ def self.up
6
+ create_table "reserved_words", :force => true do |t|
7
+ t.column :position, :integer
8
+ t.column :select, :integer
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table "reserved_words"
14
+ end
15
+ end
16
+
17
+ class ReservedWord < ActiveRecord::Base
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ class CreateStringIds < ActiveRecord::Migration
5
+ def self.up
6
+ create_table "string_ids", :force => true, :id => false do |t|
7
+ t.string :id
8
+ end
9
+ end
10
+
11
+ def self.down
12
+ drop_table "string_ids"
13
+ end
14
+ end
15
+
16
+ class StringId < ActiveRecord::Base
17
+ def self.table_name () "string_ids" end
18
+ end
@@ -0,0 +1,19 @@
1
+ class CreateValidatesUniquenessOf < ActiveRecord::Migration
2
+ def self.up
3
+ create_table "validates_uniqueness_of", :force => true do |t|
4
+ t.column :cs_string, :string
5
+ t.column :ci_string, :string
6
+ t.column :content, :text
7
+ end
8
+ end
9
+
10
+ def self.down
11
+ drop_table "validates_uniqueness_of"
12
+ end
13
+ end
14
+
15
+ class ValidatesUniquenessOfString < ActiveRecord::Base
16
+ self.set_table_name "validates_uniqueness_of"
17
+ validates_uniqueness_of :cs_string, :case_sensitive => true
18
+ validates_uniqueness_of :ci_string, :case_sensitive => false
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'jdbc_common'
2
+ require 'db/mssql'
3
+
4
+ class MsSQLSimpleTest < Test::Unit::TestCase
5
+ include SimpleTestMethods
6
+ end
@@ -0,0 +1,25 @@
1
+ require 'abstract_db_create'
2
+ require 'db/mysql'
3
+
4
+ class MysqlDbCreateTest < Test::Unit::TestCase
5
+ include AbstractDbCreate
6
+
7
+ def db_config
8
+ MYSQL_CONFIG
9
+ end
10
+
11
+ if find_executable?("mysql")
12
+ def test_rake_db_create
13
+ Rake::Task["db:create"].invoke
14
+ output = nil
15
+ IO.popen("mysql -u #{MYSQL_CONFIG[:username]} --password=#{MYSQL_CONFIG[:password]}", "r+") do |mysql|
16
+ mysql << "show databases where `Database` = '#{@db_name}';"
17
+ mysql.close_write
18
+ assert mysql.read =~ /#{@db_name}/m
19
+ end
20
+ end
21
+ else
22
+ def test_skipped
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,10 @@
1
+ require 'jdbc_common'
2
+ require 'db/mysql'
3
+
4
+ class MySQLMultibyteTest < Test::Unit::TestCase
5
+ include MultibyteTestMethods
6
+ end
7
+
8
+ class MySQLNonUTF8EncodingTest < Test::Unit::TestCase
9
+ include NonUTF8EncodingMethods
10
+ end
@@ -0,0 +1,42 @@
1
+ require 'jdbc_common'
2
+ require 'db/mysql'
3
+
4
+ class Project < ActiveRecord::Migration
5
+ def self.up
6
+ create_table :project, :primary_key => "project_id" do |t|
7
+ t.string :projectType, :limit => 31
8
+ t.boolean :published
9
+ t.datetime :created_date
10
+ t.text :abstract, :title
11
+ end
12
+ end
13
+
14
+ def self.down
15
+ drop_table :project
16
+ end
17
+
18
+ end
19
+
20
+ class MysqlNonstandardPrimaryKeyTest < Test::Unit::TestCase
21
+
22
+ def setup
23
+ Project.up
24
+ end
25
+
26
+ def teardown
27
+ Project.down
28
+ end
29
+
30
+ def standard_dump
31
+ stream = StringIO.new
32
+ ActiveRecord::SchemaDumper.ignore_tables = []
33
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
34
+ stream.string
35
+ end
36
+
37
+ def test_nonstandard_primary_key
38
+ output = standard_dump
39
+ assert_match %r(:primary_key => "project_id"), output, "non-standard primary key not preserved"
40
+ end
41
+
42
+ end
@@ -0,0 +1,32 @@
1
+ # To run this script, run the following in a mysql instance:
2
+ #
3
+ # drop database if exists weblog_development;
4
+ # create database weblog_development;
5
+ # grant all on weblog_development.* to blog@localhost;
6
+ # flush privileges;
7
+
8
+ require 'jdbc_common'
9
+ require 'db/mysql'
10
+
11
+ class MysqlSimpleTest < Test::Unit::TestCase
12
+ include SimpleTestMethods
13
+ include ActiveRecord3TestMethods
14
+
15
+ def test_string_quoting_oddity
16
+ s = "0123456789a'a"
17
+ assert_equal "'0123456789a\\'a'", ActiveRecord::Base.connection.quote(s)
18
+
19
+ s2 = s[10,3]
20
+ assert_equal "a'a", s2
21
+ assert_equal "'a\\'a'", ActiveRecord::Base.connection.quote(s2)
22
+ end
23
+
24
+ def test_table_name_quoting_with_dot
25
+ s = "weblog_development.posts"
26
+ assert_equal "`weblog_development`.`posts`", ActiveRecord::Base.connection.quote_table_name(s)
27
+ end
28
+ end
29
+
30
+ class MysqlHasManyThroughTest < Test::Unit::TestCase
31
+ include HasManyThroughMethods
32
+ end
@@ -0,0 +1,29 @@
1
+ require 'jdbc_common'
2
+ require 'db/oracle'
3
+
4
+ class OracleSimpleTest < Test::Unit::TestCase
5
+ include SimpleTestMethods
6
+ end
7
+
8
+ class OracleSpecificTest < Test::Unit::TestCase
9
+ include MultibyteTestMethods
10
+
11
+ def setup
12
+ super
13
+ @java_con.createStatement.execute "CREATE TABLE DEFAULT_NUMBER (VALUE NUMBER)"
14
+ @java_con.createStatement.execute "INSERT INTO DEFAULT_NUMBER (VALUE) VALUES (0.076)"
15
+ end
16
+
17
+ def teardown
18
+ @java_con.createStatement.execute "DROP TABLE DEFAULT_NUMBER"
19
+ super
20
+ end
21
+
22
+
23
+ def test_default_number_precision
24
+ klass = Class.new(ActiveRecord::Base)
25
+ klass.set_table_name "DEFAULT_NUMBER"
26
+ obj = klass.find(:first)
27
+ assert_equal 0.076, obj.value
28
+ end
29
+ end if defined?(JRUBY_VERSION)
@@ -0,0 +1,3 @@
1
+ # Specify version of activerecord with ENV['AR_VERSION'] if desired
2
+ gem 'rails', ENV['AR_VERSION'] if ENV['AR_VERSION']
3
+ require 'active_record/version'
@@ -0,0 +1,21 @@
1
+ require 'abstract_db_create'
2
+ require 'db/postgres'
3
+
4
+ class PostgresDbCreateTest < Test::Unit::TestCase
5
+ include AbstractDbCreate
6
+
7
+ def db_config
8
+ POSTGRES_CONFIG
9
+ end
10
+
11
+ if find_executable?("psql")
12
+ def test_rake_db_create
13
+ Rake::Task["db:create"].invoke
14
+ output = `psql -c '\\l'`
15
+ assert output =~ /#{@db_name}/m
16
+ end
17
+ else
18
+ def test_skipped
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ require 'jdbc_common'
2
+ require 'models/mixed_case'
3
+
4
+ class MixedCaseTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Migration::MixedCase.up
8
+ end
9
+
10
+ def teardown
11
+ Migration::MixedCase.down
12
+ end
13
+
14
+ def test_create
15
+ mixed_case = MixedCase.create :SOME_value => 'some value'
16
+ assert_equal 'some value', mixed_case.SOME_value
17
+ end
18
+
19
+ end
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'jdbc_common'
4
+ require 'db/postgres'
5
+
6
+ class CreateUrls < ActiveRecord::Migration
7
+ def self.up
8
+ create_table 'urls', :id => false do |t|
9
+ t.text :uhash, :null => false
10
+ t.text :url, :null => false
11
+ end
12
+ execute "ALTER TABLE urls ADD PRIMARY KEY (uhash)"
13
+ end
14
+ def self.down
15
+ drop_table 'urls'
16
+ end
17
+ end
18
+
19
+ class Url < ActiveRecord::Base
20
+ set_primary_key :uhash
21
+ #Shouldn't be needed: set_sequence_name nil
22
+ end
23
+
24
+ class PostgresNonSeqPKey < Test::Unit::TestCase
25
+ def setup
26
+ CreateUrls.up
27
+ end
28
+
29
+ def teardown
30
+ CreateUrls.down
31
+ end
32
+
33
+ def test_create
34
+ url = Url.create! do |u|
35
+ u.uhash = 'uhash'
36
+ u.url = 'http://url'
37
+ end
38
+ assert_equal( 'uhash', url.uhash )
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ require 'jdbc_common'
2
+ require 'db/postgres'
3
+ require 'models/reserved_word'
4
+
5
+ class PostgresReservedWordsTest < Test::Unit::TestCase
6
+ def setup
7
+ CreateReservedWords.up
8
+ end
9
+ def teardown
10
+ CreateReservedWords.down
11
+ end
12
+
13
+ def test_quote_reserved_word_column
14
+ columns = ReservedWord.column_names - ["id"]
15
+ ReservedWord.connection.add_index :reserved_words, columns
16
+ indexes = ReservedWord.connection.indexes("reserved_words")
17
+ assert_equal 1, indexes.size
18
+ columns.each do |c|
19
+ assert indexes[0].columns.include?(c), "#{indexes[0].columns.inspect} does not include #{c.inspect}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+ require 'jdbc_common'
4
+ require 'db/postgres'
5
+
6
+ class CreateSchema < ActiveRecord::Migration
7
+ def self.up
8
+ execute "CREATE SCHEMA test"
9
+ execute "CREATE TABLE test.people (id serial, name text)"
10
+ execute "INSERT INTO test.people (name) VALUES ('Alex')"
11
+ execute "CREATE TABLE public.people (id serial, wrongname text)"
12
+ end
13
+
14
+ def self.down
15
+ execute "DROP SCHEMA test CASCADE"
16
+ execute "DROP TABLE people"
17
+ end
18
+ end
19
+
20
+ class Person < ActiveRecord::Base
21
+ establish_connection POSTGRES_CONFIG.merge(:schema_search_path => 'test')
22
+ end
23
+
24
+ class PostgresSchemaSearchPathTest < Test::Unit::TestCase
25
+ def setup
26
+ CreateSchema.up
27
+ end
28
+
29
+ def teardown
30
+ CreateSchema.down
31
+ end
32
+
33
+ def test_columns
34
+ assert_equal(%w{id name}, Person.column_names)
35
+ end
36
+
37
+ def test_find_right
38
+ assert_not_nil Person.find_by_name("Alex")
39
+ end
40
+
41
+ def test_find_wrong
42
+ assert_raise NoMethodError do
43
+ Person.find_by_wrongname("Alex")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,13 @@
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
+
7
+ require 'jdbc_common'
8
+ require 'db/postgres'
9
+
10
+ class PostgresSimpleTest < Test::Unit::TestCase
11
+ include SimpleTestMethods
12
+ include ActiveRecord3TestMethods
13
+ end
@@ -0,0 +1,475 @@
1
+ # -*- coding: utf-8 -*-
2
+ ActiveRecord::Schema.verbose = false
3
+ ActiveRecord::Base.time_zone_aware_attributes = true if ActiveRecord::Base.respond_to?(:time_zone_aware_attributes)
4
+ ActiveRecord::Base.default_timezone = :utc
5
+ #just a random zone, unlikely to be local, and not utc
6
+ Time.zone = 'Moscow' if Time.respond_to?(:zone)
7
+
8
+ module MigrationSetup
9
+ def setup
10
+ DbTypeMigration.up
11
+ CreateStringIds.up
12
+ CreateEntries.up
13
+ CreateAutoIds.up
14
+ CreateValidatesUniquenessOf.up
15
+
16
+ @connection = ActiveRecord::Base.connection
17
+ end
18
+
19
+ def teardown
20
+ DbTypeMigration.down
21
+ CreateStringIds.down
22
+ CreateEntries.down
23
+ CreateAutoIds.down
24
+ CreateValidatesUniquenessOf.down
25
+ ActiveRecord::Base.clear_active_connections!
26
+ end
27
+ end
28
+
29
+ module FixtureSetup
30
+ include MigrationSetup
31
+ def setup
32
+ super
33
+ @title = "First post!"
34
+ @content = "Hello from JRuby on Rails!"
35
+ @new_title = "First post updated title"
36
+ @rating = 205.76
37
+ @entry = Entry.create :title => @title, :content => @content, :rating => @rating
38
+ DbType.create
39
+ end
40
+ end
41
+
42
+ module SimpleTestMethods
43
+ include FixtureSetup
44
+
45
+ def test_entries_created
46
+ assert ActiveRecord::Base.connection.tables.find{|t| t =~ /^entries$/i}, "entries not created"
47
+ end
48
+
49
+ def test_entries_empty
50
+ Entry.delete_all
51
+ assert_equal 0, Entry.count
52
+ end
53
+
54
+ def test_find_with_string_slug
55
+ new_entry = Entry.create(:title => "Blah")
56
+ entry = Entry.find(new_entry.to_param)
57
+ assert_equal new_entry.id, entry.id
58
+ end
59
+
60
+ def test_insert_returns_id
61
+ unless ActiveRecord::Base.connection.adapter_name =~ /oracle/i
62
+ value = ActiveRecord::Base.connection.insert("INSERT INTO entries (title, content, rating) VALUES('insert_title', 'some content', 1)")
63
+ assert !value.nil?
64
+ entry = Entry.find_by_title('insert_title')
65
+ assert_equal value, entry.id
66
+ end
67
+ end
68
+
69
+ def test_create_new_entry
70
+ Entry.delete_all
71
+
72
+ post = Entry.new
73
+ post.title = @title
74
+ post.content = @content
75
+ post.rating = @rating
76
+ post.save
77
+
78
+ assert_equal 1, Entry.count
79
+ end
80
+
81
+ def test_create_partial_new_entry
82
+ new_entry = Entry.create(:title => "Blah")
83
+ new_entry2 = Entry.create(:title => "Bloh")
84
+ end
85
+
86
+ def test_find_and_update_entry
87
+ post = Entry.find(:first)
88
+ assert_equal @title, post.title
89
+ assert_equal @content, post.content
90
+ assert_equal @rating, post.rating
91
+
92
+ post.title = @new_title
93
+ post.save
94
+
95
+ post = Entry.find(:first)
96
+ assert_equal @new_title, post.title
97
+ end
98
+
99
+ def test_destroy_entry
100
+ prev_count = Entry.count
101
+ post = Entry.find(:first)
102
+ post.destroy
103
+
104
+ assert_equal prev_count - 1, Entry.count
105
+ end
106
+
107
+ if Time.respond_to?(:zone)
108
+ def test_save_time
109
+ t = Time.now
110
+ #precision will only be expected to the second.
111
+ time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
112
+ e = DbType.find(:first)
113
+ e.sample_datetime = time
114
+ e.save!
115
+ e = DbType.find(:first)
116
+ assert_equal time.in_time_zone, e.sample_datetime
117
+ end
118
+
119
+ def test_save_date_time
120
+ t = Time.now
121
+ #precision will only be expected to the second.
122
+ time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
123
+ datetime = time.to_datetime
124
+ e = DbType.find(:first)
125
+ e.sample_datetime = datetime
126
+ e.save!
127
+ e = DbType.find(:first)
128
+ assert_equal time, e.sample_datetime.localtime
129
+ end
130
+
131
+ def test_save_time_with_zone
132
+ t = Time.now
133
+ #precision will only be expected to the second.
134
+ original_time = Time.local(t.year, t.month, t.day, t.hour, t.min, t.sec)
135
+ time = original_time.in_time_zone
136
+ e = DbType.find(:first)
137
+ e.sample_datetime = time
138
+ e.save!
139
+ e = DbType.find(:first)
140
+ assert_equal time, e.sample_datetime
141
+ end
142
+ end
143
+
144
+ def test_save_float
145
+ e = DbType.find(:first)
146
+ e.sample_float = 12.0
147
+ e.save!
148
+
149
+ e = DbType.find(:first)
150
+ assert_equal(12.0, e.sample_float)
151
+ end
152
+
153
+ def test_save_date
154
+ date = Date.new(2007)
155
+ e = DbType.find(:first)
156
+ e.sample_date = date
157
+ e.save!
158
+ e = DbType.find(:first)
159
+ assert_equal date, e.sample_date
160
+ end
161
+
162
+ def test_boolean
163
+ # An unset boolean should default to nil
164
+ e = DbType.find(:first)
165
+ assert_equal(nil, e.sample_boolean)
166
+
167
+ e.sample_boolean = true
168
+ e.save!
169
+
170
+ e = DbType.find(:first)
171
+ assert_equal(true, e.sample_boolean)
172
+ end
173
+
174
+ def test_integer
175
+ # An unset boolean should default to nil
176
+ e = DbType.find(:first)
177
+ assert_equal(nil, e.sample_integer)
178
+
179
+ e.sample_integer = 10
180
+ e.save!
181
+
182
+ e = DbType.find(:first)
183
+ assert_equal(10, e.sample_integer)
184
+ end
185
+
186
+ def test_text
187
+ # An unset boolean should default to nil
188
+ e = DbType.find(:first)
189
+
190
+ # Oracle adapter initializes all CLOB fields with empty_clob() function,
191
+ # so they all have a initial value of an empty string ''
192
+ assert_equal(nil, e.sample_text) unless ActiveRecord::Base.connection.adapter_name =~ /oracle/i
193
+
194
+ e.sample_text = "ooop"
195
+ e.save!
196
+
197
+ e = DbType.find(:first)
198
+ assert_equal("ooop", e.sample_text)
199
+ end
200
+
201
+ def test_string
202
+ e = DbType.find(:first)
203
+
204
+ # An empty string is treated as a null value in Oracle: http://www.techonthenet.com/oracle/questions/empty_null.php
205
+ assert_equal('', e.sample_string) unless ActiveRecord::Base.connection.adapter_name =~ /oracle/i
206
+ e.sample_string = "ooop"
207
+ e.save!
208
+
209
+ e = DbType.find(:first)
210
+ assert_equal("ooop", e.sample_string)
211
+ end
212
+
213
+ def test_save_binary
214
+ #string is 60_000 bytes
215
+ binary_string = "\000ABCDEFGHIJKLMNOPQRSTUVWXYZ'\001\003"*1#2_000
216
+ e = DbType.find(:first)
217
+ e.sample_binary = binary_string
218
+ e.send(:write_attribute, :binary, binary_string)
219
+ e.save!
220
+ e = DbType.find(:first)
221
+ assert_equal binary_string, e.sample_binary
222
+ end
223
+
224
+ def test_indexes
225
+ # Only test indexes if we have implemented it for the particular adapter
226
+ if @connection.respond_to?(:indexes)
227
+ indexes = @connection.indexes(:entries)
228
+ assert_equal(0, indexes.size)
229
+
230
+ index_name = "entries_index"
231
+ @connection.add_index(:entries, :updated_on, :name => index_name)
232
+
233
+ indexes = @connection.indexes(:entries)
234
+ assert_equal(1, indexes.size)
235
+ assert_equal "entries", indexes.first.table.to_s
236
+ assert_equal index_name, indexes.first.name
237
+ assert !indexes.first.unique
238
+ assert_equal ["updated_on"], indexes.first.columns
239
+ end
240
+ end
241
+
242
+ def test_dumping_schema
243
+ require 'active_record/schema_dumper'
244
+ @connection.add_index :entries, :title
245
+ StringIO.open do |io|
246
+ ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, io)
247
+ assert_match(/add_index "entries",/, io.string)
248
+ end
249
+ @connection.remove_index :entries, :title
250
+
251
+ end
252
+
253
+ def test_nil_values
254
+ test = AutoId.create('value' => '')
255
+ assert_nil AutoId.find(test.id).value
256
+ end
257
+
258
+ def test_invalid
259
+ e = Entry.new(:title => @title, :content => @content, :rating => ' ')
260
+ assert e.valid?
261
+ end
262
+
263
+ def test_reconnect
264
+ assert_equal 1, Entry.count
265
+ @connection.reconnect!
266
+ assert_equal 1, Entry.count
267
+ end
268
+
269
+ if jruby?
270
+ def test_connection_valid
271
+ assert_raises(ActiveRecord::ActiveRecordError) do
272
+ @connection.raw_connection.with_connection_retry_guard do |c|
273
+ begin
274
+ stmt = c.createStatement
275
+ stmt.execute "bogus sql"
276
+ ensure
277
+ stmt.close rescue nil
278
+ end
279
+ end
280
+ end
281
+ end
282
+
283
+ class Animal < ActiveRecord::Base; end
284
+
285
+ # ENEBO: Is this really ar-jdbc-specific or a bug in our adapter?
286
+ def test_fetching_columns_for_nonexistent_table_should_raise
287
+ assert_raises(ActiveRecord::ActiveRecordError) do
288
+ Animal.columns
289
+ end
290
+ end
291
+ end
292
+
293
+ def test_disconnect
294
+ assert_equal 1, Entry.count
295
+ ActiveRecord::Base.clear_active_connections!
296
+ ActiveRecord::Base.connection_pool.disconnect! if ActiveRecord::Base.respond_to?(:connection_pool)
297
+ assert !ActiveRecord::Base.connected?
298
+ assert_equal 1, Entry.count
299
+ assert ActiveRecord::Base.connected?
300
+ end
301
+
302
+ def test_add_not_null_column_to_table
303
+ AddNotNullColumnToTable.up
304
+ AddNotNullColumnToTable.down
305
+ end
306
+
307
+ def test_validates_uniqueness_of_strings_case_sensitive
308
+ # MySQL string cmps are case-insensitive by default, so skip this test
309
+ return if ActiveRecord::Base.connection.config[:adapter] =~ /mysql/
310
+
311
+ name_lower = ValidatesUniquenessOfString.new(:cs_string => "name", :ci_string => '1')
312
+ name_lower.save!
313
+
314
+ name_upper = ValidatesUniquenessOfString.new(:cs_string => "NAME", :ci_string => '2')
315
+ assert_nothing_raised do
316
+ name_upper.save!
317
+ end
318
+
319
+ name_lower_collision = ValidatesUniquenessOfString.new(:cs_string => "name", :ci_string => '3')
320
+ assert_raise ActiveRecord::RecordInvalid do
321
+ name_lower_collision.save!
322
+ end
323
+
324
+ name_upper_collision = ValidatesUniquenessOfString.new(:cs_string => "NAME", :ci_string => '4')
325
+ assert_raise ActiveRecord::RecordInvalid do
326
+ name_upper_collision.save!
327
+ end
328
+ end
329
+
330
+ def test_validates_uniqueness_of_strings_case_insensitive
331
+ name_lower = ValidatesUniquenessOfString.new(:cs_string => '1', :ci_string => "name")
332
+ name_lower.save!
333
+
334
+ name_upper = ValidatesUniquenessOfString.new(:cs_string => '2', :ci_string => "NAME")
335
+ assert_raise ActiveRecord::RecordInvalid do
336
+ name_upper.save!
337
+ end
338
+
339
+ name_lower_collision = ValidatesUniquenessOfString.new(:cs_string => '3', :ci_string => "name")
340
+ assert_raise ActiveRecord::RecordInvalid do
341
+ name_lower_collision.save!
342
+ end
343
+
344
+ alternate_name_upper = ValidatesUniquenessOfString.new(:cs_string => '4', :ci_string => "ALTERNATE_NAME")
345
+ assert_nothing_raised do
346
+ alternate_name_upper.save!
347
+ end
348
+
349
+ alternate_name_upper_collision = ValidatesUniquenessOfString.new(:cs_string => '5', :ci_string => "ALTERNATE_NAME")
350
+ assert_raise ActiveRecord::RecordInvalid do
351
+ alternate_name_upper_collision.save!
352
+ end
353
+
354
+ alternate_name_lower = ValidatesUniquenessOfString.new(:cs_string => '6', :ci_string => "alternate_name")
355
+ assert_raise ActiveRecord::RecordInvalid do
356
+ alternate_name_lower.save!
357
+ end
358
+ end
359
+
360
+ class ChangeEntriesTable < ActiveRecord::Migration
361
+ def self.up
362
+ change_table :entries do |t|
363
+ t.string :author
364
+ end if respond_to?(:change_table)
365
+ end
366
+ def self.down
367
+ change_table :entries do |t|
368
+ t.remove :author
369
+ end if respond_to?(:change_table)
370
+ end
371
+ end
372
+ def test_change_table
373
+ ChangeEntriesTable.up
374
+ ChangeEntriesTable.down
375
+ end
376
+
377
+ def test_string_id
378
+ f = StringId.new
379
+ f.id = "some_string"
380
+ f.save
381
+ f = StringId.first #reload is essential
382
+ assert_equal "some_string", f.id
383
+ end
384
+ end
385
+
386
+ module MultibyteTestMethods
387
+ include MigrationSetup
388
+
389
+ if defined?(JRUBY_VERSION)
390
+ def setup
391
+ super
392
+ config = ActiveRecord::Base.connection.config
393
+ jdbc_driver = ActiveRecord::ConnectionAdapters::JdbcDriver.new(config[:driver])
394
+ jdbc_driver.load
395
+ @java_con = jdbc_driver.connection(config[:url], config[:username], config[:password])
396
+ @java_con.setAutoCommit(true)
397
+ end
398
+
399
+ def teardown
400
+ @java_con.close
401
+ super
402
+ end
403
+
404
+ def test_select_multibyte_string
405
+ @java_con.createStatement().execute("insert into entries (id, title, content) values (1, 'テスト', '本文')")
406
+ entry = Entry.find(:first)
407
+ assert_equal "テスト", entry.title
408
+ assert_equal "本文", entry.content
409
+ assert_equal entry, Entry.find_by_title("テスト")
410
+ end
411
+
412
+ def test_update_multibyte_string
413
+ Entry.create!(:title => "テスト", :content => "本文")
414
+ rs = @java_con.createStatement().executeQuery("select title, content from entries")
415
+ assert rs.next
416
+ assert_equal "テスト", rs.getString(1)
417
+ assert_equal "本文", rs.getString(2)
418
+ end
419
+ end
420
+
421
+ def test_multibyte_aliasing
422
+ str = "テスト"
423
+ quoted_alias = Entry.connection.quote_column_name(str)
424
+ sql = "SELECT title AS #{quoted_alias} from entries"
425
+ records = Entry.connection.select_all(sql)
426
+ records.each do |rec|
427
+ rec.keys.each do |key|
428
+ assert_equal str, key
429
+ end
430
+ end
431
+ end
432
+
433
+ def test_chinese_word
434
+ chinese_word = '中文'
435
+ new_entry = Entry.create(:title => chinese_word)
436
+ new_entry.reload
437
+ assert_equal chinese_word, new_entry.title
438
+ end
439
+ end
440
+
441
+ module NonUTF8EncodingMethods
442
+ def setup
443
+ @connection = ActiveRecord::Base.remove_connection
444
+ latin2_connection = @connection.dup
445
+ latin2_connection[:encoding] = 'latin2'
446
+ latin2_connection.delete(:url) # pre-gen url gets stashed; remove to re-gen
447
+ ActiveRecord::Base.establish_connection latin2_connection
448
+ CreateEntries.up
449
+ end
450
+
451
+ def teardown
452
+ CreateEntries.down
453
+ ActiveRecord::Base.establish_connection @connection
454
+ end
455
+
456
+ def test_nonutf8_encoding_in_entry
457
+ prague_district = 'hradčany'
458
+ new_entry = Entry.create :title => prague_district
459
+ new_entry.reload
460
+ assert_equal prague_district, new_entry.title
461
+ end
462
+ end
463
+
464
+ module ActiveRecord3TestMethods
465
+ def self.included(base)
466
+ base.send :include, Tests if ActiveRecord::VERSION::MAJOR == 3
467
+ end
468
+
469
+ module Tests
470
+ def test_where
471
+ entries = Entry.where(:title => @entry.title)
472
+ assert_equal @entry, entries.first
473
+ end
474
+ end
475
+ end