Skip to content
This repository
Browse code

Added automatic dropping/creating of test tables for running the unit…

… tests on all databases #587 [adelle@bullet.net.au]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@719 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 101968f3674dfedb0549a54bf6407c287d357f0a 1 parent 8322ea4
David Heinemeier Hansson authored February 20, 2005

Showing 28 changed files with 200 additions and 5 deletions. Show diff stats Hide diff stats

  1. 2  activerecord/CHANGELOG
  2. 5  activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
  3. 4  activerecord/lib/active_record/connection_adapters/db2_adapter.rb
  4. 6  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
  5. 4  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
  6. 4  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
  7. 4  activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
  8. 58  activerecord/test/aaa_create_tables_test.rb
  9. 18  activerecord/test/fixtures/db_definitions/db2.drop.sql
  10. 1  activerecord/test/fixtures/db_definitions/db2.sql
  11. 2  activerecord/test/fixtures/db_definitions/db22.drop.sql
  12. 1  activerecord/test/fixtures/db_definitions/db22.sql
  13. 18  activerecord/test/fixtures/db_definitions/mysql.drop.sql
  14. 3  activerecord/test/fixtures/db_definitions/mysql.sql
  15. 2  activerecord/test/fixtures/db_definitions/mysql2.drop.sql
  16. 1  activerecord/test/fixtures/db_definitions/mysql2.sql
  17. 18  activerecord/test/fixtures/db_definitions/postgresql.drop.sql
  18. 3  activerecord/test/fixtures/db_definitions/postgresql.sql
  19. 2  activerecord/test/fixtures/db_definitions/postgresql2.drop.sql
  20. 3  activerecord/test/fixtures/db_definitions/postgresql2.sql
  21. 18  activerecord/test/fixtures/db_definitions/sqlite.drop.sql
  22. 3  activerecord/test/fixtures/db_definitions/sqlite.sql
  23. 2  activerecord/test/fixtures/db_definitions/sqlite2.drop.sql
  24. 1  activerecord/test/fixtures/db_definitions/sqlite2.sql
  25. 18  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
  26. 1  activerecord/test/fixtures/db_definitions/sqlserver.sql
  27. 2  activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql
  28. 1  activerecord/test/fixtures/db_definitions/sqlserver2.sql
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Added automatic dropping/creating of test tables for running the unit tests on all databases #587 [adelle@bullet.net.au]
  4
+
3 5
 * Fixed that find_by_* would fail when column names had numbers #670 [demetrius]
4 6
 
5 7
 * Fixed the SQL Server adapter on a bunch of issues #667 [DeLynn]
5  activerecord/lib/active_record/connection_adapters/abstract_adapter.rb
@@ -356,6 +356,11 @@ def quote_column_name(name)
356 356
         name
357 357
       end
358 358
 
  359
+      # Returns the human-readable name of the adapter.  Use mixed case - one can always use downcase if needed.
  360
+      def adapter_name()
  361
+        'Abstract'
  362
+      end
  363
+
359 364
       # Returns a string of the CREATE TABLE SQL statements for recreating the entire structure of the database.
360 365
       def structure_dump() end
361 366
 
4  activerecord/lib/active_record/connection_adapters/db2_adapter.rb
@@ -74,6 +74,10 @@ def rollback_db_transaction
74 74
 
75 75
         def quote_column_name(name) name; end
76 76
 
  77
+        def adapter_name()
  78
+          'DB2'
  79
+        end
  80
+
77 81
         def quote_string(s)
