File: db_system_change_generator_test.rb

package info (click to toggle)
rails 2%3A7.2.2.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,348 kB
  • sloc: ruby: 349,797; javascript: 30,703; yacc: 46; sql: 43; sh: 29; makefile: 27
file content (221 lines) | stat: -rw-r--r-- 8,419 bytes parent folder | download | duplicates (2)
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
# frozen_string_literal: true

require "generators/generators_test_helper"
require "rails/generators/rails/db/system/change/change_generator"

module Rails
  module Generators
    module Db
      module System
        class ChangeGeneratorTest < Rails::Generators::TestCase
          include GeneratorsTestHelper

          setup do
            copy_gemfile <<~ENTRY
              # Use sqlite3 as the database for Active Record
              gem "sqlite3"
            ENTRY

            copy_dockerfile
            copy_devcontainer_files
          end

          test "change to invalid database" do
            output = capture(:stderr) do
              run_generator ["--to", "invalid-db"]
            end

            assert_match <<~MSG.squish, output
              Invalid value for --to option.
              Supported preconfigurations are:
              mysql, trilogy, postgresql, sqlite3.
            MSG
          end

          test "change to postgresql" do
            run_generator ["--to", "postgresql"]

            assert_file("config/database.yml") do |content|
              assert_match "adapter: postgresql", content
              assert_match "database: tmp_production", content
              assert_match "host: <%= ENV[\"DB_HOST\"] %>", content
            end

            assert_file("Gemfile") do |content|
              assert_match "# Use pg as the database for Active Record", content
              assert_match 'gem "pg", "~> 1.1"', content
            end

            assert_file("Dockerfile") do |content|
              assert_match "build-essential git libpq-dev", content
              assert_match "curl libvips postgresql-client", content
            end

            assert_devcontainer_json_file do |content|
              assert_equal "postgres", content["containerEnv"]["DB_HOST"]
              assert_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/postgres-client"
              assert_not_includes content["features"].keys, "ghcr.io/rails/devcontainer/features/sqlite"
            end

            assert_compose_file do |compose_config|
              assert_includes compose_config["services"]["rails-app"]["depends_on"], "postgres"

              expected_postgres_config = {
                "image" => "postgres:16.1",
                "restart" => "unless-stopped",
                "networks" => ["default"],
                "volumes" => ["postgres-data:/var/lib/postgresql/data"],
                "environment" => {
                  "POSTGRES_USER" => "postgres",
                  "POSTGRES_PASSWORD" => "postgres"
                }
              }

              assert_equal expected_postgres_config, compose_config["services"]["postgres"]
              assert_includes compose_config["volumes"].keys, "postgres-data"
            end
          end

          test "change to mysql" do
            run_generator ["--to", "mysql"]

            assert_file("config/database.yml") do |content|
              assert_match "adapter: mysql2", content
              assert_match "database: tmp_production", content
            end

            assert_file("Gemfile") do |content|
              assert_match "# Use mysql2 as the database for Active Record", content
              assert_match 'gem "mysql2", "~> 0.5"', content
            end

            assert_file("Dockerfile") do |content|
              assert_match "build-essential default-libmysqlclient-dev git", content
              assert_match "curl default-mysql-client libvips", content
            end

            assert_devcontainer_json_file do |content|
              assert_equal "mysql", content["containerEnv"]["DB_HOST"]
              assert_equal({}, content["features"]["ghcr.io/rails/devcontainer/features/mysql-client"])
            end

            assert_compose_file do |compose_config|
              assert_includes compose_config["services"]["rails-app"]["depends_on"], "mysql"

              expected_mysql_config = {
                "image" => "mysql/mysql-server:8.0",
                "restart" => "unless-stopped",
                "environment" => {
                  "MYSQL_ALLOW_EMPTY_PASSWORD" => "true",
                  "MYSQL_ROOT_HOST" => "%"
                },
                "volumes" => ["mysql-data:/var/lib/mysql"],
                "networks" => ["default"],
              }

              assert_equal expected_mysql_config, compose_config["services"]["mysql"]
              assert_includes compose_config["volumes"].keys, "mysql-data"
            end
          end

          test "change to sqlite3" do
            run_generator ["--to", "sqlite3"]

            assert_file("config/database.yml") do |content|
              assert_match "adapter: sqlite3", content
              assert_match "storage/development.sqlite3", content
            end

            assert_file("Gemfile") do |content|
              assert_match "# Use sqlite3 as the database for Active Record", content
              assert_match 'gem "sqlite3", ">= 1.4"', content
            end

            assert_file("Dockerfile") do |content|
              assert_match "build-essential git", content
              assert_match "curl libvips sqlite3", content
            end

            assert_devcontainer_json_file do |content|
              assert_not_includes content["containerEnv"].keys, "DB_HOST"
            end
          end

          test "change to trilogy" do
            run_generator ["--to", "trilogy"]

            assert_file("config/database.yml") do |content|
              assert_match "adapter: trilogy", content
              assert_match "database: tmp_production", content
            end

            assert_file("Gemfile") do |content|
              assert_match "# Use trilogy as the database for Active Record", content
              assert_match 'gem "trilogy", "~> 2.7"', content
            end

            assert_file("Dockerfile") do |content|
              assert_match "build-essential git", content
              assert_match "curl libvips", content
              assert_no_match "default-libmysqlclient-dev", content
            end

            assert_devcontainer_json_file do |content|
              assert_match "mariadb", content["containerEnv"]["DB_HOST"]
            end

            assert_compose_file do |compose_config|
              assert_includes compose_config["services"]["rails-app"]["depends_on"], "mariadb"

              expected_mariadb_config = {
                "image" => "mariadb:10.5",
                "restart" => "unless-stopped",
                "networks" => ["default"],
                "volumes" => ["mariadb-data:/var/lib/mysql"],
                "environment" => {
                  "MARIADB_ALLOW_EMPTY_ROOT_PASSWORD" => "true",
                },
              }

              assert_equal expected_mariadb_config, compose_config["services"]["mariadb"]
              assert_includes compose_config["volumes"].keys, "mariadb-data"
            end
          end

          test "change from versioned gem to other versioned gem" do
            run_generator ["--to", "sqlite3"]
            run_generator ["--to", "mysql", "--force"]

            assert_file("config/database.yml") do |content|
              assert_match "adapter: mysql2", content
              assert_match "database: tmp_production", content
            end

            assert_file("Gemfile") do |content|
              assert_match "# Use mysql2 as the database for Active Record", content
              assert_match 'gem "mysql2", "~> 0.5"', content
            end
          end

          test "change from db with devcontainer service to one without" do
            copy_minimal_devcontainer_compose_file

            run_generator ["--to", "mysql"]
            run_generator ["--to", "sqlite3", "--force"]

            assert_devcontainer_json_file do |content|
              assert_not_includes content["containerEnv"].keys, "DB_HOST"
              assert_not_includes content["features"].keys, "ghcr.io\/rails\/devcontainer\/features\/mysql-client"
            end

            assert_compose_file do |compose_config|
              assert_not_includes compose_config["services"]["rails-app"].keys, "depends_on"
              assert_not_includes compose_config["services"].keys, "mysql"
              assert_not_includes compose_config.keys, "volumes"
            end
          end
        end
      end
    end
  end
end