DEV: Introduce run_theme_migration spec helper in test environment (#26845)

This commit introduces the `run_theme_migration` spec helper to allow
theme developers to write RSpec tests for theme migrations. For example,
this allows the following RSpec test to be written in themes:

```
RSpec.describe "0003-migrate-small-links-setting migration" do
  let!(:theme) { upload_theme_component }

  it "should set target property to `_blank` if previous target component is not valid or empty" do
    theme.theme_settings.create!(
      name: "small_links",
      theme: theme,
      data_type: ThemeSetting.types[:string],
      value: "some text, #|some text 2, #, invalid target",
    )

    run_theme_migration(theme, "0003-migrate-small-links-setting")

    expect(theme.settings[:small_links].value).to eq(
      [
        { "text" => "some text", "url" => "#", "target" => "_blank" },
        { "text" => "some text 2", "url" => "#", "target" => "_blank" },
      ],
    )
  end
end
```

This change is being introduced because we realised that writting just
javascript tests for the migrations is insufficient since javascript
tests do not ensure that the migrated theme settings can actually be
successfully saved into the database. Hence, we are introduce this
helper as a way for theme developers to write "end-to-end" migrations
tests.
This commit is contained in:
Alan Guo Xiang Tan
2024-05-03 06:29:18 +08:00
committed by GitHub
parent 6bfc81978c
commit 243fcb6ffc
6 changed files with 133 additions and 61 deletions

View File

@@ -53,7 +53,7 @@ class ThemeSettingsMigrationsRunner
}
JS
private_constant :Migration, :MIGRATION_ENTRY_POINT_JS
private_constant :MIGRATION_ENTRY_POINT_JS
def self.loader_js_lib_content
@loader_js_lib_content ||=
@@ -67,8 +67,8 @@ class ThemeSettingsMigrationsRunner
@memory = memory
end
def run
fields = lookup_pending_migrations_fields
def run(fields: nil, raise_error_on_out_of_sequence: true)
fields ||= lookup_pending_migrations_fields
count = fields.count
return [] if count == 0
@@ -80,12 +80,13 @@ class ThemeSettingsMigrationsRunner
current_migration_version =
@theme.theme_settings_migrations.order(version: :desc).pick(:version)
current_migration_version ||= -Float::INFINITY
current_settings = lookup_overriden_settings
migrations.map do |migration|
if migration.version <= current_migration_version
if migration.version <= current_migration_version && raise_error_on_out_of_sequence
raise_error(
"themes.import_error.migrations.out_of_sequence",
name: migration.original_name,
@@ -94,6 +95,7 @@ class ThemeSettingsMigrationsRunner
end
migrated_settings = execute(migration, current_settings)
results = {
version: migration.version,
name: migration.name,
@@ -157,6 +159,7 @@ class ThemeSettingsMigrationsRunner
.migration_fields
.left_joins(:theme_settings_migration)
.where(theme_settings_migration: { id: nil })
.order(created_at: :asc)
end
def convert_fields_to_migrations(fields)