aboutsummaryrefslogtreecommitdiffstats
path: root/railties/test/application/test_test.rb
blob: 32d2a6857cfc4f4a7adb85ef7973802e21a29e8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
require "isolation/abstract_unit"

module ApplicationTests
  class TestTest < ActiveSupport::TestCase
    include ActiveSupport::Testing::Isolation

    def setup
      build_app
    end

    def teardown
      teardown_app
    end

    test "simple successful test" do
      app_file "test/unit/foo_test.rb", <<-RUBY
        require 'test_helper'

        class FooTest < ActiveSupport::TestCase
          def test_truth
            assert true
          end
        end
      RUBY

      assert_successful_test_run "unit/foo_test.rb"
    end

    test "after_run" do
      app_file "test/unit/foo_test.rb", <<-RUBY
        require 'test_helper'

        Minitest.after_run { puts "WORLD" }
        Minitest.after_run { puts "HELLO" }

        class FooTest < ActiveSupport::TestCase
          def test_truth
            assert true
          end
        end
      RUBY

      result = assert_successful_test_run "unit/foo_test.rb"
      assert_equal ["HELLO", "WORLD"], result.scan(/HELLO|WORLD/) # only once and in correct order
    end

    test "simple failed test" do
      app_file "test/unit/foo_test.rb", <<-RUBY
        require 'test_helper'

        class FooTest < ActiveSupport::TestCase
          def test_truth
            assert false
          end
        end
      RUBY

      assert_unsuccessful_run "unit/foo_test.rb", "Failed assertion"
    end

    test "integration test" do
      controller "posts", <<-RUBY
        class PostsController < ActionController::Base
        end
      RUBY

      app_file "app/views/posts/index.html.erb", <<-HTML
        Posts#index
      HTML

      app_file "test/integration/posts_test.rb", <<-RUBY
        require 'test_helper'

        class PostsTest < ActionDispatch::IntegrationTest
          def test_index
            get '/posts'
            assert_response :success
            assert_includes @response.body, 'Posts#index'
          end
        end
      RUBY

      assert_successful_test_run "integration/posts_test.rb"
    end

    test "enable full backtraces on test failures" do
      app_file "test/unit/failing_test.rb", <<-RUBY
        require 'test_helper'

        class FailingTest < ActiveSupport::TestCase
          def test_failure
            raise "fail"
          end
        end
      RUBY

      output = run_test_file("unit/failing_test.rb", env: { "BACKTRACE" => "1" })
      assert_match %r{test/unit/failing_test\.rb}, output
      assert_match %r{test/unit/failing_test\.rb:4}, output
    end

    test "ruby schema migrations" do
      output  = script("generate model user name:string")
      version = output.match(/(\d+)_create_users\.rb/)[1]

      app_file "test/models/user_test.rb", <<-RUBY
        require 'test_helper'

        class UserTest < ActiveSupport::TestCase
          test "user" do
            User.create! name: "Jon"
          end
        end
      RUBY
      app_file "db/schema.rb", ""

      assert_unsuccessful_run "models/user_test.rb", "Migrations are pending"

      app_file "db/schema.rb", <<-RUBY
        ActiveRecord::Schema.define(version: #{version}) do
          create_table :users do |t|
            t.string :name
          end
        end
      RUBY

      app_file "config/initializers/disable_maintain_test_schema.rb", <<-RUBY
        Rails.application.config.active_record.maintain_test_schema = false
      RUBY

      assert_unsuccessful_run "models/user_test.rb", "Could not find table 'users'"

      File.delete "#{app_path}/config/initializers/disable_maintain_test_schema.rb"

      result = assert_successful_test_run("models/user_test.rb")
      assert_not_includes result, "create_table(:users)"
    end

    test "sql structure migrations" do
      output  = script("generate model user name:string")
      version = output.match(/(\d+)_create_users\.rb/)[1]

      app_file "test/models/user_test.rb", <<-RUBY
        require 'test_helper'

        class UserTest < ActiveSupport::TestCase
          test "user" do
            User.create! name: "Jon"
          end
        end
      RUBY

      app_file "db/structure.sql", ""
      app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
        Rails.application.config.active_record.schema_format = :sql
      RUBY

      assert_unsuccessful_run "models/user_test.rb", "Migrations are pending"

      app_file "db/structure.sql", <<-SQL
        CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
        CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
        CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
        INSERT INTO schema_migrations (version) VALUES ('#{version}');
      SQL

      app_file "config/initializers/disable_maintain_test_schema.rb", <<-RUBY
        Rails.application.config.active_record.maintain_test_schema = false
      RUBY

      assert_unsuccessful_run "models/user_test.rb", "Could not find table 'users'"

      File.delete "#{app_path}/config/initializers/disable_maintain_test_schema.rb"

      assert_successful_test_run("models/user_test.rb")
    end

    test "sql structure migrations when adding column to existing table" do
      output_1  = script("generate model user name:string")
      version_1 = output_1.match(/(\d+)_create_users\.rb/)[1]

      app_file "test/models/user_test.rb", <<-RUBY
        require 'test_helper'
        class UserTest < ActiveSupport::TestCase
          test "user" do
            User.create! name: "Jon"
          end
        end
      RUBY

      app_file "config/initializers/enable_sql_schema_format.rb", <<-RUBY
        Rails.application.config.active_record.schema_format = :sql
      RUBY

      app_file "db/structure.sql", <<-SQL
        CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
        CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
        CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255));
        INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
      SQL

      assert_successful_test_run("models/user_test.rb")

      output_2  = script("generate migration add_email_to_users")
      version_2 = output_2.match(/(\d+)_add_email_to_users\.rb/)[1]

      app_file "test/models/user_test.rb", <<-RUBY
        require 'test_helper'

        class UserTest < ActiveSupport::TestCase
          test "user" do
            User.create! name: "Jon", email: "jon@doe.com"
          end
        end
      RUBY

      app_file "db/structure.sql", <<-SQL
        CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL);
        CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version");
        CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255));
        INSERT INTO schema_migrations (version) VALUES ('#{version_1}');
        INSERT INTO schema_migrations (version) VALUES ('#{version_2}');
      SQL

      assert_successful_test_run("models/user_test.rb")
    end

    # TODO: would be nice if we could detect the schema change automatically.
    # For now, the user has to synchronize the schema manually.
    # This test-case serves as a reminder for this use-case.
    test "manually synchronize test schema after rollback" do
      output  = script("generate model user name:string")
      version = output.match(/(\d+)_create_users\.rb/)[1]

      app_file "test/models/user_test.rb", <<-RUBY
        require 'test_helper'

        class UserTest < ActiveSupport::TestCase
          test "user" do
            assert_equal ["id", "name"], User.columns_hash.keys
          end
        end
      RUBY
      app_file "db/schema.rb", <<-RUBY
        ActiveRecord::Schema.define(version: #{version}) do
          create_table :users do |t|
            t.string :name
          end
        end
      RUBY

      assert_successful_test_run "models/user_test.rb"

      # Simulate `db:rollback` + edit of the migration file + `db:migrate`
      app_file "db/schema.rb", <<-RUBY
        ActiveRecord::Schema.define(version: #{version}) do
          create_table :users do |t|
            t.string :name
            t.integer :age
          end
        end
      RUBY

      assert_successful_test_run "models/user_test.rb"

      Dir.chdir(app_path) { `bin/rails db:test:prepare` }

      assert_unsuccessful_run "models/user_test.rb", <<-ASSERTION
Expected: ["id", "name"]
  Actual: ["id", "name", "age"]
      ASSERTION
    end

    test "hooks for plugins" do
      output  = script("generate model user name:string")
      version = output.match(/(\d+)_create_users\.rb/)[1]

      app_file "lib/tasks/hooks.rake", <<-RUBY
        task :before_hook do
          has_user_table = ActiveRecord::Base.connection.table_exists?('users')
          puts "before: " + has_user_table.to_s
        end

        task :after_hook do
          has_user_table = ActiveRecord::Base.connection.table_exists?('users')
          puts "after: " + has_user_table.to_s
        end

        Rake::Task["db:test:prepare"].enhance [:before_hook] do
          Rake::Task[:after_hook].invoke
        end
      RUBY
      app_file "test/models/user_test.rb", <<-RUBY
        require 'test_helper'
        class UserTest < ActiveSupport::TestCase
          test "user" do
            User.create! name: "Jon"
          end
        end
      RUBY

      # Simulate `db:migrate`
      app_file "db/schema.rb", <<-RUBY
        ActiveRecord::Schema.define(version: #{version}) do
          create_table :users do |t|
            t.string :name
          end
        end
      RUBY

      output = assert_successful_test_run "models/user_test.rb"
      assert_includes output, "before: false\nafter: true"

      # running tests again won't trigger a schema update
      output = assert_successful_test_run "models/user_test.rb"
      assert_not_includes output, "before:"
      assert_not_includes output, "after:"
    end

    private
      def assert_unsuccessful_run(name, message)
        result = run_test_file(name)
        assert_not_equal 0, $?.to_i
        assert_includes result, message
        result
      end

      def assert_successful_test_run(name)
        result = run_test_file(name)
        assert_equal 0, $?.to_i, result
        result
      end

      def run_test_file(name, options = {})
        Dir.chdir(app_path) { `bin/rails test "#{app_path}/test/#{name}" 2>&1` }
      end
  end
end