78 82
           s.gsub(/'/, "''") # ' (for ruby-mode)
79 83
         end
6  activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
@@ -102,7 +102,11 @@ def rollback_db_transaction
102 102
       def quote_column_name(name)
103 103
         return "`#{name}`"
104 104
       end
105  
-      
  105
+
  106
+      def adapter_name()
  107
+        'MySQL'
  108
+      end
  109
+
106 110
       def structure_dump
107 111
         select_all("SHOW TABLES").inject("") do |structure, table|
108 112
           structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n"
4  activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -89,6 +89,10 @@ def quote_column_name(name)
89 89
         return "\"#{name}\""
90 90
       end
91 91
 
  92
+      def adapter_name()
  93
+        'PostgreSQL'
  94
+      end
  95
+
92 96
       private
93 97
         def last_insert_id(table, column = "id")
94 98
           sequence_name = "#{table}_#{column || 'id'}_seq"
4  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -141,6 +141,10 @@ def quote_column_name(name)
141 141
         return "'#{name}'"
142 142
       end
143 143
 
  144
+      def adapter_name()
  145
+        'SQLite'
  146
+      end
  147
+
144 148
       protected
145 149
         def table_structure(table_name)
146 150
           execute "PRAGMA table_info(#{table_name})"
4  activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb
@@ -43,6 +43,10 @@ def self.sqlserver_connection(config)
43 43
         raise ArgumentError, "No database specified. Missing argument: database."
44 44
       end
45 45
 
  46
+      def adapter_name()
  47
+        'SqlServer'
  48
+      end
  49
+
46 50
       conn = DBI.connect("DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};")
47 51
       conn["AutoCommit"] = true
48 52
 
58  activerecord/test/aaa_create_tables_test.rb
... ...
@@ -0,0 +1,58 @@
  1
+require 'abstract_unit'
  2
+
  3
+# The filename for this test begins with "aaa" so that
  4
+# it will be the first test.
  5
+
  6
+class SqlFile < File
  7
+  #Define an iterator that iterates over the statements in a .sql file.
  8
+  #statements are separated by a semicolon.
  9
+  def initialize(path)
  10
+    super(path)
  11
+  end
  12
+  
  13
+  def each_statement()
  14
+    statement = ''
  15
+    each_line { |line|
  16
+      #The last character of each line is a line-feed, so we will check the next-to-last character
  17
+      #to see if it is a semicolon.  A better way of doing this would be to look for a semicolon anywhere
  18
+      #within the line in case multiple statements have been put on a single line.
  19
+      #The last statement in the file must be followed by a line-feed.
  20
+      if line.slice(-2,1)==';' then
  21
+        statement = statement + line.slice(0,line.length-2) + "\n"
  22
+        yield statement
  23
+        statement = ''
  24
+      else
  25
+        statement = statement + line
  26
+      end
  27
+    }
  28
+  end
  29
+end
  30
+
  31
+class CreateTablesTest < Test::Unit::TestCase
  32
+  def setup
  33
+    # This method is required by rake.
  34
+  end
  35
+
  36
+  def run_sql_file(connection, path)
  37
+    sql_file = SqlFile.new(path)
  38
+    sql_file.each_statement { |statement|
  39
+    begin
  40
+      #Skip errors.  If there is a problem creating the tables then it will show up in other tests.
  41
+      connection.execute(statement)
  42
+    rescue ActiveRecord::StatementInvalid
  43
+    end }
  44
+  end
  45
+
  46
+  def test_table_creation
  47
+    adapter_name = ActiveRecord::Base.connection.adapter_name.downcase
  48
+    run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".drop.sql"
  49
+    run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".sql"
  50
+
  51
+    # Now do the same thing with the connection used by multiple_db_test.rb
  52
+    adapter_name = Course.retrieve_connection.adapter_name.downcase
  53
+    run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.drop.sql"
  54
+    run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.sql"
  55
+    
  56
+    assert_equal 1,1
  57
+  end
  58
+end
18  activerecord/test/fixtures/db_definitions/db2.drop.sql
... ...
@@ -0,0 +1,18 @@
  1
+DROP TABLE accounts;
  2
+DROP TABLE companies;
  3
+DROP TABLE topics;
  4
+DROP TABLE developers;
  5
+DROP TABLE projects;
  6
+DROP TABLE developers_projects;
  7
+DROP TABLE customers;
  8
+DROP TABLE movies;
  9
+DROP TABLE subscribers;
  10
+DROP TABLE booleantests;
  11
+DROP TABLE auto_id_tests;
  12
+DROP TABLE entrants;
  13
+DROP TABLE colnametests;
  14
+DROP TABLE mixins;
  15
+DROP TABLE people;
  16
+DROP TABLE binaries;
  17
+DROP TABLE computers;
  18
+
1  activerecord/test/fixtures/db_definitions/db2.sql
@@ -127,3 +127,4 @@ CREATE TABLE computers (
127 127
   id int generated by default as identity (start with +10000),
128 128
   developer int NOT NULL
129 129
 );
  130
+
2  activerecord/test/fixtures/db_definitions/db22.drop.sql
... ...
@@ -0,0 +1,2 @@
  1
+DROP TABLE courses;
  2
+
1  activerecord/test/fixtures/db_definitions/db22.sql
@@ -2,3 +2,4 @@ CREATE TABLE courses (
2 2
  id int NOT NULL PRIMARY KEY,
3 3
  name varchar(255) NOT NULL
4 4
 );
  5
+
18  activerecord/test/fixtures/db_definitions/mysql.drop.sql
... ...
@@ -0,0 +1,18 @@
  1
+DROP TABLE accounts;
  2
+DROP TABLE companies;
  3
+DROP TABLE topics;
  4
+DROP TABLE developers;
  5
+DROP TABLE projects;
  6
+DROP TABLE developers_projects;
  7
+DROP TABLE customers;
  8
+DROP TABLE movies;
  9
+DROP TABLE subscribers;
  10
+DROP TABLE booleantests;
  11
+DROP TABLE auto_id_tests;
  12
+DROP TABLE entrants;
  13
+DROP TABLE colnametests;
  14
+DROP TABLE mixins;
  15
+DROP TABLE people;
  16
+DROP TABLE binaries;
  17
+DROP TABLE computers;
  18
+
3  activerecord/test/fixtures/db_definitions/mysql.sql
@@ -126,4 +126,5 @@ CREATE TABLE `binaries` (
126 126
 CREATE TABLE `computers` (
127 127
   `id` INTEGER NOT NULL PRIMARY KEY,
128 128
   `developer` INTEGER NOT NULL
129  
-);
  129
+);
  130
+
2  activerecord/test/fixtures/db_definitions/mysql2.drop.sql
... ...
@@ -0,0 +1,2 @@
  1
+DROP TABLE courses;
  2
+
1  activerecord/test/fixtures/db_definitions/mysql2.sql
@@ -2,3 +2,4 @@ CREATE TABLE `courses` (
2 2
  `id` INTEGER NOT NULL PRIMARY KEY,
3 3
  `name` VARCHAR(255) NOT NULL
4 4
 );
  5
+
18  activerecord/test/fixtures/db_definitions/postgresql.drop.sql
... ...
@@ -0,0 +1,18 @@
  1
+DROP TABLE accounts;
  2
+DROP TABLE companies;
  3
+DROP TABLE topics;
  4
+DROP TABLE developers;
  5
+DROP TABLE projects;
  6
+DROP TABLE developers_projects;
  7
+DROP TABLE customers;
  8
+DROP TABLE movies;
  9
+DROP TABLE subscribers;
  10
+DROP TABLE booleantests;
  11
+DROP TABLE auto_id_tests;
  12
+DROP TABLE entrants;
  13
+DROP TABLE colnametests;
  14
+DROP TABLE mixins;
  15
+DROP TABLE people;
  16
+DROP TABLE binaries;
  17
+DROP TABLE computers;
  18
+
3  activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -144,4 +144,5 @@ CREATE TABLE binaries (
144 144
 CREATE TABLE computers (
145 145
   id serial,
146 146
   developer integer NOT NULL
147  
-);
  147
+);
  148
+
2  activerecord/test/fixtures/db_definitions/postgresql2.drop.sql
... ...
@@ -0,0 +1,2 @@
  1
+DROP TABLE courses;
  2
+
3  activerecord/test/fixtures/db_definitions/postgresql2.sql
... ...
@@ -1,4 +1,5 @@
1 1
 CREATE TABLE courses (
2 2
   id serial,
3 3
   name text
4  
-);
  4
+);
  5
