aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/tasks/sqlite_rake_test.rb
blob: d7e3caa2ff6a0b384a95a986b60a472302b68280 (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
# frozen_string_literal: true

require "cases/helper"
require "active_record/tasks/database_tasks"
require "pathname"

if current_adapter?(:SQLite3Adapter)
  module ActiveRecord
    class SqliteDBCreateTest < ActiveRecord::TestCase
      def setup
        @database      = "db_create.sqlite3"
        @connection    = stub :connection
        @configuration = {
          "adapter"  => "sqlite3",
          "database" => @database
        }

        File.stubs(:exist?).returns(false)
        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)

        $stdout, @original_stdout = StringIO.new, $stdout
        $stderr, @original_stderr = StringIO.new, $stderr
      end

      def teardown
        $stdout, $stderr = @original_stdout, @original_stderr
      end

      def test_db_checks_database_exists
        File.expects(:exist?).with(@database).returns(false)

        ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root"
      end

      def test_when_db_created_successfully_outputs_info_to_stdout
        ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root"

        assert_equal "Created database '#{@database}'\n", $stdout.string
      end

      def test_db_create_when_file_exists
        File.stubs(:exist?).returns(true)

        ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root"

        assert_equal "Database '#{@database}' already exists\n", $stderr.string
      end

      def test_db_create_with_file_does_nothing
        File.stubs(:exist?).returns(true)
        $stderr.stubs(:puts).returns(nil)

        ActiveRecord::Base.expects(:establish_connection).never

        ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root"
      end

      def test_db_create_establishes_a_connection
        ActiveRecord::Base.expects(:establish_connection).with(@configuration)

        ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root"
      end

      def test_db_create_with_error_prints_message
        ActiveRecord::Base.stubs(:establish_connection).raises(Exception)

        $stderr.stubs(:puts).returns(true)
        $stderr.expects(:puts).
          with("Couldn't create database for #{@configuration.inspect}")

        assert_raises(Exception) { ActiveRecord::Tasks::DatabaseTasks.create @configuration, "/rails/root" }
      end
    end

    class SqliteDBDropTest < ActiveRecord::TestCase
      def setup
        @database      = "db_create.sqlite3"
        @path          = stub(to_s: "/absolute/path", absolute?: true)
        @configuration = {
          "adapter"  => "sqlite3",
          "database" => @database
        }

        Pathname.stubs(:new).returns(@path)
        File.stubs(:join).returns("/former/relative/path")
        FileUtils.stubs(:rm).returns(true)

        $stdout, @original_stdout = StringIO.new, $stdout
        $stderr, @original_stderr = StringIO.new, $stderr
      end

      def teardown
        $stdout, $stderr = @original_stdout, @original_stderr
      end

      def test_creates_path_from_database
        Pathname.expects(:new).with(@database).returns(@path)

        ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"
      end

      def test_removes_file_with_absolute_path
        File.stubs(:exist?).returns(true)
        @path.stubs(:absolute?).returns(true)

        FileUtils.expects(:rm).with("/absolute/path")

        ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"
      end

      def test_generates_absolute_path_with_given_root
        @path.stubs(:absolute?).returns(false)

        File.expects(:join).with("/rails/root", @path).
          returns("/former/relative/path")

        ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"
      end

      def test_removes_file_with_relative_path
        File.stubs(:exist?).returns(true)
        @path.stubs(:absolute?).returns(false)

        FileUtils.expects(:rm).with("/former/relative/path")

        ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"
      end

      def test_when_db_dropped_successfully_outputs_info_to_stdout
        ActiveRecord::Tasks::DatabaseTasks.drop @configuration, "/rails/root"

        assert_equal "Dropped database '#{@database}'\n", $stdout.string
      end
    end

    class SqliteDBCharsetTest < ActiveRecord::TestCase
      def setup
        @database      = "db_create.sqlite3"
        @connection    = stub :connection
        @configuration = {
          "adapter"  => "sqlite3",
          "database" => @database
        }

        File.stubs(:exist?).returns(false)
        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
      end

      def test_db_retrieves_charset
        @connection.expects(:encoding)
        ActiveRecord::Tasks::DatabaseTasks.charset @configuration, "/rails/root"
      end
    end

    class SqliteDBCollationTest < ActiveRecord::TestCase
      def setup
        @database      = "db_create.sqlite3"
        @connection    = stub :connection
        @configuration = {
          "adapter"  => "sqlite3",
          "database" => @database
        }

        File.stubs(:exist?).returns(false)
        ActiveRecord::Base.stubs(:connection).returns(@connection)
        ActiveRecord::Base.stubs(:establish_connection).returns(true)
      end

      def test_db_retrieves_collation
        assert_raise NoMethodError do
          ActiveRecord::Tasks::DatabaseTasks.collation @configuration, "/rails/root"
        end
      end
    end

    class SqliteStructureDumpTest < ActiveRecord::TestCase
      def setup
        @database      = "db_create.sqlite3"
        @configuration = {
          "adapter"  => "sqlite3",
          "database" => @database
        }

        `sqlite3 #{@database} 'CREATE TABLE bar(id INTEGER)'`
        `sqlite3 #{@database} 'CREATE TABLE foo(id INTEGER)'`
      end

      def test_structure_dump
        dbfile   = @database
        filename = "awesome-file.sql"

        ActiveRecord::Tasks::DatabaseTasks.structure_dump @configuration, filename, "/rails/root"
        assert File.exist?(dbfile)
        assert File.exist?(filename)
        assert_match(/CREATE TABLE foo/, File.read(filename))
        assert_match(/CREATE TABLE bar/, File.read(filename))
      ensure
        FileUtils.rm_f(filename)
        FileUtils.rm_f(dbfile)
      end

      def test_structure_dump_with_ignore_tables
        dbfile   = @database
        filename = "awesome-file.sql"
        ActiveRecord::SchemaDumper.expects(:ignore_tables).returns(["foo"])

        ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root")
        assert File.exist?(dbfile)
        assert File.exist?(filename)
        assert_match(/bar/, File.read(filename))
        assert_no_match(/foo/, File.read(filename))
      ensure
        FileUtils.rm_f(filename)
        FileUtils.rm_f(dbfile)
      end

      def test_structure_dump_execution_fails
        dbfile   = @database
        filename = "awesome-file.sql"
        Kernel.expects(:system).with("sqlite3", "--noop", "db_create.sqlite3", ".schema", out: "awesome-file.sql").returns(nil)

        e = assert_raise(RuntimeError) do
          with_structure_dump_flags(["--noop"]) do
            quietly { ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root") }
          end
        end
        assert_match("failed to execute:", e.message)
      ensure
        FileUtils.rm_f(filename)
        FileUtils.rm_f(dbfile)
      end

      private
        def with_structure_dump_flags(flags)
          old = ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags
          ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = flags
          yield
        ensure
          ActiveRecord::Tasks::DatabaseTasks.structure_dump_flags = old
        end
    end

    class SqliteStructureLoadTest < ActiveRecord::TestCase
      def setup
        @database      = "db_create.sqlite3"
        @configuration = {
          "adapter"  => "sqlite3",
          "database" => @database
        }
      end

      def test_structure_load
        dbfile   = @database
        filename = "awesome-file.sql"

        open(filename, "w") { |f| f.puts("select datetime('now', 'localtime');") }
        ActiveRecord::Tasks::DatabaseTasks.structure_load @configuration, filename, "/rails/root"
        assert File.exist?(dbfile)
      ensure
        FileUtils.rm_f(filename)
        FileUtils.rm_f(dbfile)
      end
    end
  end
end