+
18  activerecord/test/fixtures/db_definitions/sqlite.drop.sql
... ...
@@ -0,0 +1,18 @@
  1
+DROP TABLE accounts;
  2
+DROP TABLE companies;
  3
+DROP TABLE topics;
  4
+DROP TABLE developers;
  5
+DROP TABLE projects;
  6
+DROP TABLE developers_projects;
  7
+DROP TABLE customers;
  8
+DROP TABLE movies;
  9
+DROP TABLE subscribers;
  10
+DROP TABLE booleantests;
  11
+DROP TABLE auto_id_tests;
  12
+DROP TABLE entrants;
  13
+DROP TABLE colnametests;
  14
+DROP TABLE mixins;
  15
+DROP TABLE people;
  16
+DROP TABLE binaries;
  17
+DROP TABLE computers;
  18
+
3  activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -113,4 +113,5 @@ CREATE TABLE 'binaries' (
113 113
 CREATE TABLE 'computers' (
114 114
   'id' INTEGER NOT NULL PRIMARY KEY,
115 115
   'developer' INTEGER NOT NULL
116  
-);
  116
+);
  117
+
2  activerecord/test/fixtures/db_definitions/sqlite2.drop.sql
... ...
@@ -0,0 +1,2 @@
  1
+DROP TABLE courses;
  2
+
1  activerecord/test/fixtures/db_definitions/sqlite2.sql
@@ -2,3 +2,4 @@ CREATE TABLE 'courses' (
2 2
  'id' INTEGER NOT NULL PRIMARY KEY,
3 3
  'name' VARCHAR(255) NOT NULL
4 4
 );
  5
+
18  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
... ...
@@ -0,0 +1,18 @@
  1
+DROP TABLE accounts;
  2
+DROP TABLE companies;
  3
+DROP TABLE topics;
  4
+DROP TABLE developers;
  5
+DROP TABLE projects;
  6
+DROP TABLE developers_projects;
  7
+DROP TABLE customers;
  8
+DROP TABLE movies;
  9
+DROP TABLE subscribers;
  10
+DROP TABLE booleantests;
  11
+DROP TABLE auto_id_tests;
  12
+DROP TABLE entrants;
  13
+DROP TABLE colnametests;
  14
+DROP TABLE mixins;
  15
+DROP TABLE people;
  16
+DROP TABLE binaries;
  17
+DROP TABLE computers;
  18
+
1  activerecord/test/fixtures/db_definitions/sqlserver.sql
@@ -127,3 +127,4 @@ CREATE TABLE computers (
127 127
   developer int NOT NULL,
128 128
   PRIMARY KEY (id)
129 129
 );
  130
+
2  activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql
... ...
@@ -0,0 +1,2 @@
  1
+DROP TABLE courses;
  2
+
1  activerecord/test/fixtures/db_definitions/sqlserver2.sql
@@ -2,3 +2,4 @@ CREATE TABLE courses (
2 2
  id int NOT NULL PRIMARY KEY,
3 3
  name varchar(255) NOT NULL
4 4
 );
  5
+

0 notes on commit 101968f

Please sign in to comment.
Something went wrong with that request. Please try again.